rsconstruct 0.9.168

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
//! 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, so the
/// message names every file that was merged. It used to name
/// rsconstruct.toml alone, even when the typo was in the overlay.
#[test]
fn config_error_names_every_merged_file() {
    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("rsconstruct.toml") && stderr.contains("rsconstruct.local.toml"),
        "error must name both merged files: {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());
}