gruff-rs 0.3.0

Rust static analyzer and quality linter for CI: dead-code, complexity, security, secrets, and architecture rules with deterministic SARIF/JSON output and baseline support.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
use super::*;

#[test]
pub(crate) fn baseline_generation_and_exact_suppression_are_stable() {
    let _guard = analysis_lock();
    let dir = tempdir().expect("tempdir");
    fs::write(dir.path().join("README.md"), "# Fixture\n").expect("readme write");
    fs::write(
        dir.path().join("sample.rs"),
        [
            r#"pub fn process(command: String) {
    "#,
            PROCESS_COMMAND_NEW,
            r#"("sh").arg(command).spawn().unwrap();
}
"#,
        ]
        .concat(),
    )
    .expect("fixture write");

    let before = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from("sample.rs")],
            no_config: true,
            no_baseline: true,
            ..default_test_options()
        },
    )
    .expect("analysis succeeds");
    assert!(!before.findings.is_empty());

    let baseline_path = dir.path().join("baseline.json");
    write_baseline(&baseline_path, std::slice::from_ref(&before.findings[0]))
        .expect("baseline write");

    let suppressed = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from("sample.rs")],
            baseline: Some(PathBuf::from("baseline.json")),
            no_config: true,
            no_baseline: false,
            ..default_test_options()
        },
    )
    .expect("analysis succeeds");
    assert_eq!(suppressed.findings.len(), before.findings.len() - 1);
    assert_eq!(
        suppressed
            .baseline
            .as_ref()
            .map(|baseline| baseline.suppressed),
        Some(1)
    );

    let mut baseline_json: Value =
        serde_json::from_str(&fs::read_to_string(&baseline_path).expect("baseline read"))
            .expect("baseline json");
    baseline_json["entries"][0]["message"] = json!("changed message stays suppressible");
    fs::write(
        &baseline_path,
        serde_json::to_string_pretty(&baseline_json).expect("baseline serialize"),
    )
    .expect("baseline rewrite");
    let message_changed = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from("sample.rs")],
            baseline: Some(PathBuf::from("baseline.json")),
            no_config: true,
            no_baseline: false,
            ..default_test_options()
        },
    )
    .expect("analysis succeeds");
    assert_eq!(
        message_changed
            .baseline
            .as_ref()
            .map(|baseline| baseline.suppressed),
        Some(1)
    );

    baseline_json["entries"][0]["filePath"] = json!("other.rs");
    fs::write(
        &baseline_path,
        serde_json::to_string_pretty(&baseline_json).expect("baseline serialize"),
    )
    .expect("baseline rewrite");
    let file_changed = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from("sample.rs")],
            baseline: Some(PathBuf::from("baseline.json")),
            no_config: true,
            no_baseline: false,
            ..default_test_options()
        },
    )
    .expect("analysis succeeds");
    assert_eq!(file_changed.findings.len(), before.findings.len());
    assert_eq!(
        file_changed
            .baseline
            .as_ref()
            .map(|baseline| baseline.suppressed),
        Some(0)
    );
}

#[test]
pub(crate) fn baseline_generation_and_failure_modes_are_reported_cleanly() {
    let _guard = analysis_lock();
    let dir = tempdir().expect("tempdir");
    fs::write(dir.path().join("README.md"), "# Fixture\n").expect("readme write");
    fs::write(dir.path().join("sample.rs"), "pub fn process() {}\n").expect("fixture write");

    let generated_path = dir.path().join("baseline.json");
    let generated = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from("sample.rs")],
            generate_baseline: Some(PathBuf::from("baseline.json")),
            no_config: true,
            ..default_test_options()
        },
    )
    .expect("baseline generation succeeds");
    assert!(generated
        .baseline
        .as_ref()
        .is_some_and(|baseline| baseline.generated));
    let baseline_json: Value =
        serde_json::from_str(&fs::read_to_string(&generated_path).expect("baseline read"))
            .expect("baseline json");
    assert_eq!(baseline_json["schemaVersion"], "gruff.baseline.v1");
    assert!(baseline_json["entries"].as_array().is_some());

    fs::write(
        dir.path().join("bad-baseline.json"),
        r#"{ "schemaVersion": "wrong", "entries": [] }"#,
    )
    .expect("bad baseline write");
    let invalid_schema = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from("sample.rs")],
            baseline: Some(PathBuf::from("bad-baseline.json")),
            no_config: true,
            no_baseline: false,
            ..default_test_options()
        },
    )
    .expect_err("invalid baseline schema rejected");
    assert!(invalid_schema.contains("unsupported baseline schema"));

    let missing = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from("sample.rs")],
            baseline: Some(PathBuf::from("missing-baseline.json")),
            no_config: true,
            no_baseline: false,
            ..default_test_options()
        },
    )
    .expect_err("missing baseline rejected");
    assert!(missing.contains("unable to read baseline"));
}

#[test]
pub(crate) fn baseline_deltas_do_not_over_count_duplicate_findings() {
    // PR #3 review: `run_analysis_in_project` used to call
    // `resolve_baseline` BEFORE `sort_and_dedupe_findings`, so a
    // duplicated raw finding (same fingerprint emitted twice) would be
    // counted twice toward `introduced` even though the final report
    // collapses it to one entry. Pin the dedupe-then-baseline order
    // here by constructing a fixture that produces duplicates and
    // asserting introduced == 1 (not 2).
    let _guard = analysis_lock();
    let dir = tempdir().expect("tempdir");
    fs::write(dir.path().join("README.md"), "# Fixture\n").expect("readme write");
    fs::write(dir.path().join("sample.rs"), "pub fn one() {}\n").expect("fixture write");

    // Write a baseline that has NO entries — every finding from the
    // current scan is "introduced". With duplicates in the raw stream,
    // a buggy pipeline would over-count introduced.
    let baseline_path = dir.path().join("baseline.json");
    fs::write(
        &baseline_path,
        json!({
            "schemaVersion": "gruff.baseline.v1",
            "generatedAt": "2026-01-01T00:00:00Z",
            "entries": [],
        })
        .to_string(),
    )
    .expect("empty baseline write");

    let report = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from("sample.rs")],
            baseline: Some(PathBuf::from("baseline.json")),
            no_config: true,
            no_baseline: false,
            ..default_test_options()
        },
    )
    .expect("baseline analysis succeeds");

    // Whatever the final per-rule finding count is, introduced must
    // equal that count exactly (the empty baseline matches nothing).
    // Over-counted duplicates would push introduced above the final
    // per-rule finding count, which the loop below would catch.
    let deltas = report
        .per_rule_deltas
        .as_ref()
        .expect("baseline run populates per_rule_deltas");
    for delta in deltas {
        let final_count = report
            .findings
            .iter()
            .filter(|finding| finding.rule_id == delta.rule_id)
            .count();
        assert_eq!(
            delta.introduced, final_count,
            "introduced count for `{}` must equal final finding count, not pre-dedupe duplicates",
            delta.rule_id,
        );
    }
}

#[test]
pub(crate) fn full_tree_run_produces_no_per_rule_deltas() {
    let _guard = analysis_lock();
    let dir = tempdir().expect("tempdir");
    fs::write(dir.path().join("README.md"), "# Fixture\n").expect("readme write");
    fs::write(dir.path().join("sample.rs"), "pub fn process() {}\n").expect("fixture write");

    let report = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from("sample.rs")],
            no_config: true,
            no_baseline: true,
            ..default_test_options()
        },
    )
    .expect("analysis succeeds");

    assert!(
        report.per_rule_deltas.is_none(),
        "full-tree runs must not populate per_rule_deltas; got {:?}",
        report.per_rule_deltas
    );

    let rendered_json = render_report(&report, OutputFormat::Json);
    assert!(
        !rendered_json.contains("perRuleDeltas"),
        "JSON output on a full-tree run must omit the perRuleDeltas key entirely:\n{rendered_json}"
    );
}

#[test]
pub(crate) fn baseline_run_emits_per_rule_deltas_with_introduced_and_removed_counts() {
    let _guard = analysis_lock();
    let dir = tempdir().expect("tempdir");
    fs::write(dir.path().join("README.md"), "# Fixture\n").expect("readme write");
    fs::write(
        dir.path().join("sample.rs"),
        "pub fn one() {}\npub fn two() {}\npub fn three() {}\n",
    )
    .expect("fixture write");

    // Generate a baseline against the same fixture so every current
    // finding is suppressed. Then mutate the baseline file to drop one
    // entry (simulates a newly introduced finding on the next scan) and
    // append a synthetic entry under a rule id that the next scan
    // cannot reproduce (simulates a resolved finding).
    let baseline_path = dir.path().join("baseline.json");
    let generated = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from("sample.rs")],
            generate_baseline: Some(PathBuf::from("baseline.json")),
            no_config: true,
            ..default_test_options()
        },
    )
    .expect("baseline generation succeeds");
    assert!(!generated.findings.is_empty());

    let mut baseline_json: Value =
        serde_json::from_str(&fs::read_to_string(&baseline_path).expect("baseline read"))
            .expect("baseline json");
    let entries = baseline_json["entries"]
        .as_array_mut()
        .expect("entries array");
    let dropped = entries.remove(0);
    let dropped_rule = dropped["ruleId"].as_str().expect("rule id").to_string();
    entries.push(json!({
        "fingerprint": "deadbeefdeadbeef",
        "ruleId": "ghost.removed-rule",
        "filePath": "sample.rs",
        "line": 1,
        "symbol": null,
        "message": "phantom",
    }));
    fs::write(
        &baseline_path,
        serde_json::to_string_pretty(&baseline_json).expect("baseline serialize"),
    )
    .expect("baseline rewrite");

    let report = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from("sample.rs")],
            baseline: Some(PathBuf::from("baseline.json")),
            no_config: true,
            no_baseline: false,
            ..default_test_options()
        },
    )
    .expect("baseline analysis succeeds");

    let deltas = report
        .per_rule_deltas
        .as_ref()
        .expect("baseline run must populate per_rule_deltas");
    let dropped_delta = deltas
        .iter()
        .find(|delta| delta.rule_id == dropped_rule)
        .expect("dropped rule appears as introduced");
    assert_eq!(dropped_delta.introduced, 1);
    assert_eq!(dropped_delta.removed, 0);
    let ghost_delta = deltas
        .iter()
        .find(|delta| delta.rule_id == "ghost.removed-rule")
        .expect("phantom rule appears as removed");
    assert_eq!(ghost_delta.removed, 1);
    assert_eq!(ghost_delta.introduced, 0);
    assert_eq!(ghost_delta.net, -1);
}

// ADR-002 addendum: baseline matching classifies each finding as new,
// unchanged, or absent, and surfaces the counts on `BaselineReport` additively.
#[test]
pub(crate) fn baseline_tri_state_counts_classify_new_unchanged_absent() {
    let _guard = analysis_lock();
    let dir = tempdir().expect("tempdir");
    fs::write(dir.path().join("README.md"), "# Fixture\n").expect("readme write");
    fs::write(
        dir.path().join("sample.rs"),
        "pub fn one() {}\npub fn two() {}\npub fn three() {}\n",
    )
    .expect("fixture write");

    // Generate a baseline that matches every current finding (all unchanged).
    let baseline_path = dir.path().join("baseline.json");
    let generated = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from("sample.rs")],
            generate_baseline: Some(PathBuf::from("baseline.json")),
            no_config: true,
            ..default_test_options()
        },
    )
    .expect("baseline generation succeeds");
    let total = generated.findings.len();
    assert!(total >= 2, "fixture must produce >=2 findings, got {total}");
    // A generate run has no comparison context: counts are zero.
    let generated_baseline = generated.baseline.as_ref().expect("generated baseline");
    assert!(generated_baseline.generated);
    assert_eq!(generated_baseline.new_count, 0);
    assert_eq!(generated_baseline.unchanged_count, 0);
    assert_eq!(generated_baseline.absent_count, 0);

    // Re-scan against the unmodified baseline: every finding is unchanged.
    let all_unchanged = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from("sample.rs")],
            baseline: Some(PathBuf::from("baseline.json")),
            no_config: true,
            no_baseline: false,
            ..default_test_options()
        },
    )
    .expect("analysis succeeds");
    let unchanged_baseline = all_unchanged.baseline.as_ref().expect("baseline present");
    assert_eq!(unchanged_baseline.new_count, 0);
    assert_eq!(unchanged_baseline.unchanged_count, total);
    assert_eq!(unchanged_baseline.absent_count, 0);
    assert_eq!(
        unchanged_baseline.suppressed,
        unchanged_baseline.unchanged_count
    );
    assert!(
        all_unchanged.findings.is_empty(),
        "default list drops unchanged findings"
    );

    // Drop one baseline entry (its finding becomes new) and append a ghost entry
    // that matches no current finding (absent).
    let mut baseline_json: Value =
        serde_json::from_str(&fs::read_to_string(&baseline_path).expect("baseline read"))
            .expect("baseline json");
    baseline_json["entries"]
        .as_array_mut()
        .expect("entries array")
        .remove(0);
    baseline_json["entries"]
        .as_array_mut()
        .expect("entries array")
        .push(json!({
            "fingerprint": "deadbeefdeadbeef",
            "ruleId": "ghost.removed-rule",
            "filePath": "sample.rs",
            "line": 1,
            "symbol": null,
            "message": "phantom",
        }));
    fs::write(
        &baseline_path,
        serde_json::to_string_pretty(&baseline_json).expect("baseline serialize"),
    )
    .expect("baseline rewrite");

    let mixed = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from("sample.rs")],
            baseline: Some(PathBuf::from("baseline.json")),
            no_config: true,
            no_baseline: false,
            ..default_test_options()
        },
    )
    .expect("analysis succeeds");
    let mixed_baseline = mixed.baseline.as_ref().expect("baseline present");
    assert_eq!(
        mixed_baseline.new_count, 1,
        "dropped entry's finding is new"
    );
    assert_eq!(mixed_baseline.unchanged_count, total - 1);
    assert_eq!(mixed_baseline.absent_count, 1, "ghost entry is absent");
    assert_eq!(mixed_baseline.suppressed, mixed_baseline.unchanged_count);
    // Default findings list = the new set only (unchanged dropped, absent not rendered).
    assert_eq!(mixed.findings.len(), mixed_baseline.new_count);
}

// An empty baseline classifies every current finding as new; `--no-baseline`
// leaves the tri-state off entirely.
#[test]
pub(crate) fn baseline_tri_state_all_new_and_no_baseline_short_circuit() {
    let _guard = analysis_lock();
    let dir = tempdir().expect("tempdir");
    fs::write(dir.path().join("README.md"), "# Fixture\n").expect("readme write");
    fs::write(dir.path().join("sample.rs"), "pub fn one() {}\n").expect("fixture write");

    let baseline_path = dir.path().join("baseline.json");
    fs::write(
        &baseline_path,
        json!({
            "schemaVersion": "gruff.baseline.v1",
            "generatedAt": "2026-01-01T00:00:00Z",
            "entries": [],
        })
        .to_string(),
    )
    .expect("empty baseline write");

    let all_new = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from("sample.rs")],
            baseline: Some(PathBuf::from("baseline.json")),
            no_config: true,
            no_baseline: false,
            ..default_test_options()
        },
    )
    .expect("analysis succeeds");
    let baseline = all_new.baseline.as_ref().expect("baseline present");
    assert_eq!(baseline.new_count, all_new.findings.len());
    assert_eq!(baseline.unchanged_count, 0);
    assert_eq!(baseline.absent_count, 0);
    assert!(baseline.new_count > 0);

    let no_baseline = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from("sample.rs")],
            baseline: Some(PathBuf::from("baseline.json")),
            no_config: true,
            no_baseline: true,
            ..default_test_options()
        },
    )
    .expect("analysis succeeds");
    assert!(
        no_baseline.baseline.is_none(),
        "--no-baseline must leave report.baseline None"
    );
}