rsconstruct 0.9.46

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

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

    // Create a simple template
    fs::write(
        project_path.join("config/cache_test.py"),
        "value = 'cached'"
    ).unwrap();

    fs::write(
        project_path.join("tera.templates/cached.txt.tera"),
        "{% set c = load_python(path='config/cache_test.py') %}{{ c.value }}"
    ).unwrap();

    // Build to populate cache
    let output = run_rsconstruct(project_path, &["build"]);
    assert!(output.status.success());
    assert!(project_path.join("cached.txt").exists());
    assert!(project_path.join(".rsconstruct/db.redb").exists());
    assert!(project_path.join(".rsconstruct/objects").exists());

    // Check cache size reports objects
    let size_output = run_rsconstruct(project_path, &["cache", "size"]);
    assert!(size_output.status.success());
    let size_stdout = String::from_utf8_lossy(&size_output.stdout);
    assert!(size_stdout.contains("Cache size:"));
    assert!(size_stdout.contains("objects"));
    // Should have at least 1 object
    assert!(!size_stdout.contains("0 objects"), "Cache should have objects after build");

    // Delete the output file, then rebuild — should restore from cache
    fs::remove_file(project_path.join("cached.txt")).unwrap();
    assert!(!project_path.join("cached.txt").exists());

    let restore_output = run_rsconstruct_with_env(project_path, &["build", "--verbose"], &[("NO_COLOR", "1")]);
    assert!(restore_output.status.success());
    let restore_stdout = String::from_utf8_lossy(&restore_output.stdout);
    assert!(restore_stdout.contains("Restored from cache:"));
    assert!(project_path.join("cached.txt").exists());

    // Verify restored content is correct
    let content = fs::read_to_string(project_path.join("cached.txt")).unwrap();
    assert_eq!(content.trim(), "cached");

    // Trim cache (nothing unreferenced, so 0 removed)
    let trim_output = run_rsconstruct(project_path, &["cache", "trim"]);
    assert!(trim_output.status.success());
    let trim_stdout = String::from_utf8_lossy(&trim_output.stdout);
    assert!(trim_stdout.contains("0 unreferenced objects"));

    // Clear cache entirely
    let clear_output = run_rsconstruct(project_path, &["cache", "clear"]);
    assert!(clear_output.status.success());
    // .rsconstruct/ exists (fresh db) but objects dir is gone
    assert!(!project_path.join(".rsconstruct").join("objects").exists());

    // Cache size after clear should be 0
    let size_after = run_rsconstruct(project_path, &["cache", "size"]);
    assert!(size_after.status.success());
    let size_after_stdout = String::from_utf8_lossy(&size_after.stdout);
    assert!(size_after_stdout.contains("0 B"));
    assert!(size_after_stdout.contains("0 objects"));
}

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

    fs::write(
        project_path.join("tera.templates/list_test.txt.tera"),
        "hello"
    ).unwrap();

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

    // List cache — output is JSON
    let output = run_rsconstruct_with_env(project_path, &["cache", "list"], &[("NO_COLOR", "1")]);
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    let entries: serde_json::Value = serde_json::from_str(&stdout)
        .unwrap_or_else(|e| panic!("Cache list should be valid JSON: {}\nOutput: {}", e, stdout));
    let arr = entries.as_array().expect("Cache list should be a JSON array");
    assert!(!arr.is_empty(), "Cache list should have entries after build");
    let first = &arr[0];
    assert!(first["cache_key"].as_str().is_some(),
        "Cache entry should have a cache_key: {}", first);
}

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

    fs::write(
        project_path.join("rsconstruct.toml"),
        "\n"
    ).unwrap();

    // Empty cache should produce an empty JSON array
    let output = run_rsconstruct_with_env(project_path, &["cache", "list"], &[("NO_COLOR", "1")]);
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    let entries: serde_json::Value = serde_json::from_str(&stdout)
        .unwrap_or_else(|e| panic!("Cache list should be valid JSON: {}\nOutput: {}", e, stdout));
    let arr = entries.as_array().expect("Cache list should be a JSON array");
    assert!(arr.is_empty(), "Empty cache should produce an empty JSON array: {}", stdout);
}

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

    fs::write(
        project_path.join("rsconstruct.toml"),
        "\n"
    ).unwrap();

    let output = run_rsconstruct_with_env(project_path, &["cache", "stats"], &[("NO_COLOR", "1")]);
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("Cache is empty"), "Expected 'Cache is empty' message, got: {}", stdout);
}

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

    fs::write(
        project_path.join("tera.templates/stats_test.txt.tera"),
        "hello"
    ).unwrap();

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

    // Check stats
    let output = run_rsconstruct_with_env(project_path, &["cache", "stats"], &[("NO_COLOR", "1")]);
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("entries"), "Expected 'entries' in stats, got: {}", stdout);
}

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

    fs::write(
        project_path.join("tera.templates/json_test.txt.tera"),
        "hello"
    ).unwrap();

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

    // Verify cache stats outputs valid JSON
    let output = run_rsconstruct_with_env(project_path, &["--json", "cache", "stats"], &[("NO_COLOR", "1")]);
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    let parsed: serde_json::Value = serde_json::from_str(&stdout)
        .unwrap_or_else(|e| panic!("Cache stats JSON should be valid: {}\nOutput: {}", e, stdout));
    assert!(parsed.is_object(), "Expected JSON object, got: {}", stdout);
    assert!(parsed.get("all").is_some(), "Expected 'all' key in stats JSON, got: {}", stdout);
}

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

    fs::write(
        project_path.join("tera.templates/clear_test.txt.tera"),
        "hello"
    ).unwrap();

    // Build to populate cache
    let build = run_rsconstruct(project_path, &["build"]);
    assert!(build.status.success());
    assert!(project_path.join(".rsconstruct").exists(), "Cache dir should exist after build");
    assert!(project_path.join(".rsconstruct/objects").exists(), "Objects dir should exist after build");
    assert!(project_path.join(".rsconstruct/descriptors").exists(), "Descriptors dir should exist after build");

    // Clear cache
    let clear = run_rsconstruct(project_path, &["cache", "clear"]);
    assert!(clear.status.success());

    // Entire .rsconstruct directory should be gone
    assert!(!project_path.join(".rsconstruct").exists(),
        "Entire .rsconstruct dir should be removed after cache clear");
    assert!(!project_path.join(".rsconstruct/objects").exists(),
        "Objects dir should not exist after cache clear");
    assert!(!project_path.join(".rsconstruct/descriptors").exists(),
        "Descriptors dir should not exist after cache clear");

    // Rebuild should work from scratch (full rebuild, no restore)
    let rebuild = run_rsconstruct_with_env(project_path, &["build", "-v"], &[("NO_COLOR", "1")]);
    assert!(rebuild.status.success());
    let stdout = String::from_utf8_lossy(&rebuild.stdout);
    assert!(stdout.contains("Processing:"), "Should do a full rebuild after cache clear: {}", stdout);
    assert!(!stdout.contains("Restored from cache:"), "Should not restore after cache clear: {}", stdout);
}

/// `cache remove-stale` must keep entries for the current project state.
///
/// Regression test: `valid_cache_keys` used to collect plaintext
/// `Product::cache_key()` strings while `remove_stale` compared them against
/// hashed descriptor keys reconstructed from on-disk paths. Nothing ever
/// matched, so `remove-stale` deleted the entire cache and `cache stale`
/// reported 100% of entries as stale.
#[test]
fn remove_stale_keeps_current_entries() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    fs::write(
        project_path.join("tera.templates/stale_test.txt.tera"),
        "stale test content"
    ).unwrap();

    // Build to populate cache
    let build = run_rsconstruct(project_path, &["build"]);
    assert!(build.status.success());
    assert!(project_path.join("stale_test.txt").exists());

    // Everything in the cache corresponds to the current project state
    let stale = run_rsconstruct_with_env(project_path, &["cache", "stale"], &[("NO_COLOR", "1")]);
    assert!(stale.status.success());
    let stale_stdout = String::from_utf8_lossy(&stale.stdout);
    assert!(!stale_stdout.lines().any(|l| l.starts_with("stale ")),
        "Fresh build must have no stale entries: {}", stale_stdout);
    assert!(stale_stdout.lines().any(|l| l.starts_with("current ")),
        "Fresh build's entries must be recognized as current: {}", stale_stdout);

    // remove-stale must not remove anything
    let remove = run_rsconstruct_with_env(project_path, &["cache", "remove-stale"], &[("NO_COLOR", "1")]);
    assert!(remove.status.success());
    let remove_stdout = String::from_utf8_lossy(&remove.stdout);
    assert!(remove_stdout.contains("Removed 0 stale index entries"),
        "remove-stale after a fresh build must remove nothing: {}", remove_stdout);

    // The cache must still work: delete the output and restore from cache
    fs::remove_file(project_path.join("stale_test.txt")).unwrap();
    let rebuild = run_rsconstruct_with_env(project_path, &["build", "--verbose"], &[("NO_COLOR", "1")]);
    assert!(rebuild.status.success());
    let rebuild_stdout = String::from_utf8_lossy(&rebuild.stdout);
    assert!(rebuild_stdout.contains("Restored from cache:"),
        "Cache must survive remove-stale: {}", rebuild_stdout);

    // Now actually make the entry stale: change the input content. The old
    // descriptor no longer matches any current product state.
    fs::write(
        project_path.join("tera.templates/stale_test.txt.tera"),
        "changed content"
    ).unwrap();
    let remove2 = run_rsconstruct_with_env(project_path, &["cache", "remove-stale"], &[("NO_COLOR", "1")]);
    assert!(remove2.status.success());
    let remove2_stdout = String::from_utf8_lossy(&remove2.stdout);
    assert!(!remove2_stdout.contains("Removed 0 stale index entries"),
        "Changing input content must make the old entry stale: {}", remove2_stdout);
}

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

    // Create a template and build it
    fs::write(
        project_path.join("tera.templates/original.txt.tera"),
        "rename test content"
    ).unwrap();

    let build1 = run_rsconstruct(project_path, &["build"]);
    assert!(build1.status.success());
    assert!(project_path.join("original.txt").exists());

    // Remove the output and rename the input (same content, different name)
    fs::remove_file(project_path.join("original.txt")).unwrap();
    fs::rename(
        project_path.join("tera.templates/original.txt.tera"),
        project_path.join("tera.templates/renamed.txt.tera"),
    ).unwrap();

    // Build again — the content is identical, so the cache should hit
    // and restore the output (under the new name) from cache
    let build2 = run_rsconstruct_with_env(project_path, &["build", "--verbose"], &[("NO_COLOR", "1")]);
    assert!(build2.status.success(),
        "Build after rename should succeed: stderr={}",
        String::from_utf8_lossy(&build2.stderr));

    let stdout = String::from_utf8_lossy(&build2.stdout);
    assert!(stdout.contains("Restored from cache:"),
        "Renamed input with same content should restore from cache: {}", stdout);
    assert!(project_path.join("renamed.txt").exists(),
        "Output under new name should exist after restore");

    // Verify content is correct
    let content = fs::read_to_string(project_path.join("renamed.txt")).unwrap();
    assert_eq!(content.trim(), "rename test content");
}

/// Upgrading a build tool must invalidate everything that tool produced.
///
/// This used to be opt-in: `processor_tool_hashes` returned an empty map
/// unless a `.tools.versions` lock file existed, so in the common case (no
/// lock file) a tool upgrade left every cached result valid — the build
/// would happily reuse output produced by the previous version.
///
/// The tool here is a shell script the test owns, so "upgrading" it is a
/// file write. `script` declares its `command` as a required tool, which is
/// what feeds the tool component of the cache key.
#[test]
fn tool_upgrade_invalidates_cached_results() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    // A "tool" we can upgrade, on PATH via a bin dir we control.
    let bin_dir = project_path.join("toolbin");
    fs::create_dir_all(&bin_dir).unwrap();
    let tool = bin_dir.join("faketool");
    fs::write(&tool, "#!/bin/sh\nexit 0\n").unwrap();
    crate::common::make_executable(&tool);

    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.script]\ncommand = \"faketool\"\nsrc_dirs = [\"src\"]\nsrc_extensions = [\".txt\"]\n",
    ).unwrap();
    fs::create_dir_all(project_path.join("src")).unwrap();
    fs::write(project_path.join("src/a.txt"), "content\n").unwrap();

    let path_env = format!("{}:{}", bin_dir.display(), std::env::var("PATH").unwrap_or_default());

    // First build populates the cache.
    let build1 = run_rsconstruct_with_env(
        project_path, &["build", "-v"],
        &[("NO_COLOR", "1"), ("PATH", &path_env)],
    );
    assert!(build1.status.success(),
        "first build failed: {}", String::from_utf8_lossy(&build1.stderr));

    // Second build with the tool unchanged: cached, nothing re-run.
    let build2 = run_rsconstruct_with_env(
        project_path, &["build", "-v"],
        &[("NO_COLOR", "1"), ("PATH", &path_env)],
    );
    assert!(build2.status.success(),
        "second build failed: {}", String::from_utf8_lossy(&build2.stderr));
    let stdout2 = String::from_utf8_lossy(&build2.stdout);
    assert!(!stdout2.contains("Processing:"),
        "unchanged tool + unchanged inputs must not re-run anything: {}", stdout2);

    // "Upgrade" the tool. Inputs and config are untouched.
    fs::write(&tool, "#!/bin/sh\n# v2\nexit 0\n").unwrap();
    crate::common::make_executable(&tool);

    let build3 = run_rsconstruct_with_env(
        project_path, &["build", "-v"],
        &[("NO_COLOR", "1"), ("PATH", &path_env)],
    );
    assert!(build3.status.success(),
        "build after tool upgrade failed: {}", String::from_utf8_lossy(&build3.stderr));
    let stdout3 = String::from_utf8_lossy(&build3.stdout);
    assert!(stdout3.contains("Processing:"),
        "a tool upgrade must invalidate cached results: {}", stdout3);
}

/// `[build] hash_tool_versions = false` restores the old behavior for
/// projects that must not have tool identity in their cache keys.
#[test]
fn hash_tool_versions_false_ignores_tool_upgrade() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    let bin_dir = project_path.join("toolbin");
    fs::create_dir_all(&bin_dir).unwrap();
    let tool = bin_dir.join("faketool2");
    fs::write(&tool, "#!/bin/sh\nexit 0\n").unwrap();
    crate::common::make_executable(&tool);

    fs::write(
        project_path.join("rsconstruct.toml"),
        "[build]\nhash_tool_versions = false\n\n\
         [processor.script]\ncommand = \"faketool2\"\nsrc_dirs = [\"src\"]\nsrc_extensions = [\".txt\"]\n",
    ).unwrap();
    fs::create_dir_all(project_path.join("src")).unwrap();
    fs::write(project_path.join("src/a.txt"), "content\n").unwrap();

    let path_env = format!("{}:{}", bin_dir.display(), std::env::var("PATH").unwrap_or_default());

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

    fs::write(&tool, "#!/bin/sh\n# v2\nexit 0\n").unwrap();
    crate::common::make_executable(&tool);

    let build2 = run_rsconstruct_with_env(
        project_path, &["build", "-v"],
        &[("NO_COLOR", "1"), ("PATH", &path_env)],
    );
    assert!(build2.status.success(),
        "second build failed: {}", String::from_utf8_lossy(&build2.stderr));
    let stdout2 = String::from_utf8_lossy(&build2.stdout);
    assert!(!stdout2.contains("Processing:"),
        "with hash_tool_versions=false a tool upgrade must not invalidate: {}", stdout2);
}