cargo-stern4rust 0.10.5

Cargo subcommand that fails the build when a Rust workspace breaks a house coding rule, such as AAA test structure or one struct per file
Documentation
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
// Copyright 2025 Umberto Gotti <umberto.gotti@umbertogotti.dev>
// Licensed under the MIT License
// SPDX-License-Identifier: MIT

// Rendering the report a person reads.
//
// render returns the document instead of writing it, which is what lets these
// assert on what comes out rather than only that nothing panicked. The print_
// tests below still pin that the writing path survives the shapes it will meet:
// no offences, one, many, and a path far wider than its column heading.

use stern4rust::reporting::offence::Offence;
use stern4rust::reporting::offence_threshold::OffenceThreshold;
use stern4rust::reporting::package_roster::PackageRoster;
use stern4rust::reporting::report_printer::ReportPrinter;

fn many(count: usize) -> Vec<Offence> {
    (1..=count)
        .map(|line| offence("src/a.rs", line, "header"))
        .collect()
}

fn offence(file: &str, line: usize, rule: &'static str) -> Offence {
    Offence::new(
        file,
        line,
        rule,
        "something is wrong".to_string(),
        "do the thing that makes it right".to_string(),
    )
}

// Columns are sized to their contents, so a path longer than the heading must
// widen the column rather than overflow it.
#[test]
fn print_with_a_path_far_wider_than_its_heading_does_not_panic() {
    // Arrange
    let printer = ReportPrinter::new(1);
    let long = "crates/deeply/nested/package/src/module/submodule/subject.rs";

    // Act & Assert
    printer.print(&[offence(long, 1234, "header")]);
}

#[test]
fn print_with_no_offences_does_not_panic() {
    // Arrange
    let printer = ReportPrinter::new(0);

    // Act & Assert
    printer.print(&[]);
}

#[test]
fn print_with_offences_from_several_rules_does_not_panic() {
    // Arrange
    let printer = ReportPrinter::new(3);

    // Act & Assert
    printer.print(&[
        offence("src/a.rs", 1, "header"),
        offence("tests/b_tests.rs", 42, "test-file-structure"),
        offence("src/c.rs", 7, "header"),
    ]);
}

#[test]
fn print_with_one_offence_does_not_panic() {
    // Arrange
    let printer = ReportPrinter::new(1);

    // Act & Assert
    printer.print(&[offence("src/a.rs", 1, "header")]);
}

// The case a bare total would hide: a pattern naming a tree that has moved or
// been deleted goes on looking like it is doing work.
#[test]
fn render_names_a_pattern_that_matched_nothing_as_such() {
    // Arrange
    let printer = ReportPrinter::new(4)
        .with_exclusions(vec![("gone/**".to_string(), 0), ("live/**".to_string(), 3)]);

    // Act
    let report = printer.render(&[]);

    // Assert
    assert!(report.contains("matched nothing: gone/**"), "{report}");
    assert!(!report.contains("matched nothing: live/**"), "{report}");
}

// A count answers "how many", which is only useful to a reader who already
// knows how many there are. The names answer what was actually checked.
// An exclusion is only acceptable if the reader can see it. A tree removed
// from the report with no number beside it is the silent skip the walker had
// until 0.4.0.
#[test]
fn render_names_each_exclusion_with_the_files_it_removed() {
    // Arrange
    let printer = ReportPrinter::new(4).with_exclusions(vec![("fixture/**".to_string(), 27)]);

    // Act
    let report = printer.render(&[]);

    // Assert
    assert!(
        report.contains("excluded: fixture/** (27 files)"),
        "{report}"
    );
    assert!(report.contains("files_excluded=27"), "{report}");
}

#[test]
fn render_names_the_rules_that_were_applied() {
    // Arrange & Act
    let report = ReportPrinter::new(1)
        .with_rules(
            vec!["header".to_string(), "tests-layout".to_string()],
            Vec::new(),
            Vec::new(),
        )
        .render(&[]);

    // Assert
    assert!(
        report.contains("applied: header, tests-layout"),
        "got {report}"
    );
}

// A run with rules switched off must never read as a run that checked
// everything. This is the same refusal as the omitted-offence note, one level
// up: what was not looked at is part of the finding.
#[test]
fn render_names_the_rules_that_were_not_applied() {
    // Arrange & Act
    let report = ReportPrinter::new(1)
        .with_rules(
            vec!["header".to_string()],
            vec!["tests-layout".to_string(), "test-free-source".to_string()],
            Vec::new(),
        )
        .render(&[offence("src/a.rs", 1, "header")]);

    // Assert
    assert!(
        report.contains("not applied: tests-layout (skipped), test-free-source (skipped)"),
        "got {report}"
    );
    assert!(
        report.contains("rules_applied=1 rules_skipped=2"),
        "got {report}"
    );
}

// The reason a rule is missing travels with it, per package, or a reader with a
// workspace has no idea which member to go and fix.
#[test]
fn render_of_a_package_whose_rule_could_not_run_names_the_requirement() {
    // Arrange
    let printer = ReportPrinter::new(9).with_package_rosters(vec![
        PackageRoster::new("node", vec!["header".to_string()], Vec::new(), Vec::new()),
        PackageRoster::new(
            "fixture",
            vec!["header".to_string()],
            Vec::new(),
            vec![(
                "spdx-matches-manifest".to_string(),
                "needs a `license` field in Cargo.toml".to_string(),
            )],
        ),
    ]);

    // Act
    let report = printer.render(&[]);

    // Assert
    assert!(report.contains("spdx-matches-manifest (needs a `license` field in Cargo.toml)"));
    assert!(report.contains("fixture:"));
}

// One package is not a workspace, and naming it would be noise.
#[test]
fn render_of_a_single_package_states_one_roster_without_naming_it() {
    // Arrange
    let printer = ReportPrinter::new(9).with_package_rosters(vec![PackageRoster::new(
        "cargo-stern4rust",
        vec!["header".to_string()],
        Vec::new(),
        Vec::new(),
    )]);

    // Act
    let report = printer.render(&[]);

    // Assert
    assert!(report.contains("applied: header"));
    assert!(!report.contains("cargo-stern4rust:"));
}

// Loudly, and naming the flag that raises it. A cap nobody was told about
// reads as "that was all of them".
#[test]
fn render_of_more_offences_than_the_threshold_says_how_many_are_not_shown() {
    // Arrange & Act
    let report = ReportPrinter::new(1)
        .with_threshold(OffenceThreshold::new(3))
        .render(&many(10));

    // Assert
    assert!(report.contains("7 more"), "got {report}");
    assert!(report.contains("--offence-threshold"), "got {report}");
}

#[test]
fn render_of_more_offences_than_the_threshold_shows_only_the_threshold() {
    // Arrange & Act
    let report = ReportPrinter::new(1)
        .with_threshold(OffenceThreshold::new(3))
        .render(&many(10));

    // Assert
    assert_eq!(report.matches("something is wrong").count(), 3);
}

#[test]
fn render_of_no_offences_says_every_rule_is_satisfied() {
    // Arrange & Act
    let report = ReportPrinter::new(9).render(&[]);

    // Assert
    assert!(report.contains("All rules are satisfied."));
    assert!(report.contains(
        "files_scanned=9 files_excluded=0 offences=0 baselined=0 fixed=0 rules_broken=0"
    ));
}

// "All rules are satisfied" is only true when all of them ran.
#[test]
fn render_of_no_offences_with_a_skipped_rule_says_applied_rules_are_satisfied() {
    // Arrange & Act
    let report = ReportPrinter::new(9)
        .with_rules(
            vec!["header".to_string()],
            vec!["tests-layout".to_string()],
            Vec::new(),
        )
        .render(&[]);

    // Assert
    assert!(
        report.contains("All applied rules are satisfied."),
        "got {report}"
    );
    assert!(!report.contains("All rules are satisfied."), "got {report}");
    assert!(report.contains("not applied: tests-layout"), "got {report}");
}

// A workspace whose members all answer to the same rules reads exactly as one
// package does. This is the case that must not change: every repository in this
// family is one rule set, every gate script parses what it prints, and a report
// that grew a block per package for no reason would be worse than the one it
// replaced.
#[test]
fn render_of_packages_that_agree_states_one_roster() {
    // Arrange
    let printer = ReportPrinter::new(9).with_package_rosters(vec![
        PackageRoster::new(
            "node",
            vec!["header".to_string(), "test-naming".to_string()],
            Vec::new(),
            Vec::new(),
        ),
        PackageRoster::new(
            "node-infra",
            vec!["header".to_string(), "test-naming".to_string()],
            Vec::new(),
            Vec::new(),
        ),
    ]);

    // Act
    let report = printer.render(&[]);

    // Assert
    assert!(report.contains("applied: header, test-naming"));
    assert!(!report.contains("node"));
    assert_eq!(report.matches("applied:").count(), 1);
}

// And the case the block exists for. `applied: header, test-naming` would be
// false for validation, which applies one of them.
#[test]
fn render_of_packages_that_differ_states_a_roster_each() {
    // Arrange
    let printer = ReportPrinter::new(9).with_package_rosters(vec![
        PackageRoster::new(
            "node",
            vec!["header".to_string(), "test-naming".to_string()],
            Vec::new(),
            Vec::new(),
        ),
        PackageRoster::new(
            "validation",
            vec!["header".to_string()],
            vec!["test-naming".to_string()],
            Vec::new(),
        ),
    ]);

    // Act
    let report = printer.render(&[]);

    // Assert
    assert!(report.contains("node:"));
    assert!(report.contains("validation:"));
    assert!(report.contains("not applied: test-naming (skipped)"));
}

// Every offence carries what to do about it, so every row is followed by one.
#[test]
fn render_puts_a_correction_under_every_offence() {
    // Arrange
    let offences = [
        offence("src/a.rs", 1, "header"),
        offence("src/b.rs", 2, "header"),
    ];

    // Act
    let report = ReportPrinter::new(2).render(&offences);

    // Assert
    assert_eq!(
        report
            .matches("fix: do the thing that makes it right")
            .count(),
        2
    );
}

// Beneath the row it belongs to, indented past the columns, so the table stays
// scannable and the correction is unambiguously attached to one offence.
#[test]
fn render_puts_the_correction_on_its_own_line_after_the_offence() {
    // Arrange & Act
    let report = ReportPrinter::new(1).render(&[offence("src/a.rs", 1, "header")]);
    let lines: Vec<&str> = report.lines().collect();

    // Assert
    let row = lines
        .iter()
        .position(|line| line.contains("something is wrong"))
        .expect("the offence row");
    assert!(lines[row + 1].trim_start().starts_with("fix: "));
    assert!(
        lines[row + 1].starts_with("    "),
        "got {:?}",
        lines[row + 1]
    );
}

// Skipped and unconfigured are both "did not run" and are not the same thing.
// One is a choice the reader made; the other is a flag they did not pass, and
// saying which is the difference between a note and an instruction.
#[test]
fn render_says_why_each_rule_was_not_applied() {
    // Arrange & Act
    let report = ReportPrinter::new(1)
        .with_rules(
            vec!["readable-source".to_string()],
            vec!["tests-layout".to_string()],
            vec![("header".to_string(), "needs --header-file".to_string())],
        )
        .render(&[]);

    // Assert
    assert!(report.contains("tests-layout (skipped)"), "got {report}");
    // The rule says what it needs, so the line carries a correction rather than
    // only a verdict. A bare "(not configured)" told the reader nothing to do.
    assert!(
        report.contains("header (needs --header-file)"),
        "got {report}"
    );
    assert!(
        report.contains("rules_applied=1 rules_skipped=1 rules_unconfigured=1"),
        "got {report}"
    );
}

#[test]
fn render_shows_the_file_line_and_rule_of_every_offence() {
    // Arrange & Act
    let report = ReportPrinter::new(2).render(&[
        offence("src/a.rs", 1, "header"),
        offence("tests/b_tests.rs", 42, "test-file-structure"),
    ]);

    // Assert
    assert!(report.contains("src/a.rs"));
    assert!(report.contains("tests/b_tests.rs"));
    assert!(report.contains("42"));
    assert!(report.contains("test-file-structure"));
}

#[test]
fn render_summarises_two_offences_of_one_rule_as_one_broken_rule() {
    // Arrange & Act
    let report = ReportPrinter::new(5).render(&[
        offence("src/a.rs", 1, "header"),
        offence("src/b.rs", 2, "header"),
    ]);

    // Assert
    assert!(report.contains(
        "files_scanned=5 files_excluded=0 offences=2 baselined=0 fixed=0 rules_broken=1"
    ));
}

// The cap is on what is shown, never on what is counted. A summary that said
// 3 when the tree holds 10 would be this tool doing the exact thing it exists
// to catch.
#[test]
fn render_summary_counts_every_offence_even_when_some_are_not_shown() {
    // Arrange & Act
    let report = ReportPrinter::new(1)
        .with_threshold(OffenceThreshold::new(3))
        .render(&many(10));

    // Assert
    assert!(
        report.contains(
            "files_scanned=1 files_excluded=0 offences=10 baselined=0 fixed=0 rules_broken=1"
        ),
        "got {report}"
    );
}

// Gate scripts across this family parse the summary with a regex. Whatever the
// roster does above it, the line they read keeps its shape.
#[test]
fn render_with_differing_rosters_keeps_the_summary_line_intact() {
    // Arrange
    let printer = ReportPrinter::new(9).with_package_rosters(vec![
        PackageRoster::new("node", vec!["header".to_string()], Vec::new(), Vec::new()),
        PackageRoster::new(
            "validation",
            Vec::new(),
            vec!["header".to_string()],
            Vec::new(),
        ),
    ]);

    // Act
    let report = printer.render(&[]);

    // Assert
    assert!(report.contains("summary: files_scanned=9"));
    assert_eq!(report.matches("summary:").count(), 1);
}

#[test]
fn with_baseline_puts_the_suppressed_count_in_the_report() {
    // Arrange
    let printer = ReportPrinter::new(1).with_baseline(Some("bl.json".to_string()), 7, 2);

    // Act
    let report = printer.render(&[]);

    // Assert
    assert!(
        report.contains("baseline: bl.json (7 suppressed)"),
        "{report}"
    );
    assert!(
        report.contains("2 baseline entries matched nothing"),
        "{report}"
    );
    assert!(report.contains("baselined=7"), "{report}");
}

#[test]
fn with_config_file_names_the_config_the_run_used() {
    // Arrange
    let printer = ReportPrinter::new(1).with_config_file(Some("stern4rust.toml".to_string()));

    // Act
    let report = printer.render(&[]);

    // Assert
    assert!(report.contains("config: stern4rust.toml"), "{report}");
}

#[test]
fn with_fixed_reports_how_many_files_were_rewritten() {
    // Arrange
    let printer = ReportPrinter::new(1).with_fixed(12);

    // Act
    let report = printer.render(&[]);

    // Assert
    assert!(report.contains("fixed: 12 file(s) rewritten"), "{report}");
    assert!(report.contains("fixed=12"), "{report}");
}