rsconstruct 0.9.83

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
use crate::common::{run_rsconstruct_with_env, setup_test_project};
use serde_json::Value;

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

    // `tools list` shows the central registry regardless of config, like
    // `processors list`. It lists tools no processor in this project needs.
    let output = run_rsconstruct_with_env(project_path, &["tools", "list"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "tools list failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        !stdout.is_empty(),
        "tools list should show at least one tool"
    );
    // The registry view is not processor-scoped, so it has no "(...)" column.
    assert!(
        !stdout.contains("("),
        "registry list should not show processor names in parentheses"
    );
    // It includes tools the minimal test project does not require.
    assert!(
        stdout.contains("clojure"),
        "registry list should include all known tools, e.g. clojure"
    );
}

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

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

    let stdout = String::from_utf8_lossy(&output.stdout);
    // Template processor requires python3, so list-configured always has output.
    assert!(
        !stdout.is_empty(),
        "tools list-configured should show at least one tool"
    );
    assert!(
        stdout.contains("("),
        "Expected processor name in parentheses for each tool"
    );
}

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

    let output_default = run_rsconstruct_with_env(
        project_path,
        &["tools", "list-configured"],
        &[("NO_COLOR", "1")],
    );
    let output_all = run_rsconstruct_with_env(
        project_path,
        &["tools", "list-configured", "-a"],
        &[("NO_COLOR", "1")],
    );

    assert!(output_default.status.success());
    assert!(output_all.status.success());

    let stdout_default = String::from_utf8_lossy(&output_default.stdout);
    let stdout_all = String::from_utf8_lossy(&output_all.stdout);

    // -a should show at least as many tool entries as the default
    let count_default = stdout_default.lines().count();
    let count_all = stdout_all.lines().count();
    assert!(
        count_all >= count_default,
        "tools list-configured -a should include at least as many tools as default ({} vs {})",
        count_all,
        count_default
    );
}

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

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

    let stdout = String::from_utf8_lossy(&output.stdout);
    let entries: Vec<serde_json::Value> =
        serde_json::from_str(&stdout).expect("Expected valid JSON array");

    // Check that every entry has the expected fields
    for entry in &entries {
        assert!(
            entry.get("tool").is_some(),
            "Entry should have 'tool' field"
        );
        assert!(
            entry.get("processors").is_some(),
            "Entry should have 'processors' field"
        );
        assert!(
            entry["processors"].is_array(),
            "'processors' should be an array"
        );
    }
}

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

    // First create the lock file so check has something to verify against
    let lock_output =
        run_rsconstruct_with_env(project_path, &["tools", "lock"], &[("NO_COLOR", "1")]);
    assert!(
        lock_output.status.success(),
        "tools lock failed: {}",
        String::from_utf8_lossy(&lock_output.stderr)
    );

    // Now check should succeed since versions match the just-created lock file
    let output = run_rsconstruct_with_env(project_path, &["tools", "check"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "tools check failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

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

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

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("Tool"), "Expected 'Tool' table header");
    assert!(
        stdout.contains("Runtime summary:"),
        "Expected 'Runtime summary:' section"
    );
    assert!(stdout.contains("Total:"), "Expected 'Total:' summary line");
    assert!(stdout.contains("installed"), "Expected 'installed' count");
}

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

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

    let stdout = String::from_utf8_lossy(&output.stdout);
    let parsed: Value = serde_json::from_str(&stdout).expect("Expected valid JSON");

    // Verify top-level structure
    assert!(parsed.get("tools").is_some(), "Expected 'tools' field");
    assert!(
        parsed.get("runtimes").is_some(),
        "Expected 'runtimes' field"
    );
    assert!(parsed.get("summary").is_some(), "Expected 'summary' field");

    // Verify tools array entries
    let tools = parsed["tools"]
        .as_array()
        .expect("'tools' should be an array");
    assert!(!tools.is_empty(), "tools array should not be empty");
    for tool in tools {
        assert!(tool.get("name").is_some(), "Tool entry should have 'name'");
        assert!(
            tool.get("installed").is_some(),
            "Tool entry should have 'installed'"
        );
        assert!(
            tool.get("runtime").is_some(),
            "Tool entry should have 'runtime'"
        );
        assert!(
            tool.get("processors").is_some(),
            "Tool entry should have 'processors'"
        );
    }

    // Verify runtimes array entries
    let runtimes = parsed["runtimes"]
        .as_array()
        .expect("'runtimes' should be an array");
    for rt in runtimes {
        assert!(
            rt.get("runtime").is_some(),
            "Runtime entry should have 'runtime'"
        );
        assert!(
            rt.get("total").is_some(),
            "Runtime entry should have 'total'"
        );
        assert!(
            rt.get("installed").is_some(),
            "Runtime entry should have 'installed'"
        );
        assert!(
            rt.get("missing").is_some(),
            "Runtime entry should have 'missing'"
        );
    }

    // Verify summary
    let summary = &parsed["summary"];
    assert!(
        summary.get("total_tools").is_some(),
        "Summary should have 'total_tools'"
    );
    assert!(
        summary.get("installed").is_some(),
        "Summary should have 'installed'"
    );
    assert!(
        summary.get("missing").is_some(),
        "Summary should have 'missing'"
    );

    // Verify consistency: total_tools == tools.len()
    let total_tools = summary["total_tools"].as_u64().unwrap();
    assert_eq!(
        total_tools as usize,
        tools.len(),
        "summary.total_tools should match tools array length"
    );

    // Verify consistency: installed + missing == total_tools
    let installed = summary["installed"].as_u64().unwrap();
    let missing = summary["missing"].as_u64().unwrap();
    assert_eq!(
        installed + missing,
        total_tools,
        "installed + missing should equal total_tools"
    );
}

/// Every install method named in the registry must be one that `install`
/// actually implements. A method string with no arm in `tools::run`
/// (there used to be a bogus "system") is not a config error the user can
/// see — it surfaces only when someone tries to install that tool, as an
/// "unknown install method" failure at the worst possible moment.
#[test]
fn tools_list_uses_only_implemented_install_methods() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

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

    // Kept in sync with the `match method` arms in tools::run().
    const IMPLEMENTED: &[&str] = &[
        "apt", "dnf", "pacman", "brew", "snap", "pip", "npm", "cargo", "gem", "binary", "manual",
    ];

    let parsed: Value =
        serde_json::from_slice(&output.stdout).expect("tools list --json should emit valid JSON");
    let tools = parsed
        .as_array()
        .expect("tools list --json should be an array");
    assert!(!tools.is_empty(), "registry should not be empty");

    for tool in tools {
        let name = tool["tool"].as_str().unwrap_or("<unnamed>");
        let methods = tool["install_methods"]
            .as_array()
            .expect("tool should have install_methods");
        assert!(
            !methods.is_empty(),
            "tool '{name}' has no install method at all"
        );
        for m in methods {
            let method = m["method"]
                .as_str()
                .expect("install method should have a 'method' string");
            assert!(
                IMPLEMENTED.contains(&method),
                "tool '{name}' declares install method '{method}', which tools::run() does not implement",
            );
        }
    }
}

/// Registry names are detection keys handed straight to `which::which`, which
/// only searches `$PATH` for names with no path separator. A name containing
/// `/` is resolved relative to the current working directory instead, so it
/// reports `missing` unless rsconstruct happens to run from the one directory
/// with that subtree beneath it — including for users who have the tool
/// installed and on `$PATH`. The registry once carried `gems/bin/mdl` and
/// `node_modules/.bin/markdownlint` for exactly this bug. Vendored paths belong
/// in the per-processor `command` config field, not here.
#[test]
fn tools_list_names_are_bare_binaries_not_paths() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

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

    let parsed: Value =
        serde_json::from_slice(&output.stdout).expect("tools list --json should emit valid JSON");
    let tools = parsed
        .as_array()
        .expect("tools list --json should be an array");
    assert!(!tools.is_empty(), "registry should not be empty");

    for tool in tools {
        let name = tool["tool"]
            .as_str()
            .expect("tool should have a 'tool' name string");
        assert!(
            !name.contains('/'),
            "tool '{name}' is a path, not a bare binary name; which() would resolve it \
             relative to the cwd and report it missing from anywhere else. Use the bare \
             binary name here and put the vendored path in the processor's `command` config.",
        );
    }
}

/// `tools install --all` must be able to install every registry entry.
/// A manual-only entry makes `--all` a hard error, which would break CI
/// provisioning, so the registry must not contain one.
#[test]
fn tools_install_all_has_no_manual_only_entries() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

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

    let parsed: Value =
        serde_json::from_slice(&output.stdout).expect("tools list --json should emit valid JSON");
    let manual_only: Vec<&str> = parsed
        .as_array()
        .expect("array")
        .iter()
        .filter(|tool| {
            tool["install_methods"].as_array().is_some_and(|ms| {
                !ms.is_empty() && ms.iter().all(|m| m["method"].as_str() == Some("manual"))
            })
        })
        .map(|tool| tool["tool"].as_str().unwrap_or("<unnamed>"))
        .collect();

    assert!(
        manual_only.is_empty(),
        "these tools have only a manual install method, so `tools install --all` cannot provision them: {manual_only:?}",
    );
}

/// `--all` walks the registry instead of the config, so it must reject a
/// tool name rather than silently ignoring one of the two.
#[test]
fn tools_install_all_conflicts_with_tool_name() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    let output = run_rsconstruct_with_env(
        project_path,
        &["tools", "install", "--all", "ruff"],
        &[("NO_COLOR", "1")],
    );
    assert!(
        !output.status.success(),
        "`tools install --all ruff` should be rejected"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("cannot be used with"),
        "expected a clap conflict error, got: {stderr}",
    );
}