use std::{collections::BTreeSet, fs, path::Path, process::Command}; use john_slop::{AnalysisContext, Config, Detector, apply_replacements, plan_replacements}; use serde::Deserialize; #[derive(Debug, Deserialize)] struct Manifest { schema_version: u32, targets: BTreeSet, cases: Vec, } #[derive(Debug, Deserialize)] struct Case { path: String, formatted: String, expected_rules: BTreeSet, expected_replacements: usize, } fn load_manifest(root: &Path) -> Manifest { let source = fs::read_to_string(root.join("manifest.toml")).expect("read fixture manifest"); toml::from_str(&source).expect("parse fixture manifest") } fn apply_planned_edits(source: &str, config: &Config) -> String { apply_replacements(source, &plan_replacements(config, source)) .expect("apply valid planned edits") .into_owned() } #[test] fn shared_examples_pin_detection_and_formatted_text() { let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/fixtures"); let manifest = load_manifest(&root); assert_eq!(manifest.schema_version, 1); assert_eq!( manifest.targets, ["cli", "fmt", "lsp"].map(String::from).into() ); let config = Config::embedded(); let detector = Detector::new(&config).expect("build detector"); for case in manifest.cases { let source = fs::read_to_string(root.join(&case.path)).expect("read example input"); let expected = fs::read_to_string(root.join(&case.formatted)).expect("read formatted text"); let analysis = detector.analyze_with(&source, &AnalysisContext::for_path(&case.path)); let actual_rules = analysis .findings .into_iter() .map(|finding| finding.rule_id.to_string()) .collect::>(); let edits = plan_replacements(&config, &source); assert_eq!(actual_rules, case.expected_rules, "rules for {}", case.path); assert_eq!( edits.len(), case.expected_replacements, "replacement count for {}", case.path ); assert_eq!( apply_planned_edits(&source, &config), expected, "formatted output for {}", case.path ); } } #[test] fn detector_cli_accepts_every_shared_example() { let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/fixtures"); let manifest = load_manifest(&root); for case in manifest.cases { let output = Command::new(env!("CARGO_BIN_EXE_john-slop")) .args(["--output", "json"]) .arg(root.join(&case.path)) .output() .expect("run detector against example"); assert!( output.status.success(), "CLI failed for {}: {}", case.path, String::from_utf8_lossy(&output.stderr) ); let record: serde_json::Value = serde_json::from_slice(&output.stdout).expect("parse detector JSON record"); let actual_rules = record["findings"] .as_array() .expect("findings array") .iter() .map(|finding| { finding["rule_id"] .as_str() .expect("rule ID string") .to_owned() }) .collect::>(); assert_eq!( actual_rules, case.expected_rules, "CLI rules for {}", case.path ); } } #[test] fn formatter_cli_matches_every_shared_expected_file() { let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/fixtures"); let manifest = load_manifest(&root); let config_home = tempfile::tempdir().expect("temp config home"); for case in manifest.cases { let expected = fs::read(root.join(&case.formatted)).expect("read expected formatted file"); let output = Command::new(env!("CARGO_BIN_EXE_john-slop-fmt")) .env("XDG_CONFIG_HOME", config_home.path()) .arg(root.join(&case.path)) .output() .expect("run formatter against example"); assert!( output.status.success(), "formatter failed for {}: {}", case.path, String::from_utf8_lossy(&output.stderr) ); assert!(output.stderr.is_empty(), "stderr for {}", case.path); assert_eq!( output.stdout, expected, "formatter output for {}", case.path ); } }