From 501328d05f75bd97d009472b85f37be8aa80766e Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Wed, 30 Sep 2026 01:06:42 +0200 Subject: [PATCH] feat: add runtime diagnostic catalog workflow --- README.md | 4 + .../assets/diagnostics-template.toml | 27 +++++ crates/john-slop-core/src/config.rs | 47 +++++++++ crates/john-slop-core/src/lib.rs | 2 +- docs/architecture.md | 14 +-- docs/configuration.md | 48 +++++++++ docs/editors.md | 6 +- docs/heuristics.md | 6 ++ docs/usage.md | 13 ++- examples/config/README.md | 13 +++ examples/config/diagnostics.toml | 24 +++++ src/bin/john-slop-fmt.rs | 11 ++- src/bin/john-slop-lsp.rs | 7 +- src/bin/john-slop.rs | 89 +++++++++++++++-- tasks.md | 2 +- tests/adapter_help.rs | 2 + tests/cli.rs | 98 +++++++++++++++++++ tests/example_fixtures.rs | 8 ++ tests/lsp.rs | 75 +++++++++++++- ...p_is_successful_and_names_the_command.snap | 2 + 20 files changed, 475 insertions(+), 23 deletions(-) create mode 100644 crates/john-slop-core/assets/diagnostics-template.toml create mode 100644 examples/config/diagnostics.toml diff --git a/README.md b/README.md index a1c96a6..083b781 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,12 @@ or individual-rule switches as needed: ```sh john-slop config init +john-slop config init-diagnostics ``` +The second command creates a runtime rule catalog: exact-phrase and regex +diagnostics can then be added without changing or recompiling the program. + Diagnostics are colored Miette reports by default. Make the command fail when a document reaches a chosen low-care evidence score with `--fail-score 0..100`: diff --git a/crates/john-slop-core/assets/diagnostics-template.toml b/crates/john-slop-core/assets/diagnostics-template.toml new file mode 100644 index 0000000..cb2ed4a --- /dev/null +++ b/crates/john-slop-core/assets/diagnostics-template.toml @@ -0,0 +1,27 @@ +# Runtime diagnostic catalog. Copy or generate this as `diagnostics.toml` and +# add data-driven rules without rebuilding John Slop. +schema_version = 1 + +# This starter rule is disabled until its phrase and metadata fit your policy. +# Duplicate this complete table for additional diagnostics; every ID must be +# unique lowercase kebab-case. +[[rules]] +id = "custom-review-marker" +category = "style" +severity = "warning" +matcher = "exact-phrase" +pattern = "REVIEW BEFORE PUBLISHING" +weight = 8.0 +message = "a project review marker remains in the source" +help = "Resolve or remove the marker before publishing." +occurrence_cap = 1 +enabled = false + +[rules.scope] +content = ["prose", "source-code"] +extensions = [] +include = [] +exclude = [] +skip_generated = true +skip_vendor = true +skip_minified = true diff --git a/crates/john-slop-core/src/config.rs b/crates/john-slop-core/src/config.rs index 8122f18..7479a97 100644 --- a/crates/john-slop-core/src/config.rs +++ b/crates/john-slop-core/src/config.rs @@ -16,12 +16,18 @@ use crate::{Category, FindingSeverity}; const EMBEDDED_CONFIG: &str = include_str!("../assets/config.toml"); const EMBEDDED_HEURISTICS: &str = include_str!("../assets/heuristics.toml"); const EMBEDDED_UNICODE: &str = include_str!("../assets/unicode.toml"); +const DIAGNOSTICS_TEMPLATE: &str = include_str!("../assets/diagnostics-template.toml"); /// Return the commented base configuration suitable for a user or project layer. pub const fn default_config_template() -> &'static str { EMBEDDED_CONFIG } +/// Return a commented, valid starter catalog for runtime diagnostics. +pub const fn default_diagnostics_template() -> &'static str { + DIAGNOSTICS_TEMPLATE +} + /// A validated named configuration without path syntax or a file extension. #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ConfigName(String); @@ -250,6 +256,18 @@ pub enum ConfigError { /// Unsupported schema version. actual: u32, }, + /// A runtime catalog attempted to introduce a rule backed by internal code. + #[error("runtime diagnostic `{id}` cannot introduce the code-backed `{matcher}` matcher")] + #[diagnostic( + code(john_slop::config::diagnostics_matcher), + help("Use exact-phrase, regex, or repeated-regex for runtime rules.") + )] + UnsupportedDiagnosticMatcher { + /// New rule ID. + id: String, + /// Rejected matcher name. + matcher: &'static str, + }, /// Figment could not read, parse, merge, or deserialize a source. #[error("failed to load embedded configuration")] #[diagnostic(code(john_slop::config::embedded_invalid))] @@ -491,6 +509,21 @@ fn apply_diagnostic_file(config: &mut Config, path: &std::path::Path) -> Result< actual: catalog.schema_version, }); } + for rule in &catalog.rules { + if config.rules.iter().all(|existing| existing.id != rule.id) { + let matcher = match rule.matcher { + Some(MatcherType::Replacement) => Some("replacement"), + Some(MatcherType::Invisible) => Some("invisible"), + _ => None, + }; + if let Some(matcher) = matcher { + return Err(ConfigError::UnsupportedDiagnosticMatcher { + id: rule.id.clone(), + matcher, + }); + } + } + } merge_rules(&mut config.rules, catalog.rules, path) } @@ -1549,6 +1582,20 @@ scope = { content = ["prose", "source-code"], extensions = [] } .unwrap_err(); assert!(matches!(error, ConfigError::Layer(layer) if layer.path == catalog)); + std::fs::write( + &catalog, + "schema_version = 1\n[[rules]]\nid = \"unsafe-runtime\"\nmatcher = \"replacement\"\n", + ) + .unwrap(); + let error = ConfigLoader::from_paths(paths.clone()) + .with_diagnostic_files([catalog.clone()]) + .load() + .unwrap_err(); + assert!(matches!( + error, + ConfigError::UnsupportedDiagnosticMatcher { .. } + )); + std::fs::write( &catalog, "schema_version = 1\n[[rules]]\nid = \"incomplete-rule\"\n", diff --git a/crates/john-slop-core/src/lib.rs b/crates/john-slop-core/src/lib.rs index d6bdbfb..1264cce 100644 --- a/crates/john-slop-core/src/lib.rs +++ b/crates/john-slop-core/src/lib.rs @@ -19,7 +19,7 @@ pub use config::{ ConfigOverrides, ConfigPaths, ConfigValidationError, ConfusablePolicy, ContentKind, DiagnosticCategories, DiagnosticPolicy, EvidenceThresholds, HeuristicRule, InputLimits, InputOverrides, MatcherType, ReplacementRule, RuleScope, ThresholdOverrides, UnicodePolicy, - default_config_template, + default_config_template, default_diagnostics_template, }; pub use detector::{AnalysisContext, Detector, DetectorBuildError, Rule}; pub use diagnostic::FindingDiagnostic; diff --git a/docs/architecture.md b/docs/architecture.md index 471550f..727ac3d 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -35,8 +35,8 @@ workspace Tokio requirement enables no features: the LSP library selects The modules within those crates retain deliberately narrow responsibilities: -- `config` owns validated embedded defaults, the generated Unicode policy, and - input limits; +- `config` owns validated embedded defaults, restricted runtime diagnostic + catalogs, the generated Unicode policy, and input limits; - `input` streams files, directory entries, and archive members as documents; - `detector` compiles configured matchers and path scopes once, then produces explainable findings and a bounded low-care evidence score; @@ -77,10 +77,12 @@ points and presentation-neutral types: `FindingDiagnostic` is the library bridge to `miette`; CLI-specific concise and JSON rendering stays in the detector adapter so it cannot alter analysis. -`ConfigLoader` applies embedded, user base, named user, explicit file, and CLI -layers in order. Stable-ID merging and semantic validation happen before a -configuration reaches analysis. Keeping these operations behind the facade -avoids duplicating policy across the three binaries. +`ConfigLoader` applies embedded catalogs, runtime diagnostic catalogs, user +base, named user, explicit file, and CLI layers in order. Stable-ID merging and +semantic validation happen before a configuration reaches analysis. Keeping +these operations behind the facade avoids duplicating policy across the three +binaries. Runtime catalogs are declarative: they can add rules using existing +matcher implementations but cannot load executable code. The formatter adapter owns filesystem mutation. It resolves configuration and preflights every requested document before writing, then uses same-directory diff --git a/docs/configuration.md b/docs/configuration.md index fd45e7c..b849e60 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -48,6 +48,24 @@ The command refuses to overwrite an existing file. Use `john-slop --config config.toml path/to/input` or inspect it with the command shown below. +Generate a dedicated runtime diagnostic catalog separately: + +```sh +john-slop config init-diagnostics +``` + +Place the result at the `user_diagnostics` path printed by `config paths` for +automatic loading, or select one or more project/team catalogs explicitly: + +```sh +john-slop --diagnostics team.toml --diagnostics project.toml article.md +john-slop-lsp --diagnostics project.toml +``` + +Explicit catalogs are applied in command-line order. All catalogs load before +`config.toml`, named configuration, and `--config`, allowing those normal +layers to override their rules. + ## Schema The current `schema_version` is `1`. The complete defaults live in @@ -102,6 +120,36 @@ can disable them or adjust severity, weight, occurrence cap, and scope without changing code. Invisible findings never authorize edits; generated confusable mappings follow the bulk policy below. +## Runtime diagnostic catalog + +`diagnostics.toml` supports new data-driven diagnostics without recompilation. +Every new rule must provide the complete rule shape: + +```toml +schema_version = 1 + +[[rules]] +id = "custom-review-marker" +category = "style" +severity = "warning" +matcher = "exact-phrase" +pattern = "REVIEW BEFORE PUBLISHING" +weight = 8.0 +message = "a project review marker remains" +help = "Resolve or remove the marker before publishing." +occurrence_cap = 1 +enabled = true +scope = { content = ["prose", "source-code"], extensions = [] } +``` + +Runtime rules may select `exact-phrase`, `regex`, or `repeated-regex`. +`replacement` and `invisible` are code-backed catalog sentinels and cannot +define new matching behavior. Categories remain the four stable values listed +above. Unknown fields, invalid or empty-match regular expressions, duplicate +IDs within one catalog, incomplete new rules, and missing explicit catalogs are +fatal. These constraints keep startup deterministic and prevent a configuration +file from executing code. + The generated UTS #39 mappings have one bulk policy: ```toml diff --git a/docs/editors.md b/docs/editors.md index 9ab1a59..f378176 100644 --- a/docs/editors.md +++ b/docs/editors.md @@ -109,8 +109,10 @@ formatter among multiple attached language servers. The server uses the same configuration layers as the other binaries. Add `args = ["--config-name", "editorial"]` to the Helix language-server table, or append those arguments to the Vim `cmd` list, when an editor-specific named -configuration is wanted. `--log-json` is available for troubleshooting and -writes lifecycle metadata to stderr without document contents. +configuration is wanted. Use `args = ["--diagnostics", "diagnostics.toml"]` (and +the equivalent Vim command argument) for a project runtime diagnostic catalog. +`--log-json` is available for troubleshooting and writes lifecycle metadata to +stderr without document contents. ## Try the shared fixtures diff --git a/docs/heuristics.md b/docs/heuristics.md index 495616a..a4e9291 100644 --- a/docs/heuristics.md +++ b/docs/heuristics.md @@ -12,6 +12,12 @@ minified paths decide whether each rule applies. Findings are deduplicated by rule and byte span, then sorted by start offset, end offset, and rule ID so every adapter observes the same order. +Additional exact-phrase, regex, and repeated-regex rules can be supplied at +runtime through `diagnostics.toml` or repeatable `--diagnostics` arguments. +They receive the same validation, scoping, scoring, ordering, CLI presentation, +and LSP publication as embedded rules. New matcher algorithms and categories +remain code changes rather than executable configuration. + The generated Unicode 16.0 profile maps every non-ASCII scalar with a printable ASCII UTS #39 skeleton, alongside ten manually reviewed punctuation mappings. The default `replace` policy gives each of these findings a formatter/LSP edit; diff --git a/docs/usage.md b/docs/usage.md index 868be9c..6e62217 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -62,7 +62,8 @@ the formula and [Limitations](limitations.md) before using thresholds in review workflows. Configuration layers can be selected with `--config-name NAME` and `--config -FILE`; command-line resource limits are the highest-precedence input overrides. +FILE`; repeat `--diagnostics FILE` to load runtime rule catalogs without +recompilation. Command-line resource limits are the highest-precedence input overrides. Use `john-slop config show` to print the resolved TOML and `john-slop config paths` to inspect source precedence. See [Configuration](configuration.md) for the schema and merge rules. @@ -113,7 +114,10 @@ The modes are mutually exclusive: - argument, configuration, UTF-8, input, or write failures exit `2`. All modes use the same embedded/user/named/explicit configuration precedence as -the detector. `--config-name NAME` and `--config FILE` select those layers. +the detector. `--config-name NAME`, `--config FILE`, and repeatable +`--diagnostics FILE` select those layers. Diagnostic rules do not change the +formatter's replacement plan, but catalog validation stays consistent across +all adapters. Replacement sources use deterministic longest-match semantics, and formatting twice is byte-for-byte identical to formatting once. @@ -136,6 +140,7 @@ it without positional arguments: john-slop-lsp john-slop-lsp --config-name editorial john-slop-lsp --config ./john-slop.toml +john-slop-lsp --diagnostics ./diagnostics.toml ``` The server advertises full-document synchronization and quick-fix code actions. @@ -151,7 +156,9 @@ code, the configured severity and remediation text, document-version metadata, and an exact UTF-16 range. Advice maps to LSP information, while warning and error retain their corresponding LSP severities. -Quick fixes remain intentionally narrower than diagnostics: they are recomputed +Runtime catalog rules are reloaded by +`workspace/didChangeConfiguration` together with normal configuration. Quick +fixes remain intentionally narrower than diagnostics: they are recomputed from the current snapshot with the shared replacement planner and returned only for findings carrying safe configured ASCII edits. Style, prompt-leakage, and source-review findings are diagnostic-only. A request carrying stale John Slop diff --git a/examples/config/README.md b/examples/config/README.md index a3c0d82..c89e78a 100644 --- a/examples/config/README.md +++ b/examples/config/README.md @@ -20,3 +20,16 @@ john-slop config init ``` It preserves an existing `config.toml` unless `--force` is explicit. + +[`diagnostics.toml`](diagnostics.toml) is a complete runtime rule definition. +Place it at the `user_diagnostics` path reported by `john-slop config paths` for +automatic loading, or keep it with a project and select it explicitly: + +```sh +john-slop --diagnostics examples/config/diagnostics.toml article.md +john-slop-lsp --diagnostics examples/config/diagnostics.toml +``` + +Generate the commented starter with `john-slop config init-diagnostics`. The +starter rule is disabled until its phrase and metadata are intentionally +customized. diff --git a/examples/config/diagnostics.toml b/examples/config/diagnostics.toml new file mode 100644 index 0000000..98550de --- /dev/null +++ b/examples/config/diagnostics.toml @@ -0,0 +1,24 @@ +# Runtime diagnostic catalog. Add data-driven rules here without rebuilding +# John Slop, then pass this file with `--diagnostics`. +schema_version = 1 + +[[rules]] +id = "custom-review-marker" +category = "style" +severity = "warning" +matcher = "exact-phrase" +pattern = "REVIEW BEFORE PUBLISHING" +weight = 8.0 +message = "a project review marker remains in the source" +help = "Resolve or remove the marker before publishing." +occurrence_cap = 1 +enabled = false + +[rules.scope] +content = ["prose", "source-code"] +extensions = [] +include = [] +exclude = [] +skip_generated = true +skip_vendor = true +skip_minified = true diff --git a/src/bin/john-slop-fmt.rs b/src/bin/john-slop-fmt.rs index b5496a8..ebda339 100644 --- a/src/bin/john-slop-fmt.rs +++ b/src/bin/john-slop-fmt.rs @@ -36,6 +36,10 @@ struct Cli { #[arg(long, value_name = "FILE")] config: Option, + /// Load a runtime diagnostic catalog; may be supplied more than once. + #[arg(long, value_name = "FILE", action = clap::ArgAction::Append)] + diagnostics: Vec, + /// Exit 1 if any input would change; do not emit formatted text. #[arg(long)] check: bool, @@ -126,7 +130,9 @@ fn main() -> ExitCode { fn run(cli: &Cli) -> Result { let mode = mode(cli); validate_invocation(cli, mode)?; - let config = ConfigLoader::discover(cli.config_name.as_ref(), cli.config.clone())?.load()?; + let config = ConfigLoader::discover(cli.config_name.as_ref(), cli.config.clone())? + .with_diagnostic_files(cli.diagnostics.clone()) + .load()?; let documents = load_documents(cli.path.as_deref(), cli.recursive, &config)?; let formatted = format_documents(documents, &config)?; let changed = formatted.iter().any(FormattedDocument::changed); @@ -333,12 +339,15 @@ mod tests { "--recursive", "--config-name", "strict", + "--diagnostics", + "team.toml", "docs", ]) .unwrap(); assert_eq!(mode(&write), Mode::Write); assert!(write.recursive); assert_eq!(write.config_name.unwrap().as_str(), "strict"); + assert_eq!(write.diagnostics, [PathBuf::from("team.toml")]); assert!(Cli::try_parse_from(["john-slop-fmt", "--check", "--write", "input.txt"]).is_err()); } diff --git a/src/bin/john-slop-lsp.rs b/src/bin/john-slop-lsp.rs index 257a359..e36ec3c 100644 --- a/src/bin/john-slop-lsp.rs +++ b/src/bin/john-slop-lsp.rs @@ -27,6 +27,10 @@ struct Cli { #[arg(long, value_name = "FILE")] config: Option, + /// Load a runtime diagnostic catalog; may be supplied more than once. + #[arg(long, value_name = "FILE", action = clap::ArgAction::Append)] + diagnostics: Vec, + /// Emit newline-delimited JSON lifecycle logs to standard error. #[arg(long)] log_json: bool, @@ -35,7 +39,8 @@ struct Cli { #[tokio::main] async fn main() -> Result<()> { let cli = Cli::parse(); - let loader = ConfigLoader::discover(cli.config_name.as_ref(), cli.config)?; + let loader = ConfigLoader::discover(cli.config_name.as_ref(), cli.config)? + .with_diagnostic_files(cli.diagnostics); let config = loader.load()?; let (service, socket) = LspService::new(|client| { // `ConfigLoader::load` validated this exact snapshot immediately above. diff --git a/src/bin/john-slop.rs b/src/bin/john-slop.rs index 0fdb6b0..e537241 100644 --- a/src/bin/john-slop.rs +++ b/src/bin/john-slop.rs @@ -14,7 +14,7 @@ use clap::{Parser, Subcommand, ValueEnum}; use globset::{Glob, GlobSet, GlobSetBuilder}; use john_slop::{ AnalysisContext, Config, ConfigLoader, ConfigName, ConfigOverrides, Detector, InputOverrides, - SourceId, default_config_template, read_inputs, read_stdin, + SourceId, default_config_template, default_diagnostics_template, read_inputs, read_stdin, }; use miette::{IntoDiagnostic, Result, WrapErr}; @@ -39,6 +39,10 @@ struct Cli { #[arg(long, global = true, value_name = "FILE")] config: Option, + /// Load a runtime diagnostic catalog; may be supplied more than once. + #[arg(long, global = true, value_name = "FILE", action = clap::ArgAction::Append)] + diagnostics: Vec, + /// Override the maximum accepted bytes for one input document. #[arg(long, global = true, value_name = "BYTES")] max_document_bytes: Option, @@ -129,6 +133,13 @@ enum ConfigCommand { #[arg(long)] force: bool, }, + /// Generate a commented diagnostics.toml starter in the current directory. + #[command(visible_alias = "diagnostics-init")] + InitDiagnostics { + /// Replace an existing diagnostics.toml instead of refusing to overwrite it. + #[arg(long)] + force: bool, + }, /// Print the fully resolved, validated configuration as deterministic TOML. Show, /// Print every selected configuration path in precedence order. @@ -165,6 +176,7 @@ struct RunOutcome { /// Execute one parsed detector invocation and retain threshold/error state. fn run(cli: &Cli) -> Result { let loader = ConfigLoader::discover(cli.config_name.as_ref(), cli.config.clone())? + .with_diagnostic_files(cli.diagnostics.clone()) .with_overrides(ConfigOverrides { input: InputOverrides { max_document_bytes: cli.max_document_bytes, @@ -180,6 +192,7 @@ fn run(cli: &Cli) -> Result { if let Some(Command::Config { command }) = &cli.command { match command { ConfigCommand::Init { force } => write_default_config(*force)?, + ConfigCommand::InitDiagnostics { force } => write_default_diagnostics(*force)?, ConfigCommand::Show => { let config = loader.load()?; print!("{}", toml::to_string_pretty(&config).into_diagnostic()?); @@ -261,7 +274,32 @@ fn run(cli: &Cli) -> Result { } fn write_default_config(force: bool) -> Result<()> { - let path = Path::new("config.toml"); + write_template( + Path::new("config.toml"), + default_config_template(), + force, + "config init --force", + ) +} + +fn write_default_diagnostics(force: bool) -> Result<()> { + write_template( + Path::new("diagnostics.toml"), + default_diagnostics_template(), + force, + "config init-diagnostics --force", + ) +} + +fn write_template(path: &Path, contents: &str, force: bool, force_hint: &str) -> Result<()> { + if force + && std::fs::symlink_metadata(path).is_ok_and(|metadata| metadata.file_type().is_symlink()) + { + return Err(miette::miette!( + "refusing to replace symlink `{}`", + path.display() + )); + } let mut options = OpenOptions::new(); options.write(true).create(true); if force { @@ -271,15 +309,18 @@ fn write_default_config(force: bool) -> Result<()> { } let mut file = options.open(path).into_diagnostic().wrap_err_with(|| { if path.exists() && !force { - "refusing to overwrite ./config.toml; rerun with `config init --force`".to_owned() + format!( + "refusing to overwrite ./{}; rerun with `{force_hint}`", + path.display() + ) } else { - "failed to create ./config.toml".to_owned() + format!("failed to create ./{}", path.display()) } })?; - file.write_all(default_config_template().as_bytes()) + file.write_all(contents.as_bytes()) .into_diagnostic() - .wrap_err("failed to write ./config.toml")?; - println!("wrote ./config.toml"); + .wrap_err_with(|| format!("failed to write ./{}", path.display()))?; + println!("wrote ./{}", path.display()); Ok(()) } @@ -334,6 +375,17 @@ fn print_config_paths(loader: &ConfigLoader) { let paths = loader.paths(); println!("embedded_config = \"\""); println!("embedded_heuristics = \"\""); + println!( + "user_diagnostics = {:?}", + paths.diagnostics().display().to_string() + ); + if loader.diagnostic_files().is_empty() { + println!("explicit_diagnostics = \"\""); + } else { + for path in loader.diagnostic_files() { + println!("explicit_diagnostics = {:?}", path.display().to_string()); + } + } println!("user_base = {:?}", paths.base.display().to_string()); println!( "user_named = {:?}", @@ -376,6 +428,7 @@ mod tests { assert_eq!(cli.fail_level, None); assert_eq!(cli.output, OutputFormat::Miette); assert_eq!(cli.config_name, None); + assert!(cli.diagnostics.is_empty()); assert!(cli.command.is_none()); assert_eq!( cli.paths, @@ -428,6 +481,28 @@ mod tests { command: ConfigCommand::Init { force: true } }) )); + + let diagnostics = Cli::try_parse_from([ + "john-slop", + "config", + "init-diagnostics", + "--force", + "--diagnostics", + "team.toml", + "--diagnostics", + "project.toml", + ]) + .unwrap(); + assert_eq!( + diagnostics.diagnostics, + [PathBuf::from("team.toml"), PathBuf::from("project.toml")] + ); + assert!(matches!( + diagnostics.command, + Some(Command::Config { + command: ConfigCommand::InitDiagnostics { force: true } + }) + )); } #[test] diff --git a/tasks.md b/tasks.md index 7ee26e0..bf47c79 100644 --- a/tasks.md +++ b/tasks.md @@ -612,7 +612,7 @@ metadata and binary versions agree on `0.1.23`. configuration overrides, and accept repeatable explicit diagnostic catalogs. Restrict these files to schema metadata and `[[rules]]`, retain merge-by-ID, reject malformed/incomplete/duplicate definitions, and test precedence. -- [ ] Add repeatable `--diagnostics ` support to the detector, formatter, +- [x] Add repeatable `--diagnostics ` support to the detector, formatter, and LSP adapters; add a safe command that generates a valid commented `./diagnostics.toml` starter without overwriting it unless forced. Cover CLI detection, LSP reload, help, missing files, and generated-template loading. diff --git a/tests/adapter_help.rs b/tests/adapter_help.rs index c26a089..5eb4b2e 100644 --- a/tests/adapter_help.rs +++ b/tests/adapter_help.rs @@ -17,6 +17,7 @@ fn formatter_help_contains_helix_and_vim_setup() { assert!(stdout.contains("Helix ~/.config/helix/languages.toml")); assert!(stdout.contains("formatter = { command = \"john-slop-fmt\" }")); assert!(stdout.contains("setlocal formatprg=john-slop-fmt")); + assert!(stdout.contains("--diagnostics ")); } #[test] @@ -32,4 +33,5 @@ fn lsp_help_contains_helix_and_vim_setup() { assert!(stdout.contains("--log-json")); assert!(stdout.contains("lsp#register_server")); assert!(stdout.contains("['john-slop-lsp']")); + assert!(stdout.contains("--diagnostics ")); } diff --git a/tests/cli.rs b/tests/cli.rs index 08c8494..7434374 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -57,6 +57,7 @@ fn config_help_lists_generation_and_inspection_commands() { assert!(output.status.success()); assert!(stdout.contains("init")); + assert!(stdout.contains("init-diagnostics")); assert!(stdout.contains("show")); assert!(stdout.contains("paths")); } @@ -193,6 +194,101 @@ fn config_init_writes_valid_defaults_and_requires_force_to_overwrite() { assert_eq!(fs::read_to_string(&config).unwrap(), generated); } +#[test] +fn diagnostics_init_generates_a_loadable_runtime_catalog() { + let directory = tempdir().expect("temp dir"); + let catalog = directory.path().join("diagnostics.toml"); + let config_home = directory.path().join("config-home"); + + let created = detector() + .current_dir(directory.path()) + .env("XDG_CONFIG_HOME", &config_home) + .args(["config", "init-diagnostics"]) + .output() + .expect("generate diagnostic catalog"); + assert!(created.status.success()); + assert_eq!(created.stdout, b"wrote ./diagnostics.toml\n"); + let generated = fs::read_to_string(&catalog).unwrap(); + let parsed = toml::from_str::(&generated).expect("valid catalog TOML"); + assert_eq!(parsed["schema_version"].as_integer(), Some(1)); + assert_eq!(parsed["rules"].as_array().unwrap().len(), 1); + + let loaded = detector() + .current_dir(directory.path()) + .env("XDG_CONFIG_HOME", &config_home) + .args(["config", "show", "--diagnostics", "diagnostics.toml"]) + .output() + .expect("load generated catalog"); + assert!( + loaded.status.success(), + "{}", + String::from_utf8_lossy(&loaded.stderr) + ); + let resolved = toml::from_slice::(&loaded.stdout).unwrap(); + assert_eq!(resolved["rules"].as_array().unwrap().len(), 13); + + let refused = detector() + .current_dir(directory.path()) + .env("XDG_CONFIG_HOME", &config_home) + .args(["config", "init-diagnostics"]) + .output() + .unwrap(); + assert_eq!(refused.status.code(), Some(2)); + assert_eq!(fs::read_to_string(&catalog).unwrap(), generated); + + fs::write(&catalog, "invalid").unwrap(); + let forced = detector() + .current_dir(directory.path()) + .env("XDG_CONFIG_HOME", &config_home) + .args(["config", "diagnostics-init", "--force"]) + .output() + .unwrap(); + assert!(forced.status.success()); + assert_eq!(fs::read_to_string(&catalog).unwrap(), generated); +} + +#[test] +fn explicit_runtime_catalog_adds_a_diagnostic_without_recompilation() { + let directory = tempdir().expect("temp dir"); + let source = directory.path().join("article.md"); + let catalog = directory.path().join("diagnostics.toml"); + fs::write(&source, "REVIEW BEFORE PUBLISHING\n").unwrap(); + fs::write( + &catalog, + r#"schema_version = 1 +[[rules]] +id = "custom-review-marker" +category = "style" +severity = "warning" +matcher = "exact-phrase" +pattern = "REVIEW BEFORE PUBLISHING" +weight = 8.0 +message = "a project review marker remains" +help = "Resolve or remove the marker before publishing." +occurrence_cap = 1 +enabled = true +scope = { content = ["prose", "source-code"], extensions = [] } +"#, + ) + .unwrap(); + + let output = detector() + .env( + "XDG_CONFIG_HOME", + directory.path().join("empty-config-home"), + ) + .args(["--output", "json", "--diagnostics"]) + .arg(&catalog) + .arg(&source) + .output() + .expect("run detector with runtime catalog"); + + assert!(output.status.success()); + let records = json_records(&output); + assert_eq!(records[0]["findings"].as_array().unwrap().len(), 1); + assert_eq!(records[0]["findings"][0]["rule_id"], "custom-review-marker"); +} + #[test] fn category_and_individual_configuration_disable_cli_diagnostics() { let directory = tempdir().expect("temp dir"); @@ -262,6 +358,8 @@ fn config_paths_lists_embedded_and_platform_sources() { assert!(output.status.success()); let stdout = String::from_utf8(output.stdout).expect("UTF-8 stdout"); assert!(stdout.contains("")); + assert!(stdout.contains("diagnostics.toml")); + assert!(stdout.contains("explicit_diagnostics")); assert!(stdout.contains("config.toml")); assert!(stdout.contains("")); } diff --git a/tests/example_fixtures.rs b/tests/example_fixtures.rs index dde3b8b..a31a818 100644 --- a/tests/example_fixtures.rs +++ b/tests/example_fixtures.rs @@ -217,4 +217,12 @@ fn commented_base_and_named_configs_form_a_valid_layered_example() { .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 + ); } diff --git a/tests/lsp.rs b/tests/lsp.rs index bf08108..7a1de82 100644 --- a/tests/lsp.rs +++ b/tests/lsp.rs @@ -19,23 +19,37 @@ struct Harness { socket: tower_lsp_server::ClientSocket, _directory: TempDir, explicit: Option, + diagnostics: Option, } impl Harness { async fn new(explicit_config: Option<&str>) -> Self { + Self::new_with_diagnostics(explicit_config, None).await + } + + async fn new_with_diagnostics( + explicit_config: Option<&str>, + diagnostic_catalog: Option<&str>, + ) -> Self { let directory = tempfile::tempdir().unwrap(); let explicit = explicit_config.map(|contents| { let path = directory.path().join("lsp.toml"); fs::write(&path, contents).unwrap(); path }); + let diagnostics = diagnostic_catalog.map(|contents| { + let path = directory.path().join("runtime-diagnostics.toml"); + fs::write(&path, contents).unwrap(); + path + }); let paths = ConfigPaths { directory: directory.path().to_owned(), base: directory.path().join("missing-base.toml"), named: None, explicit: explicit.clone(), }; - let loader = ConfigLoader::from_paths(paths); + let loader = + ConfigLoader::from_paths(paths).with_diagnostic_files(diagnostics.iter().cloned()); let config = loader.load().unwrap(); let (service, socket) = LspService::new(|client| { LspBackend::new(client, loader, config).expect("build test LSP backend") @@ -45,6 +59,7 @@ impl Harness { socket, _directory: directory, explicit, + diagnostics, }; let response = harness .request("initialize", json!({ "capabilities": {} }), 1) @@ -105,6 +120,30 @@ impl Harness { fn explicit_path(&self) -> &std::path::Path { self.explicit.as_deref().unwrap() } + + fn diagnostics_path(&self) -> &std::path::Path { + self.diagnostics.as_deref().unwrap() + } +} + +fn custom_catalog(enabled: bool) -> String { + format!( + r#"schema_version = 1 + +[[rules]] +id = "custom-review-marker" +category = "style" +severity = "warning" +matcher = "exact-phrase" +pattern = "REVIEW BEFORE PUBLISHING" +weight = 8.0 +message = "a project review marker remains" +help = "Resolve or remove the marker before publishing." +occurrence_cap = 1 +enabled = {enabled} +scope = {{ content = ["prose", "source-code"], extensions = [] }} +"# + ) } fn open_params(uri: &str, version: i32, text: &str) -> Value { @@ -564,6 +603,40 @@ async fn disabled_category_suppresses_lsp_diagnostics() { assert!(published["diagnostics"].as_array().unwrap().is_empty()); } +#[tokio::test] +async fn runtime_diagnostic_catalog_loads_and_reloads_without_recompilation() { + let enabled = custom_catalog(true); + let mut harness = Harness::new_with_diagnostics(None, Some(&enabled)).await; + let uri = "file:///tmp/catalog.md"; + harness + .notify( + "textDocument/didOpen", + open_params(uri, 1, "REVIEW BEFORE PUBLISHING"), + ) + .await; + let published = harness + .notification("textDocument/publishDiagnostics") + .await; + let diagnostics = published["diagnostics"].as_array().unwrap(); + assert_eq!(diagnostics.len(), 1); + assert_eq!(diagnostics[0]["code"], "custom-review-marker"); + + fs::write(harness.diagnostics_path(), custom_catalog(false)).unwrap(); + let reload = Request::build("workspace/didChangeConfiguration") + .params(json!({ "settings": {} })) + .finish(); + let service = &mut harness.service; + let socket = &mut harness.socket; + let reload_future = async { service.ready().await.unwrap().call(reload).await.unwrap() }; + let notification_future = async { socket.next().await.unwrap() }; + let (reload_response, notification) = tokio::join!(reload_future, notification_future); + + assert!(reload_response.is_none()); + let refreshed = notification.params().cloned().unwrap(); + assert_eq!(refreshed["uri"], uri); + assert!(refreshed["diagnostics"].as_array().unwrap().is_empty()); +} + #[tokio::test] async fn malformed_messages_and_invalid_uris_are_rejected_without_panics() { assert!("not JSON".parse::().is_err()); diff --git a/tests/snapshots/cli__help_is_successful_and_names_the_command.snap b/tests/snapshots/cli__help_is_successful_and_names_the_command.snap index 40067d4..88fdf1c 100644 --- a/tests/snapshots/cli__help_is_successful_and_names_the_command.snap +++ b/tests/snapshots/cli__help_is_successful_and_names_the_command.snap @@ -19,6 +19,8 @@ Options: Load a named user configuration from the platform config directory --config Load an explicit TOML configuration after user configuration + --diagnostics + Load a runtime diagnostic catalog; may be supplied more than once --max-document-bytes Override the maximum accepted bytes for one input document --max-archive-bytes