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
/// RED TEST: args() should emit "$@" syntax
/// Tests that ShellValue::Arg { position: None } generates "$@" in shell
#[test]
fn test_args_emits_all_args_syntax() {
    use crate::models::Config;

    let ir = crate::ir::shell_ir::ShellIR::Let {
        name: "all".to_string(),
        value: ShellValue::Arg { position: None },
        effects: crate::ir::effects::EffectSet::pure(),
    };

    let config = Config::default();
    let output = super::emit(&ir).unwrap();

    // RED: This will fail until we implement args() emission
    assert!(
        output.contains("\"$@\""),
        "args() should emit $@ with quotes, got: {}",
        output
    );
    assert!(
        output.contains("all=\"$@\""),
        "Should assign quoted all args to variable, got: {}",
        output
    );
}

/// RED TEST: arg_count() should emit "$#" syntax
/// Tests that ShellValue::ArgCount generates "$#" in shell
#[test]
fn test_arg_count_emits_count_syntax() {
    use crate::models::Config;

    let ir = crate::ir::shell_ir::ShellIR::Let {
        name: "count".to_string(),
        value: ShellValue::ArgCount,
        effects: crate::ir::effects::EffectSet::pure(),
    };

    let config = Config::default();
    let output = super::emit(&ir).unwrap();

    // RED: This will fail until we implement ArgCount emission
    assert!(
        output.contains("\"$#\""),
        "arg_count() should emit $# with quotes, got: {}",
        output
    );
    assert!(
        output.contains("count=\"$#\""),
        "Should assign quoted arg count to variable, got: {}",
        output
    );
}

/// RED TEST: Arguments must be quoted for safety
/// Tests that all argument accesses include proper quoting
#[test]
fn test_args_quoted_for_safety() {
    use crate::models::Config;

    let ir = crate::ir::shell_ir::ShellIR::Sequence(vec![
        crate::ir::shell_ir::ShellIR::Let {
            name: "x".to_string(),
            value: ShellValue::Arg { position: Some(1) },
            effects: crate::ir::effects::EffectSet::pure(),
        },
        crate::ir::shell_ir::ShellIR::Let {
            name: "y".to_string(),
            value: ShellValue::Arg { position: None },
            effects: crate::ir::effects::EffectSet::pure(),
        },
    ]);

    let config = Config::default();
    let output = super::emit(&ir).unwrap();

    // RED: Must have quotes around $1 and $@ for safety
    // Should NOT have unquoted versions like =$ 1 or =$1 (without quotes)
    // The proper form is ="$1" and ="$@"

    // Must have quoted versions
    assert!(
        output.contains("\"$1\""),
        "Should have quoted $1: {}",
        output
    );
    assert!(
        output.contains("\"$@\""),
        "Should have quoted $@: {}",
        output
    );
}

/// RED TEST: Multiple arg() calls in sequence
/// Tests that multiple positional arguments can be accessed together
#[test]
fn test_multiple_args_in_sequence() {
    use crate::models::Config;

    let ir = crate::ir::shell_ir::ShellIR::Sequence(vec![
        crate::ir::shell_ir::ShellIR::Let {
            name: "first".to_string(),
            value: ShellValue::Arg { position: Some(1) },
            effects: crate::ir::effects::EffectSet::pure(),
        },
        crate::ir::shell_ir::ShellIR::Let {
            name: "second".to_string(),
            value: ShellValue::Arg { position: Some(2) },
            effects: crate::ir::effects::EffectSet::pure(),
        },
        crate::ir::shell_ir::ShellIR::Let {
            name: "count".to_string(),
            value: ShellValue::ArgCount,
            effects: crate::ir::effects::EffectSet::pure(),
        },
    ]);

    let config = Config::default();
    let output = super::emit(&ir).unwrap();

    // RED: All three should be emitted correctly
    assert!(
        output.contains("first=\"$1\""),
        "Should contain first=$1, got: {}",
        output
    );
    assert!(
        output.contains("second=\"$2\""),
        "Should contain second=$2, got: {}",
        output
    );
    assert!(
        output.contains("count=\"$#\""),
        "Should contain count=$#, got: {}",
        output
    );
}

// ============= Sprint 27c: Exit Code Handling - RED PHASE =============

/// RED TEST: exit_code() should emit "$?" syntax
/// Tests that ShellValue::ExitCode generates "$?" in shell
#[test]
fn test_exit_code_emits_question_mark_syntax() {
    use crate::models::Config;

    let ir = crate::ir::shell_ir::ShellIR::Let {
        name: "status".to_string(),
        value: ShellValue::ExitCode,
        effects: crate::ir::effects::EffectSet::pure(),
    };

    let config = Config::default();
    let output = super::emit(&ir).unwrap();

    // RED: This will fail until we implement ExitCode emission
    assert!(
        output.contains("\"$?\""),
        "exit_code() should emit $? with quotes, got: {}",
        output
    );
    assert!(
        output.contains("status=\"$?\""),
        "Should assign quoted exit code to variable, got: {}",
        output
    );
}

/// RED TEST: exit_code() in comparison context
/// Tests that exit_code() works in if condition comparisons
#[test]
fn test_exit_code_in_comparison() {
    use crate::ir::shell_ir::ComparisonOp;
    use crate::models::Config;

    let ir = crate::ir::shell_ir::ShellIR::If {
        test: ShellValue::Comparison {
            op: ComparisonOp::StrEq,
            left: Box::new(ShellValue::ExitCode),
            right: Box::new(ShellValue::String("0".to_string())),
        },
        then_branch: Box::new(crate::ir::shell_ir::ShellIR::Echo {
            value: ShellValue::String("success".to_string()),
        }),
        else_branch: None,
    };

    let config = Config::default();
    let output = super::emit(&ir).unwrap();

    // RED: This will fail until ExitCode is implemented in comparison
    assert!(
        output.contains("\"$?\""),
        "Should contain exit code in comparison, got: {}",
        output
    );
    assert!(
        output.contains("[ \"$?\" = "),
        "Should emit exit code comparison, got: {}",
        output
    );
}

/// RED TEST: Exit code must be quoted for safety
/// Tests that exit code accesses include proper quoting
#[test]
fn test_exit_code_quoted_for_safety() {
    use crate::models::Config;

    let ir = crate::ir::shell_ir::ShellIR::Sequence(vec![
        crate::ir::shell_ir::ShellIR::Let {
            name: "x".to_string(),
            value: ShellValue::ExitCode,
            effects: crate::ir::effects::EffectSet::pure(),
        },
        crate::ir::shell_ir::ShellIR::Let {
            name: "y".to_string(),
            value: ShellValue::ExitCode,
            effects: crate::ir::effects::EffectSet::pure(),
        },
    ]);

    let config = Config::default();
    let output = super::emit(&ir).unwrap();

    // RED: Must have quotes around $? for safety and consistency
    assert!(
        output.contains("\"$?\""),
        "Exit code accesses must be quoted: {}",
        output
    );

    // Should appear twice (for both variables)
    let count = output.matches("\"$?\"").count();
    assert!(
        count >= 2,
        "Should have at least 2 quoted exit code accesses, found {}: {}",
        count,
        output
    );
}

/// RED TEST: exit_code() in concatenation
/// Tests that exit_code() can be used in string concatenation
#[test]
fn test_exit_code_in_concatenation() {
    use crate::models::Config;

    let ir = crate::ir::shell_ir::ShellIR::Let {
        name: "msg".to_string(),
        value: ShellValue::Concat(vec![
            ShellValue::String("Exit code: ".to_string()),
            ShellValue::ExitCode,
        ]),
        effects: crate::ir::effects::EffectSet::pure(),
    };

    let config = Config::default();
    let output = super::emit(&ir).unwrap();

    // RED: This will fail until ExitCode works in concatenation
    assert!(
        output.contains("msg=\"Exit code: $?\""),
        "Should emit concatenated exit code, got: {}",
        output
    );
}

// ============= Sprint 28: Complete Missing Stdlib Functions - RED PHASE =============

/// RED TEST: string_split() should be in runtime
/// Tests that string_split generates shell function
#[test]
fn test_string_split_in_runtime() {
    use crate::ir::{Command, EffectSet};
    use crate::models::Config;

    // Use IR that references rash_string_split to trigger selective emission
    let ir = crate::ir::shell_ir::ShellIR::Exec {
        cmd: Command::new("rash_string_split")
            .arg(crate::ir::shell_ir::ShellValue::String(
                "hello world".to_string(),
            ))
            .arg(crate::ir::shell_ir::ShellValue::String(" ".to_string())),
        effects: EffectSet::pure(),
    };

    let config = Config::default();
    let output = super::emit(&ir).unwrap();

    assert!(
        output.contains("rash_string_split()"),
        "Runtime should include rash_string_split function, got: {}",
        output
    );
}

/// Tests that string_split uses POSIX tools
#[test]
fn test_string_split_basic() {
    use crate::ir::{Command, EffectSet};
    use crate::models::Config;

    let ir = crate::ir::shell_ir::ShellIR::Exec {
        cmd: Command::new("rash_string_split")
            .arg(crate::ir::shell_ir::ShellValue::String("a,b,c".to_string()))
            .arg(crate::ir::shell_ir::ShellValue::String(",".to_string())),
        effects: EffectSet::pure(),
    };

    let config = Config::default();
    let output = super::emit(&ir).unwrap();

    assert!(
        output.contains("rash_string_split()"),
        "Should have string_split function"
    );

    // Should use tr or similar for POSIX split
    assert!(
        output.contains("tr") || output.contains("sed"),
        "string_split should use POSIX tools for splitting"
    );
}

/// RED TEST: array_len() should be in runtime
/// Tests that array_len generates shell function
#[test]
fn test_array_len_in_runtime() {
    use crate::ir::{Command, EffectSet};
    use crate::models::Config;

    let ir = crate::ir::shell_ir::ShellIR::Exec {
        cmd: Command::new("rash_array_len").arg(crate::ir::shell_ir::ShellValue::String(
            "myarray".to_string(),
        )),
        effects: EffectSet::pure(),
    };

    let config = Config::default();
    let output = super::emit(&ir).unwrap();

    assert!(
        output.contains("rash_array_len()"),
        "Runtime should include rash_array_len function, got: {}",
        output
    );
}

/// Tests that array_len uses POSIX counting
#[test]
fn test_array_len_basic() {
    use crate::ir::{Command, EffectSet};
    use crate::models::Config;

    let ir = crate::ir::shell_ir::ShellIR::Exec {
        cmd: Command::new("rash_array_len").arg(crate::ir::shell_ir::ShellValue::String(
            "myarray".to_string(),
        )),
        effects: EffectSet::pure(),
    };

    let config = Config::default();
    let output = super::emit(&ir).unwrap();

    assert!(
        output.contains("rash_array_len()"),
        "Should have array_len function"
    );

    // Should use wc -l for counting lines
    assert!(
        output.contains("wc -l"),
        "array_len should use wc -l for counting"
    );
}

/// RED TEST: array_join() should be in runtime
/// Tests that array_join generates shell function
#[test]
fn test_array_join_in_runtime() {
    use crate::ir::{Command, EffectSet};
    use crate::models::Config;

    let ir = crate::ir::shell_ir::ShellIR::Exec {
        cmd: Command::new("rash_array_join")
            .arg(crate::ir::shell_ir::ShellValue::String(
                "myarray".to_string(),
            ))
            .arg(crate::ir::shell_ir::ShellValue::String(",".to_string())),
        effects: EffectSet::pure(),
    };

    let config = Config::default();
    let output = super::emit(&ir).unwrap();

    assert!(
        output.contains("rash_array_join()"),
        "Runtime should include rash_array_join function, got: {}",
        output
    );
}

/// Tests that array_join uses loop for joining
#[test]
fn test_array_join_basic() {
    use crate::ir::{Command, EffectSet};
    use crate::models::Config;

    let ir = crate::ir::shell_ir::ShellIR::Exec {
        cmd: Command::new("rash_array_join")
            .arg(crate::ir::shell_ir::ShellValue::String(
                "myarray".to_string(),
            ))
            .arg(crate::ir::shell_ir::ShellValue::String(",".to_string())),
        effects: EffectSet::pure(),
    };

    let config = Config::default();
    let output = super::emit(&ir).unwrap();

    assert!(
        output.contains("rash_array_join()"),
        "Should have array_join function"
    );

    // Should use while loop or similar for joining
    assert!(
        output.contains("while") || output.contains("for"),
        "array_join should use while/for loop for joining"
    );
}