bashrs 6.66.0

Rust-to-Shell transpiler for deterministic bootstrap scripts
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
//! CLI Integration tests for installer command
//!
//! These tests follow EXTREME TDD principles and use assert_cmd as MANDATORY per CLAUDE.md.
//! Test naming convention: test_<TASK_ID>_<feature>_<scenario>

#![allow(clippy::unwrap_used)]
#![allow(clippy::expect_used)]
#![allow(non_snake_case)]

use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::TempDir;

/// Helper function to create bashrs command (MANDATORY pattern per CLAUDE.md)
fn bashrs_cmd() -> Command {
    assert_cmd::cargo_bin_cmd!("bashrs")
}

// =============================================================================
// INSTALLER_CLI_001: Init command tests
// =============================================================================

#[test]
fn test_INSTALLER_CLI_001_init_creates_project() {
    let temp_dir = TempDir::new().unwrap();
    let project_name = "test-installer";
    let project_path = temp_dir.path().join(project_name);

    bashrs_cmd()
        .arg("installer")
        .arg("init")
        .arg(&project_path)
        .assert()
        .success()
        .stdout(predicate::str::contains("Initialized installer project"))
        .stdout(predicate::str::contains("installer.toml"));

    // Verify project structure
    assert!(project_path.join("installer.toml").exists());
    assert!(project_path.join("tests").exists());
    assert!(project_path.join("tests").join("mod.rs").exists());
    assert!(project_path.join("tests").join("falsification.rs").exists());
    assert!(project_path.join("templates").exists());
}

#[test]
fn test_INSTALLER_CLI_001_init_with_description() {
    let temp_dir = TempDir::new().unwrap();
    let project_path = temp_dir.path().join("my-project");

    bashrs_cmd()
        .arg("installer")
        .arg("init")
        .arg(&project_path)
        .arg("--description")
        .arg("My custom installer")
        .assert()
        .success();

    // Verify description in installer.toml
    let content = fs::read_to_string(project_path.join("installer.toml")).unwrap();
    assert!(content.contains("My custom installer"));
}

// =============================================================================
// INSTALLER_CLI_002: Validate command tests
// =============================================================================

#[test]
fn test_INSTALLER_CLI_002_validate_valid_installer() {
    let temp_dir = TempDir::new().unwrap();
    let project_path = temp_dir.path().join("valid-installer");

    // First create a valid installer
    bashrs_cmd()
        .arg("installer")
        .arg("init")
        .arg(&project_path)
        .assert()
        .success();

    // Now validate it
    bashrs_cmd()
        .arg("installer")
        .arg("validate")
        .arg(&project_path)
        .assert()
        .success()
        .stdout(predicate::str::contains("Installer is valid"));
}

#[test]
fn test_INSTALLER_CLI_002_validate_missing_installer() {
    let temp_dir = TempDir::new().unwrap();
    let nonexistent = temp_dir.path().join("nonexistent");

    bashrs_cmd()
        .arg("installer")
        .arg("validate")
        .arg(&nonexistent)
        .assert()
        .failure();
}

#[test]
fn test_INSTALLER_CLI_002_validate_invalid_toml() {
    let temp_dir = TempDir::new().unwrap();
    let project_path = temp_dir.path();

    // Write invalid TOML
    fs::write(project_path.join("installer.toml"), "INVALID [[[").unwrap();

    bashrs_cmd()
        .arg("installer")
        .arg("validate")
        .arg(project_path)
        .assert()
        .failure();
}

// =============================================================================
// INSTALLER_CLI_003: Run command tests (dry-run mode)
// =============================================================================

#[test]
fn test_INSTALLER_CLI_003_run_dry_run() {
    let temp_dir = TempDir::new().unwrap();
    let project_path = temp_dir.path().join("installer");

    // Create installer first
    bashrs_cmd()
        .arg("installer")
        .arg("init")
        .arg(&project_path)
        .assert()
        .success();

    // Run in dry-run mode
    bashrs_cmd()
        .arg("installer")
        .arg("run")
        .arg(&project_path)
        .arg("--dry-run")
        .assert()
        .success()
        .stdout(predicate::str::contains("Dry-run mode"));
}

#[test]
fn test_INSTALLER_CLI_003_run_dry_run_diff() {
    let temp_dir = TempDir::new().unwrap();
    let project_path = temp_dir.path().join("installer");

    // Create installer first
    bashrs_cmd()
        .arg("installer")
        .arg("init")
        .arg(&project_path)
        .assert()
        .success();

    // Run in dry-run with diff
    bashrs_cmd()
        .arg("installer")
        .arg("run")
        .arg(&project_path)
        .arg("--dry-run")
        .arg("--diff")
        .assert()
        .success()
        .stdout(predicate::str::contains("Dry-Run Diff Preview"));
}

// =============================================================================
// INSTALLER_CLI_004: Graph command tests
// =============================================================================

#[test]
fn test_INSTALLER_CLI_004_graph_mermaid() {
    let temp_dir = TempDir::new().unwrap();
    let project_path = temp_dir.path().join("installer");

    // Create installer first
    bashrs_cmd()
        .arg("installer")
        .arg("init")
        .arg(&project_path)
        .assert()
        .success();

    // Generate mermaid graph
    bashrs_cmd()
        .arg("installer")
        .arg("graph")
        .arg(&project_path)
        .arg("--format")
        .arg("mermaid")
        .assert()
        .success()
        .stdout(predicate::str::contains("graph TD"));
}

#[test]
fn test_INSTALLER_CLI_004_graph_dot() {
    let temp_dir = TempDir::new().unwrap();
    let project_path = temp_dir.path().join("installer");

    // Create installer first
    bashrs_cmd()
        .arg("installer")
        .arg("init")
        .arg(&project_path)
        .assert()
        .success();

    // Generate DOT graph
    bashrs_cmd()
        .arg("installer")
        .arg("graph")
        .arg(&project_path)
        .arg("--format")
        .arg("dot")
        .assert()
        .success()
        .stdout(predicate::str::contains("digraph InstallerGraph"));
}

#[test]
fn test_INSTALLER_CLI_004_graph_json() {
    let temp_dir = TempDir::new().unwrap();
    let project_path = temp_dir.path().join("installer");

    // Create installer first
    bashrs_cmd()
        .arg("installer")
        .arg("init")
        .arg(&project_path)
        .assert()
        .success();

    // Generate JSON graph
    bashrs_cmd()
        .arg("installer")
        .arg("graph")
        .arg(&project_path)
        .arg("--format")
        .arg("json")
        .assert()
        .success()
        .stdout(predicate::str::contains("\"nodes\""));
}

// =============================================================================
// INSTALLER_CLI_005: Lock command tests
// =============================================================================

#[test]
fn test_INSTALLER_CLI_005_lock_generate() {
    let temp_dir = TempDir::new().unwrap();
    let project_path = temp_dir.path().join("installer");

    // Create installer first
    bashrs_cmd()
        .arg("installer")
        .arg("init")
        .arg(&project_path)
        .assert()
        .success();

    // Generate lockfile
    bashrs_cmd()
        .arg("installer")
        .arg("lock")
        .arg(&project_path)
        .assert()
        .success()
        .stdout(predicate::str::contains("Managing lockfile"));
}

// =============================================================================
// INSTALLER_CLI_006: Test matrix command tests
// =============================================================================

#[test]
fn test_INSTALLER_CLI_006_test_matrix() {
    let temp_dir = TempDir::new().unwrap();
    let project_path = temp_dir.path().join("installer");

    // Create installer first
    bashrs_cmd()
        .arg("installer")
        .arg("init")
        .arg(&project_path)
        .assert()
        .success();

    // Run tests with matrix
    bashrs_cmd()
        .arg("installer")
        .arg("test")
        .arg(&project_path)
        .arg("--matrix")
        .arg("ubuntu:22.04,debian:12")
        .assert()
        .success()
        .stdout(predicate::str::contains("Container Test Matrix"))
        .stdout(predicate::str::contains("ubuntu:22.04"))
        .stdout(predicate::str::contains("debian:12"));
}

// =============================================================================
// INSTALLER_CLI_007: Keyring command tests
// =============================================================================

#[test]
fn test_INSTALLER_CLI_007_keyring_init() {
    bashrs_cmd()
        .arg("installer")
        .arg("keyring")
        .arg("init")
        .assert()
        .success()
        .stdout(predicate::str::contains("Initialized keyring"));
}

#[test]
fn test_INSTALLER_CLI_007_keyring_list() {
    bashrs_cmd()
        .arg("installer")
        .arg("keyring")
        .arg("list")
        .assert()
        .success()
        .stdout(predicate::str::contains("Keyring")); // Outputs "Keyring not initialized" or "Keyring contents"
}

// =============================================================================
// INSTALLER_CLI_008: Help command tests
// =============================================================================

#[test]
fn test_INSTALLER_CLI_008_help_shows_subcommands() {
    bashrs_cmd()
        .arg("installer")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("init"))
        .stdout(predicate::str::contains("validate"))
        .stdout(predicate::str::contains("run"))
        .stdout(predicate::str::contains("test"))
        .stdout(predicate::str::contains("graph"));
}

#[test]
fn test_INSTALLER_CLI_008_init_help() {
    bashrs_cmd()
        .arg("installer")
        .arg("init")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("TDD-first test harness"));
}

// =============================================================================
// INSTALLER_CLI_009: From-bash command tests (#115)
// =============================================================================

#[test]
fn test_INSTALLER_CLI_009_from_bash_converts_script() {
    let temp_dir = TempDir::new().unwrap();
    let script_path = temp_dir.path().join("install.sh");
    let output_path = temp_dir.path().join("my-installer");

    // Write a test bash script
    fs::write(
        &script_path,
        r#"#!/bin/bash
apt-get update
apt-get install -y docker-ce nginx
mkdir -p /opt/myapp
"#,
    )
    .unwrap();

    bashrs_cmd()
        .arg("installer")
        .arg("from-bash")
        .arg(&script_path)
        .arg("--output")
        .arg(&output_path)
        .assert()
        .success()
        .stdout(predicate::str::contains("Conversion complete"))
        .stdout(predicate::str::contains("Steps generated"));

    // Verify output structure
    assert!(output_path.join("installer.toml").exists());
    assert!(output_path.join("templates").exists());

    // Verify installer.toml content
    let content = fs::read_to_string(output_path.join("installer.toml")).unwrap();
    assert!(content.contains("[installer]"));
    assert!(content.contains("[[step]]"));
}

#[test]
fn test_INSTALLER_CLI_009_from_bash_missing_file() {
    let temp_dir = TempDir::new().unwrap();
    let nonexistent = temp_dir.path().join("nonexistent.sh");

    bashrs_cmd()
        .arg("installer")
        .arg("from-bash")
        .arg(&nonexistent)
        .assert()
        .failure();
}

#[test]
fn test_INSTALLER_CLI_009_from_bash_default_output() {
    let temp_dir = TempDir::new().unwrap();
    let script_path = temp_dir.path().join("docker-setup.sh");

    // Write a minimal test script
    fs::write(&script_path, "apt-get update").unwrap();

    // Run from the temp directory so output goes there
    bashrs_cmd()
        .current_dir(temp_dir.path())
        .arg("installer")
        .arg("from-bash")
        .arg(&script_path)
        .assert()
        .success()
        .stdout(predicate::str::contains("docker-setup-installer"));
}