Skip to content

Commit

Permalink
Remove unwrap_str_err method from UnwrapError test trait
Browse files Browse the repository at this point in the history
With upcoming changes we intend to move towards a model where we do not
distinguish the individual error variants the program deals with in a
global enum.
In preparation of such a change, this patch marks a first step in
removing the UnwrapError test trait, which relies on the existence of
exactly such typed errors. In particular, we remove the unwrap_str_err
method from it, basically falling back to just working with strings.
  • Loading branch information
d-e-s-o committed Jul 8, 2020
1 parent 99fde3c commit efb6fbf
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 23 deletions.
6 changes: 4 additions & 2 deletions src/tests/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ $"#,

#[test_device]
fn set_wrong_usage(model: nitrokey::Model) {
let res = Nitrocli::with_model(model).handle(&["config", "set", "--numlock", "2", "-N"]);
let err = res.unwrap_str_err();
let err = Nitrocli::with_model(model)
.handle(&["config", "set", "--numlock", "2", "-N"])
.unwrap_err()
.to_string();
assert!(
err.contains("The argument '--numlock <numlock>' cannot be used with '--no-numlock'"),
err,
Expand Down
9 changes: 6 additions & 3 deletions src/tests/encrypted.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// encrypted.rs

// *************************************************************************
// * Copyright (C) 2019 Daniel Mueller ([email protected]) *
// * Copyright (C) 2019-2020 Daniel Mueller ([email protected]) *
// * *
// * This program is free software: you can redistribute it and/or modify *
// * it under the terms of the GNU General Public License as published by *
Expand Down Expand Up @@ -61,9 +61,12 @@ $"#,

#[test_device(pro)]
fn encrypted_open_on_pro(model: nitrokey::Model) {
let res = Nitrocli::with_model(model).handle(&["encrypted", "open"]);
let err = Nitrocli::with_model(model)
.handle(&["encrypted", "open"])
.unwrap_err()
.to_string();
assert_eq!(
res.unwrap_str_err(),
err,
"This command is only available on the Nitrokey Storage",
);
}
Expand Down
16 changes: 0 additions & 16 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ mod unencrypted;

/// A trait simplifying checking for expected errors.
pub trait UnwrapError {
/// Unwrap an Error::Error variant.
fn unwrap_str_err(self) -> String;
/// Unwrap a Error::CommandError variant.
fn unwrap_cmd_err(self) -> (Option<&'static str>, nitrokey::CommandError);
/// Unwrap a Error::LibraryError variant.
Expand All @@ -49,20 +47,6 @@ impl<T> UnwrapError for crate::Result<T>
where
T: fmt::Debug,
{
fn unwrap_str_err(self) -> String {
match self.unwrap_err() {
crate::Error::ClapError(err) => {
if err.use_stderr() {
err.message
} else {
String::new()
}
}
crate::Error::Error(err) => err,
err => panic!("Unexpected error variant found: {:?}", err),
}
}

fn unwrap_cmd_err(self) -> (Option<&'static str>, nitrokey::CommandError) {
match self.unwrap_err() {
crate::Error::NitrokeyError(ctx, err) => match err {
Expand Down
5 changes: 3 additions & 2 deletions src/tests/status.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// status.rs

// *************************************************************************
// * Copyright (C) 2019 Daniel Mueller ([email protected]) *
// * Copyright (C) 2019-2020 Daniel Mueller ([email protected]) *
// * *
// * This program is free software: you can redistribute it and/or modify *
// * it under the terms of the GNU General Public License as published by *
Expand Down Expand Up @@ -33,7 +33,8 @@ fn not_found_raw() {
#[test_device]
fn not_found() {
let res = Nitrocli::new().handle(&["status"]);
assert_eq!(res.unwrap_str_err(), "Nitrokey device not found");
let err = res.unwrap_err().to_string();
assert_eq!(err, "Nitrokey device not found");
}

#[test_device(pro)]
Expand Down

0 comments on commit efb6fbf

Please sign in to comment.