test: cover configuration and diagnostic branches
This commit is contained in:
parent
5442f3ccec
commit
8d1965b4c5
322
src/config.rs
322
src/config.rs
|
|
@ -1473,4 +1473,326 @@ weight = 9.5
|
|||
|
||||
assert!(matches!(error, ConfigError::Layer(layer) if layer.path == path));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn all_cli_scalar_overrides_are_applied_and_serialize_compactly() {
|
||||
let overrides = ConfigOverrides {
|
||||
input: InputOverrides {
|
||||
max_document_bytes: Some(11),
|
||||
max_archive_bytes: Some(22),
|
||||
max_expanded_bytes: Some(33),
|
||||
max_archive_entries: Some(44),
|
||||
max_compression_ratio: Some(55),
|
||||
max_walk_depth: Some(66),
|
||||
},
|
||||
thresholds: ThresholdOverrides {
|
||||
low: Some(10),
|
||||
medium: Some(40),
|
||||
high: Some(90),
|
||||
},
|
||||
};
|
||||
let mut config = Config::embedded();
|
||||
|
||||
apply_cli_overrides(&mut config, &overrides);
|
||||
|
||||
assert_eq!(config.input.max_document_bytes, 11);
|
||||
assert_eq!(config.input.max_archive_bytes, 22);
|
||||
assert_eq!(config.input.max_expanded_bytes, 33);
|
||||
assert_eq!(config.input.max_archive_entries, 44);
|
||||
assert_eq!(config.input.max_compression_ratio, 55);
|
||||
assert_eq!(config.input.max_walk_depth, 66);
|
||||
assert_eq!(
|
||||
config.thresholds,
|
||||
ConfidenceThresholds {
|
||||
low: 10,
|
||||
medium: 40,
|
||||
high: 90
|
||||
}
|
||||
);
|
||||
assert_eq!(toml::to_string(&ConfigOverrides::default()).unwrap(), "");
|
||||
let serialized = toml::to_string(&overrides).unwrap();
|
||||
assert!(serialized.contains("max_archive_entries = 44"));
|
||||
assert!(serialized.contains("high = 90"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_existing_patches_and_input_layers_update_every_field() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
std::fs::write(
|
||||
directory.path().join("config.toml"),
|
||||
r#"
|
||||
schema_version = 1
|
||||
|
||||
[input]
|
||||
max_document_bytes = 101
|
||||
max_archive_bytes = 102
|
||||
max_expanded_bytes = 103
|
||||
max_archive_entries = 104
|
||||
max_compression_ratio = 105
|
||||
max_walk_depth = 106
|
||||
|
||||
[thresholds]
|
||||
low = 10
|
||||
medium = 40
|
||||
high = 90
|
||||
|
||||
[[replacements]]
|
||||
id = "em-dash"
|
||||
source = "⸺"
|
||||
replacement = "--"
|
||||
rationale = "Test every replacement patch field."
|
||||
enabled = false
|
||||
|
||||
[[rules]]
|
||||
id = "templated-conclusion"
|
||||
category = "source-code"
|
||||
severity = "error"
|
||||
matcher = "exact-phrase"
|
||||
pattern = "patched phrase"
|
||||
weight = 7.5
|
||||
message = "patched message"
|
||||
help = "patched help"
|
||||
occurrence_cap = 4
|
||||
enabled = false
|
||||
scope = { content = ["source-code"], extensions = ["rs"], include = ["src/**"], exclude = ["vendor/**"], skip_generated = false, skip_vendor = false, skip_minified = false }
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let config = ConfigLoader::from_paths(ConfigPaths::in_directory(
|
||||
directory.path().to_owned(),
|
||||
None,
|
||||
None,
|
||||
))
|
||||
.load()
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(config.input.max_document_bytes, 101);
|
||||
assert_eq!(config.input.max_archive_bytes, 102);
|
||||
assert_eq!(config.input.max_expanded_bytes, 103);
|
||||
assert_eq!(config.input.max_archive_entries, 104);
|
||||
assert_eq!(config.input.max_compression_ratio, 105);
|
||||
assert_eq!(config.input.max_walk_depth, 106);
|
||||
assert_eq!(
|
||||
config.thresholds,
|
||||
ConfidenceThresholds {
|
||||
low: 10,
|
||||
medium: 40,
|
||||
high: 90
|
||||
}
|
||||
);
|
||||
let replacement = config
|
||||
.replacements
|
||||
.iter()
|
||||
.find(|rule| rule.id == "em-dash")
|
||||
.unwrap();
|
||||
assert_eq!(replacement.source, "⸺");
|
||||
assert_eq!(replacement.replacement, "--");
|
||||
assert_eq!(replacement.rationale, "Test every replacement patch field.");
|
||||
assert!(!replacement.enabled);
|
||||
let rule = config
|
||||
.rules
|
||||
.iter()
|
||||
.find(|rule| rule.id == "templated-conclusion")
|
||||
.unwrap();
|
||||
assert_eq!(rule.category, Category::SourceCode);
|
||||
assert_eq!(rule.severity, FindingSeverity::Error);
|
||||
assert_eq!(rule.matcher, MatcherType::ExactPhrase);
|
||||
assert_eq!(rule.pattern, "patched phrase");
|
||||
assert_eq!(rule.weight, 7.5);
|
||||
assert_eq!(rule.message, "patched message");
|
||||
assert_eq!(rule.help, "patched help");
|
||||
assert_eq!(rule.occurrence_cap, 4);
|
||||
assert!(!rule.enabled);
|
||||
assert_eq!(rule.scope.extensions, ["rs"]);
|
||||
assert!(!rule.scope.skip_generated);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validation_reports_each_semantic_error_family() {
|
||||
fn assert_invalid(
|
||||
mutator: impl FnOnce(&mut Config),
|
||||
expected: fn(&ConfigValidationError) -> bool,
|
||||
) {
|
||||
let mut config = Config::embedded();
|
||||
mutator(&mut config);
|
||||
let error = config.validate().unwrap_err();
|
||||
assert!(expected(&error), "unexpected validation error: {error:?}");
|
||||
}
|
||||
|
||||
assert_invalid(
|
||||
|config| config.schema_version = 2,
|
||||
|error| {
|
||||
matches!(
|
||||
error,
|
||||
ConfigValidationError::UnsupportedSchema { actual: 2 }
|
||||
)
|
||||
},
|
||||
);
|
||||
for field in 0..6 {
|
||||
assert_invalid(
|
||||
|config| match field {
|
||||
0 => config.input.max_document_bytes = 0,
|
||||
1 => config.input.max_archive_bytes = 0,
|
||||
2 => config.input.max_expanded_bytes = 0,
|
||||
3 => config.input.max_archive_entries = 0,
|
||||
4 => config.input.max_compression_ratio = 0,
|
||||
_ => config.input.max_walk_depth = 0,
|
||||
},
|
||||
|error| matches!(error, ConfigValidationError::ZeroInputLimit { .. }),
|
||||
);
|
||||
}
|
||||
assert_invalid(
|
||||
|config| config.replacements[0].id = "Bad_ID".into(),
|
||||
|error| {
|
||||
matches!(
|
||||
error,
|
||||
ConfigValidationError::InvalidId {
|
||||
kind: "replacement",
|
||||
..
|
||||
}
|
||||
)
|
||||
},
|
||||
);
|
||||
assert_invalid(
|
||||
|config| config.replacements[1].id = config.replacements[0].id.clone(),
|
||||
|error| {
|
||||
matches!(
|
||||
error,
|
||||
ConfigValidationError::DuplicateId {
|
||||
kind: "replacement",
|
||||
..
|
||||
}
|
||||
)
|
||||
},
|
||||
);
|
||||
assert_invalid(
|
||||
|config| config.replacements[0].rationale = " ".into(),
|
||||
|error| {
|
||||
matches!(
|
||||
error,
|
||||
ConfigValidationError::EmptyField {
|
||||
kind: "replacement",
|
||||
field: "rationale",
|
||||
..
|
||||
}
|
||||
)
|
||||
},
|
||||
);
|
||||
assert_invalid(
|
||||
|config| config.rules[0].id = "-bad".into(),
|
||||
|error| matches!(error, ConfigValidationError::InvalidId { kind: "rule", .. }),
|
||||
);
|
||||
assert_invalid(
|
||||
|config| config.rules[1].id = config.rules[0].id.clone(),
|
||||
|error| {
|
||||
matches!(
|
||||
error,
|
||||
ConfigValidationError::DuplicateId { kind: "rule", .. }
|
||||
)
|
||||
},
|
||||
);
|
||||
assert_invalid(
|
||||
|config| config.rules[0].message.clear(),
|
||||
|error| {
|
||||
matches!(
|
||||
error,
|
||||
ConfigValidationError::EmptyField {
|
||||
kind: "rule",
|
||||
field: "message",
|
||||
..
|
||||
}
|
||||
)
|
||||
},
|
||||
);
|
||||
assert_invalid(
|
||||
|config| config.rules[0].weight = -1.0,
|
||||
|error| matches!(error, ConfigValidationError::InvalidWeight { .. }),
|
||||
);
|
||||
assert_invalid(
|
||||
|config| config.rules[0].occurrence_cap = 0,
|
||||
|error| matches!(error, ConfigValidationError::ZeroOccurrenceCap { .. }),
|
||||
);
|
||||
assert_invalid(
|
||||
|config| {
|
||||
config.rules[0].matcher = MatcherType::Regex;
|
||||
config.rules[0].pattern = "a*".into();
|
||||
},
|
||||
|error| matches!(error, ConfigValidationError::EmptyMatch { .. }),
|
||||
);
|
||||
assert_invalid(
|
||||
|config| {
|
||||
config.rules[0].matcher = MatcherType::Replacement;
|
||||
config.rules[0].pattern = "wrong".into();
|
||||
},
|
||||
|error| {
|
||||
matches!(
|
||||
error,
|
||||
ConfigValidationError::InvalidReplacementMatcher { .. }
|
||||
)
|
||||
},
|
||||
);
|
||||
assert_invalid(
|
||||
|config| config.rules[0].scope.content.clear(),
|
||||
|error| matches!(error, ConfigValidationError::EmptyScope { .. }),
|
||||
);
|
||||
assert_invalid(
|
||||
|config| config.rules[0].scope.extensions = vec!["Rust".into()],
|
||||
|error| matches!(error, ConfigValidationError::InvalidExtension { .. }),
|
||||
);
|
||||
assert_invalid(
|
||||
|config| config.rules[0].scope.exclude.push("[".into()),
|
||||
|error| {
|
||||
matches!(
|
||||
error,
|
||||
ConfigValidationError::InvalidGlob {
|
||||
field: "exclude",
|
||||
..
|
||||
}
|
||||
)
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicate_and_incomplete_new_layer_entries_are_actionable() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let path = directory.path().join("config.toml");
|
||||
std::fs::write(
|
||||
&path,
|
||||
"[[replacements]]\nid = \"em-dash\"\n[[replacements]]\nid = \"em-dash\"\n",
|
||||
)
|
||||
.unwrap();
|
||||
let error = ConfigLoader::from_paths(ConfigPaths::in_directory(
|
||||
directory.path().to_owned(),
|
||||
None,
|
||||
None,
|
||||
))
|
||||
.load()
|
||||
.unwrap_err();
|
||||
assert!(matches!(
|
||||
error,
|
||||
ConfigError::DuplicateLayerId {
|
||||
kind: "replacement",
|
||||
..
|
||||
}
|
||||
));
|
||||
|
||||
std::fs::write(&path, "[[rules]]\nid = \"new-rule\"\n").unwrap();
|
||||
let error = ConfigLoader::from_paths(ConfigPaths::in_directory(
|
||||
directory.path().to_owned(),
|
||||
None,
|
||||
None,
|
||||
))
|
||||
.load()
|
||||
.unwrap_err();
|
||||
assert!(matches!(
|
||||
error,
|
||||
ConfigError::IncompleteEntry {
|
||||
kind: "rule",
|
||||
field: "category",
|
||||
..
|
||||
}
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -123,5 +123,34 @@ mod tests {
|
|||
&source[label.offset()..label.offset() + label.len()],
|
||||
finding.matched_text
|
||||
);
|
||||
assert_eq!(diagnostic.rule_id(), &finding.rule_id);
|
||||
assert_eq!(diagnostic.finding_severity(), FindingSeverity::Warning);
|
||||
assert_eq!(diagnostic.span(), finding.byte_span.clone().into());
|
||||
assert_eq!(diagnostic.to_string(), finding.message);
|
||||
assert!(diagnostic.source_code().is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_finding_severity_maps_to_miette_and_help_remains_optional() {
|
||||
for (finding_severity, miette_severity) in [
|
||||
(FindingSeverity::Advice, Severity::Advice),
|
||||
(FindingSeverity::Warning, Severity::Warning),
|
||||
(FindingSeverity::Error, Severity::Error),
|
||||
] {
|
||||
let finding = Finding {
|
||||
rule_id: RuleId::from_static("severity-test"),
|
||||
category: crate::Category::Style,
|
||||
severity: finding_severity,
|
||||
message: "review this".into(),
|
||||
help: None,
|
||||
byte_span: 0..1,
|
||||
matched_text: "x".into(),
|
||||
weight: 1.0,
|
||||
fix: None,
|
||||
};
|
||||
let diagnostic = FindingDiagnostic::new(&SourceId::Stdin, "x", &finding);
|
||||
assert_eq!(diagnostic.severity(), Some(miette_severity));
|
||||
assert!(diagnostic.help().is_none());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
30
src/model.rs
30
src/model.rs
|
|
@ -344,4 +344,34 @@ mod tests {
|
|||
assert_eq!(summary.findings.errors, 0);
|
||||
assert_eq!(summary.findings.total(), analysis.findings.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enum_names_are_stable_for_display_and_serialization() {
|
||||
for (category, expected) in [
|
||||
(Category::Typography, "typography"),
|
||||
(Category::Style, "style"),
|
||||
(Category::PromptLeakage, "prompt-leakage"),
|
||||
(Category::SourceCode, "source-code"),
|
||||
] {
|
||||
assert_eq!(category.as_str(), expected);
|
||||
assert_eq!(category.to_string(), expected);
|
||||
}
|
||||
for (severity, expected) in [
|
||||
(FindingSeverity::Advice, "advice"),
|
||||
(FindingSeverity::Warning, "warning"),
|
||||
(FindingSeverity::Error, "error"),
|
||||
] {
|
||||
assert_eq!(severity.as_str(), expected);
|
||||
assert_eq!(severity.to_string(), expected);
|
||||
}
|
||||
for (band, expected) in [
|
||||
(ConfidenceBand::BelowThreshold, "below-threshold"),
|
||||
(ConfidenceBand::Low, "low"),
|
||||
(ConfidenceBand::Medium, "medium"),
|
||||
(ConfidenceBand::High, "high"),
|
||||
] {
|
||||
assert_eq!(band.as_str(), expected);
|
||||
assert_eq!(band.to_string(), expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
15
tasks.md
15
tasks.md
|
|
@ -240,6 +240,21 @@ Accepted on 2026-09-29 after the full all-feature/minimal-feature quality gate,
|
|||
dependency policy checks, fuzz-seed runs, reproducible packaging, and an 80.51%
|
||||
line-coverage run.
|
||||
|
||||
## Milestone 8 — Meaningful coverage expansion
|
||||
|
||||
- [x] Cover configuration merging, validation failures, domain formatting, and
|
||||
diagnostic accessors with behavior-focused unit tests.
|
||||
- [ ] Cover archive and input resource-policy branches with generated fixtures,
|
||||
including unsafe, binary, malformed, and oversized members.
|
||||
- [ ] Cover remaining practical CLI, formatter, and LSP adapter branches without
|
||||
coupling tests to unreachable platform failures or coverage-tool artifacts.
|
||||
- [ ] Raise measured Tarpaulin line coverage toward 90%, refresh badges and
|
||||
development documentation, and record any intentionally uncovered remainder.
|
||||
|
||||
Acceptance: the suite exercises at least 90% of measurable source lines when
|
||||
Tarpaulin can attribute them reliably; remaining misses are documented and do
|
||||
not justify brittle tests or production changes made solely for instrumentation.
|
||||
|
||||
## Deferred by design
|
||||
|
||||
- Statistical or model-based authorship classification.
|
||||
|
|
|
|||
Loading…
Reference in New Issue