rsconstruct 0.2.10

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
use std::fs;
use crate::common::{setup_test_project, run_rsconstruct, run_rsconstruct_with_env, run_rsconstruct_json};

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

    // Create and build a template
    fs::write(
        project_path.join("config/clean_test.py"),
        "test = 'clean'"
    ).expect("Failed to write config");

    fs::write(
        project_path.join("templates.tera/cleanme.txt.tera"),
        "{% set c = load_python(path='config/clean_test.py') %}{{ c.test }}"
    ).expect("Failed to write template");

    // Build
    let build_output = run_rsconstruct(project_path, &["build"]);
    assert!(build_output.status.success());

    // Verify files exist
    assert!(project_path.join("cleanme.txt").exists());
    assert!(project_path.join(".rsconstruct/db.redb").exists());

    // Clean
    let clean_output = run_rsconstruct(project_path, &["clean", "outputs"]);
    assert!(clean_output.status.success());

    // Verify build outputs are removed but cache is preserved
    assert!(!project_path.join("cleanme.txt").exists());
    assert!(project_path.join(".rsconstruct").exists());
}

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

    // Create template
    fs::write(
        project_path.join("config/force.py"),
        "mode = 'force'"
    ).expect("Failed to write config");

    fs::write(
        project_path.join("templates.tera/force.txt.tera"),
        "{% set c = load_python(path='config/force.py') %}Mode: {{ c.mode }}"
    ).expect("Failed to write template");

    // First build
    let first_build = run_rsconstruct(project_path, &["build"]);
    assert!(first_build.status.success(), "First build failed: {}", String::from_utf8_lossy(&first_build.stderr));

    // Force rebuild - should process, not skip
    let result = run_rsconstruct_json(project_path, &["build", "--force"]);
    assert!(result.exit_success);
    assert_eq!(result.success, 1, "Should have 1 successful build");
    assert_eq!(result.skipped, 0, "Should not skip anything with --force");
}

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

    // Create a sleep file so there's something to process
    fs::create_dir_all(project_path.join("sleep")).unwrap();
    fs::write(project_path.join("sleep/color_test.sleep"), "0.01").unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor]\nenabled = [\"sleep\"]\n"
    ).unwrap();

    // Run with NO_COLOR set
    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // ANSI escape codes start with \x1b[
    assert!(!stdout.contains("\x1b["), "Output should not contain ANSI escape codes when NO_COLOR is set");
}

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

    // Create a sleep file
    fs::create_dir_all(project_path.join("sleep")).unwrap();
    fs::write(project_path.join("sleep/timing_test.sleep"), "0.01").unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor]\nenabled = [\"sleep\"]\n"
    ).unwrap();

    // Run with --timings
    let output = run_rsconstruct_with_env(project_path, &["build", "--timings"], &[("NO_COLOR", "1")]);
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should contain timing information
    assert!(stdout.contains("Timing:"), "Output should contain 'Timing:' header");
    assert!(stdout.contains("Total:"), "Output should contain 'Total:' line");
}

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

    // Create a sleep file
    fs::create_dir_all(project_path.join("sleep")).unwrap();
    fs::write(project_path.join("sleep/no_timing.sleep"), "0.01").unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor]\nenabled = [\"sleep\"]\n"
    ).unwrap();

    // Run without --timings (and without --verbose)
    let output = run_rsconstruct(project_path, &["build"]);
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should NOT contain timing information
    assert!(!stdout.contains("Timing:"), "Output should not contain timing info without --timings flag");
    assert!(!stdout.contains("Total:"), "Output should not contain total timing without --timings flag");
}

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

    // Create sleep directory with one bad file and one good file
    fs::create_dir_all(project_path.join("sleep")).unwrap();
    fs::write(project_path.join("sleep/bad.sleep"), "not_a_number").unwrap();
    fs::write(project_path.join("sleep/good.sleep"), "0.01").unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor]\nenabled = [\"sleep\"]\n"
    ).unwrap();

    // Run with --keep-going
    let output = run_rsconstruct_with_env(project_path, &["build", "-v", "--keep-going"], &[("NO_COLOR", "1")]);

    // Should exit non-zero because of the failure
    assert!(!output.status.success(), "Build should fail with bad sleep file");

    // The good sleep file should still have been processed (verify via output)
    let stdout = String::from_utf8_lossy(&output.stdout);
    // With --keep-going, both files should be attempted to be processed
    assert!(stdout.contains("Processing:"), "Files should be processed with --keep-going");
}

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

    // Create sleep directory with one bad file
    fs::create_dir_all(project_path.join("sleep")).unwrap();
    fs::write(project_path.join("sleep/bad_k.sleep"), "invalid").unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor]\nenabled = [\"sleep\"]\n"
    ).unwrap();

    // Run with -k (short form)
    let output = run_rsconstruct_with_env(project_path, &["build", "-k"], &[("NO_COLOR", "1")]);

    // Should exit non-zero since the sleep file has invalid content
    assert!(!output.status.success(), "Build should fail with bad sleep file");
    let stderr = String::from_utf8_lossy(&output.stderr);
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should contain error reporting in stdout or stderr
    let combined = format!("{}{}", stdout, stderr);
    assert!(combined.contains("error") || combined.contains("Error"),
        "Should report errors: stdout={}, stderr={}", stdout, stderr);
}

#[test]
fn build_stops_on_first_error() {
    // Without --keep-going, build should stop immediately on first error
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    fs::create_dir_all(project_path.join("sleep")).unwrap();
    // "aaa" sorts before "zzz" alphabetically, so it will be processed first
    fs::write(project_path.join("sleep/aaa.sleep"), "not_a_number").unwrap();
    fs::write(project_path.join("sleep/zzz.sleep"), "0.01").unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor]\nenabled = [\"sleep\"]\n"
    ).unwrap();

    // Build should fail on aaa.sleep and stop
    let result = run_rsconstruct_json(project_path, &["build"]);
    assert!(!result.exit_success, "Build should fail with bad sleep file");
    assert_eq!(result.failed, 1, "Should have exactly 1 failure");
    // zzz.sleep should NOT be processed because we stop on first error
    assert!(!result.has_product("zzz.sleep", "success"),
        "Second file should NOT be processed after first error");
}

#[test]
fn keep_going_continues_after_error() {
    // With --keep-going, independent products should still be processed
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    fs::create_dir_all(project_path.join("sleep")).unwrap();
    fs::write(project_path.join("sleep/bad.sleep"), "not_a_number").unwrap();
    fs::write(project_path.join("sleep/good.sleep"), "0.01").unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor]\nenabled = [\"sleep\"]\n"
    ).unwrap();

    // First build with --keep-going — should fail but process all files
    let result1 = run_rsconstruct_json(project_path, &["build", "--keep-going"]);
    assert!(!result1.exit_success, "Build should fail with bad sleep file");
    assert_eq!(result1.failed, 1, "Should have 1 failure");
    assert_eq!(result1.success, 1, "Should have 1 success (good.sleep)");
    assert!(result1.has_product("good.sleep", "success"),
        "Good sleep file should be processed with --keep-going");

    // Fix the bad file
    fs::write(project_path.join("sleep/bad.sleep"), "0.01").unwrap();

    // Second build — good.sleep should be skipped (cached)
    let result2 = run_rsconstruct_json(project_path, &["build"]);
    assert!(result2.exit_success, "Second build should succeed");
    assert_eq!(result2.skipped, 1, "Good sleep file should be skipped (cached)");
    assert_eq!(result2.success, 1, "Bad sleep file (now fixed) should be processed");
}

#[test]
fn parallel_build_with_j_flag() {
    // Verify -j flag enables parallel execution and all products are built
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    fs::create_dir_all(project_path.join("sleep")).unwrap();
    for name in &["alpha", "beta", "gamma", "delta"] {
        fs::write(
            project_path.join(format!("sleep/{}.sleep", name)),
            "0.01"
        ).unwrap();
    }
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor]\nenabled = [\"sleep\"]\n"
    ).unwrap();

    let result = run_rsconstruct_json(project_path, &["build", "-j2"]);
    assert!(result.exit_success, "Parallel build with -j2 should succeed");
    assert_eq!(result.success, 4, "Should process all 4 sleep files");
    assert_eq!(result.total_products, 4);
}

#[test]
fn parallel_keep_going_continues_after_failure() {
    // Verify --keep-going processes all independent products even when one fails
    // under parallel execution
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    fs::create_dir_all(project_path.join("sleep")).unwrap();
    fs::write(project_path.join("sleep/aaa_bad.sleep"), "not_a_number").unwrap();
    fs::write(project_path.join("sleep/good1.sleep"), "0.01").unwrap();
    fs::write(project_path.join("sleep/good2.sleep"), "0.01").unwrap();
    fs::write(project_path.join("sleep/good3.sleep"), "0.01").unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor]\nenabled = [\"sleep\"]\n\n[build]\nparallel = 2\n"
    ).unwrap();

    let result = run_rsconstruct_json(project_path, &["build", "--keep-going"]);

    // Should fail overall
    assert!(!result.exit_success, "Build should fail with bad sleep file even with --keep-going");
    assert_eq!(result.failed, 1, "Should have 1 failure");
    assert_eq!(result.success, 3, "All 3 good sleep files should be processed with --keep-going");
}

#[test]
fn parallel_builds_all_independent_products() {
    // Verify parallel config in rsconstruct.toml works and all products complete
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    fs::create_dir_all(project_path.join("sleep")).unwrap();
    for i in 0..8 {
        fs::write(
            project_path.join(format!("sleep/task_{:02}.sleep", i)),
            "0.01"
        ).unwrap();
    }
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor]\nenabled = [\"sleep\"]\n\n[build]\nparallel = 4\n"
    ).unwrap();

    let result = run_rsconstruct_json(project_path, &["build"]);
    assert!(result.exit_success, "Parallel build with 8 products and 4 jobs should succeed");
    assert_eq!(result.success, 8, "Should process all 8 sleep files");
    assert_eq!(result.total_products, 8);

    // Incremental: second build should skip everything
    let result2 = run_rsconstruct_json(project_path, &["build"]);
    assert!(result2.exit_success);
    assert_eq!(result2.skipped, 8, "All 8 products should be skipped on second build");
}

#[test]
fn parallel_timings_flag() {
    // Verify --timings output works with parallel builds
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    fs::create_dir_all(project_path.join("sleep")).unwrap();
    for name in &["one", "two", "three"] {
        fs::write(
            project_path.join(format!("sleep/{}.sleep", name)),
            "0.01"
        ).unwrap();
    }
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor]\nenabled = [\"sleep\"]\n\n[build]\nparallel = 2\n"
    ).unwrap();

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

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("Timing:"), "Should contain 'Timing:' header in parallel mode");
    assert!(stdout.contains("Total:"), "Should contain 'Total:' line in parallel mode");

    // Should have timing entries (may be batched or individual depending on parallel execution)
    let timing_lines = stdout.lines()
        .filter(|l| l.contains("[sleep]") && l.contains("(0."))
        .count();
    assert!(timing_lines >= 1,
        "Should have at least one timing entry: {}", stdout);
}

#[test]
fn deterministic_build_order() {
    // Run two separate builds with multiple sleep files and verify
    // that the processing order is identical both times.
    let outputs: Vec<Vec<String>> = (0..2).map(|_| {
        let temp_dir = setup_test_project();
        let project_path = temp_dir.path();

        fs::create_dir_all(project_path.join("sleep")).unwrap();
        // Create several sleep files with distinct names
        for name in &["zebra", "alpha", "mango", "banana", "cherry"] {
            fs::write(
                project_path.join(format!("sleep/{}.sleep", name)),
                "0.01"
            ).unwrap();
        }

        fs::write(
            project_path.join("rsconstruct.toml"),
            "[processor]\nenabled = [\"sleep\"]\n"
        ).unwrap();

        let output = run_rsconstruct_with_env(project_path, &["build", "-v"], &[("NO_COLOR", "1")]);
        assert!(output.status.success(),
            "Build failed: {}",
            String::from_utf8_lossy(&output.stderr));

        // Extract the target name from "Processing: <name>" lines
        let stdout = String::from_utf8_lossy(&output.stdout);
        let processing_names: Vec<String> = stdout
            .lines()
            .filter(|l| l.contains("Processing:"))
            .filter_map(|l| {
                l.split("Processing:").nth(1).map(|s| s.trim().to_string())
            })
            .collect();
        assert_eq!(processing_names.len(), 5, "Should process all 5 sleep files: {}", stdout);
        processing_names
    }).collect();

    assert_eq!(outputs[0], outputs[1],
        "Build order must be deterministic across runs.\nFirst:  {:?}\nSecond: {:?}",
        outputs[0], outputs[1]);
}

/// Test that classify_products propagates dependency changes transitively.
/// Setup: tera generates step1.txt, sleep depends on step1.txt via extra_inputs.
/// When the tera template changes, both products should be classified as needing
/// rebuild — not just the tera product.
#[test]
fn classify_propagates_through_dependencies() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    // Create sleep directory
    fs::create_dir_all(project_path.join("sleep")).unwrap();

    // Phase 1: build with tera only to create the output file
    fs::write(
        project_path.join("config/gen.py"),
        "val = 1"
    ).unwrap();
    fs::write(
        project_path.join("templates.tera/step1.txt.tera"),
        "{% set c = load_python(path='config/gen.py') %}step1={{ c.val }}"
    ).unwrap();

    let output1 = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(output1.status.success(),
        "Phase 1 build failed: stdout={}, stderr={}",
        String::from_utf8_lossy(&output1.stdout),
        String::from_utf8_lossy(&output1.stderr));
    assert!(project_path.join("step1.txt").exists(), "Tera should generate step1.txt");

    // Phase 2: enable sleep with extra_inputs pointing to the tera output
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor]\nenabled = [\"tera\", \"sleep\"]\n\n[processor.sleep]\nextra_inputs = [\"step1.txt\"]\n"
    ).unwrap();
    fs::write(project_path.join("sleep/test.sleep"), "0.01").unwrap();

    // Build both processors
    let output2 = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(output2.status.success(),
        "Phase 2 build failed: stdout={}, stderr={}",
        String::from_utf8_lossy(&output2.stdout),
        String::from_utf8_lossy(&output2.stderr));

    // Verify everything is up-to-date
    let output3 = run_rsconstruct_with_env(
        project_path, &["build", "--stop-after", "classify"], &[("NO_COLOR", "1")]
    );
    assert!(output3.status.success());
    let stdout3 = String::from_utf8_lossy(&output3.stdout);
    assert!(stdout3.contains("2 up-to-date, 0 to restore, 0 to build"),
        "Both products should be up-to-date: {}", stdout3);

    // Wait so mtime differs
    std::thread::sleep(std::time::Duration::from_millis(100));

    // Modify the tera template
    fs::write(
        project_path.join("templates.tera/step1.txt.tera"),
        "{% set c = load_python(path='config/gen.py') %}modified={{ c.val }}"
    ).unwrap();

    // Classify: both products should need work (tera rebuild + sleep rebuild/restore)
    let output4 = run_rsconstruct_with_env(
        project_path, &["build", "--stop-after", "classify"], &[("NO_COLOR", "1")]
    );
    assert!(output4.status.success());
    let stdout4 = String::from_utf8_lossy(&output4.stdout);
    assert!(stdout4.contains("0 up-to-date"),
        "No products should be up-to-date when root dependency changed: {}", stdout4);
}