blockwatch 0.5.2

Language agnostic linter that keeps your code and documentation in sync and valid
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
use assert_cmd::assert::OutputAssertExt;
use assert_cmd::cargo_bin_cmd;
use predicates::prelude::predicate;
use serde_json::Value;

/// Matches three files with one block each, plus one file whose extension has no parser.
const CLEAN_GLOB: &str = "tests/testdata/verbosity/clean/**";
/// Matches one file with a block that fails validation.
const FAILING_GLOB: &str = "tests/testdata/verbosity/failing/**";
/// Matches one file whose single block carries both a synchronous and an asynchronous validator.
const MIXED_GLOB: &str = "tests/testdata/verbosity/mixed/**";
/// Matches one file holding an `affects` source and the block it points at.
const AFFECTS_GLOB: &str = "tests/testdata/verbosity/affects/**";

/// Touches the content of both blocks in `verbosity_affects/pair.py`, so the `affects` obligation
/// the source declares is met.
const DIFF_TOUCHING_THE_AFFECTS_PAIR: &str = r#"
diff --git a/tests/testdata/verbosity/affects/pair.py b/tests/testdata/verbosity/affects/pair.py
index 1111111..2222222 100644
--- a/tests/testdata/verbosity/affects/pair.py
+++ b/tests/testdata/verbosity/affects/pair.py
@@ -1,7 +1,7 @@
 # <block name="source" affects=":target">
-SOURCE = "old"
+SOURCE = "value"
 # </block>

 # <block name="target">
-TARGET = "old"
+TARGET = "value"
 # </block>"#;

/// Touches only the `target` block in `verbosity_affects/pair.py`, leaving the `affects` source
/// block untouched — so the rule that block carries never gets a chance to run.
const DIFF_TOUCHING_ONLY_THE_AFFECTS_TARGET: &str = r#"
diff --git a/tests/testdata/verbosity/affects/pair.py b/tests/testdata/verbosity/affects/pair.py
index 1111111..2222222 100644
--- a/tests/testdata/verbosity/affects/pair.py
+++ b/tests/testdata/verbosity/affects/pair.py
@@ -4,4 +4,4 @@

 # <block name="target">
-TARGET = "old"
+TARGET = "value"
 # </block>"#;

#[test]
fn without_the_flag_stdout_stays_empty() {
    let mut cmd = cargo_bin_cmd!();
    cmd.arg(CLEAN_GLOB);
    let output = cmd.output().unwrap();

    output.assert().success().stdout(predicate::str::is_empty());
}

#[test]
fn summary_level_prints_one_line_of_counts() {
    let mut cmd = cargo_bin_cmd!();
    cmd.arg(CLEAN_GLOB).arg("--verbosity").arg("summary");
    let output = cmd.output().unwrap();

    output
        .assert()
        .success()
        .stdout(
            "blockwatch: mode=all, 3/3 files, 3 blocks (1 unchecked, 0 needs --diff), 2 checks, 0 violations\n",
        );
}

#[test]
fn summary_level_with_no_matching_files_reports_zeroes() {
    let mut cmd = cargo_bin_cmd!();
    cmd.arg("tests/testdata/no_such_directory/**")
        .arg("--verbosity")
        .arg("summary");
    let output = cmd.output().unwrap();

    output
        .assert()
        .success()
        .stdout(
            "blockwatch: mode=all, 0/0 files, 0 blocks (0 unchecked, 0 needs --diff), 0 checks, 0 violations\n",
        );
}

#[test]
fn summary_level_without_a_diff_counts_the_rules_it_could_not_check() {
    let mut cmd = cargo_bin_cmd!();
    cmd.arg(AFFECTS_GLOB).arg("--verbosity").arg("summary");
    let output = cmd.output().unwrap();

    // The `affects` block is checked for reference integrity even without a diff, so it counts as a
    // check; its modified-together rule still needs a diff, so it also counts under `needs --diff`.
    output.assert().success().stdout(
        "blockwatch: mode=all, 1/1 files, 2 blocks (1 unchecked, 1 needs --diff), 1 checks, 0 violations\n",
    );
}

#[test]
fn summary_level_with_the_diff_gated_validator_disabled_counts_no_rules_as_unchecked() {
    let mut cmd = cargo_bin_cmd!();
    cmd.arg(AFFECTS_GLOB)
        .arg("--disable=affects")
        .arg("--verbosity")
        .arg("summary");
    let output = cmd.output().unwrap();

    // A rule the user switched off is not a rule that went unchecked.
    output.assert().success().stdout(
        "blockwatch: mode=all, 1/1 files, 2 blocks (2 unchecked, 0 needs --diff), 0 checks, 0 violations\n",
    );
}

#[test]
fn summary_level_with_a_diff_reports_the_all_plus_diff_mode() {
    let mut cmd = cargo_bin_cmd!();
    cmd.arg("--diff")
        .arg(AFFECTS_GLOB)
        .arg("--verbosity")
        .arg("summary")
        .write_stdin(DIFF_TOUCHING_THE_AFFECTS_PAIR);
    let output = cmd.output().unwrap();

    // The whole tree is still scanned, but the diff says which blocks changed, so `affects` runs
    // and nothing is left needing a diff.
    output.assert().success().stdout(
        "blockwatch: mode=all+diff, 1/1 files, 2 blocks (1 unchecked), 1 checks, 0 violations\n",
    );
}

#[test]
fn summary_level_with_a_diff_that_misses_the_rule_omits_the_needs_diff_clause() {
    let mut cmd = cargo_bin_cmd!();
    cmd.arg("--diff")
        .arg(AFFECTS_GLOB)
        .arg("--verbosity")
        .arg("summary")
        .write_stdin(DIFF_TOUCHING_ONLY_THE_AFFECTS_TARGET);
    let output = cmd.output().unwrap();

    // The diff never reached the `affects` block, so its modified-together rule did not run (only
    // its reference-integrity check, which needs no diff). A block the diff simply did not touch is
    // the normal state of an incremental check, not something to report, so once a diff is given the
    // `needs --diff` clause stays out: counting it would say nothing a reader could act on.
    output.assert().success().stdout(
        "blockwatch: mode=all+diff, 1/1 files, 2 blocks (1 unchecked), 1 checks, 0 violations\n",
    );
}

#[test]
fn summary_level_with_only_changed_reports_the_only_changed_mode() {
    let mut cmd = cargo_bin_cmd!();
    cmd.args(["--diff", "--only-changed"])
        .arg("--verbosity")
        .arg("summary")
        .write_stdin(DIFF_TOUCHING_THE_AFFECTS_PAIR);
    let output = cmd.output().unwrap();

    output.assert().success().stdout(
        "blockwatch: mode=only-changed, 1/1 files, 2 blocks (1 unchecked), 1 checks, 0 violations\n",
    );
}

#[test]
fn full_level_lists_every_block_and_the_validators_that_examined_it() {
    let mut cmd = cargo_bin_cmd!();
    cmd.arg(CLEAN_GLOB).arg("--verbosity").arg("full");
    let output = cmd.output().unwrap();

    let report: Value = serde_json::from_slice(&output.stdout).unwrap();
    output.assert().success();

    assert_eq!(report["summary"]["blocks"], 3);
    assert_eq!(report["summary"]["checks"], 2);
    assert_eq!(report["summary"]["files_skipped"], 1);
    assert_eq!(
        report["files"]["tests/testdata/verbosity/clean/sorted.py"][0]["checks"],
        serde_json::json!(["keep-sorted"])
    );
}

#[test]
fn full_level_names_the_count_of_blocks_that_needed_a_diff() {
    let mut cmd = cargo_bin_cmd!();
    cmd.arg(AFFECTS_GLOB).arg("--verbosity").arg("full");
    let output = cmd.output().unwrap();

    let report: Value = serde_json::from_slice(&output.stdout).unwrap();
    output.assert().success();

    // Whatever parses the report reads this key by name, so it is asserted directly rather than
    // only through the summary line: a missing key reads as null here and fails.
    assert_eq!(report["summary"]["blocks_needing_diff"], 1);
}

#[test]
fn full_level_under_a_diff_omits_the_needs_diff_key() {
    let mut cmd = cargo_bin_cmd!();
    cmd.arg("--diff")
        .arg(AFFECTS_GLOB)
        .arg("--verbosity")
        .arg("full")
        .write_stdin(DIFF_TOUCHING_ONLY_THE_AFFECTS_TARGET);
    let output = cmd.output().unwrap();

    let report: Value = serde_json::from_slice(&output.stdout).unwrap();
    output.assert().success();

    // Absent, not zero: a run given a diff has no opinion on how many blocks need one.
    assert_eq!(report["summary"].get("blocks_needing_diff"), None);
    // The `affects` block is checked for reference integrity, so only the plain `name` block is left
    // unchecked.
    assert_eq!(report["summary"]["blocks_unchecked"], 1);
}

#[test]
fn full_level_shows_a_block_no_validator_examined() {
    let mut cmd = cargo_bin_cmd!();
    cmd.arg(CLEAN_GLOB).arg("--verbosity").arg("full");
    let output = cmd.output().unwrap();

    let report: Value = serde_json::from_slice(&output.stdout).unwrap();
    let block = &report["files"]["tests/testdata/verbosity/clean/noop.py"][0];

    // A block carrying only a `name` declares no rule, so no validator claims it. It is still
    // listed, with an empty check list, and it counts towards `blocks_unchecked`.
    assert_eq!(block["name"], "noop");
    assert_eq!(block["checks"], serde_json::json!([]));
    assert_eq!(report["summary"]["blocks_unchecked"], 1);
}

#[test]
fn full_level_with_violations_writes_two_parseable_documents() {
    let mut cmd = cargo_bin_cmd!();
    cmd.arg(FAILING_GLOB).arg("--verbosity").arg("full");
    let output = cmd.output().unwrap();

    // The report goes to stdout and the violations go to stderr, so each one parses on its own.
    let report: Value = serde_json::from_slice(&output.stdout).unwrap();
    let violations: Value = serde_json::from_slice(&output.stderr).unwrap();
    output.assert().failure().code(1);

    assert_eq!(report["summary"]["violations"], 1);
    assert_eq!(
        report["files"]["tests/testdata/verbosity/failing/unsorted.py"][0]["checks"],
        serde_json::json!(["keep-sorted"])
    );
    assert!(
        violations["tests/testdata/verbosity/failing/unsorted.py"]
            .as_array()
            .is_some_and(|entries| !entries.is_empty())
    );
}

/// Touches the first of the two blocks in `verbosity_diff/two_blocks.py`, leaving the second alone.
const DIFF_TOUCHING_ONE_BLOCK: &str = r#"
diff --git a/tests/testdata/verbosity/diff/two_blocks.py b/tests/testdata/verbosity/diff/two_blocks.py
index 1111111..2222222 100644
--- a/tests/testdata/verbosity/diff/two_blocks.py
+++ b/tests/testdata/verbosity/diff/two_blocks.py
@@ -1,6 +1,6 @@
 fruits = [
     # <block name="fruits" keep-sorted="asc">
     'apple',
-    'blueberry',
+    'banana',
     # </block>
 ]"#;

#[test]
fn full_level_under_a_diff_describes_only_the_blocks_in_scope() {
    let mut cmd = cargo_bin_cmd!();
    cmd.args(["--diff", "--only-changed"])
        .arg("--verbosity")
        .arg("full")
        .write_stdin(DIFF_TOUCHING_ONE_BLOCK);
    let output = cmd.output().unwrap();

    let report: Value = serde_json::from_slice(&output.stdout).unwrap();
    output.assert().success();

    // A diff puts only the blocks it touches in scope, so the untouched `vegetables` block in the
    // same file is absent from the report rather than listed as unchecked.
    let blocks = report["files"]["tests/testdata/verbosity/diff/two_blocks.py"]
        .as_array()
        .expect("the touched file is reported");
    assert_eq!(blocks.len(), 1);
    assert_eq!(blocks[0]["name"], "fruits");
    assert_eq!(blocks[0]["is_content_modified"], true);
    assert_eq!(blocks[0]["checks"], serde_json::json!(["keep-sorted"]));

    // The mode name matches the flag that selects it, and carries no space, so the summary line
    // it also appears on stays tokenizable on whitespace.
    assert_eq!(report["summary"]["mode"], "only-changed");

    // Only the file named by the diff is read, so the wider testdata tree is never scanned.
    assert_eq!(report["summary"]["files_scanned"], 1);
    assert_eq!(report["summary"]["blocks"], 1);
    assert_eq!(report["summary"]["blocks_unchecked"], 0);
    assert_eq!(report["summary"]["checks"], 1);
}

#[test]
fn summary_level_under_a_diff_counts_only_the_blocks_in_scope() {
    let mut cmd = cargo_bin_cmd!();
    cmd.args(["--diff", "--only-changed"])
        .arg("--verbosity")
        .arg("summary")
        .write_stdin(DIFF_TOUCHING_ONE_BLOCK);
    let output = cmd.output().unwrap();

    output
        .assert()
        .success()
        .stdout(
            "blockwatch: mode=only-changed, 1/1 files, 1 blocks (0 unchecked), 1 checks, 0 violations\n",
        );
}

#[test]
fn diff_mode_with_a_violation_writes_the_report_and_the_violations() {
    let diff = r#"
diff --git a/tests/testdata/verbosity/failing/unsorted.py b/tests/testdata/verbosity/failing/unsorted.py
index 1111111..2222222 100644
--- a/tests/testdata/verbosity/failing/unsorted.py
+++ b/tests/testdata/verbosity/failing/unsorted.py
@@ -1,6 +1,6 @@
 vegetables = [
     # <block name="vegetables" keep-sorted="asc">
     'tomato',
-    'squash',
+    'potato',
     # </block>
 ]"#;
    let mut cmd = cargo_bin_cmd!();
    cmd.args(["--diff", "--only-changed"])
        .arg("--verbosity")
        .arg("full")
        .write_stdin(diff);
    let output = cmd.output().unwrap();

    let report: Value = serde_json::from_slice(&output.stdout).unwrap();
    let violations: Value = serde_json::from_slice(&output.stderr).unwrap();
    output.assert().failure().code(1);

    assert_eq!(report["summary"]["violations"], 1);
    assert_eq!(
        report["files"]["tests/testdata/verbosity/failing/unsorted.py"][0]["checks"],
        serde_json::json!(["keep-sorted"])
    );
    assert!(
        violations["tests/testdata/verbosity/failing/unsorted.py"]
            .as_array()
            .is_some_and(|entries| !entries.is_empty())
    );
}

#[test]
fn full_level_under_a_diff_omits_a_reference_target_the_diff_did_not_touch() {
    // Only the source side is in the diff. The target lives in another file and is resolved from
    // disk to run the comparison.
    let diff = r#"
diff --git a/tests/testdata/same_as/source.rs b/tests/testdata/same_as/source.rs
index 1111111..2222222 100644
--- a/tests/testdata/same_as/source.rs
+++ b/tests/testdata/same_as/source.rs
@@ -1,3 +1,3 @@
 // <block same-as="tests/testdata/same_as/target.md:port">
-const PORT: u16 = 8000;
+const PORT: u16 = 8080;
 // </block>"#;
    let mut cmd = cargo_bin_cmd!();
    cmd.args(["--diff", "--only-changed"])
        .arg("--verbosity")
        .arg("full")
        .write_stdin(diff);
    let output = cmd.output().unwrap();

    let report: Value = serde_json::from_slice(&output.stdout).unwrap();
    output.assert().success();

    // Reading a block to compare against it is not checking it, so the target file is absent from
    // the report and uncounted, even though the run had to parse it.
    assert!(report["files"]["tests/testdata/same_as/target.md"].is_null());
    assert_eq!(report["summary"]["files_scanned"], 1);
    assert_eq!(report["summary"]["blocks"], 1);
    assert_eq!(
        report["files"]["tests/testdata/same_as/source.rs"][0]["checks"],
        serde_json::json!(["same-as"])
    );
}

#[test]
fn full_level_names_both_a_sync_and_an_async_validator_of_one_block() {
    let mut cmd = cargo_bin_cmd!();
    cmd.arg(MIXED_GLOB).arg("--verbosity").arg("full");
    let output = cmd.output().unwrap();

    let report: Value = serde_json::from_slice(&output.stdout).unwrap();
    output.assert().success();

    // Synchronous and asynchronous validators run apart from each other and their results are
    // combined afterwards, so a block checked by one of each has to end up naming both.
    assert_eq!(
        report["files"]["tests/testdata/verbosity/mixed/both_kinds.py"][0]["checks"],
        serde_json::json!(["check-lua", "keep-sorted"])
    );
    assert_eq!(report["summary"]["checks"], 2);
    assert_eq!(
        report["summary"]["validators"],
        serde_json::json!({ "check-lua": 1, "keep-sorted": 1 })
    );
}

#[test]
fn verbosity_with_the_list_subcommand_fails() {
    let mut cmd = cargo_bin_cmd!();
    cmd.arg("list").arg("--verbosity").arg("full");
    let output = cmd.output().unwrap();

    output
        .assert()
        .failure()
        .stderr(predicate::str::contains("`list` subcommand"));
}