use assert_cmd::Command;
use predicates::prelude::*;
fn diffx() -> Command {
Command::cargo_bin("diffx").unwrap()
}
fn fixtures(name: &str) -> String {
format!("tests/fixtures/{name}")
}
#[test]
fn output_diffx_default() {
diffx()
.arg(fixtures("file1.json"))
.arg(fixtures("file2.json"))
.assert()
.failure()
.code(1)
.stdout(predicate::str::contains("~").or(predicate::str::contains("+")));
}
#[test]
fn output_diffx_explicit() {
diffx()
.arg("--output")
.arg("diffx")
.arg(fixtures("file1.json"))
.arg(fixtures("file2.json"))
.assert()
.failure()
.code(1);
}
#[test]
fn output_diffx_symbols() {
diffx()
.arg(fixtures("file1.json"))
.arg(fixtures("file2.json"))
.assert()
.failure()
.stdout(predicate::str::contains("~")); }
#[test]
fn output_json() {
diffx()
.arg("--output")
.arg("json")
.arg(fixtures("file1.json"))
.arg(fixtures("file2.json"))
.assert()
.failure()
.code(1)
.stdout(predicate::str::starts_with("["))
.stdout(predicate::str::contains("Modified").or(predicate::str::contains("Added")));
}
#[test]
fn output_json_valid_syntax() {
let output = diffx()
.arg("--output")
.arg("json")
.arg(fixtures("file1.json"))
.arg(fixtures("file2.json"))
.output()
.unwrap();
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
serde_json::from_str::<serde_json::Value>(&stdout).is_ok(),
"Output should be valid JSON: {stdout}"
);
}
#[test]
fn output_json_no_diff() {
let output = diffx()
.arg("--output")
.arg("json")
.arg(fixtures("file1.json"))
.arg(fixtures("file1.json"))
.output()
.unwrap();
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.trim() == "[]" || stdout.trim().is_empty(),
"No diff should output empty array: {stdout}"
);
}
#[test]
fn output_yaml() {
diffx()
.arg("--output")
.arg("yaml")
.arg(fixtures("file1.json"))
.arg(fixtures("file2.json"))
.assert()
.failure()
.code(1)
.stdout(predicate::str::contains("Modified").or(predicate::str::contains("Added")));
}
#[test]
fn output_no_color() {
let output = diffx()
.arg("--no-color")
.arg(fixtures("file1.json"))
.arg(fixtures("file2.json"))
.output()
.unwrap();
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
!stdout.contains("\x1b["),
"Output should not contain ANSI codes: {stdout}"
);
}
#[test]
fn output_quiet_with_diff() {
diffx()
.arg("--quiet")
.arg(fixtures("file1.json"))
.arg(fixtures("file2.json"))
.assert()
.failure()
.code(1)
.stdout(predicate::str::is_empty());
}
#[test]
fn output_quiet_no_diff() {
diffx()
.arg("--quiet")
.arg(fixtures("file1.json"))
.arg(fixtures("file1.json"))
.assert()
.success()
.code(0)
.stdout(predicate::str::is_empty());
}
#[test]
fn output_quiet_short_form() {
diffx()
.arg("-q")
.arg(fixtures("file1.json"))
.arg(fixtures("file2.json"))
.assert()
.failure()
.code(1)
.stdout(predicate::str::is_empty());
}
#[test]
fn output_brief_with_diff() {
diffx()
.arg("--brief")
.arg(fixtures("file1.json"))
.arg(fixtures("file2.json"))
.assert()
.failure()
.code(1)
.stdout(predicate::str::contains("differ"));
}
#[test]
fn output_brief_no_diff() {
diffx()
.arg("--brief")
.arg(fixtures("file1.json"))
.arg(fixtures("file1.json"))
.assert()
.success()
.code(0)
.stdout(predicate::str::is_empty());
}
#[test]
fn output_verbose() {
diffx()
.arg("--verbose")
.arg(fixtures("file1.json"))
.arg(fixtures("file2.json"))
.assert()
.failure()
.code(1)
.stderr(predicate::str::contains("Input file information"))
.stderr(predicate::str::contains("bytes"));
}
#[test]
fn output_verbose_short_form() {
diffx()
.arg("-v")
.arg(fixtures("file1.json"))
.arg(fixtures("file2.json"))
.assert()
.failure()
.code(1)
.stderr(predicate::str::contains("Input"));
}
#[test]
fn output_help() {
diffx()
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("Usage"))
.stdout(predicate::str::contains("Options"));
}
#[test]
fn output_help_short() {
diffx()
.arg("-h")
.assert()
.success()
.stdout(predicate::str::contains("Usage"));
}
#[test]
fn output_version() {
diffx()
.arg("--version")
.assert()
.success()
.stdout(predicate::str::contains("diffx"));
}
#[test]
fn output_version_short() {
diffx()
.arg("-V")
.assert()
.success()
.stdout(predicate::str::contains("diffx"));
}