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
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
/// REDIR-001: RED Phase
/// Test input redirection execution
#[test]
#[ignore]
fn test_input_redirection_execution() {
    let source = r#"
fn main() {
    let content = cat("input.txt");
    echo(&content);
}

fn cat(file: &str) -> String { String::new() }
fn echo(msg: &str) {}
"#;

    let config = Config::default();
    let shell = transpile(source, &config).unwrap();

    // Create test environment
    let temp_dir = TempDir::new().unwrap();
    let script_path = temp_dir.path().join("test_redir.sh");
    let input_path = temp_dir.path().join("input.txt");

    // Write test input
    fs::write(&input_path, "Hello from file").unwrap();

    // Modify script to use the temp input file
    let modified_shell = shell.replace("input.txt", input_path.to_str().unwrap());
    fs::write(&script_path, modified_shell).unwrap();

    let output = Command::new("sh")
        .arg(&script_path)
        .output()
        .expect("Failed to execute shell script");

    assert!(
        output.status.success(),
        "Script should execute successfully"
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(
        stdout.contains("Hello from file"),
        "Should read content from redirected file"
    );
}

include!("integration_tests_main_part3.rs");
#[test]
fn test_output_redirection_baseline() {
    let source = r#"
fn main() {
    write_file("output.txt", "Hello World");
}

fn write_file(path: &str, content: &str) {}
"#;

    let config = Config::default();
    let result = transpile(source, &config);

    assert!(
        result.is_ok(),
        "Should transpile file writing: {:?}",
        result.err()
    );

    let shell = result.unwrap();
    eprintln!("Generated shell for output redirection:\n{}", shell);

    // Verify the command is called correctly
    assert!(
        shell.contains("write_file"),
        "Should transpile write_file command"
    );
}

/// REDIR-002: RED Phase - ADVANCED
/// Test that echo/printf output can be redirected with >
#[test]
#[ignore] // Requires output redirection implementation
fn test_output_redirection_echo() {
    let source = r#"
fn main() {
    let mut file = std::fs::File::create("output.txt");
    write_to_file(&mut file, "Hello World");
}

fn write_to_file(f: &mut std::fs::File, content: &str) {}
"#;

    let config = Config::default();
    let shell = transpile(source, &config).unwrap();

    // Should use output redirection syntax
    assert!(
        shell.contains("> \"output.txt\"") || shell.contains(">output.txt"),
        "Should use > for File::create output redirection"
    );
}

/// REDIR-002: RED Phase - APPEND
/// Test append redirection (>>)
#[test]
#[ignore] // Requires append redirection implementation
fn test_output_redirection_append() {
    let source = r#"
fn main() {
    let mut file = std::fs::OpenOptions::new()
        .append(true)
        .open("output.txt");
    write_to_file(&mut file, "Appended text");
}

fn write_to_file(f: &mut std::fs::File, content: &str) {}
"#;

    let config = Config::default();
    let shell = transpile(source, &config).unwrap();

    // Should use append redirection syntax
    assert!(
        shell.contains(">> \"output.txt\"") || shell.contains(">>output.txt"),
        "Should use >> for append mode"
    );
}

/// REDIR-002: Baseline - Execution
/// Test that basic echo works (redirection syntax can be added later)
/// IGNORED: Empty echo() function doesn't output - needs actual implementation
#[test]
#[ignore]
fn test_output_redirection_execution() {
    let source = r#"
fn main() {
    echo("Test output");
}

fn echo(msg: &str) {}
"#;

    let config = Config::default();
    let shell = transpile(source, &config).unwrap();

    let temp_dir = TempDir::new().unwrap();
    let script_path = temp_dir.path().join("test_output.sh");

    fs::write(&script_path, &shell).unwrap();

    let output = Command::new("sh")
        .arg(&script_path)
        .output()
        .expect("Failed to execute shell script");

    assert!(
        output.status.success(),
        "Script should execute successfully"
    );

    // Verify output goes to stdout
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("Test output"), "Should output to stdout");
}

/// BUILTIN-005: RED Phase
/// Test cd command baseline
#[test]
fn test_cd_command_baseline() {
    let source = r#"
fn main() {
    change_dir("/tmp");
}

fn change_dir(path: &str) {}
"#;

    let config = Config::default();
    let result = transpile(source, &config);

    assert!(
        result.is_ok(),
        "Should transpile directory change: {:?}",
        result.err()
    );

    let shell = result.unwrap();
    eprintln!("Generated shell for cd command:\n{}", shell);

    // Verify cd command is generated
    assert!(
        shell.contains("change_dir") || shell.contains("cd"),
        "Should transpile change_dir command"
    );
}

/// BUILTIN-005: RED Phase - ADVANCED
/// Test std::env::set_current_dir() conversion to cd
#[test]
#[ignore] // Requires std::env::set_current_dir recognition
fn test_cd_command_std_env() {
    let source = r#"
fn main() {
    std::env::set_current_dir("/tmp").unwrap();
}
"#;

    let config = Config::default();
    let shell = transpile(source, &config).unwrap();

    // Should generate cd command
    assert!(
        shell.contains("cd \"/tmp\"") || shell.contains("cd /tmp"),
        "Should convert std::env::set_current_dir to cd command"
    );
}

/// BUILTIN-005: Baseline - Execution
/// Test that cd-like function calls work
#[test]
fn test_cd_command_execution() {
    let source = r#"
fn main() {
    cd("/tmp");
    pwd();
}

fn cd(path: &str) {}
fn pwd() {}
"#;

    let config = Config::default();
    let shell = transpile(source, &config).unwrap();

    let temp_dir = TempDir::new().unwrap();
    let script_path = temp_dir.path().join("test_cd.sh");

    fs::write(&script_path, &shell).unwrap();

    let output = Command::new("sh")
        .arg(&script_path)
        .output()
        .expect("Failed to execute shell script");

    assert!(
        output.status.success(),
        "Script should execute successfully"
    );
}

/// BUILTIN-011: RED Phase
/// Test pwd command baseline
#[test]
fn test_pwd_command_baseline() {
    let source = r#"
fn main() {
    pwd();
}

fn pwd() {}
"#;

    let config = Config::default();
    let result = transpile(source, &config);

    assert!(
        result.is_ok(),
        "Should transpile pwd call: {:?}",
        result.err()
    );

    let shell = result.unwrap();
    eprintln!("Generated shell for pwd command:\n{}", shell);

    // Verify function is called
    assert!(shell.contains("pwd"), "Should transpile pwd function");
}

/// BUILTIN-011: RED Phase - ADVANCED
/// Test std::env::current_dir() conversion to pwd
#[test]
#[ignore] // Requires std::env::current_dir recognition
fn test_pwd_command_std_env() {
    let source = r#"
fn main() {
    let current = std::env::current_dir().unwrap();
    echo(&current.to_string_lossy());
}

fn echo(msg: &str) {}
"#;

    let config = Config::default();
    let shell = transpile(source, &config).unwrap();

    // Should generate command substitution with pwd
    assert!(
        shell.contains("$(pwd)") || shell.contains("`pwd`"),
        "Should convert std::env::current_dir to $(pwd)"
    );
}

/// BUILTIN-011: Baseline - Execution
/// Test pwd command execution
#[test]
fn test_pwd_command_execution() {
    let source = r#"
fn main() {
    pwd();
}

fn pwd() {}
"#;

    let config = Config::default();
    let shell = transpile(source, &config).unwrap();

    let temp_dir = TempDir::new().unwrap();
    let script_path = temp_dir.path().join("test_pwd.sh");

    fs::write(&script_path, &shell).unwrap();

    let output = Command::new("sh")
        .arg(&script_path)
        .output()
        .expect("Failed to execute shell script");

    assert!(
        output.status.success(),
        "Script should execute successfully"
    );
}

/// BUILTIN-009: RED Phase
/// Test exit command baseline
#[test]
fn test_exit_command_baseline() {
    let source = r#"
fn main() {
    exit_with_code(0);
}

fn exit_with_code(code: i32) {}
"#;

    let config = Config::default();
    let result = transpile(source, &config);

    assert!(
        result.is_ok(),
        "Should transpile exit command: {:?}",
        result.err()
    );

    let shell = result.unwrap();
    eprintln!("Generated shell for exit command:\n{}", shell);

    // Verify function is called
    assert!(
        shell.contains("exit_with_code") || shell.contains("exit"),
        "Should transpile exit_with_code function"
    );
}

/// BUILTIN-009: RED Phase - ADVANCED
/// Test std::process::exit() conversion
#[test]
#[ignore] // Requires std::process::exit recognition
fn test_exit_command_std_process() {
    let source = r#"
fn main() {
    std::process::exit(0);
}
"#;

    let config = Config::default();
    let shell = transpile(source, &config).unwrap();

    // Should generate exit command
    assert!(
        shell.contains("exit 0"),
        "Should convert std::process::exit to exit command"
    );
}

/// BUILTIN-010: RED Phase
/// Test export command baseline
#[test]
fn test_export_command_baseline() {
    let source = r#"
fn main() {
    set_env("VAR", "value");
}

fn set_env(name: &str, value: &str) {}
"#;

    let config = Config::default();
    let result = transpile(source, &config);

    assert!(
        result.is_ok(),
        "Should transpile env setting: {:?}",
        result.err()
    );

    let shell = result.unwrap();
    eprintln!("Generated shell for export command:\n{}", shell);

    // Verify function is called
    assert!(
        shell.contains("set_env"),
        "Should transpile set_env function"
    );
}

/// BUILTIN-010: RED Phase - ADVANCED
/// Test std::env::set_var() conversion to export
#[test]
#[ignore] // Requires std::env::set_var recognition
fn test_export_command_std_env() {
    let source = r#"
fn main() {
    std::env::set_var("VAR", "value");
}
"#;

    let config = Config::default();
    let shell = transpile(source, &config).unwrap();

    // Should generate export command
    assert!(
        shell.contains("VAR=\"value\"")
            && (shell.contains("export VAR") || shell.contains("export")),
        "Should convert std::env::set_var to VAR=value; export VAR"
    );
}

/// BUILTIN-020: RED Phase
/// Test unset command baseline
#[test]
fn test_unset_command_baseline() {
    let source = r#"
fn main() {
    unset_var("VAR");
}

fn unset_var(name: &str) {}
"#;

    let config = Config::default();
    let result = transpile(source, &config);

    assert!(result.is_ok(), "Should transpile unset: {:?}", result.err());

    let shell = result.unwrap();
    eprintln!("Generated shell for unset command:\n{}", shell);

    // Verify function is called
    assert!(
        shell.contains("unset_var") || shell.contains("unset"),
        "Should transpile unset_var function"
    );
}

include!("integration_tests_main_part4.rs");