50 lines
1.5 KiB
Rust
50 lines
1.5 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());
|
|
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));
|
|
}
|