Skip to content

Commit

Permalink
Actually check results in test_serialport
Browse files Browse the repository at this point in the history
Previously, tests were reported successful in every case. For example
even with non-existent devices. So make the serial ports to use for
testing explicit and actually check for success.
  • Loading branch information
sirhcel authored and eldruin committed May 5, 2024
1 parent 90f3e3b commit 7d0484e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ target
Cargo.lock
*.bk
*~
.env
27 changes: 16 additions & 11 deletions tests/test_serialport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
use serialport::*;
use std::time::Duration;

const PORT_1: &str = env!("SERIALPORT_RS_TEST_PORT_1");
const PORT_2: &str = env!("SERIALPORT_RS_TEST_PORT_2");

#[test]
fn test_listing_ports() {
let ports = serialport::available_ports().expect("No ports found!");
Expand All @@ -14,44 +17,46 @@ fn test_listing_ports() {

#[test]
fn test_opening_found_ports() {
// There is no guarantee that we even might open the ports returned by `available_ports`. But
// the ports we are using for testing shall be among them.
let ports = serialport::available_ports().unwrap();
for p in ports {
let _port = serialport::new(p.port_name, 9600).open();
}
assert!(ports.iter().any(|info| info.port_name == PORT_1));
assert!(ports.iter().any(|info| info.port_name == PORT_2));
}

#[test]
fn test_opening_port() {
let _port = serialport::new("/dev/ttyUSB0", 9600).open();
serialport::new(PORT_1, 9600).open().unwrap();
}

#[test]
fn test_opening_native_port() {
let _port = serialport::new("/dev/ttyUSB0", 9600).open_native();
serialport::new(PORT_1, 9600).open_native().unwrap();
}

#[test]
fn test_configuring_ports() {
let _port = serialport::new("/dev/ttyUSB0", 9600)
serialport::new(PORT_1, 9600)
.data_bits(DataBits::Five)
.flow_control(FlowControl::None)
.parity(Parity::None)
.stop_bits(StopBits::One)
.timeout(Duration::from_millis(1))
.open();
.open()
.unwrap();
}

#[test]
fn test_duplicating_port_config() {
let port1_config = serialport::new("/dev/ttyUSB0", 9600)
let port1_config = serialport::new(PORT_1, 9600)
.data_bits(DataBits::Five)
.flow_control(FlowControl::None)
.parity(Parity::None)
.stop_bits(StopBits::One)
.timeout(Duration::from_millis(1));

let port2_config = port1_config.clone().path("/dev/ttyUSB1").baud_rate(115_200);
let port2_config = port1_config.clone().path(PORT_2).baud_rate(115_200);

let _port1 = port1_config.open();
let _port1 = port2_config.open();
let _port1 = port1_config.open().unwrap();
let _port1 = port2_config.open().unwrap();
}

0 comments on commit 7d0484e

Please sign in to comment.