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
505
506
// Copyright 2025 Umberto Gotti <umberto.gotti@umbertogotti.dev>
// Licensed under the MIT License
// SPDX-License-Identifier: MIT

// A test file reads top to bottom in one order: header, imports, constants,
// helpers, tests. Each group is alphabetical, and the spacing between entries is
// part of the shape rather than a matter of taste -- imports run together, and
// everything else is separated by exactly one blank line.
//
// The order is what makes a test file skimmable without reading it. Once a
// constant appears below a helper, or a test lands between two others out of
// order, the file stops having a shape and every later addition is placed
// wherever the last one happened to end.
//
// Helpers are defined by exclusion: whatever is neither header, nor `use`, nor
// constant, nor test. A `struct` with an `impl` block -- a recording double, a
// fake -- is a helper and sorts among the functions.

use stern4rust::rule::Rule;
use stern4rust::rules::testing::test_file_structure_rule::TestFileStructureRule;
use stern4rust::source_file::SourceFile;

const HEADER: &str = "// Copyright 2025 Umberto Gotti <umberto.gotti@umbertogotti.dev>\n\
                      // Licensed under the MIT License\n\
                      // SPDX-License-Identifier: MIT\n";

const RULE: &str = "test-file-structure";

fn check(contents: &str) -> Vec<stern4rust::reporting::offence::Offence> {
    TestFileStructureRule::new().check(&test_file(contents))
}

fn descriptions(contents: &str) -> Vec<String> {
    check(contents)
        .into_iter()
        .map(|offence| offence.description)
        .collect()
}

fn registry_file(path: &str) -> SourceFile {
    SourceFile::new(
        path,
        &format!("{HEADER}\npub mod alpha_tests;\npub mod beta_tests;\n"),
    )
}

fn source_file(contents: &str) -> SourceFile {
    SourceFile::new("src/subject.rs", &format!("{HEADER}\n{contents}"))
}

fn test_file(contents: &str) -> SourceFile {
    SourceFile::new("tests/subject_tests.rs", &format!("{HEADER}\n{contents}"))
}

// Imports run together. A blank line inside them is how an import block turns
// into two blocks that each sort independently.
#[test]
fn check_a_blank_line_between_imports_reports_it() {
    // Arrange
    let contents = "use alpha::One;\n\
                    \n\
                    use beta::Two;\n";

    // Act
    let offences = check(contents);

    // Assert
    assert_eq!(offences.len(), 1);
    assert!(offences[0].description.contains("blank"));
}

// A comment introducing a test belongs to that test, so the blank line before
// the comment is the separator and the comment itself is not a gap.
#[test]
fn check_a_comment_introducing_a_test_is_part_of_that_test() {
    // Arrange
    let contents = "#[test]\n\
                    fn alpha_does_something() {}\n\
                    \n\
                    // Why this one matters.\n\
                    #[test]\n\
                    fn beta_does_something() {}\n";

    // Act
    let offences = check(contents);

    // Assert
    assert!(offences.is_empty(), "expected none, got {offences:?}");
}

// The sections have one order. A constant below a helper is the first step to a
// file with no shape at all.
#[test]
fn check_a_constant_after_a_helper_reports_the_section_order() {
    // Arrange
    let contents = "fn helper() {}\n\
                    \n\
                    const LATE: usize = 1;\n";

    // Act
    let offences = check(contents);

    // Assert
    assert_eq!(offences.len(), 1);
    assert!(offences[0].description.contains("constant"));
}

#[test]
fn check_a_file_holding_only_a_header_reports_nothing() {
    // Arrange & Act
    let offences = check("");

    // Assert
    assert!(offences.is_empty());
}

#[test]
fn check_a_file_in_the_expected_shape_reports_nothing() {
    // Arrange
    let contents = "use alpha::One;\n\
                    use beta::Two;\n\
                    \n\
                    const FIRST: usize = 1;\n\
                    \n\
                    const SECOND: usize = 2;\n\
                    \n\
                    fn helper_a() {}\n\
                    \n\
                    fn helper_b() {}\n\
                    \n\
                    #[test]\n\
                    fn alpha_does_something() {}\n\
                    \n\
                    #[test]\n\
                    fn beta_does_something() {}\n";

    // Act
    let offences = check(contents);

    // Assert
    assert!(offences.is_empty(), "expected none, got {offences:?}");
}

// Every section is optional but the order of those present still holds.
#[test]
fn check_a_file_of_tests_alone_reports_nothing() {
    // Arrange
    let contents = "#[test]\n\
                    fn alpha_does_something() {}\n\
                    \n\
                    #[test]\n\
                    fn beta_does_something() {}\n";

    // Act
    let offences = check(contents);

    // Assert
    assert!(offences.is_empty(), "expected none, got {offences:?}");
}

// The rule is about test files. A source file has a different shape and is not
// this rule's business.
#[test]
fn check_a_file_outside_the_tests_tree_reports_nothing() {
    // Arrange
    let contents = "use beta::Two;\n\
                    use alpha::One;\n";

    // Act
    let offences = TestFileStructureRule::new().check(&source_file(contents));

    // Assert
    assert!(offences.is_empty());
}

// A file that does not parse is not this rule's problem to report -- rustc will
// say so far more clearly, and guessing at a shape from broken source would
// produce noise on top of a compile error.
#[test]
fn check_a_file_that_does_not_parse_reports_nothing() {
    // Arrange
    let contents = "fn broken( {\n";

    // Act
    let offences = check(contents);

    // Assert
    assert!(offences.is_empty());
}

#[test]
fn check_a_helper_after_a_test_reports_the_section_order() {
    // Arrange
    let contents = "#[test]\n\
                    fn alpha_does_something() {}\n\
                    \n\
                    fn helper() {}\n";

    // Act
    let offences = check(contents);

    // Assert
    assert_eq!(offences.len(), 1);
    assert!(offences[0].description.contains("helper"));
}

#[test]
fn check_a_nested_registry_file_reports_nothing() {
    // Arrange
    let registry = registry_file("tests/rules/mod.rs");

    // Act
    let offences = TestFileStructureRule::new().check(&registry);

    // Assert
    assert!(offences.is_empty(), "expected none, got {offences:?}");
}

// A registry holds nothing but `pub mod` lines and reads as a list. Demanding a
// blank line between each entry would make the one file whose whole job is to be
// scannable the hardest one to scan.
#[test]
fn check_a_registry_file_reports_nothing() {
    // Arrange
    let registry = registry_file("tests/all_tests.rs");

    // Act
    let offences = TestFileStructureRule::new().check(&registry);

    // Assert
    assert!(offences.is_empty(), "expected none, got {offences:?}");
}

#[test]
fn check_a_section_with_a_single_entry_reports_nothing() {
    // Arrange
    let contents = "use only::One;\n\
                    \n\
                    const ONLY: usize = 1;\n\
                    \n\
                    fn only_helper() {}\n\
                    \n\
                    #[test]\n\
                    fn only_test() {}\n";

    // Act
    let offences = check(contents);

    // Assert
    assert!(offences.is_empty(), "expected none, got {offences:?}");
}

#[test]
fn check_a_struct_helper_out_of_order_reports_it() {
    // Arrange
    let contents = "struct Recorder;\n\
                    \n\
                    fn build() {}\n";

    // Act
    let offences = check(contents);

    // Assert
    assert_eq!(offences.len(), 1);
    assert!(offences[0].description.contains("build"));
}

// A recording double is a helper like any other, and sorts by its type name
// among the helper functions rather than sitting in a section of its own.
#[test]
fn check_a_struct_helper_sorts_among_the_helper_functions() {
    // Arrange
    let contents = "fn build() {}\n\
                    \n\
                    struct Recorder;\n\
                    \n\
                    fn verify() {}\n";

    // Act
    let offences = check(contents);

    // Assert
    assert!(offences.is_empty(), "expected none, got {offences:?}");
}

// An impl block belongs to the type it implements, so it sorts under that name
// and sits next to the struct rather than drifting to the end of the section.
#[test]
fn check_an_impl_block_sorts_under_the_type_it_implements() {
    // Arrange
    let contents = "fn build() {}\n\
                    \n\
                    struct Recorder;\n\
                    \n\
                    impl Recorder {\n\
                    fn record(&self) {}\n\
                    }\n";

    // Act
    let offences = check(contents);

    // Assert
    assert!(offences.is_empty(), "expected none, got {offences:?}");
}

#[test]
fn check_an_import_after_a_test_reports_the_section_order() {
    // Arrange
    let contents = "#[test]\n\
                    fn alpha_does_something() {}\n\
                    \n\
                    use late::Import;\n";

    // Act
    let offences = check(contents);

    // Assert
    assert_eq!(offences.len(), 1);
    assert!(offences[0].description.contains("import"));
}

#[test]
fn check_constants_out_of_alphabetic_order_reports_the_later_one() {
    // Arrange
    let contents = "const SECOND: usize = 2;\n\
                    \n\
                    const FIRST: usize = 1;\n";

    // Act
    let offences = check(contents);

    // Assert
    assert_eq!(offences.len(), 1);
    assert!(offences[0].description.contains("FIRST"));
}

#[test]
fn check_constants_run_together_without_a_blank_line_reports_it() {
    // Arrange
    let contents = "const FIRST: usize = 1;\n\
                    const SECOND: usize = 2;\n";

    // Act
    let offences = check(contents);

    // Assert
    assert_eq!(offences.len(), 1);
    assert!(offences[0].description.contains("blank"));
}

#[test]
fn check_helpers_out_of_alphabetic_order_reports_the_later_one() {
    // Arrange
    let contents = "fn zulu() {}\n\
                    \n\
                    fn alpha() {}\n";

    // Act
    let offences = check(contents);

    // Assert
    assert_eq!(offences.len(), 1);
    assert!(offences[0].description.contains("alpha"));
}

// rustfmt sorts crate/self/super ahead of every other path, so demanding the
// alphabet here would make the file unsatisfiable: cargo fmt writes one order,
// this rule demands the other, and stage 1 runs the formatter first. The rule
// stands down on that pair rather than start a fight it cannot win.
//
// The shape this exists for is a shared helper inside the tests tree, reached
// from a sibling as `use crate::support::...`.
#[test]
fn check_imports_out_of_alphabetic_order_around_a_crate_path_reports_nothing() {
    // Arrange
    let contents = "use crate::support::builders;\n\
                    use anyhow::Result;\n";

    // Act
    let offences = check(contents);

    // Assert
    assert!(offences.is_empty(), "expected none, got {offences:?}");
}

// Standing down on the keyword pair does not mean standing down on the file.
// Among ordinary paths rustfmt's comparator and the alphabet agree, so those
// are still ordered.
#[test]
fn check_imports_out_of_alphabetic_order_beside_a_crate_path_still_reports_them() {
    // Arrange
    let contents = "use crate::support::builders;\n\
                    use zebra::Z;\n\
                    use anyhow::Result;\n";

    // Act
    let offences = check(contents);

    // Assert
    assert_eq!(offences.len(), 1);
    assert!(offences[0].description.contains("anyhow::Result"));
}

#[test]
fn check_imports_out_of_alphabetic_order_reports_the_later_one() {
    // Arrange
    let contents = "use beta::Two;\n\
                    use alpha::One;\n";

    // Act
    let offences = check(contents);

    // Assert
    assert_eq!(offences.len(), 1);
    assert_eq!(offences[0].rule, RULE);
    assert!(offences[0].description.contains("alpha::One"));
}

#[test]
fn check_names_the_file_it_judged() {
    // Arrange
    let contents = "use beta::Two;\n\
                    use alpha::One;\n";

    // Act
    let files = descriptions(contents);

    // Assert
    assert_eq!(files.len(), 1);
}

#[test]
fn check_reports_every_offence_in_a_file_rather_than_only_the_first() {
    // Arrange
    let contents = "use beta::Two;\n\
                    use alpha::One;\n\
                    \n\
                    #[test]\n\
                    fn zulu_does_something() {}\n\
                    \n\
                    #[test]\n\
                    fn alpha_does_something() {}\n";

    // Act
    let offences = check(contents);

    // Assert
    assert_eq!(offences.len(), 2);
}

#[test]
fn check_reports_the_line_the_offending_item_starts_on() {
    // Arrange
    let contents = "use beta::Two;\n\
                    use alpha::One;\n";

    // Act
    let offences = check(contents);

    // Assert
    assert_eq!(offences[0].line, 6);
}

#[test]
fn check_tests_out_of_alphabetic_order_reports_the_later_one() {
    // Arrange
    let contents = "#[test]\n\
                    fn zulu_does_something() {}\n\
                    \n\
                    #[test]\n\
                    fn alpha_does_something() {}\n";

    // Act
    let offences = check(contents);

    // Assert
    assert_eq!(offences.len(), 1);
    assert!(offences[0].description.contains("alpha_does_something"));
}

#[test]
fn check_two_blank_lines_between_tests_reports_it() {
    // Arrange
    let contents = "#[test]\n\
                    fn alpha_does_something() {}\n\
                    \n\
                    \n\
                    #[test]\n\
                    fn beta_does_something() {}\n";

    // Act
    let offences = check(contents);

    // Assert
    assert_eq!(offences.len(), 1);
    assert!(offences[0].description.contains("blank"));
}

#[test]
fn name_is_the_kebab_case_rule_name_used_in_the_report() {
    // Arrange & Act
    let name = TestFileStructureRule::new().name();

    // Assert
    assert_eq!(name, RULE);
}