blockwatch 0.5.3

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
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
use assert_cmd::assert::OutputAssertExt;
use assert_cmd::cargo_bin_cmd;
use predicates::prelude::predicate;
use serde_json::json;

#[test]
fn diff_with_unsatisfied_blocks_fails() {
    let diff_content = r#"
diff --git a/tests/testdata/affects/sibling.md b/tests/testdata/affects/sibling.md
index abc123..def456 100644
--- a/tests/testdata/affects/sibling.md
+++ b/tests/testdata/affects/sibling.md
@@ -1,6 +1,5 @@
 # Testing data for integration tests

 [//]: # (<block affects=":foo">)
-First block.

 [//]: # (</block>)
"#;

    let mut cmd = cargo_bin_cmd!();
    cmd.args(["--diff", "--only-changed"]);
    let output = cmd.write_stdin(diff_content).output().unwrap();

    output.assert()
        .failure()
        .code(1)
        .stderr(predicate::function(|output: &str| {
            let output_json: serde_json::Value = serde_json::from_str(output).unwrap();
            let value: serde_json::Value = json!({
              "tests/testdata/affects/sibling.md": [
                {
                  "range": {
                    "start": {
                        "line": 3,
                        "character": 10
                    },
                    "end": {
                        "line": 3,
                        "character": 31
                    }
                  },
                  "code": "affects",
                  "message": "Block tests/testdata/affects/sibling.md:(unnamed) at line 3 is modified, but tests/testdata/affects/sibling.md:foo is not",
                  "severity": 1,
                  "data": {
                    "affected_block_file_path": "tests/testdata/affects/sibling.md",
                    "affected_block_name": "foo",
                  }
                }
              ]
            });
            assert_eq!(output_json, value);
            true
        }));
}

#[test]
fn diff_with_satisfied_blocks_succeeds() {
    // The diff deletes one line inside each block, so its post-image matches the on-disk fixture
    // and both blocks' contents count as modified.
    let diff_content = r#"
diff --git a/tests/testdata/affects/sibling.md b/tests/testing_data
index abc123..def456 100644
--- a/tests/testdata/affects/sibling.md
+++ b/tests/testdata/affects/sibling.md
@@ -1,13 +1,11 @@
 # Testing data for integration tests

 [//]: # (<block affects=":foo">)
-Deleted first line.
 First block.

 [//]: # (</block>)

 [//]: # (<block name="foo">)
-Deleted second line.
 Second block.

 [//]: # (</block>)
"#;

    let mut cmd = cargo_bin_cmd!();
    cmd.args(["--diff", "--only-changed"]);
    cmd.write_stdin(diff_content);

    let output = cmd.output().expect("Failed to get command output");

    output.assert().success();
}

#[test]
fn diff_with_satisfied_blocks_non_root_dir_succeeds() {
    // Same post-image-consistent diff as `diff_with_satisfied_blocks_succeeds`.
    let diff_content = r#"
diff --git a/tests/testdata/affects/sibling.md b/tests/testing_data
index abc123..def456 100644
--- a/tests/testdata/affects/sibling.md
+++ b/tests/testdata/affects/sibling.md
@@ -1,13 +1,11 @@
 # Testing data for integration tests

 [//]: # (<block affects=":foo">)
-Deleted first line.
 First block.

 [//]: # (</block>)

 [//]: # (<block name="foo">)
-Deleted second line.
 Second block.

 [//]: # (</block>)
"#;

    let mut cmd = cargo_bin_cmd!();
    cmd.args(["--diff", "--only-changed"]);
    cmd.current_dir("./tests");
    cmd.write_stdin(diff_content);

    let output = cmd.output().expect("Failed to get command output");

    output.assert().success();
}

#[test]
fn diff_with_only_tag_modified_succeeds() {
    let diff_content = r#"
diff --git a/tests/testdata/affects/sibling.md b/tests/testdata/affects/sibling.md
index abc123..def456 100644
--- a/tests/testdata/affects/sibling.md
+++ b/tests/testdata/affects/sibling.md
@@ -1,6 +1,6 @@
 # Testing data for integration tests

-[//]: # (<block affects=":foo" name="first">)
+[//]: # (<block affects=":foo">)
 First block.

 [//]: # (</block>)
"#;

    let mut cmd = cargo_bin_cmd!();
    cmd.args(["--diff", "--only-changed"]);
    cmd.write_stdin(diff_content);

    let output = cmd.output().expect("Failed to get command output");

    output.assert().success();
}

#[test]
fn diff_with_both_the_tag_and_the_content_modified_succeeds() {
    // The target's edit adds a comment above the block, changes its start tag and changes its
    // content, all in one change group. The content change is the one that satisfies `affects`, and
    // the two changes above it must not hide it.
    let diff_content = r#"
diff --git a/tests/testdata/affects/tag_and_content_source.ts b/tests/testdata/affects/tag_and_content_source.ts
index abc123..def456 100644
--- a/tests/testdata/affects/tag_and_content_source.ts
+++ b/tests/testdata/affects/tag_and_content_source.ts
@@ -1,3 +1,3 @@
 // <block name="source" affects="tests/testdata/affects/tag_and_content_target.ts:target">
-const value = 1;
+const value = 2;
 // </block>
diff --git a/tests/testdata/affects/tag_and_content_target.ts b/tests/testdata/affects/tag_and_content_target.ts
index abc123..def456 100644
--- a/tests/testdata/affects/tag_and_content_target.ts
+++ b/tests/testdata/affects/tag_and_content_target.ts
@@ -1,3 +1,4 @@
-// <block name="target" affects="tests/testdata/affects/tag_and_content_source.ts:source">
-const value = 1;
+// Added explanation.
+// <block name="target" affects="tests/testdata/affects/tag_and_content_source.ts:source" severity="warning">
+const value = 2;
 // </block>
"#;

    let mut cmd = cargo_bin_cmd!();
    cmd.args(["--diff", "--only-changed"]);
    cmd.write_stdin(diff_content);

    let output = cmd.output().expect("Failed to get command output");

    output.assert().success();
}

#[test]
fn diff_with_only_tag_modified_in_hunk_with_more_added_than_deleted_lines_succeeds() {
    let diff_content = r#"
diff --git a/tests/testdata/affects/more_added_than_deleted.py b/tests/testdata/affects/more_added_than_deleted.py
index abc123..def456 100644
--- a/tests/testdata/affects/more_added_than_deleted.py
+++ b/tests/testdata/affects/more_added_than_deleted.py
@@ -1,2 +1,3 @@
-# Project dependencies.
-# <block name="deps" affects=":deps-docs">
+# Project dependencies, kept sorted
+# and unique.
+# <block name="deps" affects=":deps-docs" keep-sorted="asc">
"#;

    let mut cmd = cargo_bin_cmd!();
    cmd.args(["--diff", "--only-changed"]);
    cmd.write_stdin(diff_content);

    let output = cmd.output().expect("Failed to get command output");

    output.assert().success();
}

#[test]
fn diff_dependent_block_with_only_tag_modified_fails() {
    let diff_content = r#"
diff --git a/tests/testdata/affects/sibling.md b/tests/testing_data
index abc123..def456 100644
--- a/tests/testdata/affects/sibling.md
+++ b/tests/testdata/affects/sibling.md
@@ -1,11 +1,9 @@
 # Testing data for integration tests

 [//]: # (<block affects=":foo">)
-First block.

 [//]: # (</block>)

- [//]: # (<block name="foo" test="value">)
+ [//]: # (<block name="foo">)
 Second block.

 [//]: # (</block>)
"#;

    let mut cmd = cargo_bin_cmd!();
    cmd.args(["--diff", "--only-changed"]);
    cmd.write_stdin(diff_content);

    let output = cmd.output().expect("Failed to get command output");

    output.assert()
        .failure()
        .code(1)
        .stderr(predicate::function(|output: &str| {
            let output_json: serde_json::Value = serde_json::from_str(output).unwrap();
            let value: serde_json::Value = json!({
              "tests/testdata/affects/sibling.md": [
                {
                  "range": {
                    "start": {
                        "line": 3,
                        "character": 10
                    },
                    "end": {
                        "line": 3,
                        "character": 31
                    }
                  },
                  "code": "affects",
                  "message": "Block tests/testdata/affects/sibling.md:(unnamed) at line 3 is modified, but tests/testdata/affects/sibling.md:foo is not",
                  "severity": 1,
                  "data": {
                    "affected_block_file_path": "tests/testdata/affects/sibling.md",
                    "affected_block_name": "foo",
                  }
                }
              ]
            });
            assert_eq!(output_json, value);
            true
        }));
}

/// The diff that both cross-file tests below feed in. It changes the source block and its `affects`
/// target together, which is exactly what the rule asks for, so no invocation over it may fail.
const CROSS_FILE_DIFF: &str = r#"
diff --git a/tests/testdata/affects/cross_file_source.rs b/tests/testdata/affects/cross_file_source.rs
index abc123..def456 100644
--- a/tests/testdata/affects/cross_file_source.rs
+++ b/tests/testdata/affects/cross_file_source.rs
@@ -1,3 +1,3 @@
 // <block name="limits" affects="tests/testdata/affects/cross_file_target.md:limits">
-pub const MAX: usize = 10;
+pub const MAX: usize = 20;
 // </block>
diff --git a/tests/testdata/affects/cross_file_target.md b/tests/testdata/affects/cross_file_target.md
index abc123..def456 100644
--- a/tests/testdata/affects/cross_file_target.md
+++ b/tests/testdata/affects/cross_file_target.md
@@ -1,5 +1,5 @@
 [//]: # (<block name="limits">)

-Max is 10.
+Max is 20.

 [//]: # (</block>)
"#;

#[test]
fn only_changed_with_globs_excluding_a_modified_affects_target_succeeds() {
    // Globs choose what a run validates; they must not shrink the set of files `affects` resolves
    // its targets against, or narrowing a run to one language would fail every cross-language rule.
    let mut cmd = cargo_bin_cmd!();
    cmd.args([
        "--diff",
        "--only-changed",
        "tests/testdata/affects/cross_file_source.rs",
    ]);
    cmd.write_stdin(CROSS_FILE_DIFF);

    let output = cmd.output().expect("Failed to get command output");

    output.assert().success();
}

#[test]
fn diff_with_globs_excluding_a_modified_affects_target_succeeds() {
    // The same guarantee on a full-tree run, where the globs filter the walk rather than the diff.
    let mut cmd = cargo_bin_cmd!();
    cmd.args(["--diff", "tests/testdata/affects/cross_file_source.rs"]);
    cmd.write_stdin(CROSS_FILE_DIFF);

    let output = cmd.output().expect("Failed to get command output");

    output.assert().success();
}

#[test]
fn only_changed_with_globs_excluding_an_unmodified_affects_target_fails() {
    // The counterpart of the two tests above: resolving targets outside the validated set must
    // still report the ones the diff left alone, rather than assuming any excluded target is fine.
    let diff_content = r#"
diff --git a/tests/testdata/affects/cross_file_source.rs b/tests/testdata/affects/cross_file_source.rs
index abc123..def456 100644
--- a/tests/testdata/affects/cross_file_source.rs
+++ b/tests/testdata/affects/cross_file_source.rs
@@ -1,3 +1,3 @@
 // <block name="limits" affects="tests/testdata/affects/cross_file_target.md:limits">
-pub const MAX: usize = 10;
+pub const MAX: usize = 20;
 // </block>
"#;

    let mut cmd = cargo_bin_cmd!();
    cmd.args([
        "--diff",
        "--only-changed",
        "tests/testdata/affects/cross_file_source.rs",
    ]);
    cmd.write_stdin(diff_content);

    let output = cmd.output().expect("Failed to get command output");

    output
        .assert()
        .failure()
        .code(1)
        .stderr(predicate::str::contains(
            "tests/testdata/affects/cross_file_target.md:limits is not",
        ));
}

#[test]
fn custom_extension_mapping_applies_to_a_target_outside_the_globs() {
    // The globs keep the target file out of the validation context, so `affects` reads it from disk
    // to see whether the diff changed it. That read has to honor `-E`: without the mapping the
    // file looks unparseable and its modified block is reported as unmodified.
    let diff = r#"
diff --git a/tests/testdata/affects/custom_ext_source.javascript b/tests/testdata/affects/custom_ext_source.javascript
index 1111111..2222222 100644
--- a/tests/testdata/affects/custom_ext_source.javascript
+++ b/tests/testdata/affects/custom_ext_source.javascript
@@ -1,3 +1,3 @@
 // <block affects="tests/testdata/affects/custom_ext_target.javascript:port">
-const PORT = 8000;
+const PORT = 8080;
 // </block>
diff --git a/tests/testdata/affects/custom_ext_target.javascript b/tests/testdata/affects/custom_ext_target.javascript
index 1111111..2222222 100644
--- a/tests/testdata/affects/custom_ext_target.javascript
+++ b/tests/testdata/affects/custom_ext_target.javascript
@@ -1,3 +1,3 @@
 // <block name="port">
-const PORT = 8000;
+const PORT = 8080;
 // </block>"#;
    let mut cmd = cargo_bin_cmd!();
    cmd.args([
        "--diff",
        "--only-changed",
        "-E",
        "javascript=js",
        "tests/testdata/affects/custom_ext_source.javascript",
    ]);
    cmd.write_stdin(diff).output().unwrap().assert().success();
}

#[test]
fn full_tree_run_reports_a_dangling_affects_reference() {
    let mut cmd = cargo_bin_cmd!();
    cmd.arg("tests/testdata/affects/dangling.md");
    cmd.output()
        .unwrap()
        .assert()
        .failure()
        .code(1)
        .stderr(predicate::str::contains(
            "references tests/testdata/affects/dangling.md:missing, which does not exist",
        ));
}

#[test]
fn diff_touching_only_the_source_of_a_whole_file_reference_fails() {
    // The target is a JSON file, which carries no blocks to reference; the whole file is the
    // target, and the diff leaves it untouched.
    let diff_content = r#"
diff --git a/tests/testdata/affects/whole_file_source.rs b/tests/testdata/affects/whole_file_source.rs
index abc123..def456 100644
--- a/tests/testdata/affects/whole_file_source.rs
+++ b/tests/testdata/affects/whole_file_source.rs
@@ -1,4 +1,4 @@
 // A source block linked to a file that carries no blocks of its own.
 // <block affects="tests/testdata/affects/whole_file_target.json">
-const PORT: u16 = 8000;
+const PORT: u16 = 8080;
 // </block>
"#;

    let mut cmd = cargo_bin_cmd!();
    cmd.args(["--diff", "--only-changed"]);
    let output = cmd.write_stdin(diff_content).output().unwrap();

    output
        .assert()
        .failure()
        .code(1)
        .stderr(predicate::function(|output: &str| {
            let output_json: serde_json::Value = serde_json::from_str(output).unwrap();
            let violations = &output_json["tests/testdata/affects/whole_file_source.rs"];
            assert_eq!(
                violations[0]["message"],
                json!(
                    "Block tests/testdata/affects/whole_file_source.rs:(unnamed) at line 2 is \
                     modified, but file tests/testdata/affects/whole_file_target.json is not"
                )
            );
            assert_eq!(
                violations[0]["data"],
                json!({
                    "affected_block_file_path": "tests/testdata/affects/whole_file_target.json"
                })
            );
            true
        }));
}

#[test]
fn diff_touching_a_whole_file_reference_target_succeeds() {
    // Any change to the target file satisfies the reference, including one that touches no block.
    let diff_content = r#"
diff --git a/tests/testdata/affects/whole_file_source.rs b/tests/testdata/affects/whole_file_source.rs
index abc123..def456 100644
--- a/tests/testdata/affects/whole_file_source.rs
+++ b/tests/testdata/affects/whole_file_source.rs
@@ -1,4 +1,4 @@
 // A source block linked to a file that carries no blocks of its own.
 // <block affects="tests/testdata/affects/whole_file_target.json">
-const PORT: u16 = 8000;
+const PORT: u16 = 8080;
 // </block>
diff --git a/tests/testdata/affects/whole_file_target.json b/tests/testdata/affects/whole_file_target.json
index abc123..def456 100644
--- a/tests/testdata/affects/whole_file_target.json
+++ b/tests/testdata/affects/whole_file_target.json
@@ -1,3 +1,3 @@
 {
-  "port": 8000
+  "port": 8080
 }
"#;

    let mut cmd = cargo_bin_cmd!();
    cmd.args(["--diff", "--only-changed"]);
    cmd.write_stdin(diff_content)
        .output()
        .unwrap()
        .assert()
        .success();
}

#[test]
fn diff_that_only_renames_a_whole_file_reference_target_fails() {
    // A rename with no content change carries no hunks, so the diff yields nothing at all for the
    // target. Moving a file must not discharge the references pointing at it: none of its content
    // changed.
    let diff_content = r#"
diff --git a/tests/testdata/affects/whole_file_source.rs b/tests/testdata/affects/whole_file_source.rs
index abc123..def456 100644
--- a/tests/testdata/affects/whole_file_source.rs
+++ b/tests/testdata/affects/whole_file_source.rs
@@ -1,4 +1,4 @@
 // A source block linked to a file that carries no blocks of its own.
 // <block affects="tests/testdata/affects/whole_file_target.json">
-const PORT: u16 = 8000;
+const PORT: u16 = 8080;
 // </block>
diff --git a/tests/testdata/affects/old_name.json b/tests/testdata/affects/whole_file_target.json
similarity index 100%
rename from tests/testdata/affects/old_name.json
rename to tests/testdata/affects/whole_file_target.json
"#;

    let mut cmd = cargo_bin_cmd!();
    cmd.args(["--diff", "--only-changed"]);
    let output = cmd.write_stdin(diff_content).output().unwrap();

    output
        .assert()
        .failure()
        .code(1)
        .stderr(predicate::function(|output: &str| {
            let output_json: serde_json::Value = serde_json::from_str(output).unwrap();
            let violations = &output_json["tests/testdata/affects/whole_file_source.rs"];
            assert_eq!(
                violations[0]["message"],
                json!(
                    "Block tests/testdata/affects/whole_file_source.rs:(unnamed) at line 2 is \
                     modified, but file tests/testdata/affects/whole_file_target.json is not"
                )
            );
            true
        }));
}

#[test]
fn diff_creating_the_whole_file_reference_target_empty_succeeds() {
    // A file the diff creates empty has a patch entry but no hunks. File creation counts as a
    // change, so the reference is satisfied.
    let diff_content = r#"
diff --git a/tests/testdata/affects/whole_file_source.rs b/tests/testdata/affects/whole_file_source.rs
index abc123..def456 100644
--- a/tests/testdata/affects/whole_file_source.rs
+++ b/tests/testdata/affects/whole_file_source.rs
@@ -1,4 +1,4 @@
 // A source block linked to a file that carries no blocks of its own.
 // <block affects="tests/testdata/affects/whole_file_target.json">
-const PORT: u16 = 8000;
+const PORT: u16 = 8080;
 // </block>
diff --git a/tests/testdata/affects/whole_file_target.json b/tests/testdata/affects/whole_file_target.json
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/testdata/affects/whole_file_target.json
"#;

    let mut cmd = cargo_bin_cmd!();
    cmd.args(["--diff", "--only-changed"]);
    cmd.write_stdin(diff_content)
        .output()
        .unwrap()
        .assert()
        .success();
}