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
#[cfg(test)]
mod tests {
    use super::super::*;
    use crate::bash_parser::parser_arith::ArithToken;
    #[test]
    fn test_EXT_GLOB_PATH_001_at_glob_in_for() {
        let input = "for f in /tmp/@(a|b|c).sh; do\n    echo \"$f\"\ndone";
        let mut parser = BashParser::new(input).expect("parser");
        let ast = parser.parse();
        assert!(ast.is_ok(), "@() in path should parse: {:?}", ast.err());
    }

    #[test]
    fn test_EXT_GLOB_PATH_002_plus_glob_in_path() {
        let input = "ls /tmp/file+(a|b).txt";
        let mut parser = BashParser::new(input).expect("parser");
        let ast = parser.parse();
        assert!(ast.is_ok(), "+() in path should parse: {:?}", ast.err());
    }

    #[test]
    fn test_EXT_GLOB_PATH_003_question_glob_in_path() {
        let input = "ls /tmp/?(opt).txt";
        let mut parser = BashParser::new(input).expect("parser");
        let ast = parser.parse();
        assert!(ast.is_ok(), "?() in path should parse: {:?}", ast.err());
    }

    #[test]
    fn test_coverage_case_statement() {
        let input = r#"case $var in
    a) echo "a";;
    b) echo "b";;
    *) echo "other";;
esac"#;
        let mut parser = BashParser::new(input).unwrap();
        let ast = parser.parse().unwrap();
        assert!(ast
            .statements
            .iter()
            .any(|s| matches!(s, BashStmt::Case { .. })));
    }

    #[test]
    fn test_coverage_select_statement() {
        let input = r#"select opt in "opt1" "opt2" "opt3"; do
    echo "Selected: $opt"
    break
done"#;
        let mut parser = BashParser::new(input).unwrap();
        let ast = parser.parse().unwrap();
        assert!(ast
            .statements
            .iter()
            .any(|s| matches!(s, BashStmt::Select { .. })));
    }

    #[test]
    fn test_coverage_until_loop() {
        let input = r#"until [ $count -ge 5 ]; do
    echo $count
    count=$((count + 1))
done"#;
        let mut parser = BashParser::new(input).unwrap();
        let ast = parser.parse().unwrap();
        assert!(ast
            .statements
            .iter()
            .any(|s| matches!(s, BashStmt::Until { .. })));
    }

    #[test]
    fn test_coverage_function_posix() {
        let input = r#"greet() {
    echo "Hello $1"
}"#;
        let mut parser = BashParser::new(input).unwrap();
        let ast = parser.parse().unwrap();
        assert!(ast
            .statements
            .iter()
            .any(|s| matches!(s, BashStmt::Function { .. })));
    }

    #[test]
    fn test_coverage_trap_command() {
        let input = "trap 'cleanup' EXIT";
        let mut parser = BashParser::new(input).unwrap();
        let ast = parser.parse().unwrap();
        assert!(!ast.statements.is_empty());
    }

    #[test]
    fn test_coverage_return_statement() {
        let input = "return 0";
        let mut parser = BashParser::new(input).unwrap();
        let ast = parser.parse().unwrap();
        assert!(ast
            .statements
            .iter()
            .any(|s| matches!(s, BashStmt::Return { .. })));
    }

    #[test]
    fn test_coverage_break_statement() {
        let input = "break";
        let mut parser = BashParser::new(input).unwrap();
        let ast = parser.parse().unwrap();
        assert!(!ast.statements.is_empty());
    }

    #[test]
    fn test_coverage_continue_statement() {
        let input = "continue";
        let mut parser = BashParser::new(input).unwrap();
        let ast = parser.parse().unwrap();
        assert!(!ast.statements.is_empty());
    }

    #[test]
    fn test_coverage_export_statement() {
        let input = "export VAR=value";
        let mut parser = BashParser::new(input).unwrap();
        let ast = parser.parse().unwrap();
        assert!(!ast.statements.is_empty());
    }

    #[test]
    fn test_coverage_local_statement() {
        let input = "local var=value";
        let mut parser = BashParser::new(input).unwrap();
        let ast = parser.parse().unwrap();
        assert!(!ast.statements.is_empty());
    }

    #[test]
    fn test_coverage_readonly_statement() {
        // readonly with name=value should parse as a command with literal arg
        let input = "readonly VAR=value";
        let mut parser = BashParser::new(input).unwrap();
        let ast = parser.parse().unwrap();
        assert!(!ast.statements.is_empty());
    }

    #[test]
    fn test_KEYWORD_001_echo_done_parses() {
        let input = "echo done";
        let mut parser = BashParser::new(input).unwrap();
        let ast = parser.parse().unwrap();
        assert_eq!(ast.statements.len(), 1);
        match &ast.statements[0] {
            BashStmt::Command { name, args, .. } => {
                assert_eq!(name, "echo");
                assert_eq!(args.len(), 1);
                assert!(matches!(&args[0], BashExpr::Literal(s) if s == "done"));
            }
            other => panic!("Expected Command, got {other:?}"),
        }
    }

    #[test]
    fn test_KEYWORD_002_echo_fi_then_else() {
        let input = "echo fi then else";
        let mut parser = BashParser::new(input).unwrap();
        let ast = parser.parse().unwrap();
        assert_eq!(ast.statements.len(), 1);
        match &ast.statements[0] {
            BashStmt::Command { name, args, .. } => {
                assert_eq!(name, "echo");
                assert_eq!(args.len(), 3);
                assert!(matches!(&args[0], BashExpr::Literal(s) if s == "fi"));
                assert!(matches!(&args[1], BashExpr::Literal(s) if s == "then"));
                assert!(matches!(&args[2], BashExpr::Literal(s) if s == "else"));
            }
            other => panic!("Expected Command, got {other:?}"),
        }
    }

    #[test]
    fn test_KEYWORD_003_echo_done_in_for_loop() {
        // echo done inside a for loop — done as arg, then done terminates loop
        let input = "for i in 1 2; do\necho done\ndone";
        let mut parser = BashParser::new(input).unwrap();
        let ast = parser.parse().unwrap();
        assert_eq!(ast.statements.len(), 1);
        match &ast.statements[0] {
            BashStmt::For { body, .. } => {
                assert_eq!(body.len(), 1);
                match &body[0] {
                    BashStmt::Command { name, args, .. } => {
                        assert_eq!(name, "echo");
                        assert_eq!(args.len(), 1);
                        assert!(matches!(&args[0], BashExpr::Literal(s) if s == "done"));
                    }
                    other => panic!("Expected Command in body, got {other:?}"),
                }
            }
            other => panic!("Expected For, got {other:?}"),
        }
    }

    #[test]
    fn test_KEYWORD_004_echo_all_keywords() {
        // All keyword tokens should be parseable as echo arguments
        let input = "echo if then elif else fi for while until do done case esac in";
        let mut parser = BashParser::new(input).unwrap();
        let ast = parser.parse().unwrap();
        assert_eq!(ast.statements.len(), 1);
        match &ast.statements[0] {
            BashStmt::Command { name, args, .. } => {
                assert_eq!(name, "echo");
                let kws: Vec<&str> = args
                    .iter()
                    .map(|a| match a {
                        BashExpr::Literal(s) => s.as_str(),
                        _ => panic!("Expected Literal"),
                    })
                    .collect();
                assert_eq!(
                    kws,
                    vec![
                        "if", "then", "elif", "else", "fi", "for", "while", "until", "do", "done",
                        "case", "esac", "in"
                    ]
                );
            }
            other => panic!("Expected Command, got {other:?}"),
        }
    }

    #[test]
    fn test_KEYWORD_005_for_in_done_item() {
        // `done` as a for-in item
        let input = "for word in hello done world; do echo $word; done";
        let mut parser = BashParser::new(input).unwrap();
        let ast = parser.parse().unwrap();
        assert_eq!(ast.statements.len(), 1);
        assert!(matches!(&ast.statements[0], BashStmt::For { .. }));
    }

    #[test]
    fn test_GLOB_001_unquoted_star_is_glob() {
        let input = "ls *.sh";
        let mut parser = BashParser::new(input).unwrap();
        let ast = parser.parse().unwrap();
        match &ast.statements[0] {
            BashStmt::Command { args, .. } => {
                assert!(matches!(&args[0], BashExpr::Glob(p) if p == "*.sh"));
            }
            other => panic!("Expected Command, got {other:?}"),
        }
    }

    #[test]
    fn test_GLOB_002_path_glob_preserved() {
        let input = "cp dist/* /tmp/";
        let mut parser = BashParser::new(input).unwrap();
        let ast = parser.parse().unwrap();
        match &ast.statements[0] {
            BashStmt::Command { args, .. } => {
                assert!(matches!(&args[0], BashExpr::Glob(p) if p == "dist/*"));
            }
            other => panic!("Expected Command, got {other:?}"),
        }
    }

    #[test]
    fn test_GLOB_003_absolute_path_glob() {
        let input = "rm -f /tmp/*.log";
        let mut parser = BashParser::new(input).unwrap();
        let ast = parser.parse().unwrap();
        match &ast.statements[0] {
            BashStmt::Command { args, .. } => {
                assert!(matches!(&args[1], BashExpr::Glob(p) if p == "/tmp/*.log"));
            }
            other => panic!("Expected Command, got {other:?}"),
        }
    }

    #[test]
    fn test_GLOB_004_quoted_star_not_glob() {
        // Quoted * should remain a Literal, not a Glob
        let input = r#"find . -name "*.txt""#;
        let mut parser = BashParser::new(input).unwrap();
        let ast = parser.parse().unwrap();
        match &ast.statements[0] {
            BashStmt::Command { args, .. } => {
                // The "*.txt" comes from Token::String, so it's a Literal
                assert!(matches!(&args[2], BashExpr::Literal(s) if s == "*.txt"));
            }
            other => panic!("Expected Command, got {other:?}"),
        }
    }

    #[test]
    fn test_NAMEVALUE_001_echo_name_equals_value() {
        let input = "echo name=myapp";
        let mut parser = BashParser::new(input).expect("parser should init");
        let ast = parser.parse().expect("should parse name=value in argument");
        match &ast.statements[0] {
            BashStmt::Command { name, args, .. } => {
                assert_eq!(name, "echo");
                assert_eq!(args.len(), 1);
                assert!(matches!(&args[0], BashExpr::Literal(s) if s == "name=myapp"));
            }
            other => panic!("Expected Command, got {other:?}"),
        }
    }

    #[test]
    fn test_NAMEVALUE_002_docker_filter() {
        let input = "docker ps --filter name=myapp";
        let mut parser = BashParser::new(input).expect("parser should init");
        let ast = parser
            .parse()
            .expect("should parse docker --filter name=value");
        match &ast.statements[0] {
            BashStmt::Command { name, args, .. } => {
                assert_eq!(name, "docker");
                assert!(args.len() >= 3); // ps, --filter, name=myapp
                                          // Find the name=myapp argument
                let has_namevalue = args
                    .iter()
                    .any(|a| matches!(a, BashExpr::Literal(s) if s == "name=myapp"));
                assert!(has_namevalue, "args should contain name=myapp: {args:?}");
            }
            other => panic!("Expected Command, got {other:?}"),
        }
    }

    #[test]
    fn test_NAMEVALUE_003_env_var_equals_val() {
        let input = "env LANG=C sort file.txt";
        let mut parser = BashParser::new(input).expect("parser should init");
        let ast = parser.parse().expect("should parse env VAR=value");
        match &ast.statements[0] {
            BashStmt::Command { name, args, .. } => {
                assert_eq!(name, "env");
                assert!(matches!(&args[0], BashExpr::Literal(s) if s == "LANG=C"));
            }
            other => panic!("Expected Command, got {other:?}"),
        }
    }

    #[test]
    fn test_NAMEVALUE_004_multiple_equals() {
        let input = "docker run -e DB_HOST=localhost -e DB_PORT=5432 myimage";
        let mut parser = BashParser::new(input).expect("parser should init");
        let ast = parser
            .parse()
            .expect("should parse multiple name=value args");
        match &ast.statements[0] {
            BashStmt::Command { name, args, .. } => {
                assert_eq!(name, "docker");
                let has_host = args
                    .iter()
                    .any(|a| matches!(a, BashExpr::Literal(s) if s == "DB_HOST=localhost"));
                let has_port = args
                    .iter()
                    .any(|a| matches!(a, BashExpr::Literal(s) if s == "DB_PORT=5432"));
                assert!(has_host, "should have DB_HOST=localhost: {args:?}");
                assert!(has_port, "should have DB_PORT=5432: {args:?}");
            }
            other => panic!("Expected Command, got {other:?}"),
        }
    }

    #[test]
    fn test_URL_001_http_url_single_token() {
        let input = "curl http://localhost:8080/health";
        let mut parser = BashParser::new(input).expect("parser should init");
        let ast = parser.parse().expect("should parse URL as single token");
        match &ast.statements[0] {
            BashStmt::Command { name, args, .. } => {
                assert_eq!(name, "curl");
                assert_eq!(args.len(), 1);
                assert!(
                    matches!(&args[0], BashExpr::Literal(s) if s == "http://localhost:8080/health"),
                    "URL should be single token: {args:?}"
                );
            }
            other => panic!("Expected Command, got {other:?}"),
        }
    }

    #[test]
    fn test_URL_002_port_mapping_single_token() {
        let input = "docker run -p 8080:8080 myimage";
        let mut parser = BashParser::new(input).expect("parser should init");
        let ast = parser
            .parse()
            .expect("should parse port mapping as single token");
        match &ast.statements[0] {
            BashStmt::Command { name, args, .. } => {
                assert_eq!(name, "docker");
                let has_port = args
                    .iter()
                    .any(|a| matches!(a, BashExpr::Literal(s) if s == "8080:8080"));
                assert!(has_port, "should have 8080:8080 as single token: {args:?}");
            }
            other => panic!("Expected Command, got {other:?}"),
        }
    }

    #[test]
    fn test_URL_003_https_url() {
        let input = "wget https://example.com/file.tar.gz";
        let mut parser = BashParser::new(input).expect("parser should init");
        let ast = parser.parse().expect("should parse HTTPS URL");
        match &ast.statements[0] {
            BashStmt::Command { name, args, .. } => {
                assert_eq!(name, "wget");
                assert_eq!(args.len(), 1);
                assert!(
                    matches!(&args[0], BashExpr::Literal(s) if s == "https://example.com/file.tar.gz"),
                    "HTTPS URL should be single token: {args:?}"
                );
            }
            other => panic!("Expected Command, got {other:?}"),
        }
    }

    #[test]
    fn test_COMPOUND_001_if_and_condition() {
        let input = r#"if [ "$X" = "a" ] && [ "$Y" -gt 0 ]; then
    echo yes
fi"#;
        let mut parser = BashParser::new(input).expect("parser should init");
        let ast = parser.parse().expect("should parse && in if condition");
        assert_eq!(ast.statements.len(), 1);
        match &ast.statements[0] {
            BashStmt::If {
                condition,
                then_block,
                ..
            } => {
                // Condition should be a compound test with And
                let cond_str = format!("{condition:?}");
                assert!(
                    cond_str.contains("And"),
                    "condition should contain And: {cond_str}"
                );
                assert!(!then_block.is_empty());
            }
            other => panic!("Expected If, got {other:?}"),
        }
    }

    #[test]
    fn test_COMPOUND_002_if_or_condition() {
        let input = r#"if [ -f /tmp/a ] || [ -f /tmp/b ]; then
    echo found
fi"#;
        let mut parser = BashParser::new(input).expect("parser should init");
        let ast = parser.parse().expect("should parse || in if condition");
        match &ast.statements[0] {
            BashStmt::If { condition, .. } => {
                let cond_str = format!("{condition:?}");
                assert!(
                    cond_str.contains("Or"),
                    "condition should contain Or: {cond_str}"
                );
            }
            other => panic!("Expected If, got {other:?}"),
        }
    }
}