john-slop/tests/cli.rs

96 lines
3.0 KiB
Rust

use std::{fs, process::Command};
use tempfile::tempdir;
fn detector() -> Command {
Command::new(env!("CARGO_BIN_EXE_john-slop"))
}
#[test]
fn help_is_successful_and_names_the_command() {
let output = detector().arg("--help").output().expect("run detector");
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
insta::assert_snapshot!(String::from_utf8(output.stdout).expect("UTF-8 stdout"));
}
#[test]
fn invalid_confidence_is_a_usage_error() {
let output = detector()
.args(["--fail-confidence", "101", "file.txt"])
.output()
.expect("run detector");
assert_eq!(output.status.code(), Some(2));
insta::assert_snapshot!(String::from_utf8(output.stderr).expect("UTF-8 stderr"));
}
#[test]
fn findings_control_the_threshold_exit_code() {
let directory = tempdir().expect("temp dir");
let clean = directory.path().join("clean.txt");
let suspicious = directory.path().join("suspicious.txt");
fs::write(&clean, "ordinary-word").expect("write clean fixture");
fs::write(&suspicious, "suspicious—dash").expect("write suspicious fixture");
let clean_output = detector()
.args(["--fail-confidence", "10"])
.arg(&clean)
.output()
.expect("run clean detector");
let suspicious_output = detector()
.args(["--fail-confidence", "10"])
.arg(&suspicious)
.output()
.expect("run suspicious detector");
assert_eq!(clean_output.status.code(), Some(0));
assert_eq!(suspicious_output.status.code(), Some(1));
}
#[test]
fn config_show_emits_resolved_toml_with_explicit_overrides() {
let directory = tempdir().expect("temp dir");
let config = directory.path().join("project.toml");
fs::write(&config, "[thresholds]\nlow = 23\n").expect("write config");
let output = detector()
.env("XDG_CONFIG_HOME", directory.path())
.args(["config", "show"])
.arg("--config")
.arg(&config)
.output()
.expect("run config show");
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
let stdout = String::from_utf8(output.stdout).expect("UTF-8 stdout");
let resolved = toml::from_str::<toml::Value>(&stdout).expect("valid TOML");
assert_eq!(resolved["schema_version"].as_integer(), Some(1));
assert_eq!(resolved["thresholds"]["low"].as_integer(), Some(23));
assert_eq!(resolved["replacements"].as_array().unwrap().len(), 10);
}
#[test]
fn config_paths_lists_embedded_and_platform_sources() {
let directory = tempdir().expect("temp dir");
let output = detector()
.env("XDG_CONFIG_HOME", directory.path())
.args(["config", "paths"])
.output()
.expect("run config paths");
assert!(output.status.success());
let stdout = String::from_utf8(output.stdout).expect("UTF-8 stdout");
assert!(stdout.contains("<embedded:assets/config.toml>"));
assert!(stdout.contains("config.toml"));
assert!(stdout.contains("<not selected>"));
}