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
fn test_t118_vec_len() {
    // STMT: let v = vec![1]; let _ = v.len(); - vec length
    let (ok, output) = transpile_stmt("let v = vec![1]; let _ = v.len();");
    if ok && !output.contains("${#") && !output.contains("len") {
        println!("T118: WARNING - vec.len() should produce ${{#v[@]}} or similar");
    }
}

#[test]
fn test_t119_vec_index() {
    // STMT: let v = vec![1]; let _ = v[0]; - vec indexing
    let (ok, output) = transpile_stmt("let v = vec![1]; let _ = v[0];");
    if ok && !output.contains("${v[0]}") && !output.contains("[0]") {
        println!("T119: WARNING - v[0] should produce ${{v[0]}} or similar");
    }
}

#[test]
fn test_t120_contains() {
    // STMT: let v = vec![1]; v.contains(&1); - collection contains
    let (ok, output) = transpile_stmt("let v = vec![1]; let _ = v.contains(&1);");
    if !ok {
        println!("T120: contains unsupported: {}", output);
    }
}

// ============================================================================
// SECTION 4.8: Edge Cases (T121-T130)
// ============================================================================

#[test]
fn test_t121_thread_spawn() {
    // STMT: std::thread::spawn(|| {}) - should error (no threads in shell)
    let (ok, output) = transpile_stmt("std::thread::spawn(|| {});");
    if ok {
        println!("T121: Thread spawn should NOT be supported in shell");
    } else {
        // Expected to fail - threads are not available in shell
        println!(
            "T121: Correctly rejects thread::spawn: {}",
            output.lines().next().unwrap_or("")
        );
    }
}

#[test]
fn test_t122_print_no_newline() {
    // STMT: print!("no newline") - should produce printf without newline
    let (ok, output) = transpile_stmt("print!(\"no newline\");");
    if !ok {
        println!(
            "T122: print! unsupported: {}",
            output.lines().next().unwrap_or("")
        );
    } else if !output.contains("printf") && !output.contains("-n") {
        println!("T122: WARNING - print! should use printf or echo -n (no trailing newline)");
    }
}

#[test]
fn test_t123_setvar_spaces() {
    // STMT: std::env::set_var("A", "b c") - value with spaces needs quoting
    let (ok, output) = transpile_stmt("std::env::set_var(\"A\", \"b c\");");
    if !ok {
        println!(
            "T123: set_var unsupported: {}",
            output.lines().next().unwrap_or("")
        );
    } else if !output.contains("\"") && !output.contains("'") {
        println!("T123: WARNING - export with spaces needs quoting");
    }
}

#[test]
fn test_t124_hard_link() {
    // STMT: std::fs::hard_link("a", "b") - should produce ln (without -s)
    let (ok, output) = transpile_stmt("std::fs::hard_link(\"a\", \"b\");");
    if !ok {
        println!(
            "T124: hard_link unsupported: {}",
            output.lines().next().unwrap_or("")
        );
    } else if !output.contains("ln ") || output.contains("-s") {
        println!("T124: WARNING - hard_link should use 'ln' without -s flag");
    }
}

#[test]
fn test_t125_copy_file() {
    // STMT: std::fs::copy("a", "b") - should produce cp
    let (ok, output) = transpile_stmt("std::fs::copy(\"a\", \"b\");");
    if !ok {
        println!(
            "T125: copy unsupported: {}",
            output.lines().next().unwrap_or("")
        );
    } else if !output.contains("cp ") && !output.contains("cp\n") {
        println!("T125: WARNING - fs::copy should produce 'cp' command");
    }
}

#[test]
fn test_t126_rename_file() {
    // STMT: std::fs::rename("a", "b") - should produce mv
    let (ok, output) = transpile_stmt("std::fs::rename(\"a\", \"b\");");
    if !ok {
        println!(
            "T126: rename unsupported: {}",
            output.lines().next().unwrap_or("")
        );
    } else if !output.contains("mv ") && !output.contains("mv\n") {
        println!("T126: WARNING - fs::rename should produce 'mv' command");
    }
}

#[test]
fn test_t127_raw_string() {
    // STMT: let s = r"a\b"; - raw string preserves backslash
    let (ok, output) = transpile_stmt("let s = r\"a\\b\";");
    if !ok {
        println!(
            "T127: raw string unsupported: {}",
            output.lines().next().unwrap_or("")
        );
    } else {
        // Raw string should preserve the literal backslash
        println!("T127: Raw string handled");
    }
}

#[test]
fn test_t128_format_macro() {
    // STMT: let _ = format!("x: {}", 1); - string formatting
    let (ok, output) = transpile_stmt("let _ = format!(\"x: {}\", 1);");
    if !ok {
        println!(
            "T128: format! unsupported: {}",
            output.lines().next().unwrap_or("")
        );
    } else {
        // format! should produce some string construction
        println!("T128: format! handled");
    }
}

#[test]
fn test_t129_iterator_map() {
    // STMT: vec![1, 2].iter().map(|x| x+1) - functional map
    let (ok, output) = transpile_stmt("let _ = vec![1, 2].iter().map(|x| x + 1);");
    if !ok {
        println!(
            "T129: iterator map unsupported: {}",
            output.lines().next().unwrap_or("")
        );
    } else if !output.contains("for") && !output.to_lowercase().contains("loop") {
        println!("T129: WARNING - iter().map() should produce a loop");
    }
}

#[test]
fn test_t130_iterator_filter() {
    // STMT: vec![1].iter().filter(|x| *x>0) - functional filter
    let (ok, output) = transpile_stmt("let _ = vec![1, 2, 3].iter().filter(|x| *x > 1);");
    if !ok {
        println!(
            "T130: iterator filter unsupported: {}",
            output.lines().next().unwrap_or("")
        );
    } else if !output.contains("if") {
        println!("T130: WARNING - iter().filter() should produce conditional logic");
    }
}

// ============================================================================
// COMPREHENSIVE SUMMARY TEST
// ============================================================================

#[test]
fn test_tcode_comprehensive_summary() {
    println!("\n");
    println!("╔══════════════════════════════════════════════════════════════════════════════╗");
    println!("║        T-CODE TRANSPILER TEST SUMMARY (SPEC-TB-2025-001 v2.2.0)              ║");
    println!("╠══════════════════════════════════════════════════════════════════════════════╣");
    println!("║                                                                              ║");
    println!("║  Known Bugs (from bug hunt):                                                 ║");
    println!("║    TB-001: User-defined functions not transpiled                             ║");
    println!("║    TB-002: Function parameters not passed                                    ║");
    println!("║    TB-003: Multiple function definitions fail                                ║");
    println!("║    TB-004: String literal validation fails                                   ║");
    println!("║    TB-005: Range-based for loops unsupported                                 ║");
    println!("║    TB-006: Function return values not handled                                ║");
    println!("║    TB-007: Multiplication not computed                                       ║");
    println!("║    TB-008: Modulo not computed                                               ║");
    println!("║    TB-010: match statements unsupported                                      ║");
    println!("║                                                                              ║");
    println!("║  Run individual T-code tests for detailed failure analysis.                  ║");
    println!("║                                                                              ║");
    println!("╚══════════════════════════════════════════════════════════════════════════════╝");
}

// ============================================================================
// BASELINE VERIFICATION - Count passing vs failing
// ============================================================================

#[test]
fn test_tcode_baseline_verification() {
    let mut results: Vec<TCodeResult> = Vec::new();

    // T001: Empty main
    let (ok, output) = transpile_prog("fn main() {}");
    if ok && output.contains("main()") {
        results.push(TCodeResult::pass("T001"));
    } else {
        results.push(TCodeResult::fail("T001", "Missing main()"));
    }

    // T002: Integer
    let (ok, output) = transpile_stmt("let a = 1;");
    if ok && !output.contains("unknown") {
        results.push(TCodeResult::pass("T002"));
    } else {
        results.push(TCodeResult::fail("T002", "Integer assignment failed"));
    }

    // T003: Negative
    let (ok, output) = transpile_stmt("let a = -1;");
    if ok && !output.contains("unknown") {
        results.push(TCodeResult::pass("T003"));
    } else {
        results.push(TCodeResult::fail("T003", "Negative integer failed"));
    }

    // T071: Function definition
    let (ok, output) = transpile_prog("fn foo() {} fn main() { foo(); }");
    if ok && output.contains("foo()") {
        results.push(TCodeResult::pass("T071"));
    } else {
        results.push(TCodeResult::fail(
            "T071",
            "TB-001: Functions not transpiled",
        ));
    }

    // Count results
    let passed = results.iter().filter(|r| r.passed).count();
    let failed = results.iter().filter(|r| !r.passed).count();

    println!("\n╔═══════════════════════════════════════════╗");
    println!("║     T-CODE BASELINE VERIFICATION          ║");
    println!("╠═══════════════════════════════════════════╣");
    println!("║  Passed: {:<3}", passed);
    println!("║  Failed: {:<3}", failed);
    println!("╠═══════════════════════════════════════════╣");

    for r in &results {
        if r.passed {
            println!("║  ✅ {}", r.id);
        } else {
            println!(
                "║  ❌ {} - {}",
                r.id,
                r.reason.chars().take(20).collect::<String>()
            );
        }
    }

    println!("╚═══════════════════════════════════════════╝");
}

// ============================================================================
// PROPERTY TESTS - Per SPEC-TB-2025-001 Section 5
// ============================================================================

#[cfg(test)]
#[cfg(feature = "property-tests")] // Disabled by default - flaky in CI
mod property_tests {
    use super::*;
    use proptest::prelude::*;

    proptest! {
        // Reduced cases to prevent timeout/long execution

        /// Property 5.1: Symmetry - Transpilation is deterministic
        /// Same input always produces same output
        #[test]
        fn prop_transpile_deterministic(n in 0i32..1000) {
            let code = format!("fn main() {{ let x = {}; }}", n);
            let (ok1, out1) = transpile_prog(&code);
            let (ok2, out2) = transpile_prog(&code);

            prop_assert_eq!(ok1, ok2, "Transpilation success should be consistent");
            if ok1 {
                prop_assert_eq!(out1, out2, "Output should be identical for same input");
            }
        }

        /// Property: Integer literals always transpile (no 'unknown')
        #[test]
        fn prop_integer_literals_never_unknown(n in -1000i32..1000) {
            let code = format!("fn main() {{ let x = {}; }}", n);
            let (ok, output) = transpile_prog(&code);

            if ok {
                prop_assert!(
                    !output.contains("unknown"),
                    "Integer {} should not produce 'unknown'", n
                );
            }
        }

        /// Property: Empty main always transpiles successfully
        #[test]
        fn prop_empty_main_always_works(_dummy in 0..10u32) {
            let (ok, output) = transpile_prog("fn main() {}");
            prop_assert!(ok, "Empty main should always transpile");
            prop_assert!(output.contains("main()"), "Output should contain main()");
        }

        /// Property: Variable names are preserved in output
        #[test]
        fn prop_variable_names_preserved(
            name in "[a-z][a-z0-9_]{0,10}"
        ) {
            // Skip Rust keywords
            if ["fn", "let", "if", "else", "while", "for", "loop", "match", "return", "break", "continue", "true", "false", "mut", "pub", "mod", "use", "struct", "enum", "impl", "trait", "type", "where", "as", "in", "ref", "self", "super", "crate", "const", "static", "extern", "unsafe", "async", "await", "dyn", "move"].contains(&name.as_str()) {
                return Ok(());
            }

            let code = format!("fn main() {{ let {} = 42; }}", name);
            let (ok, output) = transpile_prog(&code);

            if ok {
                prop_assert!(
                    output.contains(&name) || output.contains(&format!("{}=", name)),
                    "Variable '{}' should appear in output", name
                );
            }
        }

        /// Property: Positive integers produce valid shell assignments
        #[test]
        fn prop_positive_int_valid_shell(n in 0u32..10000) {
            let code = format!("fn main() {{ let x = {}; }}", n);
            let (ok, output) = transpile_prog(&code);

            if ok {
                // Should contain x= assignment
                prop_assert!(
                    output.contains("x="),
                    "Should have x= assignment for {}", n
                );
            }
        }

        /// Property: Arithmetic produces shell arithmetic or literal result
        #[test]
        fn prop_arithmetic_produces_result(
            a in 1i32..100,
            b in 1i32..100
        ) {
            let code = format!("fn main() {{ let r = {} + {}; }}", a, b);
            let (ok, output) = transpile_prog(&code);

            if ok {
                // Should contain either $(( arithmetic )) or the computed result
                let expected_sum = a + b;
                let has_arith = output.contains("$((")
                    || output.contains(&expected_sum.to_string());

                prop_assert!(
                    has_arith || output.contains("r="),
                    "Addition {}+{} should produce arithmetic or result", a, b
                );
            }
        }

        /// Property 5.3: Quoting Safety - String content is quoted
        #[test]
        fn prop_println_content_quoted(s in "[a-zA-Z0-9 ]{1,20}") {
            let code = format!(r#"fn main() {{ println!("{}"); }}"#, s);
            let (ok, output) = transpile_prog(&code);

            if ok {
                // The string should appear quoted in some form
                let has_quoted = output.contains(&format!("'{}'", s))
                    || output.contains(&format!("\"{}\"", s))
                    || output.contains(&s); // At least the content exists

                prop_assert!(
                    has_quoted,
                    "println content '{}' should appear in output", s
                );
            }
        }

        // ================================================================
        // SECTION 5: Spec-Mandated Property Tests
        // ================================================================

        /// Property 5.2: Idempotency - transpile output is stable
        /// transpile(transpile(E)) should be stable
        #[test]
        fn prop_sec5_idempotency(n in 0i32..100) {
            let code = format!("fn main() {{ let x = {}; }}", n);
            let (ok1, out1) = transpile_prog(&code);

            if ok1 {
                // Transpiling again should produce identical output
                let (ok2, out2) = transpile_prog(&code);
                prop_assert_eq!(ok1, ok2, "Idempotency: success should be consistent");
                prop_assert_eq!(out1, out2, "Idempotency: output should be identical");
            }
        }

        /// Property 5.3: Quoting Safety - shell metacharacters are escaped
        /// String literals with $, `, \, " must prevent shell expansion
        #[test]
        fn prop_sec5_quoting_safety_dollar(s in "[a-zA-Z]{1,5}") {
            // Test that $VAR patterns don't get expanded
            let var_name = format!("${}", s);
            let code = format!(r#"fn main() {{ let msg = "{}"; let _ = msg; }}"#, var_name);
            let (ok, output) = transpile_prog(&code);

            if ok {
                // The $ should be escaped or quoted to prevent expansion
                // Either single quotes, escaped $, or the literal preserved
                let is_safe = output.contains(&format!("'{}'", var_name))
                    || output.contains(&format!("\"{}\"", var_name))
                    || output.contains("\\$")
                    || output.contains("'$");

                prop_assert!(
                    is_safe || !output.contains(&format!("${}", s)),
                    "Quoting safety: ${} should be escaped/quoted", s
                );
            }
        }

        /// Property 5.1: Symmetry partial check - exit codes are preserved
        /// For process::exit(N), shell should exit with N
        #[test]
        fn prop_sec5_symmetry_exit_code(n in 0u8..128) {
            let code = format!("fn main() {{ std::process::exit({}); }}", n);
            let (ok, output) = transpile_prog(&code);

            if ok {
                // The exit code should appear in the shell script
                let has_exit = output.contains(&format!("exit {}", n))
                    || output.contains(&format!("exit({})", n));

                prop_assert!(
                    has_exit || output.contains("exit"),
                    "Symmetry: exit({}) should produce exit {}", n, n
                );
            }
        }
    }
}