Skip to content

Commit

Permalink
test: use assert2 for assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
xstefanox committed Nov 20, 2024
1 parent e3a1c32 commit 1e6157e
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 49 deletions.
104 changes: 104 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]

[dev-dependencies]
assert2 = "0.3.15"
tempfile = "3.3.0"

[profile.release]
Expand Down
27 changes: 12 additions & 15 deletions src/command_runner_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use assert2::{check, let_assert};
use crate::command_runner::CommandError::NoCommandToExecute;
use crate::command_runner::{parse_command_line, Process};
use crate::{assert_err, assert_ok};

macro_rules! svec {
($($x:expr),*) => (vec![$($x.to_string()),*]);
Expand All @@ -10,37 +10,34 @@ macro_rules! svec {
fn do_not_build_a_process_if_no_arguments_given() {
let args = svec!["self"];

let process = parse_command_line(args);
let result = parse_command_line(args);

assert_err!(NoCommandToExecute, process);
let_assert!(Err(error) = result);
check!(error == NoCommandToExecute);
}

#[test]
fn the_first_argument_should_be_set_as_the_executable() {
let args = svec!["self", "my_bin"];

let process = parse_command_line(args);
let result = parse_command_line(args);

assert_ok!(
Process {
let_assert!(Ok(process) = result);
check!(process == Process {
executable: "my_bin".into(),
args: svec![]
},
process
);
});
}

#[test]
fn the_following_arguments_should_be_set_as_executable_arguments() {
let args = svec!["self", "my_bin", "arg1", "arg2"];

let process = parse_command_line(args);
let result = parse_command_line(args);

assert_ok!(
Process {
let_assert!(Ok(process) = result);
check!(process == Process {
executable: "my_bin".into(),
args: svec!["arg1", "arg2"]
},
process
);
});
}
3 changes: 0 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ use std::path::Path;
mod command_runner;
mod secret_loader;

#[cfg(test)]
mod test_macros;

fn main() -> Result<(), String> {
let result = load_and_run();

Expand Down
40 changes: 22 additions & 18 deletions src/secret_loader_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ use crate::secret_loader::load_from_path;
use crate::FilesystemError::NotDirectory;
use crate::FilesystemError::NotFile;
use crate::FilesystemError::Unreadable;
use crate::{assert_err, assert_ok};
use std::collections::HashMap;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use assert2::{check, let_assert};

#[test]
fn no_secrets_directory_should_produce_an_empty_map() {
let root = tempfile::tempdir().unwrap();

let secrets = load_from_path(root.path(), "var/run/secrets");
let result = load_from_path(root.path(), "var/run/secrets");

assert_ok!(HashMap::new(), secrets);
let_assert!(Ok(secrets) = result);
check!(secrets.is_empty());
}

#[test]
Expand All @@ -27,9 +28,10 @@ fn fail_if_secrets_path_is_not_a_directory() {
let secrets_path = var_run_path.join(Path::new("secrets"));
File::create(&secrets_path).unwrap();

let secrets = load_from_path(root.path(), "var/run/secrets");
let result = load_from_path(root.path(), "var/run/secrets");

assert_err!(NotDirectory(secrets_path), secrets);
let_assert!(Err(error) = result);
check!(error == NotDirectory(secrets_path));
}

#[test]
Expand All @@ -38,9 +40,10 @@ fn empty_secrets_directory_should_produce_an_empty_map() {
let secrets_path = root.path().join(Path::new("var/run/secrets"));
fs::create_dir_all(secrets_path.as_path()).unwrap();

let secrets = load_from_path(root.path(), "var/run/secrets");
let result = load_from_path(root.path(), "var/run/secrets");

assert_ok!(HashMap::new(), secrets);
let_assert!(Ok(secrets) = result);
check!(secrets.is_empty());
}

#[test]
Expand All @@ -49,9 +52,10 @@ fn fail_if_secret_entry_is_a_directory() {
let secrets_path = root.path().join(Path::new("var/run/secrets/something"));
fs::create_dir_all(secrets_path.as_path()).unwrap();

let secrets = load_from_path(root.path(), "var/run/secrets");
let result = load_from_path(root.path(), "var/run/secrets");

assert_err!(NotFile(secrets_path), secrets);
let_assert!(Err(error) = result);
check!(error == NotFile(secrets_path));
}

#[test]
Expand All @@ -63,12 +67,10 @@ fn a_valid_secret_should_be_returned() {
let secret_file = File::create(&secret_path).unwrap();
write!(&secret_file, "{}", "a_secret_value").unwrap();

let secrets = load_from_path(root.path(), "var/run/secrets");
let result = load_from_path(root.path(), "var/run/secrets");

assert_ok!(
HashMap::from([("A_SECRET".into(), "a_secret_value".to_string())]),
secrets
);
let_assert!(Ok(secrets) = result);
check!(secrets == HashMap::from([("A_SECRET".into(), "a_secret_value".to_string())]));
}

#[test]
Expand All @@ -80,9 +82,10 @@ fn fail_if_secrets_directory_is_not_readable() {
perms.set_mode(0644);
fs::set_permissions(&secrets_path, perms).unwrap();

let secrets = load_from_path(root.path(), "var/run/secrets");
let result = load_from_path(root.path(), "var/run/secrets");

assert_err!(Unreadable(secrets_path), secrets);
let_assert!(Err(error) = result);
check!(error == Unreadable(secrets_path));
}

#[test]
Expand All @@ -96,7 +99,8 @@ fn fail_if_secret_file_is_not_readable() {
perms.set_mode(0000);
fs::set_permissions(&secret_path, perms).unwrap();

let secrets = load_from_path(root.path(), "var/run/secrets");
let result = load_from_path(root.path(), "var/run/secrets");

assert_err!(Unreadable(secret_path), secrets);
let_assert!(Err(error) = result);
check!(error == Unreadable(secret_path));
}
13 changes: 0 additions & 13 deletions src/test_macros.rs

This file was deleted.

0 comments on commit 1e6157e

Please sign in to comment.