rsconstruct 0.9.175

Rust based fast build system
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
use crate::common::{run_rsconstruct_with_env, setup_test_project};
use std::fs;

fn write_terms_dirs(project_path: &std::path::Path, unambiguous: &[&str], ambiguous: &[&str]) {
    let una_dir = project_path.join("terms.unambiguous");
    let amb_dir = project_path.join("terms.ambiguous");
    fs::create_dir_all(&una_dir).unwrap();
    fs::create_dir_all(&amb_dir).unwrap();
    fs::write(una_dir.join("words.txt"), unambiguous.join("\n") + "\n").unwrap();
    fs::write(amb_dir.join("words.txt"), ambiguous.join("\n") + "\n").unwrap();
}

#[test]
fn terms_two_dirs_disjoint_passes() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    write_terms_dirs(
        project_path,
        &["Kubernetes", "Docker"],
        &["server", "client"],
    );

    fs::write(
        project_path.join("README.md"),
        "# Doc\n\nWe deploy on `Kubernetes` with `Docker`.\n",
    )
    .unwrap();

    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.checker.terms]\ndir_terms_unambiguous = \"terms.unambiguous\"\ndir_terms_ambiguous = \"terms.ambiguous\"\nsrc_dirs = [\".\"]\n",
    ).unwrap();

    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "Build should succeed with disjoint dirs: stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn terms_two_dirs_overlap_fails() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    // "Docker" appears in both -> must fail
    write_terms_dirs(
        project_path,
        &["Kubernetes", "Docker"],
        &["Docker", "client"],
    );

    fs::write(project_path.join("README.md"), "# Doc\n").unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.checker.terms]\ndir_terms_unambiguous = \"terms.unambiguous\"\ndir_terms_ambiguous = \"terms.ambiguous\"\nsrc_dirs = [\".\"]\n",
    ).unwrap();

    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        !output.status.success(),
        "Build should fail when terms overlap"
    );

    let combined = format!(
        "{}{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        combined.contains("Docker") && combined.contains("ambiguous"),
        "Error should mention overlapping term and ambiguous: {}",
        combined
    );
}

#[test]
fn terms_backticked_ambiguous_fails_by_default() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    write_terms_dirs(project_path, &["Kubernetes"], &["server"]);

    // Backticked `server` should fail — server is ambiguous, so backticks
    // falsely assert the technical reading.
    fs::write(
        project_path.join("README.md"),
        "# Doc\n\nThe `server` runs on `Kubernetes`.\n",
    )
    .unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.checker.terms]\ndir_terms_unambiguous = \"terms.unambiguous\"\ndir_terms_ambiguous = \"terms.ambiguous\"\nsrc_dirs = [\".\"]\n",
    ).unwrap();

    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        !output.status.success(),
        "Backticked ambiguous term must fail by default"
    );

    let combined = format!(
        "{}{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        combined.contains("server") && combined.contains("ambiguous"),
        "Error must mention 'server' and 'ambiguous': {}",
        combined
    );
}

#[test]
fn terms_backticked_ambiguous_allowed_when_flag_off() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    write_terms_dirs(project_path, &["Kubernetes"], &["server"]);

    fs::write(
        project_path.join("README.md"),
        "# Doc\n\nThe `server` runs on `Kubernetes`.\n",
    )
    .unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.checker.terms]\ndir_terms_unambiguous = \"terms.unambiguous\"\ndir_terms_ambiguous = \"terms.ambiguous\"\nforbid_backticked_ambiguous = false\nsrc_dirs = [\".\"]\n",
    ).unwrap();

    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "With forbid_backticked_ambiguous=false, backticked ambiguous term must pass: stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn terms_fix_strips_ambiguous_backticks() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    write_terms_dirs(project_path, &["Kubernetes"], &["server"]);

    fs::write(
        project_path.join("README.md"),
        "# Doc\n\nThe `server` runs on `Kubernetes`.\n",
    )
    .unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.checker.terms]\ndir_terms_unambiguous = \"terms.unambiguous\"\ndir_terms_ambiguous = \"terms.ambiguous\"\nsrc_dirs = [\".\"]\n",
    ).unwrap();

    let output = run_rsconstruct_with_env(project_path, &["term", "fix"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "term fix must succeed: stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );

    let after = fs::read_to_string(project_path.join("README.md")).unwrap();
    assert!(
        !after.contains("`server`") && after.contains("`Kubernetes`"),
        "fix must strip `server` backticks but keep `Kubernetes`: {after}",
    );
}

#[test]
fn terms_fix_fails_when_no_terms_found() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    // Both term directories exist but contain zero terms — a misconfigured
    // setup (e.g. wrong dir_terms_unambiguous) must fail loudly, not
    // silently report success while checking nothing.
    write_terms_dirs(project_path, &[], &[]);

    fs::write(
        project_path.join("README.md"),
        "# Doc\n\nWe deploy on Kubernetes.\n",
    )
    .unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.checker.terms]\ndir_terms_unambiguous = \"terms.unambiguous\"\ndir_terms_ambiguous = \"terms.ambiguous\"\nsrc_dirs = [\".\"]\n",
    ).unwrap();

    let output = run_rsconstruct_with_env(project_path, &["term", "fix"], &[("NO_COLOR", "1")]);
    assert!(
        !output.status.success(),
        "term fix must fail when no terms are loaded"
    );

    let combined = format!(
        "{}{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        combined.contains("no terms found") && combined.contains("terms.unambiguous"),
        "Error must say no terms were found and name the directory: {combined}",
    );
}

/// A `dir_terms_unambiguous` that does not exist (a typo) used to declare no
/// products, so the check passed without checking anything.
#[test]
fn terms_build_fails_when_the_terms_directory_is_missing() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    fs::write(
        project_path.join("README.md"),
        "# Doc\n\nWe deploy on Kubernetes.\n",
    )
    .unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.checker.terms]\ndir_terms_unambiguous = \"terms.unambigous\"\nsrc_dirs = [\".\"]\n",
    )
    .unwrap();

    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        !output.status.success(),
        "build must fail when the terms directory does not exist"
    );
    let combined = format!(
        "{}{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        combined.contains("is not a directory"),
        "Error must name the missing directory: {combined}",
    );
}

#[test]
fn terms_build_fails_when_no_terms_found() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    write_terms_dirs(project_path, &[], &[]);

    fs::write(
        project_path.join("README.md"),
        "# Doc\n\nWe deploy on Kubernetes.\n",
    )
    .unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.checker.terms]\ndir_terms_unambiguous = \"terms.unambiguous\"\ndir_terms_ambiguous = \"terms.ambiguous\"\nsrc_dirs = [\".\"]\n",
    ).unwrap();

    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        !output.status.success(),
        "build must fail when the terms processor loads zero terms"
    );

    let combined = format!(
        "{}{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        combined.contains("no terms found"),
        "Error must say no terms were found: {combined}",
    );
}

#[test]
fn terms_matching_is_case_sensitive() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    // Single-meaning list has "Docker" with capital D.
    write_terms_dirs(project_path, &["Docker"], &[]);

    // Prose uses lowercase "docker" — must NOT be flagged because the
    // scanner is case-sensitive and the list says "Docker".
    fs::write(
        project_path.join("README.md"),
        "# Doc\n\nWe run docker on every host.\n",
    )
    .unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.checker.terms]\ndir_terms_unambiguous = \"terms.unambiguous\"\nsrc_dirs = [\".\"]\n",
    )
    .unwrap();

    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "Lowercase 'docker' should not match list entry 'Docker': stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    // Now write the term with the canonical casing — must be flagged.
    fs::write(
        project_path.join("README.md"),
        "# Doc\n\nWe run Docker on every host.\n",
    )
    .unwrap();
    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        !output.status.success(),
        "Canonical casing 'Docker' must be flagged"
    );
}

#[test]
fn terms_ambiguous_terms_are_not_flagged() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    write_terms_dirs(project_path, &["Kubernetes"], &["server"]);

    // "server" is ambiguous - must NOT be required to be backticked.
    // "Kubernetes" is unambiguous and IS backticked - should pass.
    fs::write(
        project_path.join("README.md"),
        "# Doc\n\nThe server runs on `Kubernetes`.\n",
    )
    .unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.checker.terms]\ndir_terms_unambiguous = \"terms.unambiguous\"\ndir_terms_ambiguous = \"terms.ambiguous\"\nsrc_dirs = [\".\"]\n",
    ).unwrap();

    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "Ambiguous terms should not be required to be backticked: stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn terms_inside_urls_are_ignored() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    write_terms_dirs(project_path, &["bash", "jq"], &["server"]);

    // The terms appear only inside URLs (a markdown link target and a bare
    // URL). Backticking them there would corrupt the address, so the checker
    // must not ask for it.
    fs::write(
        project_path.join("README.md"),
        "# Doc\n\n\
         * [Bash Manual](https://www.gnu.org/software/bash/manual/)\n\
         * [Manual](https://stedolan.github.io/jq/manual/)\n\
         * https://example.com/bash/jq/index.html\n",
    )
    .unwrap();

    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.checker.terms]\ndir_terms_unambiguous = \"terms.unambiguous\"\ndir_terms_ambiguous = \"terms.ambiguous\"\nsrc_dirs = [\".\"]\n",
    ).unwrap();

    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "Terms inside URLs must not be flagged: stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn terms_in_prose_still_flagged_alongside_urls() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    write_terms_dirs(project_path, &["bash"], &["server"]);

    // Same term in a URL (ignored) and in prose (must still be flagged).
    fs::write(
        project_path.join("README.md"),
        "# Doc\n\n\
         See [manual](https://www.gnu.org/software/bash/manual/) for bash usage.\n",
    )
    .unwrap();

    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.checker.terms]\ndir_terms_unambiguous = \"terms.unambiguous\"\ndir_terms_ambiguous = \"terms.ambiguous\"\nsrc_dirs = [\".\"]\n",
    ).unwrap();

    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        !output.status.success(),
        "Unbackticked term in prose must still fail"
    );
}