use clap::Parser;
use crate::cli::{Cli, Command, ProjectAction};
use crate::commands::project::trust_cmd_in;
#[test]
fn cli_parses_project_trust() {
let cli = Cli::try_parse_from(["trusty-mpm", "project", "trust", "--dir", "/work/p"]).unwrap();
match cli.command.unwrap() {
Command::Project {
action: ProjectAction::Trust { dir, revoke },
} => {
assert_eq!(dir.as_deref(), Some("/work/p"));
assert!(!revoke);
}
other => panic!("expected project trust, got {other:?}"),
}
}
#[test]
fn trust_command_hint_parses_as_the_cli_accepts_it() {
let hint = trusty_mpm::core::project_trust::trust_command_hint(std::path::Path::new("/work/p"));
assert_eq!(hint, "tm project trust --dir /work/p");
let cli = Cli::try_parse_from(hint.split_whitespace())
.unwrap_or_else(|e| panic!("the trust hint `{hint}` does not parse: {e}"));
match cli.command.unwrap() {
Command::Project {
action: ProjectAction::Trust { dir, revoke },
} => {
assert_eq!(dir.as_deref(), Some("/work/p"));
assert!(!revoke, "a grant hint must never revoke");
}
other => panic!("expected project trust, got {other:?}"),
}
}
#[test]
fn trust_command_hint_quotes_a_path_with_spaces() {
let dir = "/work/my project";
let hint = trusty_mpm::core::project_trust::trust_command_hint(std::path::Path::new(dir));
assert_eq!(hint, "tm project trust --dir '/work/my project'");
let cli = Cli::try_parse_from(["tm", "project", "trust", "--dir", dir])
.expect("the shell hands the quoted path over as one argument");
match cli.command.unwrap() {
Command::Project {
action: ProjectAction::Trust { dir: parsed, .. },
} => assert_eq!(parsed.as_deref(), Some(dir)),
other => panic!("expected project trust, got {other:?}"),
}
}
#[test]
fn cli_parses_project_trust_revoke() {
let cli = Cli::try_parse_from(["trusty-mpm", "project", "trust", "--revoke"]).unwrap();
match cli.command.unwrap() {
Command::Project {
action: ProjectAction::Trust { dir, revoke },
} => {
assert_eq!(dir, None);
assert!(revoke);
}
other => panic!("expected project trust --revoke, got {other:?}"),
}
}
#[test]
fn project_trust_grants_and_revokes() {
let store_root = tempfile::tempdir().unwrap();
let project = tempfile::tempdir().unwrap();
let project_dir = project.path().to_string_lossy().to_string();
let root = store_root.path();
assert!(!trusty_mpm::core::project_trust::is_project_trusted_at(
project.path(),
root
));
trust_cmd_in(Some(project_dir.clone()), false, root).expect("trust succeeds");
assert!(trusty_mpm::core::project_trust::is_project_trusted_at(
project.path(),
root
));
assert!(
!project.path().join("project-trust.json").exists(),
"trust state must never be written inside the project directory"
);
assert!(
root.join("project-trust.json").exists(),
"trust state must live under the user-scope tm config dir"
);
trust_cmd_in(Some(project_dir), true, root).expect("revoke succeeds");
assert!(!trusty_mpm::core::project_trust::is_project_trusted_at(
project.path(),
root
));
}
#[test]
fn trust_store_root_is_the_user_scope_tm_config_dir() {
let root = trusty_mpm::core::project_trust::trust_store_root()
.expect("a home directory resolves on every supported platform");
assert!(
root.ends_with(std::path::Path::new(".trusty-tools").join("trusty-mpm")),
"the trust store must live under the user-scope tm config dir, got {}",
root.display()
);
}