Skip to content

Commit

Permalink
Add config unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Defelo committed Jun 15, 2023
1 parent 50e9711 commit 7d383dc
Showing 1 changed file with 59 additions and 3 deletions.
62 changes: 59 additions & 3 deletions tests/config.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,76 @@
use std::env;
use std::{env, path::PathBuf};

use sandkasten::config;

#[test]
fn test_config() {
fn valid() {
env::set_var("NSJAIL_PATH", "/");
env::set_var("TIME_PATH", "/");
env::set_var(
"CONFIG_PATH",
concat!(env!("CARGO_MANIFEST_DIR"), "/config.toml"),
);
config::load().unwrap();
let conf = config::load().unwrap();
assert_eq!(conf.nsjail_path, PathBuf::from("/"));
assert_eq!(conf.time_path, PathBuf::from("/"));
}

#[test]
fn invalid() {
env::set_var(
"CONFIG_PATH",
concat!(env!("CARGO_MANIFEST_DIR"), "/Cargo.toml"),
);
assert!(config::load().is_err());
}

#[test]
fn not_found() {
env::set_var("CONFIG_PATH", "/does/not/exist.toml");
assert!(config::load().is_err());
}

#[test]
fn cannot_canonicalize() {
env::set_var("NSJAIL_PATH", "./does/not/exist");
env::set_var("TIME_PATH", "/");
env::set_var(
"CONFIG_PATH",
concat!(env!("CARGO_MANIFEST_DIR"), "/config.toml"),
);
let err = config::load().unwrap_err();
assert!(err
.to_string()
.starts_with("Failed to resolve `nsjail_path`"));

env::set_var("NSJAIL_PATH", "/");
env::set_var("TIME_PATH", "./does/not/exist");
env::set_var(
"CONFIG_PATH",
concat!(env!("CARGO_MANIFEST_DIR"), "/config.toml"),
);
let err = config::load().unwrap_err();
assert!(err.to_string().starts_with("Failed to resolve `time_path`"));
}

#[test]
fn environments_path_from_env() {
env::set_var("NSJAIL_PATH", "/");
env::set_var("TIME_PATH", "/");
env::set_var("ENVIRONMENTS_PATH", "/foo:/bar:/baz");
env::set_var(
"CONFIG_PATH",
concat!(env!("CARGO_MANIFEST_DIR"), "/config.toml"),
);
let conf = config::load().unwrap();
assert_eq!(conf.nsjail_path, PathBuf::from("/"));
assert_eq!(conf.time_path, PathBuf::from("/"));
assert_eq!(
conf.environments_path,
[
PathBuf::from("/foo"),
PathBuf::from("/bar"),
PathBuf::from("/baz"),
]
);
}

0 comments on commit 7d383dc

Please sign in to comment.