john-slop/tests/example_fixtures.rs

229 lines
7.9 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use std::{collections::BTreeSet, fs, path::Path, process::Command};
use john_slop::{
AnalysisContext, Config, ConfigLoader, ConfigPaths, Detector, apply_replacements,
plan_replacements,
};
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct Manifest {
schema_version: u32,
targets: BTreeSet<String>,
cases: Vec<Case>,
}
#[derive(Debug, Deserialize)]
struct Case {
path: String,
formatted: String,
expected_rules: BTreeSet<String>,
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::<BTreeSet<_>>();
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::<BTreeSet<_>>();
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
);
}
}
#[test]
fn diagnostic_showcase_spans_prose_source_and_severities() {
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
let config_home = tempfile::tempdir().expect("temp config home");
let output = Command::new(env!("CARGO_BIN_EXE_john-slop"))
.env("XDG_CONFIG_HOME", config_home.path())
.args(["--output", "miette", "--fail-level", "low", "--config"])
.arg(root.join("examples/diagnostic-showcase.toml"))
.arg(root.join("examples/diagnostic-showcase.txt"))
.arg(root.join("examples/diagnostic-showcase/source.rs"))
.output()
.expect("run expected-failure diagnostic showcase");
let stdout = String::from_utf8(output.stdout).expect("UTF-8 diagnostic output");
// `--nocapture` makes this a convenient manual tour of every diagnostic.
print!("{stdout}");
assert_eq!(output.status.code(), Some(1));
assert!(output.stderr.is_empty());
assert!(stdout.contains("assistant-self-identification"));
assert!(stdout.contains("prompt-response-scaffolding"));
assert!(stdout.contains("unicode-ascii-replacement"));
assert!(stdout.contains("unicode-invisible-control"));
assert!(stdout.contains("source-placeholder-todo"));
assert!(stdout.contains("source-tutorial-narration"));
assert!(stdout.contains('‐'));
assert!(stdout.contains('-'));
assert!(stdout.contains("low-care evidence score: 71 (medium)"));
assert!(stdout.matches("low-care evidence score:").count() >= 2);
assert!(stdout.contains("advice"));
assert!(stdout.contains("warning"));
assert!(stdout.contains("error"));
#[cfg(feature = "fancy-diagnostics")]
{
assert!(stdout.contains('\u{1b}'));
assert!(stdout.contains('╭'));
assert!(stdout.matches("◆ ").count() >= 12);
let lines = stdout.lines().collect::<Vec<_>>();
for (index, line) in lines.iter().enumerate() {
if line.starts_with("◆ ") {
assert!(!lines[index + 1].is_empty());
}
if line.contains("help:") {
assert_eq!(lines[index + 1], "");
}
}
}
}
#[test]
fn commented_base_and_named_configs_form_a_valid_layered_example() {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/config");
let config = ConfigLoader::from_paths(ConfigPaths {
directory: root.clone(),
base: root.join("config.toml"),
named: Some(root.join("editorial.toml")),
explicit: None,
})
.load()
.expect("load commented sample layers");
assert_eq!(config.input.max_document_bytes, 4_194_304);
assert_eq!(config.thresholds.low, 15);
assert_eq!(config.thresholds.medium, 40);
assert_eq!(config.thresholds.high, 70);
assert!(
!config
.replacements
.iter()
.find(|replacement| replacement.id == "em-dash")
.expect("embedded replacement remains present")
.enabled
);
let rule = config
.rules
.iter()
.find(|rule| rule.id == "assistant-self-identification")
.expect("embedded rule remains present");
assert_eq!(rule.scope.include, vec!["articles/**", "docs/**"]);
assert!(
!config
.rules
.iter()
.find(|rule| rule.id == "custom-review-marker")
.expect("runtime diagnostic example remains present")
.enabled
);
}