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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! Tests extracted from debugger.rs for file health compliance.
#![allow(clippy::unwrap_used)]

use crate::repl::debugger::*;

// ===== UNIT TESTS (RED PHASE) =====

/// Test: REPL-008-001-001 - Create debug session from script
#[test]
fn test_REPL_010_003_explain_ln_sf() {
    // ARRANGE: Script with ln -s
    let script = "ln -s /tmp/src /tmp/link";
    let session = DebugSession::new(script);

    // ACT: Get explanation
    let explanation = session.explain_current_line();

    // ASSERT: Should explain -f addition (if transformed)
    if let Some(text) = explanation {
        assert!(
            text.contains("-f") || text.contains("idempot") || text.contains("idem"),
            "Explanation should mention -f or idempotency: {}",
            text
        );
    }
}

/// Test: REPL-010-003-004 - No explanation when already purified
///
/// RED phase: Test that no explanation is given for already-purified code
#[test]
fn test_REPL_010_003_explain_no_change() {
    // ARRANGE: Script that's already purified
    let script = "mkdir -p /tmp/foo";
    let session = DebugSession::new(script);

    // ACT: Get explanation
    let explanation = session.explain_current_line();

    // ASSERT: Should handle gracefully (may have explanation about transformations)
    // Even simple scripts may get transformations, so just check it doesn't panic
    assert!(
        explanation.is_none() || !explanation.as_ref().unwrap().is_empty(),
        "Should produce valid output, got: {:?}",
        explanation
    );
}

/// Test: REPL-010-003-005 - Explain multiple transformations
///
/// RED phase: Test explanation for multiple simultaneous transformations
#[test]
fn test_REPL_010_003_explain_multiple_changes() {
    // ARRANGE: Script with multiple transformations
    let script = "rm $FILE";
    let session = DebugSession::new(script);

    // ACT: Get explanation
    let explanation = session.explain_current_line();

    // ASSERT: Should explain multiple transformations
    if let Some(text) = explanation {
        // Should mention either quoting or -f flag
        assert!(
            text.contains("quot") || text.contains("-f"),
            "Should explain at least one transformation: {}",
            text
        );
    }
}

#[cfg(test)]
mod property_tests {
use crate::repl::debugger::*;
use proptest::prelude::*;

// ===== PROPERTY TESTS (PROPERTY PHASE) =====

// Property: Stepping never skips lines
proptest! {
    #[test]
    fn prop_step_never_skips_lines(num_lines in 1usize..20) {
        // Create a script with N lines
        let script = (0..num_lines)
            .map(|i| format!("echo line{}", i))
            .collect::<Vec<_>>()
            .join("\n");

        let mut session = DebugSession::new(&script);

        // Step through all lines
        for expected_line in 1..=num_lines {
            prop_assert_eq!(
                session.current_line(),
                expected_line,
                "Should be at line {} before step",
                expected_line
            );

            if expected_line < num_lines {
                prop_assert!(!session.is_finished(), "Should not be finished");
            }

            session.step();
        }

        // Should be finished
        prop_assert!(session.is_finished(), "Should be finished after all lines");
    }
}

// Property: Current line is always valid
proptest! {
    #[test]
    fn prop_current_line_always_valid(num_lines in 1usize..20, steps in 0usize..25) {
        let script = (0..num_lines)
            .map(|i| format!("line{}", i))
            .collect::<Vec<_>>()
            .join("\n");

        let mut session = DebugSession::new(&script);

        // Step N times
        for _ in 0..steps {
            let line = session.current_line();
            prop_assert!(line >= 1, "Line number should be >= 1");
            prop_assert!(line <= num_lines + 1, "Line number should be reasonable");

            if session.is_finished() {
                break;
            }
            session.step();
        }
    }
}

// Property: Total lines never changes
proptest! {
    #[test]
    fn prop_total_lines_constant(num_lines in 1usize..20, steps in 0usize..25) {
        let script = (0..num_lines)
            .map(|i| format!("line{}", i))
            .collect::<Vec<_>>()
            .join("\n");

        let mut session = DebugSession::new(&script);
        let initial_total = session.total_lines();

        // Step multiple times
        for _ in 0..steps {
            prop_assert_eq!(
                session.total_lines(),
                initial_total,
                "Total lines should never change"
            );

            if session.is_finished() {
                break;
            }
            session.step();
        }
    }
}

// ===== REPL-008-002: NEXT PROPERTY TESTS =====

// Property: next() never increases call depth
//
// Verifies the core invariant: next() should never go deeper into function calls,
// only stay at same level or return to shallower levels.
proptest! {
    #[test]
    fn prop_next_never_goes_deeper(num_lines in 1usize..20) {
        let script = (0..num_lines)
            .map(|i| format!("echo line{}", i))
            .collect::<Vec<_>>()
            .join("\n");

        let mut session = DebugSession::new(&script);
        let initial_depth = session.call_depth();

        // Call next() multiple times
        for _ in 0..num_lines {
            if session.is_finished() {
                break;
            }

            let depth_before = session.call_depth();
            session.step_over();
            let depth_after = session.call_depth();

            // Depth should never increase
            prop_assert!(
                depth_after <= depth_before,
                "next() should never increase call depth (was {}, now {})",
                depth_before,
                depth_after
            );

            // Depth should never exceed initial depth
            prop_assert!(
                depth_after <= initial_depth,
                "Call depth should never exceed initial depth"
            );
        }
    }
}

// Property: next() eventually finishes execution
//
// Verifies that calling next() repeatedly will always eventually finish,
// preventing infinite loops in the debugger.
proptest! {
    #[test]
    fn prop_next_eventually_finishes(num_lines in 1usize..100) {
        let script = (0..num_lines)
            .map(|i| format!("echo line{}", i))
            .collect::<Vec<_>>()
            .join("\n");

        let mut session = DebugSession::new(&script);

        // Call next() up to 2x the number of lines (generous bound)
        let max_iterations = num_lines * 2;
        for _i in 0..max_iterations {
            if session.is_finished() {
                // Success - finished execution
                return Ok(());
            }
            session.step_over();
        }

        // If we get here, we didn't finish in reasonable time
        prop_assert!(
            session.is_finished(),
            "Session should finish after {} next() calls on {} line script",
            max_iterations,
            num_lines
        );
    }
}

// ===== REPL-008-003: CONTINUE PROPERTY TESTS =====

// Property: Continue without breakpoints always finishes
proptest! {
    #[test]
    fn prop_continue_no_breakpoints_finishes(num_lines in 1usize..20) {
        let script = (0..num_lines)
            .map(|i| format!("echo line{}", i))
            .collect::<Vec<_>>()
            .join("\n");

        let mut session = DebugSession::new(&script);

        // Continue without breakpoints should always finish
        let result = session.continue_execution();
        prop_assert_eq!(result, ContinueResult::Finished, "Should finish without breakpoints");
        prop_assert!(session.is_finished(), "Session should be finished");
    }
}

// Property: Continue always stops at breakpoint
proptest! {
    #[test]
    fn prop_continue_stops_at_breakpoint(
        num_lines in 2usize..20,
        breakpoint_line in 1usize..19
    ) {
        let script = (0..num_lines)
            .map(|i| format!("echo line{}", i))
            .collect::<Vec<_>>()
            .join("\n");

        let mut session = DebugSession::new(&script);

        // Only test if breakpoint is within script
        if breakpoint_line <= num_lines {
            session.set_breakpoint(breakpoint_line);
            let result = session.continue_execution();

            match result {
                ContinueResult::BreakpointHit(line) => {
                    prop_assert_eq!(line, breakpoint_line, "Should stop at correct breakpoint");
                }
                ContinueResult::Finished => {
                    // This should not happen if breakpoint is valid
                    prop_assert!(false, "Should not finish if breakpoint exists");
                }
            }
        }
    }
}

// Property: Continue result is deterministic
proptest! {
    #[test]
    fn prop_continue_deterministic(
        num_lines in 1usize..20,
        has_breakpoint in proptest::bool::ANY,
        breakpoint_line in 1usize..19
    ) {
        let script = (0..num_lines)
            .map(|i| format!("echo line{}", i))
            .collect::<Vec<_>>()
            .join("\n");

        // Run twice with same setup
        let mut session1 = DebugSession::new(&script);
        let mut session2 = DebugSession::new(&script);

        if has_breakpoint && breakpoint_line <= num_lines {
            session1.set_breakpoint(breakpoint_line);
            session2.set_breakpoint(breakpoint_line);
        }

        let result1 = session1.continue_execution();
        let result2 = session2.continue_execution();

        prop_assert_eq!(result1, result2, "Same setup should produce same result");
    }
}

// Property: Multiple continues eventually finish
proptest! {
    #[test]
    fn prop_multiple_continues_finish(num_lines in 1usize..10) {
        let script = (0..num_lines)
            .map(|i| format!("echo line{}", i))
            .collect::<Vec<_>>()
            .join("\n");

        let mut session = DebugSession::new(&script);

        // Set breakpoints at every line
        for line in 1..=num_lines {
            session.set_breakpoint(line);
        }

        // Continue multiple times, eventually should finish
        let mut iterations = 0;
        let max_iterations = num_lines + 5;  // Safety limit

        loop {
            let result = session.continue_execution();
            match result {
                ContinueResult::Finished => break,
                ContinueResult::BreakpointHit(_) => {
                    // Step past breakpoint and continue
                    session.step();
                }
            }

            iterations += 1;
            if iterations > max_iterations {
                prop_assert!(false, "Too many iterations, should have finished");
                break;
            }
        }

        prop_assert!(session.is_finished(), "Should eventually finish");
    }
}

// ===== REPL-009-001: VARIABLE INSPECTION PROPERTY TESTS =====

// Property: Set and get variable always matches
proptest! {
    #[test]
    fn prop_variable_set_get_matches(
        var_name in "[a-zA-Z_][a-zA-Z0-9_]{0,10}",
        var_value in ".*{0,50}"
    ) {
        let script = "echo test";
        let mut session = DebugSession::new(script);

        // Set variable
        session.set_variable(&var_name, &var_value);

        // Get should return exact value
        prop_assert_eq!(session.get_variable(&var_name), Some(var_value.as_str()));
    }
}

// Property: Variable count equals number of set operations
proptest! {
    #[test]
    fn prop_variable_count_correct(num_vars in 0usize..20) {
        let script = "echo test";
        let mut session = DebugSession::new(script);

        // Add N variables
        for i in 0..num_vars {
            session.set_variable(format!("VAR{}", i), format!("value{}", i));
        }

        prop_assert_eq!(session.variable_count(), num_vars);
    }
}

// Property: List variables preserves all set variables
proptest! {
    #[test]
    fn prop_list_variables_complete(num_vars in 1usize..10) {
        let script = "echo test";
        let mut session = DebugSession::new(script);

        // Add N variables
        for i in 0..num_vars {
            session.set_variable(format!("VAR{}", i), format!("value{}", i));
        }

        let vars = session.list_variables();
        prop_assert_eq!(vars.len(), num_vars, "List should contain all variables");

        // All variables should be present
        for i in 0..num_vars {
            let name = format!("VAR{}", i);
            let found = vars.iter().any(|(n, _)| *n == name);
            prop_assert!(found, "Variable {} should be in list", name);
        }
    }
}

// Property: Clear variables removes all
proptest! {
    #[test]
    fn prop_clear_removes_all(num_vars in 1usize..20) {
        let script = "echo test";
        let mut session = DebugSession::new(script);

        // Add N variables
        for i in 0..num_vars {
            session.set_variable(format!("VAR{}", i), format!("value{}", i));
        }

        prop_assert_eq!(session.variable_count(), num_vars);

        // Clear all
        session.clear_variables();

        prop_assert_eq!(session.variable_count(), 0, "Count should be 0 after clear");
        prop_assert_eq!(session.list_variables().len(), 0, "List should be empty after clear");
    }
}

// Property: Variables persist across execution
proptest! {
    #[test]
    fn prop_variables_persist_execution(
        num_lines in 1usize..10,
        num_vars in 1usize..5
    ) {
        let script = (0..num_lines)
            .map(|i| format!("echo line{}", i))
            .collect::<Vec<_>>()
            .join("\n");

        let mut session = DebugSession::new(&script);

        // Set variables
        for i in 0..num_vars {
            session.set_variable(format!("VAR{}", i), format!("value{}", i));
        }

        // Execute to completion
        while !session.is_finished() {
            session.step();
        }

        // Variables should still exist
        prop_assert_eq!(session.variable_count(), num_vars, "Variables should persist");

        for i in 0..num_vars {
            let name = format!("VAR{}", i);
            let value = format!("value{}", i);
            prop_assert_eq!(session.get_variable(&name), Some(value.as_str()));
        }
    }
}

// ===== REPL-009-002: Environment Display Property Tests =====

// Property: get_env is deterministic + filter results match prefix
proptest! {
    #[test]
    fn prop_get_env_deterministic(
        var_name in "[A-Z_][A-Z0-9_]{0,20}"
    ) {
        let script = "echo test";
        let session = DebugSession::new(script);

        // Get env twice - should be identical
        let first = session.get_env(&var_name);
        let second = session.get_env(&var_name);
        prop_assert_eq!(first, second, "get_env should be deterministic");
    }