use std::collections::BTreeMap;
use crate::baseline::{Baseline, BaselineEntry};
use crate::finding_anchor::FindingAnchor;
use crate::ratchet::{DEFAULT_RATCHET_PATH, Ratchet, RatchetEntry};
use crate::state_check::{StateCheckViolation, compare_state};
fn ratchet_with(production: &[(&str, f64, u32)], dependencies: &[(&str, f64, u32)]) -> Ratchet {
let mut ratchet = Ratchet::default();
for &(metric, ceiling, metric_version) in production {
ratchet.source_sets.production.insert(
metric.to_owned(),
RatchetEntry {
ceiling,
metric_version,
},
);
}
for &(metric, ceiling, metric_version) in dependencies {
ratchet.dependencies.insert(
metric.to_owned(),
RatchetEntry {
ceiling,
metric_version,
},
);
}
ratchet
}
fn baseline_with(
production: &[(&str, u32, Vec<FindingAnchor>)],
dependencies: &[(&str, u32, Vec<FindingAnchor>)],
) -> Baseline {
let mut baseline = Baseline::default();
for (metric, metric_version, findings) in production {
baseline.source_sets.production.insert(
(*metric).to_owned(),
BaselineEntry {
metric_version: *metric_version,
findings: findings.clone(),
},
);
}
for (metric, metric_version, findings) in dependencies {
baseline.dependencies.insert(
(*metric).to_owned(),
BaselineEntry {
metric_version: *metric_version,
findings: findings.clone(),
},
);
}
baseline
}
#[test]
fn first_time_adoption_and_absent_files_pass() {
let renames = BTreeMap::new();
let head_ratchet = ratchet_with(&[("erosion", 0.2, 1)], &[]);
let head_baseline = baseline_with(&[("erosion", 1, vec![FindingAnchor::id_only("a")])], &[]);
assert!(compare_state(DEFAULT_RATCHET_PATH, None, None, None, None, &renames).passed());
assert!(
compare_state(
DEFAULT_RATCHET_PATH,
None,
Some(&head_ratchet),
None,
Some(&head_baseline),
&renames,
)
.passed()
);
}
#[test]
fn ratchet_deletion_entry_removal_and_ceiling_raise_are_reported() {
let renames = BTreeMap::new();
let base = ratchet_with(
&[("erosion", 0.15, 1), ("duplication", 0.10, 2)],
&[("unused_dependencies", 0.05, 1)],
);
let deleted = compare_state(
"custom/ratchet.json",
Some(&base),
None,
None,
None,
&renames,
);
assert_eq!(
deleted.violations,
vec![StateCheckViolation::RatchetFileDeleted {
path: "custom/ratchet.json".to_owned(),
}]
);
let head = ratchet_with(
&[("erosion", 0.25, 1), ("verbosity", 0.30, 2)],
&[("unused_dependencies", 0.02, 1)],
);
let compared = compare_state(
DEFAULT_RATCHET_PATH,
Some(&base),
Some(&head),
None,
None,
&renames,
);
assert_eq!(
compared.violations,
vec![
StateCheckViolation::RatchetEntryRemoved {
source_set: Some("production".to_owned()),
metric: "duplication".to_owned(),
base_ceiling: 0.10,
metric_version: 2,
},
StateCheckViolation::RatchetCeilingRaised {
source_set: Some("production".to_owned()),
metric: "erosion".to_owned(),
base_ceiling: 0.15,
head_ceiling: 0.25,
metric_version: 1,
},
]
);
}
#[test]
fn ratchet_tightening_new_entries_and_metric_version_upgrades_pass() {
let renames = BTreeMap::new();
let base = ratchet_with(&[("erosion", 0.15, 1), ("duplication", 0.10, 1)], &[]);
let head = ratchet_with(
&[
("erosion", 0.10, 1),
("duplication", 0.45, 2),
("verbosity", 0.20, 2),
],
&[("unused_dependencies", 0.05, 1)],
);
let compared = compare_state(
DEFAULT_RATCHET_PATH,
Some(&base),
Some(&head),
None,
None,
&renames,
);
assert!(
compared.passed(),
"unexpected violations: {:?}",
compared.violations
);
}
#[test]
fn baseline_removal_finding_removal_and_metric_version_upgrade_pass() {
let renames = BTreeMap::new();
let f1 = FindingAnchor::from_hashes("id-1".to_owned(), 0x11, Some(0x1), Some(0x2), Some(10));
let f2 = FindingAnchor::from_hashes("id-2".to_owned(), 0x22, Some(0x3), Some(0x4), Some(20));
let f3 = FindingAnchor::from_hashes("id-3".to_owned(), 0x33, Some(0x5), Some(0x6), Some(30));
let base = baseline_with(
&[
("erosion", 1, vec![f1.clone(), f2.clone()]),
("duplication", 1, vec![f1.clone()]),
],
&[],
);
assert!(
compare_state(
DEFAULT_RATCHET_PATH,
None,
None,
Some(&base),
None,
&renames
)
.passed()
);
let head = baseline_with(
&[
("erosion", 1, vec![f1.clone()]),
("duplication", 2, vec![f2, f3]),
("verbosity", 2, Vec::new()),
],
&[],
);
let compared = compare_state(
DEFAULT_RATCHET_PATH,
None,
None,
Some(&base),
Some(&head),
&renames,
);
assert!(
compared.passed(),
"unexpected violations: {:?}",
compared.violations
);
}
#[test]
fn baseline_added_findings_swapped_finding_and_new_metric_key_fail() {
let renames = BTreeMap::new();
let f1 = FindingAnchor::from_hashes("id-1".to_owned(), 0x11, None, None, Some(10));
let f2 = FindingAnchor::from_hashes("id-2".to_owned(), 0x22, None, None, Some(20));
let f3 = FindingAnchor::from_hashes("id-3".to_owned(), 0x33, None, None, Some(30));
let base = baseline_with(&[("erosion", 1, vec![f1.clone()])], &[]);
let head = baseline_with(
&[
("erosion", 1, vec![f2.clone()]),
("verbosity", 2, vec![f3.clone()]),
],
&[],
);
let compared = compare_baselines(&base, &head, &renames);
assert_eq!(
compared.violations,
vec![
baseline_added("erosion", 1, 1, &["id-2"], 1),
baseline_added("verbosity", 0, 1, &["id-3"], 2),
]
);
}
fn compare_baselines(
base: &Baseline,
head: &Baseline,
renames: &BTreeMap<String, String>,
) -> crate::StateCheckComparison {
compare_state(
DEFAULT_RATCHET_PATH,
None,
None,
Some(base),
Some(head),
renames,
)
}
fn baseline_added(
metric: &str,
base_count: usize,
head_count: usize,
added_ids: &[&str],
metric_version: u32,
) -> StateCheckViolation {
StateCheckViolation::BaselineFindingsAdded {
source_set: Some("production".to_owned()),
metric: metric.to_owned(),
base_count,
head_count,
added_count: added_ids.len(),
added_ids: added_ids.iter().map(|id| (*id).to_owned()).collect(),
metric_version,
}
}
#[test]
fn baseline_matches_v1_and_v2_findings_across_file_renames() {
let renames = BTreeMap::from([("src/old.ts".to_owned(), "src/new.ts".to_owned())]);
let no_renames = BTreeMap::new();
let v1_base = baseline_with(
&[(
"erosion",
1,
vec![FindingAnchor::id_only("src/old.ts:10:fn")],
)],
&[],
);
let v1_head = baseline_with(
&[(
"erosion",
1,
vec![FindingAnchor::id_only("src/new.ts:10:fn")],
)],
&[],
);
assert!(
compare_state(
DEFAULT_RATCHET_PATH,
None,
None,
Some(&v1_base),
Some(&v1_head),
&renames,
)
.passed()
);
assert!(
!compare_state(
DEFAULT_RATCHET_PATH,
None,
None,
Some(&v1_base),
Some(&v1_head),
&no_renames,
)
.passed()
);
let v2_base = baseline_with(
&[(
"hazards",
1,
vec![FindingAnchor::from_hashes(
"src/old.ts:12:rule".to_owned(),
0xaaaa,
Some(0x10),
Some(0x20),
Some(12),
)],
)],
&[],
);
let v2_head = baseline_with(
&[(
"hazards",
1,
vec![FindingAnchor::from_hashes(
"src/new.ts:12:rule".to_owned(),
0xbbbb,
Some(0x10),
Some(0x20),
Some(12),
)],
)],
&[],
);
assert!(
compare_state(
DEFAULT_RATCHET_PATH,
None,
None,
Some(&v2_base),
Some(&v2_head),
&renames,
)
.passed()
);
assert!(
!compare_state(
DEFAULT_RATCHET_PATH,
None,
None,
Some(&v2_base),
Some(&v2_head),
&no_renames,
)
.passed()
);
}
#[test]
fn baseline_matches_opaque_hash_anchors_with_body_and_paths_across_renames() {
let renames = BTreeMap::from([("src/legacy.ts".to_owned(), "src/moved.ts".to_owned())]);
let no_renames = BTreeMap::new();
let base_anchor = FindingAnchor::from_hashes(
"1111111111111111".to_owned(),
0xaaaa_aaaa_aaaa_aaaa,
Some(0x10),
Some(0x20),
Some(1),
)
.with_body_and_paths(
Some(0xdead_beef_cafe_0001),
vec!["src/legacy.ts".to_owned()],
);
let moved_anchor = FindingAnchor::from_hashes(
"2222222222222222".to_owned(),
0xbbbb_bbbb_bbbb_bbbb,
Some(0x10),
Some(0x20),
Some(1),
)
.with_body_and_paths(Some(0xdead_beef_cafe_0001), vec!["src/moved.ts".to_owned()]);
let added_same_body = FindingAnchor::from_hashes(
"3333333333333333".to_owned(),
0xcccc_cccc_cccc_cccc,
Some(0x30),
Some(0x40),
Some(40),
)
.with_body_and_paths(Some(0xdead_beef_cafe_0001), vec!["src/moved.ts".to_owned()]);
let added_other_body = FindingAnchor::from_hashes(
"4444444444444444".to_owned(),
0xdddd_dddd_dddd_dddd,
Some(0x50),
Some(0x60),
Some(60),
)
.with_body_and_paths(Some(0xdead_beef_cafe_0002), vec!["src/moved.ts".to_owned()]);
let base = baseline_with(&[("erosion", 1, vec![base_anchor])], &[]);
let head_moved = baseline_with(&[("erosion", 1, vec![moved_anchor.clone()])], &[]);
assert!(
compare_state(
DEFAULT_RATCHET_PATH,
None,
None,
Some(&base),
Some(&head_moved),
&renames,
)
.passed()
);
assert!(
!compare_state(
DEFAULT_RATCHET_PATH,
None,
None,
Some(&base),
Some(&head_moved),
&no_renames,
)
.passed()
);
let head_with_duplicate = baseline_with(
&[("erosion", 1, vec![moved_anchor.clone(), added_same_body])],
&[],
);
let dup_check = compare_baselines(&base, &head_with_duplicate, &renames);
assert_eq!(
dup_check.violations,
vec![baseline_added("erosion", 1, 2, &["3333333333333333"], 1)]
);
let head_with_new = baseline_with(&[("erosion", 1, vec![moved_anchor, added_other_body])], &[]);
let new_check = compare_baselines(&base, &head_with_new, &renames);
assert_eq!(
new_check.violations,
vec![baseline_added("erosion", 1, 2, &["4444444444444444"], 1)]
);
}