299 lines
9.4 KiB
Rust
299 lines
9.4 KiB
Rust
use std::{
|
||
fs,
|
||
io::Write,
|
||
path::Path,
|
||
process::{Command, Output, Stdio},
|
||
};
|
||
|
||
use tempfile::tempdir;
|
||
|
||
fn formatter(config_home: &Path) -> Command {
|
||
let mut command = Command::new(env!("CARGO_BIN_EXE_john-slop-fmt"));
|
||
command.env("XDG_CONFIG_HOME", config_home);
|
||
command
|
||
}
|
||
|
||
fn formatter_with_stdin(config_home: &Path, arguments: &[&str], input: &[u8]) -> Output {
|
||
let mut child = formatter(config_home)
|
||
.args(arguments)
|
||
.stdin(Stdio::piped())
|
||
.stdout(Stdio::piped())
|
||
.stderr(Stdio::piped())
|
||
.spawn()
|
||
.expect("spawn formatter");
|
||
child
|
||
.stdin
|
||
.take()
|
||
.expect("formatter stdin")
|
||
.write_all(input)
|
||
.expect("write formatter stdin");
|
||
child.wait_with_output().expect("wait for formatter")
|
||
}
|
||
|
||
#[test]
|
||
fn stdin_stdout_is_pure_and_idempotent() {
|
||
let directory = tempdir().expect("temp dir");
|
||
let source = "Résumé—value 5−2\r\n";
|
||
let first = formatter_with_stdin(directory.path(), &[], source.as_bytes());
|
||
let second = formatter_with_stdin(directory.path(), &[], &first.stdout);
|
||
|
||
assert!(first.status.success());
|
||
assert!(first.stderr.is_empty());
|
||
assert_eq!(first.stdout, "Résumé-value 5-2\r\n".as_bytes());
|
||
assert!(second.status.success());
|
||
assert!(second.stderr.is_empty());
|
||
assert_eq!(second.stdout, first.stdout);
|
||
}
|
||
|
||
#[test]
|
||
fn check_and_diff_have_stable_exit_statuses() {
|
||
let directory = tempdir().expect("temp dir");
|
||
let clean = directory.path().join("clean.txt");
|
||
let dirty = directory.path().join("dirty.txt");
|
||
fs::write(&clean, "plain-ascii\n").expect("write clean input");
|
||
fs::write(&dirty, "review—this\n").expect("write dirty input");
|
||
|
||
let clean_check = formatter(directory.path())
|
||
.args(["--check"])
|
||
.arg(&clean)
|
||
.output()
|
||
.expect("check clean file");
|
||
let dirty_check = formatter(directory.path())
|
||
.args(["--check"])
|
||
.arg(&dirty)
|
||
.output()
|
||
.expect("check dirty file");
|
||
let diff = formatter(directory.path())
|
||
.arg("--diff")
|
||
.arg(&dirty)
|
||
.output()
|
||
.expect("diff dirty file");
|
||
|
||
assert_eq!(clean_check.status.code(), Some(0));
|
||
assert!(clean_check.stdout.is_empty());
|
||
assert!(clean_check.stderr.is_empty());
|
||
assert_eq!(dirty_check.status.code(), Some(1));
|
||
assert!(dirty_check.stdout.is_empty());
|
||
assert!(dirty_check.stderr.is_empty());
|
||
assert_eq!(diff.status.code(), Some(1));
|
||
assert!(diff.stderr.is_empty());
|
||
let stdout = String::from_utf8(diff.stdout).expect("UTF-8 diff");
|
||
assert!(stdout.contains("--- "));
|
||
assert!(stdout.contains("+++ "));
|
||
assert!(stdout.contains("-review—this"));
|
||
assert!(stdout.contains("+review-this"));
|
||
}
|
||
|
||
#[test]
|
||
fn default_file_mode_does_not_modify_the_source() {
|
||
let directory = tempdir().expect("temp dir");
|
||
let path = directory.path().join("article.txt");
|
||
let source = "review—this\n";
|
||
fs::write(&path, source).expect("write input");
|
||
|
||
let output = formatter(directory.path())
|
||
.arg(&path)
|
||
.output()
|
||
.expect("format file");
|
||
|
||
assert!(output.status.success());
|
||
assert!(output.stderr.is_empty());
|
||
assert_eq!(output.stdout, b"review-this\n");
|
||
assert_eq!(fs::read_to_string(path).expect("read source"), source);
|
||
}
|
||
|
||
#[test]
|
||
fn explicit_write_is_idempotent_and_leaves_no_temporary_file() {
|
||
let directory = tempdir().expect("temp dir");
|
||
let path = directory.path().join("article.txt");
|
||
fs::write(&path, "one—two−three\n").expect("write input");
|
||
|
||
for _ in 0..2 {
|
||
let output = formatter(directory.path())
|
||
.arg("--write")
|
||
.arg(&path)
|
||
.output()
|
||
.expect("write formatted file");
|
||
assert!(output.status.success());
|
||
assert!(output.stdout.is_empty());
|
||
assert!(output.stderr.is_empty());
|
||
}
|
||
|
||
assert_eq!(
|
||
fs::read_to_string(&path).expect("read formatted file"),
|
||
"one-two-three\n"
|
||
);
|
||
assert_eq!(
|
||
fs::read_dir(directory.path())
|
||
.expect("read temp directory")
|
||
.count(),
|
||
1
|
||
);
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn atomic_write_preserves_unix_permissions() {
|
||
use std::os::unix::fs::PermissionsExt;
|
||
|
||
let directory = tempdir().expect("temp dir");
|
||
let path = directory.path().join("script.txt");
|
||
fs::write(&path, "run—this\n").expect("write input");
|
||
fs::set_permissions(&path, fs::Permissions::from_mode(0o754)).expect("set permissions");
|
||
|
||
let output = formatter(directory.path())
|
||
.arg("--write")
|
||
.arg(&path)
|
||
.output()
|
||
.expect("write formatted file");
|
||
|
||
assert!(output.status.success());
|
||
assert_eq!(
|
||
fs::metadata(path)
|
||
.expect("file metadata")
|
||
.permissions()
|
||
.mode()
|
||
& 0o777,
|
||
0o754
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn explicit_configuration_controls_formatter_output() {
|
||
let directory = tempdir().expect("temp dir");
|
||
let config = directory.path().join("custom.toml");
|
||
fs::write(
|
||
&config,
|
||
r#"
|
||
[[replacements]]
|
||
id = "em-dash"
|
||
replacement = "--"
|
||
"#,
|
||
)
|
||
.expect("write config");
|
||
|
||
let output = formatter_with_stdin(
|
||
directory.path(),
|
||
&["--config", config.to_str().expect("UTF-8 path")],
|
||
"a—b".as_bytes(),
|
||
);
|
||
|
||
assert!(output.status.success());
|
||
assert!(output.stderr.is_empty());
|
||
assert_eq!(output.stdout, b"a--b");
|
||
}
|
||
|
||
#[test]
|
||
fn recursive_write_plans_every_input_before_mutation() {
|
||
let directory = tempdir().expect("temp dir");
|
||
let valid = directory.path().join("a-valid.txt");
|
||
let invalid = directory.path().join("z-invalid.txt");
|
||
fs::write(&valid, "still—original\n").expect("write valid input");
|
||
fs::write(&invalid, [0xff]).expect("write invalid input");
|
||
|
||
let output = formatter(directory.path())
|
||
.args(["--write", "--recursive"])
|
||
.arg(directory.path())
|
||
.output()
|
||
.expect("run recursive write");
|
||
|
||
assert_eq!(output.status.code(), Some(2));
|
||
assert!(output.stdout.is_empty());
|
||
assert!(!output.stderr.is_empty());
|
||
assert_eq!(
|
||
fs::read_to_string(valid).expect("read untouched valid input"),
|
||
"still—original\n"
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn malformed_configuration_never_rewrites_the_input() {
|
||
let directory = tempdir().expect("temp dir");
|
||
let path = directory.path().join("article.txt");
|
||
let config = directory.path().join("broken.toml");
|
||
fs::write(&path, "still—original\n").expect("write input");
|
||
fs::write(&config, "[replacements\n").expect("write malformed config");
|
||
|
||
let output = formatter(directory.path())
|
||
.args(["--write", "--config"])
|
||
.arg(&config)
|
||
.arg(&path)
|
||
.output()
|
||
.expect("run formatter with malformed config");
|
||
|
||
assert_eq!(output.status.code(), Some(2));
|
||
assert_eq!(
|
||
fs::read_to_string(path).expect("read untouched input"),
|
||
"still—original\n"
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn detector_fixes_reconstruct_formatter_output() {
|
||
let directory = tempdir().expect("temp dir");
|
||
let path = directory.path().join("article.txt");
|
||
let source = "a—b−c\n";
|
||
fs::write(&path, source).expect("write input");
|
||
|
||
let formatted = formatter(directory.path())
|
||
.arg(&path)
|
||
.output()
|
||
.expect("run formatter");
|
||
let detected = Command::new(env!("CARGO_BIN_EXE_john-slop"))
|
||
.env("XDG_CONFIG_HOME", directory.path())
|
||
.args(["--output", "json"])
|
||
.arg(&path)
|
||
.output()
|
||
.expect("run detector");
|
||
let record: serde_json::Value =
|
||
serde_json::from_slice(&detected.stdout).expect("parse detector JSON");
|
||
let mut reconstructed = source.to_owned();
|
||
for finding in record["findings"]
|
||
.as_array()
|
||
.expect("findings array")
|
||
.iter()
|
||
.rev()
|
||
{
|
||
let fix = &finding["fix"];
|
||
let start = fix["byte_span"]["start"].as_u64().expect("start") as usize;
|
||
let end = fix["byte_span"]["end"].as_u64().expect("end") as usize;
|
||
let replacement = fix["replacement"].as_str().expect("replacement");
|
||
reconstructed.replace_range(start..end, replacement);
|
||
}
|
||
|
||
assert!(formatted.status.success());
|
||
assert!(detected.status.success());
|
||
assert_eq!(formatted.stdout, reconstructed.as_bytes());
|
||
}
|
||
|
||
#[test]
|
||
fn directories_and_archives_require_explicit_supported_policies() {
|
||
let directory = tempdir().expect("temp dir");
|
||
let archive = directory.path().join("report.docx");
|
||
fs::write(&archive, b"not opened").expect("write archive placeholder");
|
||
|
||
let implicit_directory = formatter(directory.path())
|
||
.arg(directory.path())
|
||
.output()
|
||
.expect("format directory without recursive policy");
|
||
let invalid_recursive = formatter(directory.path())
|
||
.arg("--recursive")
|
||
.arg(directory.path())
|
||
.output()
|
||
.expect("format recursive stdout");
|
||
let archive_output = formatter(directory.path())
|
||
.arg("--write")
|
||
.arg(&archive)
|
||
.output()
|
||
.expect("format archive");
|
||
|
||
assert_eq!(implicit_directory.status.code(), Some(2));
|
||
assert!(String::from_utf8_lossy(&implicit_directory.stderr).contains("requires --recursive"));
|
||
assert_eq!(invalid_recursive.status.code(), Some(2));
|
||
assert!(
|
||
String::from_utf8_lossy(&invalid_recursive.stderr).contains("requires --check or --write")
|
||
);
|
||
assert_eq!(archive_output.status.code(), Some(2));
|
||
assert!(String::from_utf8_lossy(&archive_output.stderr).contains("unsupported"));
|
||
}
|