From 6a89f65e5ba3a55b86f1cd285ae44ac017697b6e Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Fri, 16 Apr 2021 15:40:54 +0200 Subject: [PATCH] Add message to assertions on bytes Some of our tests compare byte slices and are very hard to debug if the assertion fails due to they way these slices are printed. This patch adds assertion messages containing the (lossy) string representation of the byte slice to make it easier to debug errors. Fixes #152 --- src/tests/config.rs | 2 +- src/tests/otp.rs | 9 +++++++-- src/tests/run.rs | 10 +++++----- src/tests/status.rs | 9 +++++++-- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/tests/config.rs b/src/tests/config.rs index 1ed71b66..1f36998b 100644 --- a/src/tests/config.rs +++ b/src/tests/config.rs @@ -11,7 +11,7 @@ fn mutually_exclusive_set_options() { let (rc, out, err) = Nitrocli::new().run(&["config", "set", option1, option2]); assert_ne!(rc, 0); - assert_eq!(out, b""); + assert_eq!(out, b"", "{}", String::from_utf8_lossy(&out)); let err = String::from_utf8(err).unwrap(); assert!(err.contains("cannot be used with"), "{}", err); diff --git a/src/tests/otp.rs b/src/tests/otp.rs index 45ae7843..a87a8653 100644 --- a/src/tests/otp.rs +++ b/src/tests/otp.rs @@ -14,8 +14,13 @@ fn set_invalid_slot_raw(model: nitrokey::Model) { .run(&["otp", "set", "100", "name", "1234", "-f", "hex"]); assert_ne!(rc, 0); - assert_eq!(out, b""); - assert_eq!(&err[..24], b"Failed to write OTP slot"); + assert_eq!(out, b"", "{}", String::from_utf8_lossy(&out)); + assert_eq!( + &err[..24], + b"Failed to write OTP slot", + "{}", + String::from_utf8_lossy(&out) + ); } #[test_device] diff --git a/src/tests/run.rs b/src/tests/run.rs index afa2128a..81ac9706 100644 --- a/src/tests/run.rs +++ b/src/tests/run.rs @@ -21,7 +21,7 @@ fn no_command_or_option() { let (rc, out, err) = Nitrocli::new().run(&[]); assert_ne!(rc, 0); - assert_eq!(out, b""); + assert_eq!(out, b"", "{}", String::from_utf8_lossy(&out)); let s = String::from_utf8_lossy(&err).into_owned(); assert!(s.starts_with("nitrocli"), "{}", s); @@ -37,7 +37,7 @@ fn help_options() { let (rc, out, err) = Nitrocli::new().run(&all); assert_eq!(rc, 0); - assert_eq!(err, b""); + assert_eq!(err, b"", "{}", String::from_utf8_lossy(&err)); let s = String::from_utf8_lossy(&out).into_owned(); let mut args = args.to_vec(); @@ -93,7 +93,7 @@ fn version_option() { let (rc, out, err) = Nitrocli::new().run(&[opt]); assert_eq!(rc, 0); - assert_eq!(err, b""); + assert_eq!(err, b"", "{}", String::from_utf8_lossy(&err)); let s = String::from_utf8_lossy(&out).into_owned(); let _ = re; @@ -346,8 +346,8 @@ sys.exit(42); let (rc, out, err) = ncli.run(&["ext"]); assert_eq!(rc, 42); - assert_eq!(out, b""); - assert_eq!(err, b""); + assert_eq!(out, b"", "{}", String::from_utf8_lossy(&out)); + assert_eq!(err, b"", "{}", String::from_utf8_lossy(&out)); Ok(()) } diff --git a/src/tests/status.rs b/src/tests/status.rs index 1c90a722..7df70cb2 100644 --- a/src/tests/status.rs +++ b/src/tests/status.rs @@ -10,8 +10,13 @@ fn not_found_raw() { let (rc, out, err) = Nitrocli::new().run(&["status"]); assert_ne!(rc, 0); - assert_eq!(out, b""); - assert_eq!(err, b"Nitrokey device not found\n"); + assert_eq!(out, b"", "{}", String::from_utf8_lossy(&out)); + assert_eq!( + err, + b"Nitrokey device not found\n", + "{}", + String::from_utf8_lossy(&err) + ); } #[test_device]