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 16, 2021
1 parent fe47dbb commit 0338cd2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/tests/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// config.rs

// Copyright (C) 2019-2020 The Nitrocli Developers
// Copyright (C) 2019-2021 The Nitrocli Developers
// SPDX-License-Identifier: GPL-3.0-or-later

use super::*;
Expand All @@ -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
11 changes: 8 additions & 3 deletions src/tests/otp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// otp.rs

// Copyright (C) 2019-2020 The Nitrocli Developers
// Copyright (C) 2019-2021 The Nitrocli Developers
// SPDX-License-Identifier: GPL-3.0-or-later

use super::*;
Expand All @@ -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(&err)
);
}

#[test_device]
Expand Down
12 changes: 6 additions & 6 deletions src/tests/run.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// run.rs

// Copyright (C) 2019-2020 The Nitrocli Developers
// Copyright (C) 2019-2021 The Nitrocli Developers
// SPDX-License-Identifier: GPL-3.0-or-later

use std::collections;
Expand All @@ -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(&err));
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 0338cd2

Please sign in to comment.