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
use super::*;
use crate::bash_parser::ast::{ArithExpr, BashExpr, BashStmt, Redirect, Span, TestExpr};
use crate::bash_parser::parser_arith::ArithToken;
#[test]
fn test_parse_remove_longest_prefix() {
    let input = "echo ${x##pattern}";
    let mut parser = BashParser::new(input).unwrap();
    let ast = parser.parse().unwrap();
    match &ast.statements[0] {
        BashStmt::Command { args, .. } => {
            assert!(matches!(&args[0], BashExpr::RemoveLongestPrefix { .. }));
        }
        _ => panic!("Expected Command with RemoveLongestPrefix"),
    }
}

#[test]
fn test_parse_remove_suffix() {
    let input = "echo ${x%pattern}";
    let mut parser = BashParser::new(input).unwrap();
    let ast = parser.parse().unwrap();
    match &ast.statements[0] {
        BashStmt::Command { args, .. } => {
            assert!(matches!(&args[0], BashExpr::RemoveSuffix { .. }));
        }
        _ => panic!("Expected Command with RemoveSuffix"),
    }
}

#[test]
fn test_parse_remove_longest_suffix() {
    let input = "echo ${x%%pattern}";
    let mut parser = BashParser::new(input).unwrap();
    let ast = parser.parse().unwrap();
    match &ast.statements[0] {
        BashStmt::Command { args, .. } => {
            assert!(matches!(&args[0], BashExpr::RemoveLongestSuffix { .. }));
        }
        _ => panic!("Expected Command with RemoveLongestSuffix"),
    }
}

// ============================================================================
// Coverage Tests - Arithmetic Operations
// ============================================================================

#[test]
fn test_parse_arithmetic_subtraction() {
    let input = "x=$((a - b))";
    let mut parser = BashParser::new(input).unwrap();
    let ast = parser.parse().unwrap();
    match &ast.statements[0] {
        BashStmt::Assignment { value, .. } => match value {
            BashExpr::Arithmetic(arith) => {
                assert!(matches!(arith.as_ref(), ArithExpr::Sub(_, _)));
            }
            _ => panic!("Expected Arithmetic expression"),
        },
        _ => panic!("Expected Assignment"),
    }
}

#[test]
fn test_parse_arithmetic_division() {
    let input = "x=$((a / b))";
    let mut parser = BashParser::new(input).unwrap();
    let ast = parser.parse().unwrap();
    match &ast.statements[0] {
        BashStmt::Assignment { value, .. } => match value {
            BashExpr::Arithmetic(arith) => {
                assert!(matches!(arith.as_ref(), ArithExpr::Div(_, _)));
            }
            _ => panic!("Expected Arithmetic expression"),
        },
        _ => panic!("Expected Assignment"),
    }
}

#[test]
fn test_parse_arithmetic_modulo() {
    let input = "x=$((a % b))";
    let mut parser = BashParser::new(input).unwrap();
    let ast = parser.parse().unwrap();
    match &ast.statements[0] {
        BashStmt::Assignment { value, .. } => match value {
            BashExpr::Arithmetic(arith) => {
                assert!(matches!(arith.as_ref(), ArithExpr::Mod(_, _)));
            }
            _ => panic!("Expected Arithmetic expression"),
        },
        _ => panic!("Expected Assignment"),
    }
}

#[test]
fn test_parse_arithmetic_negative() {
    let input = "x=$((-5))";
    let mut parser = BashParser::new(input).unwrap();
    let ast = parser.parse().unwrap();
    assert!(matches!(&ast.statements[0], BashStmt::Assignment { .. }));
}

#[test]
fn test_parse_arithmetic_parentheses() {
    let input = "x=$(((1 + 2) * 3))";
    let mut parser = BashParser::new(input).unwrap();
    let ast = parser.parse().unwrap();
    assert!(matches!(&ast.statements[0], BashStmt::Assignment { .. }));
}

// ============================================================================
// Coverage Tests - Arithmetic Tokenizer & Parser (ARITH_COV_001-040)
// ============================================================================

/// Helper: parse arithmetic expression from `x=$((expr))` pattern
fn parse_arith(expr: &str) -> ArithExpr {
    let input = format!("x=$(({expr}))");
    let mut parser = BashParser::new(&input).unwrap();
    let ast = parser.parse().unwrap();
    match &ast.statements[0] {
        BashStmt::Assignment { value, .. } => match value {
            BashExpr::Arithmetic(arith) => arith.as_ref().clone(),
            other => panic!("Expected Arithmetic, got {other:?}"),
        },
        other => panic!("Expected Assignment, got {other:?}"),
    }
}

// --- Tokenizer: comparison operators ---

#[test]
fn test_ARITH_COV_001_less_than() {
    let arith = parse_arith("a < b");
    assert!(matches!(arith, ArithExpr::Sub(_, _)));
}

#[test]
fn test_ARITH_COV_002_less_equal() {
    let arith = parse_arith("a <= b");
    assert!(matches!(arith, ArithExpr::Sub(_, _)));
}

#[test]
fn test_ARITH_COV_003_greater_than() {
    let arith = parse_arith("a > b");
    assert!(matches!(arith, ArithExpr::Sub(_, _)));
}

#[test]
fn test_ARITH_COV_004_greater_equal() {
    let arith = parse_arith("a >= b");
    assert!(matches!(arith, ArithExpr::Sub(_, _)));
}

#[test]
fn test_ARITH_COV_005_shift_left() {
    let arith = parse_arith("a << b");
    // Shift left represented as Mul
    assert!(matches!(arith, ArithExpr::Mul(_, _)));
}

#[test]
fn test_ARITH_COV_006_shift_right() {
    let arith = parse_arith("a >> b");
    // Shift right represented as Div
    assert!(matches!(arith, ArithExpr::Div(_, _)));
}

// --- Tokenizer: equality operators ---

#[test]
fn test_ARITH_COV_007_equal() {
    let arith = parse_arith("a == b");
    assert!(matches!(arith, ArithExpr::Sub(_, _)));
}

#[test]
fn test_ARITH_COV_008_not_equal() {
    let arith = parse_arith("a != b");
    assert!(matches!(arith, ArithExpr::Sub(_, _)));
}

// --- Tokenizer: logical operators ---

#[test]
fn test_ARITH_COV_009_logical_and() {
    let arith = parse_arith("a && b");
    // Logical AND represented as Mul
    assert!(matches!(arith, ArithExpr::Mul(_, _)));
}

#[test]
fn test_ARITH_COV_010_logical_or() {
    let arith = parse_arith("a || b");
    // Logical OR represented as Add
    assert!(matches!(arith, ArithExpr::Add(_, _)));
}

#[test]
fn test_ARITH_COV_011_logical_not() {
    let arith = parse_arith("!a");
    // Logical NOT represented as Sub(-1, operand)
    assert!(matches!(arith, ArithExpr::Sub(_, _)));
}

// --- Tokenizer: bitwise operators ---

#[test]
fn test_ARITH_COV_012_bit_and() {
    let arith = parse_arith("a & b");
    // Bitwise AND represented as Mul
    assert!(matches!(arith, ArithExpr::Mul(_, _)));
}

#[test]
fn test_ARITH_COV_013_bit_or() {
    let arith = parse_arith("a | b");
    // Bitwise OR represented as Add
    assert!(matches!(arith, ArithExpr::Add(_, _)));
}

#[test]
fn test_ARITH_COV_014_bit_xor() {
    let arith = parse_arith("a ^ b");
    // Bitwise XOR represented as Sub
    assert!(matches!(arith, ArithExpr::Sub(_, _)));
}

#[test]
fn test_ARITH_COV_015_bit_not() {
    let arith = parse_arith("~a");
    // Bitwise NOT represented as Sub(-1, operand)
    assert!(matches!(arith, ArithExpr::Sub(_, _)));
}

// --- Tokenizer: ternary operator ---

#[test]
fn test_ARITH_COV_016_ternary() {
    let arith = parse_arith("a ? 1 : 0");
    // Ternary represented as Add(Mul(cond, then), Mul(Sub(1, cond), else))
    assert!(matches!(arith, ArithExpr::Add(_, _)));
}

// --- Tokenizer: comma operator ---

#[test]
fn test_ARITH_COV_017_comma() {
    let arith = parse_arith("1, 2");
    // Comma returns the right value
    assert!(matches!(arith, ArithExpr::Number(2)));
}

// --- Tokenizer: assignment ---

#[test]
fn test_ARITH_COV_018_assign() {
    // Single = in arithmetic is assignment; parsed through assign level
    // The tokenizer produces Assign token, but parse_assign just calls parse_ternary
    // So this just tests that '=' alone doesn't crash
    let input = "x=$((y = 5))";
    let mut parser = BashParser::new(input).unwrap();
    let _ast = parser.parse();
    // May or may not parse successfully depending on grammar, just ensure no panic
}

// --- Tokenizer: hex and octal numbers ---

#[test]
fn test_ARITH_COV_019_hex_number() {
    let arith = parse_arith("0xff");
    assert!(matches!(arith, ArithExpr::Number(255)));
}

#[test]
fn test_ARITH_COV_020_hex_uppercase() {
    let arith = parse_arith("0XFF");
    assert!(matches!(arith, ArithExpr::Number(255)));
}

#[test]
fn test_ARITH_COV_021_octal_number() {
    let arith = parse_arith("077");
    assert!(matches!(arith, ArithExpr::Number(63)));
}

#[test]
fn test_ARITH_COV_022_zero_literal() {
    let arith = parse_arith("0");
    assert!(matches!(arith, ArithExpr::Number(0)));
}

// --- Tokenizer: dollar variable ---

#[test]
fn test_ARITH_COV_023_dollar_variable() {
    let arith = parse_arith("$x + 1");
    match arith {
        ArithExpr::Add(left, right) => {
            assert!(matches!(left.as_ref(), ArithExpr::Variable(v) if v == "x"));
            assert!(matches!(right.as_ref(), ArithExpr::Number(1)));
        }
        other => panic!("Expected Add, got {other:?}"),
    }
}

// --- Tokenizer: whitespace handling ---

#[test]
fn test_ARITH_COV_024_whitespace_tab_newline() {
    let arith = parse_arith("\t1\n+\t2\n");
    assert!(matches!(arith, ArithExpr::Add(_, _)));
}

// --- Parser: unary plus ---

#[test]
fn test_ARITH_COV_025_unary_plus() {
    let arith = parse_arith("+5");
    assert!(matches!(arith, ArithExpr::Number(5)));
}

// --- Parser: complex expressions hitting multiple levels ---

#[test]
fn test_ARITH_COV_026_comparison_chain() {
    let arith = parse_arith("a < b < c");
    // Two comparisons chained
    assert!(matches!(arith, ArithExpr::Sub(_, _)));
}

#[test]
fn test_ARITH_COV_027_equality_chain() {
    let arith = parse_arith("a == b != c");
    assert!(matches!(arith, ArithExpr::Sub(_, _)));
}

#[test]
fn test_ARITH_COV_028_nested_ternary() {
    let arith = parse_arith("a ? b ? 1 : 2 : 3");
    assert!(matches!(arith, ArithExpr::Add(_, _)));
}

#[test]
fn test_ARITH_COV_029_all_bitwise_combined() {
    // a | b ^ c & d — exercises bitwise OR, XOR, AND levels
    let arith = parse_arith("a | b ^ c & d");
    assert!(matches!(arith, ArithExpr::Add(_, _)));
}

#[test]
fn test_ARITH_COV_030_logical_combined() {
    // a || b && c — exercises logical OR and AND levels
    let arith = parse_arith("a || b && c");
    assert!(matches!(arith, ArithExpr::Add(_, _)));
}

#[test]
fn test_ARITH_COV_031_shift_combined() {
    // 1 << 2 >> 3 — exercises both shift directions
    let arith = parse_arith("1 << 2 >> 3");
    assert!(matches!(arith, ArithExpr::Div(_, _)));
}

#[test]
fn test_ARITH_COV_032_hex_arithmetic() {
    let arith = parse_arith("0xa + 0xb");
    match arith {
        ArithExpr::Add(left, right) => {
            assert!(matches!(left.as_ref(), ArithExpr::Number(10)));
            assert!(matches!(right.as_ref(), ArithExpr::Number(11)));
        }
        other => panic!("Expected Add, got {other:?}"),
    }
}

#[test]
fn test_ARITH_COV_033_octal_arithmetic() {
    let arith = parse_arith("010 + 010");
    match arith {
        ArithExpr::Add(left, right) => {
            assert!(matches!(left.as_ref(), ArithExpr::Number(8)));
            assert!(matches!(right.as_ref(), ArithExpr::Number(8)));
        }
        other => panic!("Expected Add, got {other:?}"),
    }
}

#[test]
fn test_ARITH_COV_034_underscore_variable() {
    let arith = parse_arith("_foo + _bar");
    match arith {
        ArithExpr::Add(left, right) => {
            assert!(matches!(left.as_ref(), ArithExpr::Variable(v) if v == "_foo"));
            assert!(matches!(right.as_ref(), ArithExpr::Variable(v) if v == "_bar"));
        }
        other => panic!("Expected Add, got {other:?}"),
    }
}

#[test]
fn test_ARITH_COV_035_complex_precedence() {
    // 1 + 2 * 3 — mul before add
    let arith = parse_arith("1 + 2 * 3");
    match &arith {
        ArithExpr::Add(left, right) => {
            assert!(matches!(left.as_ref(), ArithExpr::Number(1)));
            assert!(matches!(right.as_ref(), ArithExpr::Mul(_, _)));
        }
        other => panic!("Expected Add(1, Mul(2,3)), got {other:?}"),
    }
}

#[test]
fn test_ARITH_COV_036_unary_minus_in_expression() {
    let arith = parse_arith("-a + b");
    match arith {
        ArithExpr::Add(left, _right) => {
            // Unary minus is Sub(0, a)
            assert!(matches!(left.as_ref(), ArithExpr::Sub(_, _)));
        }
        other => panic!("Expected Add(Sub(0,a), b), got {other:?}"),
    }
}

#[test]
fn test_ARITH_COV_037_parenthesized_comma() {
    // Comma in parenthesized expression
    let arith = parse_arith("(1, 2) + 3");
    assert!(matches!(arith, ArithExpr::Add(_, _)));
}

#[test]
fn test_ARITH_COV_038_nested_parentheses() {
    let arith = parse_arith("((a + b))");
    assert!(matches!(arith, ArithExpr::Add(_, _)));
}

#[test]
fn test_ARITH_COV_039_multi_digit_number() {
    let arith = parse_arith("12345");
    assert!(matches!(arith, ArithExpr::Number(12345)));
}

#[test]
fn test_ARITH_COV_040_all_multiplicative_ops() {
    // 10 * 3 / 2 % 5 — exercises all three multiplicative operators
    let arith = parse_arith("10 * 3 / 2 % 5");
    assert!(matches!(arith, ArithExpr::Mod(_, _)));
}

// ============================================================================
// Coverage Tests - Command Substitution
// ============================================================================

#[test]
fn test_parse_command_substitution() {
    let input = "x=$(pwd)";
    let mut parser = BashParser::new(input).unwrap();
    let ast = parser.parse().unwrap();
    match &ast.statements[0] {
        BashStmt::Assignment { value, .. } => {
            assert!(matches!(value, BashExpr::CommandSubst(_)));
        }
        _ => panic!("Expected Assignment with CommandSubst"),
    }
}

// ============================================================================
// Coverage Tests - Comments
// ============================================================================

fn tokenize(input: &str) -> Vec<ArithToken> {
    let parser = BashParser::new("echo x").expect("parser init");
    parser.tokenize_arithmetic(input).expect("tokenize")
}