Skip to content

Commit

Permalink
Add message to assertions on bytes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
robinkrahl authored and d-e-s-o committed Apr 17, 2021
1 parent 7b181e9 commit 6a89f65
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/tests/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions src/tests/otp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
10 changes: 5 additions & 5 deletions src/tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(())
}

Expand Down
9 changes: 7 additions & 2 deletions src/tests/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 6a89f65

Please sign in to comment.