blockwatch 0.5.0

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
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",
        ));
}