From 7d0484e29e4d47b4a176d35af30c2db489764fd9 Mon Sep 17 00:00:00 2001 From: Christian Meusel Date: Sat, 4 May 2024 22:45:13 +0200 Subject: [PATCH] Actually check results in test_serialport 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. --- .gitignore | 1 + tests/test_serialport.rs | 27 ++++++++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 5a10e5db..c814159b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ target Cargo.lock *.bk *~ +.env diff --git a/tests/test_serialport.rs b/tests/test_serialport.rs index 04bad8f1..9213b847 100644 --- a/tests/test_serialport.rs +++ b/tests/test_serialport.rs @@ -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!"); @@ -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(); }