rsconstruct 0.9.176

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
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
566
567
568
569
570
571
572
573
574
575
576
577
578
//! Tests for two config features:
//! - Missing `src_dirs` entries are a config error; `[build]
//!   allow_missing_src_dirs = true` restores the old skip.
//! - `rsconstruct.local.toml` — a per-repo overlay deep-merged over the main
//!   config at load time.

use crate::common::{
    run_rsconstruct, run_rsconstruct_with_env, setup_project_with_config, write_file,
};
use std::fs;
use tempfile::TempDir;

/// A src_dirs entry that doesn't exist is a config error, not a silent skip:
/// a listed directory that is not there is a stale stanza or a copy of
/// another repo's config, and either way a processor that checks nothing.
#[test]
fn missing_src_dirs_is_a_config_error() {
    let temp_dir = setup_project_with_config(
        "[processor.generator.tera]\nsrc_dirs = [\"missing.templates\"]\n",
    );
    let output = run_rsconstruct(temp_dir.path(), &["build"]);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert_eq!(
        output.status.code(),
        Some(2),
        "a missing src_dirs entry must fail as a config error: {stderr}"
    );
    assert!(
        stderr.contains("processor.generator.tera") && stderr.contains("missing.templates"),
        "error must name the processor and the entry: {stderr}"
    );
}

/// With the skip restored, a missing entry does not disturb its siblings:
/// the instance with a real directory still builds.
#[test]
fn allow_missing_src_dirs_does_not_disturb_sibling_instances() {
    let temp_dir = setup_project_with_config(concat!(
        "[build]\n",
        "allow_missing_src_dirs = true\n",
        "\n",
        "[processor.generator.tera.real]\n",
        "src_dirs = [\"tera.templates\"]\n",
        "\n",
        "[processor.generator.tera.ghost]\n",
        "src_dirs = [\"missing.templates\"]\n",
    ));
    let project = temp_dir.path();
    write_file(project, "tera.templates/hello.txt.tera", "hello");

    let output = run_rsconstruct(project, &["build"]);
    assert!(
        output.status.success(),
        "build should skip the missing dir: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    // The instance with a real directory still produced its output.
    assert!(project.join("hello.txt").exists());
}

#[test]
fn tool_check_is_deferred_to_processors_with_products() {
    // A declared processor whose tool is absent must not fail the build
    // when it has no products in this repo: its directory exists but holds
    // no file it matches.
    let temp_dir = setup_project_with_config(concat!(
        "[processor.generator.tera]\n",
        "src_dirs = [\"tera.templates\"]\n",
        "\n",
        "[processor.checker.script.ghost_check]\n",
        "command = \"scripts/does_not_exist.py\"\n",
        "src_dirs = [\"ghost_src\"]\n",
        "src_extensions = [\".md\"]\n",
    ));
    let project = temp_dir.path();
    write_file(project, "tera.templates/out.txt.tera", "ok");
    fs::create_dir_all(project.join("ghost_src")).unwrap();

    let output = run_rsconstruct(project, &["build"]);
    assert!(
        output.status.success(),
        "tool-less zero-product processor should not fail the build: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(project.join("out.txt").exists());
}

/// Deferring the tool check to processors with products must not weaken it:
/// a processor that DOES match files still fails when its tool is absent.
#[test]
fn missing_tool_fails_when_processor_has_products() {
    let temp_dir = setup_project_with_config(concat!(
        "[processor.checker.script.real_check]\n",
        "command = \"scripts/does_not_exist.py\"\n",
        "src_dirs = [\"checked\"]\n",
        "src_extensions = [\".md\"]\n",
    ));
    let project = temp_dir.path();
    write_file(project, "checked/doc.md", "# doc");

    let output = run_rsconstruct(project, &["build"]);
    assert!(
        !output.status.success(),
        "missing tool must fail when the processor has products"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("Missing required tools"),
        "expected strict tool preflight, got: {stderr}"
    );
}

/// The other half of the preflight rule: a repo-local script command is
/// something the build requires to EXIST, but never something `tool install`
/// can provision. Treating the path as a registry tool made `tool install`
/// fail with "No install method for 'scripts/check_md.py'" for a script
/// checked into the repo, which broke CI across every repo using the shared
/// config.
#[test]
fn tools_install_skips_repo_local_script_commands() {
    let temp_dir = setup_project_with_config(concat!(
        "[processor.checker.script.check_md]\n",
        "command = \"scripts/check_md.py\"\n",
        "src_dirs = [\"checked\"]\n",
        "src_extensions = [\".md\"]\n",
    ));
    let project = temp_dir.path();
    write_file(project, "checked/doc.md", "# doc");
    write_file(project, "scripts/check_md.py", "#!/usr/bin/env python\n");

    let output = run_rsconstruct(project, &["tool", "install", "machine"]);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        output.status.success(),
        "a repo-local script command is not an installable tool: {stderr}"
    );
    assert!(
        !stderr.contains("No install method"),
        "script path must not be resolved against the tool registry: {stderr}"
    );
}

#[test]
fn local_overlay_disables_processor() {
    let temp_dir =
        setup_project_with_config("[processor.generator.tera]\nsrc_dirs = [\"tera.templates\"]\n");
    let project = temp_dir.path();
    write_file(project, "tera.templates/gen.txt.tera", "generated");
    fs::write(
        project.join("rsconstruct.local.toml"),
        "[processor.generator.tera]\nenabled = false\n",
    )
    .unwrap();

    let output = run_rsconstruct(project, &["build"]);
    assert!(
        output.status.success(),
        "build failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    // The overlay disabled tera, so nothing was rendered.
    assert!(!project.join("gen.txt").exists());
}

#[test]
fn local_overlay_adds_sections() {
    let temp_dir =
        setup_project_with_config("[processor.generator.tera]\nsrc_dirs = [\"tera.templates\"]\n");
    let project = temp_dir.path();
    write_file(project, "tera.templates/gen.txt.tera", "generated");
    // The overlay adds a global [build] setting and a whole new processor
    // section — both must merge in.
    fs::write(
        project.join("rsconstruct.local.toml"),
        concat!(
            "[build]\n",
            "max_discovery_passes = 7\n",
            "\n",
            "[processor.checker.zspell]\n",
            "src_dirs = [\"empty_docs\"]\n",
        ),
    )
    .unwrap();
    fs::create_dir_all(project.join("empty_docs")).unwrap();

    let output = run_rsconstruct(project, &["build"]);
    assert!(
        output.status.success(),
        "build failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(project.join("gen.txt").exists());

    // Both overlay sections merged: the [build] value is visible in the
    // merged config, and the added processor section is a known instance.
    let cfg = run_rsconstruct(
        project,
        &["processor", "config", "processor.checker.zspell"],
    );
    assert!(
        cfg.status.success(),
        "overlay-added [processor.checker.zspell] should exist in the merged config: {}",
        String::from_utf8_lossy(&cfg.stderr)
    );
}

#[test]
fn local_overlay_field_wins_over_main() {
    let temp_dir =
        setup_project_with_config("[processor.generator.tera]\ndep_inputs = [\"config/a.py\"]\n");
    let project = temp_dir.path();
    fs::write(
        project.join("rsconstruct.local.toml"),
        "[processor.generator.tera]\ndep_inputs = [\"config/b.py\"]\n",
    )
    .unwrap();

    let output = run_rsconstruct(
        project,
        &["processor", "config", "processor.generator.tera"],
    );
    assert!(
        output.status.success(),
        "processor config failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("b.py"),
        "local value should win, got: {stdout}"
    );
    assert!(
        !stdout.contains("a.py"),
        "main value should be replaced, got: {stdout}"
    );
}

#[test]
fn local_overlay_without_main_config_fails() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    fs::write(
        temp_dir.path().join("rsconstruct.local.toml"),
        "[processor.generator.tera]\n",
    )
    .unwrap();

    let output = run_rsconstruct(temp_dir.path(), &["build"]);
    assert!(
        !output.status.success(),
        "build should fail without a main config"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("rsconstruct.local.toml found without rsconstruct.toml"),
        "expected overlay-without-main error, got: {stderr}"
    );
}

/// The user-level config (`$XDG_CONFIG_HOME/rsconstruct/config.toml`) sets
/// `[build]` defaults under the repo config: it applies when the repo says
/// nothing, and the repo's own `[build]` wins key by key.
#[test]
fn user_config_sets_build_defaults_under_the_repo() {
    let temp_dir = setup_project_with_config("[processor.generator.tera]\nsrc_dirs = [\".\"]\n");
    let project = temp_dir.path();
    write_file(project, "tera.templates/hello.txt.tera", "hello");
    let xdg = TempDir::new().unwrap();
    fs::create_dir_all(xdg.path().join("rsconstruct")).unwrap();
    fs::write(
        xdg.path().join("rsconstruct/config.toml"),
        "[build]\nreject_dot_src_dirs = true\n",
    )
    .unwrap();
    let xdg_path = xdg.path().to_str().unwrap();

    // The user default applies: "." is rejected.
    let output = run_rsconstruct_with_env(project, &["build"], &[("XDG_CONFIG_HOME", xdg_path)]);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert_eq!(
        output.status.code(),
        Some(2),
        "the user config's reject_dot_src_dirs must apply: {stderr}"
    );
    assert!(
        stderr.contains("reject_dot_src_dirs"),
        "unexpected error: {stderr}"
    );

    // The repo overrides the user default key by key.
    fs::write(
        project.join("rsconstruct.toml"),
        "[build]\nreject_dot_src_dirs = false\n\n[processor.generator.tera]\nsrc_dirs = [\".\"]\n",
    )
    .unwrap();
    let output = run_rsconstruct_with_env(project, &["build"], &[("XDG_CONFIG_HOME", xdg_path)]);
    assert!(
        output.status.success(),
        "the repo's own [build] must win over the user config: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(project.join("hello.txt").exists());

    // Without the user file the default is off.
    let empty = TempDir::new().unwrap();
    fs::write(
        project.join("rsconstruct.toml"),
        "[processor.generator.tera]\nsrc_dirs = [\".\"]\n",
    )
    .unwrap();
    let output = run_rsconstruct_with_env(
        project,
        &["build"],
        &[("XDG_CONFIG_HOME", empty.path().to_str().unwrap())],
    );
    assert!(
        output.status.success(),
        "no user file means the built-in default: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

/// The user config may carry `[build]` only: anything that could change what
/// a repo builds must live in the repo, where CI sees it.
#[test]
fn user_config_rejects_sections_other_than_build() {
    let temp_dir =
        setup_project_with_config("[processor.generator.tera]\nsrc_dirs = [\"tera.templates\"]\n");
    let project = temp_dir.path();
    write_file(project, "tera.templates/hello.txt.tera", "hello");
    let xdg = TempDir::new().unwrap();
    fs::create_dir_all(xdg.path().join("rsconstruct")).unwrap();
    fs::write(
        xdg.path().join("rsconstruct/config.toml"),
        "[build]\nreject_dot_src_dirs = true\n\n[processor.checker.ruff]\nsrc_dirs = [\"src\"]\n",
    )
    .unwrap();
    let output = run_rsconstruct_with_env(
        project,
        &["build"],
        &[("XDG_CONFIG_HOME", xdg.path().to_str().unwrap())],
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert_eq!(
        output.status.code(),
        Some(2),
        "a [processor] table in the user config must be refused: {stderr}"
    );
    assert!(
        stderr.contains("only a [build] table and a [tables] table")
            && stderr.contains("[processor]"),
        "error must say what is allowed and what was found: {stderr}"
    );
}

/// A type or unknown-field error is found in the merged config. The message
/// names the one file that holds it: it used to name rsconstruct.toml alone
/// even when the typo was in the overlay, and then every merged file with
/// "the error is in one of them".
#[test]
fn config_error_names_the_file_that_holds_it() {
    let temp_dir = setup_project_with_config("[build]\nparallel = 2\n");
    write_file(
        temp_dir.path(),
        "rsconstruct.local.toml",
        "[build]\nparalel = 4\n",
    );
    let output = run_rsconstruct(temp_dir.path(), &["toml", "check"]);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert_eq!(output.status.code(), Some(2), "{stderr}");
    assert!(
        stderr.contains("Failed to parse config file rsconstruct.local.toml")
            && stderr.contains("line 2, column 1"),
        "error must name the overlay and the line: {stderr}"
    );
}

/// `toml check` rejects what every building command rejects: compression
/// with the hardlink restore method used to pass the check and fail later.
#[test]
fn toml_check_rejects_compression_with_hardlink_restore() {
    let temp_dir =
        setup_project_with_config("[cache]\ncompression = true\nrestore_method = \"hardlink\"\n");
    let output = run_rsconstruct(temp_dir.path(), &["toml", "check"]);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert_eq!(output.status.code(), Some(2), "{stderr}");
    assert!(stderr.contains("compression"), "{stderr}");
}

/// Discovery converges when a pass declares no new file. With a cap of one
/// pass, a project whose products feed nothing used to fail as
/// "did not converge", because the check looked at new products.
#[test]
fn one_discovery_pass_is_enough_when_nothing_is_routed() {
    let temp_dir = setup_project_with_config(
        "[build]\nmax_discovery_passes = 1\n\n[processor.generator.tera]\nsrc_dirs = [\"tera.templates\"]\n",
    );
    write_file(temp_dir.path(), "tera.templates/hello.txt.tera", "hello");
    let output = run_rsconstruct(temp_dir.path(), &["build"]);
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(temp_dir.path().join("hello.txt").exists());
}

/// What `toml check` prints on stderr for a project with these two files
/// (the overlay is left out when `local` is empty). Every case here is a
/// config error.
fn check_stderr(main: &str, local: &str) -> String {
    let temp_dir = setup_project_with_config(main);
    if !local.is_empty() {
        write_file(temp_dir.path(), "rsconstruct.local.toml", local);
    }
    let output = run_rsconstruct(temp_dir.path(), &["toml", "check"]);
    let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
    assert_eq!(output.status.code(), Some(2), "{stderr}");
    stderr
}

/// A schema mistake is found in the merged config, which has no positions:
/// the message used to name the section and leave the file and the line to
/// the reader. Each one now leads with the file and line of its key, and a
/// key set in the overlay is reported in the overlay.
#[test]
fn schema_errors_name_the_file_and_line() {
    let main = "# main\n[processor.checker.ruff]\nbogus = 1\n\n[processor.ruff]\n\n[analyzer.python]\nenabeld = true\n";
    let stderr = check_stderr(main, "");
    for expected in [
        "rsconstruct.toml:3: [processor.checker.ruff]: unknown field 'bogus'",
        "rsconstruct.toml:5: [processor.ruff]: unknown processor type",
        "rsconstruct.toml:8: [analyzer.python]: unknown field 'enabeld'",
    ] {
        assert!(
            stderr.contains(expected),
            "missing `{expected}` in: {stderr}"
        );
    }

    let stderr = check_stderr(
        "[processor.checker.ruff]\nargs = [\"-q\"]\n",
        "\n\n[processor.checker.ruff]\nmax_jobs = 0\n",
    );
    assert!(
        stderr.contains("rsconstruct.local.toml:4: [processor.checker.ruff]: 'max_jobs'"),
        "{stderr}"
    );
}

/// A required field that is missing has no line of its own: the stanza's
/// header is where it has to be added.
#[test]
fn a_missing_required_field_is_located_at_its_stanza() {
    let stderr = check_stderr(
        "[build]\nparallel = 1\n\n[processor.explicit.generic.a]\ninputs = [\"x\"]\n",
        "",
    );
    assert!(
        stderr.contains("rsconstruct.toml:4: [processor.explicit.generic.a]: required field"),
        "{stderr}"
    );
}

/// A value of the wrong type in a top-level section was reported against
/// "config (merged from ...; the error is in one of them)". The file that
/// holds it fails the same way on its own, with line, column and the line
/// itself.
#[test]
fn a_type_error_is_reported_in_the_file_that_holds_it() {
    let stderr = check_stderr(
        "[build]\nparallel = 2\n",
        "# overlay\n[build]\nparallel = \"x\"\n",
    );
    assert!(
        stderr.contains("Failed to parse config file rsconstruct.local.toml")
            && stderr.contains("line 3, column 12"),
        "{stderr}"
    );
    assert!(!stderr.contains("merged from"), "{stderr}");

    let stderr = check_stderr(
        "[build]\nparallel = 2\n\n[cache]\nbogus = 1\n",
        "[build]\nparallel = 3\n",
    );
    assert!(
        stderr.contains("Failed to parse config file rsconstruct.toml")
            && stderr.contains("line 5, column 1"),
        "{stderr}"
    );
}

/// The range checks on `[build]` and the cross-field check on `[cache]`
/// run on the loaded config and name the line of the offending key.
#[test]
fn value_checks_name_the_file_and_line() {
    let stderr = check_stderr("[build]\nparallel = 2\n", "[build]\n\nmax_arg_len = 0\n");
    assert!(
        stderr.contains("rsconstruct.local.toml:3: [build] max_arg_len"),
        "{stderr}"
    );
    let stderr = check_stderr(
        "[cache]\nrestore_method = \"hardlink\"\ncompression = true\n",
        "",
    );
    assert!(
        stderr.contains("rsconstruct.toml:3: [cache] compression"),
        "{stderr}"
    );
}

/// A value only the processor's or analyzer's own config type rejects is
/// found when the stanza is built, long after the files were merged, and
/// is still reported at the stanza's header.
#[test]
fn a_stanza_that_cannot_be_built_is_located() {
    let stderr = check_stderr(
        "[build]\nparallel = 1\n\n[processor.checker.ruff.core]\nmax_jobs = -1\n",
        "",
    );
    assert!(
        stderr.contains("rsconstruct.toml:4: [processor.checker.ruff.core]:"),
        "{stderr}"
    );
}

/// An undefined or embedded `${...}` reference names its line.
#[test]
fn variable_errors_name_the_line() {
    let stderr = check_stderr(
        "[vars]\na = \"x\"\n\n[processor.checker.ruff]\nargs = [\"${nope}\"]\n",
        "",
    );
    assert!(
        stderr.contains("rsconstruct.toml: line 5: Undefined variable: ${nope}"),
        "{stderr}"
    );
    let stderr = check_stderr(
        "[vars]\na = \"x\"\n\n[processor.checker.ruff]\n\nargs = [\"${a}/src\"]\n",
        "",
    );
    assert!(
        stderr.contains("rsconstruct.toml: line 6: Unresolved variable reference ${a}"),
        "{stderr}"
    );
}

/// Each file is type-checked on its own. A mistyped value that a later
/// file overrides is gone from the merged config, so it used to pass until
/// the day the override was removed — in the project file under an
/// overlay, and in the user config under the project file.
#[test]
fn an_overridden_mistake_is_still_reported() {
    let stderr = check_stderr("[build]\nparallel = \"x\"\n", "[build]\nparallel = 3\n");
    assert!(
        stderr.contains("Failed to parse config file rsconstruct.toml")
            && stderr.contains("line 2, column 12"),
        "{stderr}"
    );

    let temp_dir = setup_project_with_config("[build]\nparallel = 2\n");
    let xdg = TempDir::new().unwrap();
    write_file(
        xdg.path(),
        "rsconstruct/config.toml",
        "[tables]\n\n[build]\nparallel = \"x\"\n",
    );
    let output = run_rsconstruct_with_env(
        temp_dir.path(),
        &["toml", "check"],
        &[("XDG_CONFIG_HOME", xdg.path().to_str().unwrap())],
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert_eq!(output.status.code(), Some(2), "{stderr}");
    assert!(
        stderr.contains("rsconstruct/config.toml") && stderr.contains("line 4, column 12"),
        "{stderr}"
    );
}