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
//! Tests extracted from lexer.rs for file health compliance.
#![allow(clippy::unwrap_used)]

use crate::bash_parser::lexer::*;

#[test]
fn test_token_clone() {
    let tokens = vec![
        Token::If,
        Token::Then,
        Token::Identifier("x".to_string()),
        Token::String("hello".to_string()),
        Token::Number(42),
        Token::Variable("x".to_string()),
        Token::Eof,
    ];
    for token in tokens {
        let _ = token.clone();
    }
}

#[test]
fn test_token_eq() {
    assert_eq!(Token::If, Token::If);
    assert_ne!(Token::If, Token::Then);
    assert_eq!(Token::Number(42), Token::Number(42));
    assert_ne!(Token::Number(42), Token::Number(43));
}

#[test]
fn test_lexer_error_debug() {
    let err = LexerError::UnexpectedChar('x', 1, 1);
    let debug = format!("{:?}", err);
    assert!(debug.contains("UnexpectedChar"));
}

#[test]
fn test_lexer_complex_script() {
    let input = r#"
#!/bin/bash
# Comment
FOO=bar
if [ "$FOO" == "bar" ]; then
echo "Hello $FOO"
fi
"#;
    let mut lexer = Lexer::new(input);
    let result = lexer.tokenize();
    assert!(result.is_ok());
}

#[test]
fn test_lexer_escape_in_string() {
    let mut lexer = Lexer::new(r#""hello\nworld""#);
    let tokens = lexer.tokenize().unwrap();
    assert!(matches!(tokens[0], Token::String(_)));
}

#[test]
fn test_lexer_dollar_sign_context() {
    // $ followed by space might be handled differently
    let mut lexer = Lexer::new("echo $FOO");
    let tokens = lexer.tokenize().unwrap();
    // Should have a variable token
    assert!(tokens.iter().any(|t| matches!(t, Token::Variable(_))));
}

// ============================================================================
// Coverage Tests - read_operator (LEX_OP_COV_001-020)
// ============================================================================

/// Helper: tokenize and return the token types
fn lex(input: &str) -> Vec<Token> {
    let mut lexer = Lexer::new(input);
    lexer.tokenize().unwrap_or_default()
}

#[test]
fn test_LEX_OP_COV_001_ne_operator() {
    let tokens = lex("[ a != b ]");
    assert!(tokens.iter().any(|t| matches!(t, Token::Ne)));
}

#[test]
fn test_LEX_OP_COV_002_le_operator() {
    let tokens = lex("[[ a <= b ]]");
    assert!(tokens.iter().any(|t| matches!(t, Token::Le)));
}

#[test]
fn test_LEX_OP_COV_003_ge_operator() {
    let tokens = lex("[[ a >= b ]]");
    assert!(tokens.iter().any(|t| matches!(t, Token::Ge)));
}

#[test]
fn test_LEX_OP_COV_004_append_redirect() {
    let tokens = lex("echo hi >> file");
    assert!(tokens.iter().any(|t| matches!(t, Token::GtGt)));
}

#[test]
fn test_LEX_OP_COV_005_and_operator() {
    let tokens = lex("true && false");
    assert!(tokens.iter().any(|t| matches!(t, Token::And)));
}

#[test]
fn test_LEX_OP_COV_006_or_operator() {
    let tokens = lex("true || false");
    assert!(tokens.iter().any(|t| matches!(t, Token::Or)));
}

#[test]
fn test_LEX_OP_COV_007_double_brackets() {
    let tokens = lex("[[ x == y ]]");
    assert!(tokens.iter().any(|t| matches!(t, Token::DoubleLeftBracket)));
    assert!(tokens
        .iter()
        .any(|t| matches!(t, Token::DoubleRightBracket)));
}

#[test]
fn test_LEX_OP_COV_008_plus_equals() {
    let tokens = lex("arr+=(val)");
    assert!(tokens
        .iter()
        .any(|t| matches!(t, Token::Identifier(s) if s == "+=")));
}

#[test]
fn test_LEX_OP_COV_009_not_operator() {
    let tokens = lex("! true");
    assert!(tokens.iter().any(|t| matches!(t, Token::Not)));
}

#[test]
fn test_LEX_OP_COV_010_pipe() {
    let tokens = lex("ls | grep foo");
    assert!(tokens.iter().any(|t| matches!(t, Token::Pipe)));
}

#[test]
fn test_LEX_OP_COV_011_case_double_semicolon() {
    let tokens = lex(";;");
    assert!(tokens
        .iter()
        .any(|t| matches!(t, Token::Identifier(s) if s == ";;")));
}

#[test]
fn test_LEX_OP_COV_012_case_semicolon_ampersand() {
    let tokens = lex(";&");
    assert!(tokens
        .iter()
        .any(|t| matches!(t, Token::Identifier(s) if s == ";&")));
}

#[test]
fn test_LEX_OP_COV_013_ampersand_background() {
    let tokens = lex("sleep 1 &");
    assert!(tokens.iter().any(|t| matches!(t, Token::Ampersand)));
}

#[test]
fn test_LEX_OP_COV_014_parens() {
    let tokens = lex("(echo hi)");
    assert!(tokens.iter().any(|t| matches!(t, Token::LeftParen)));
    assert!(tokens.iter().any(|t| matches!(t, Token::RightParen)));
}

#[test]
fn test_LEX_OP_COV_015_braces() {
    let tokens = lex("{ echo hi; }");
    assert!(tokens.iter().any(|t| matches!(t, Token::LeftBrace)));
    assert!(tokens.iter().any(|t| matches!(t, Token::RightBrace)));
}

#[test]
fn test_LEX_OP_COV_016_brackets() {
    let tokens = lex("[ -f file ]");
    assert!(tokens.iter().any(|t| matches!(t, Token::LeftBracket)));
    assert!(tokens.iter().any(|t| matches!(t, Token::RightBracket)));
}

#[test]
fn test_LEX_OP_COV_017_noclobber_redirect() {
    let tokens = lex("echo hi >| file");
    assert!(tokens
        .iter()
        .any(|t| matches!(t, Token::Identifier(s) if s == ">|")));
}

#[test]
fn test_LEX_OP_COV_018_readwrite_redirect() {
    let tokens = lex("exec 3<> file");
    assert!(tokens
        .iter()
        .any(|t| matches!(t, Token::Identifier(s) if s == "<>")));
}

#[test]
fn test_LEX_OP_COV_019_question_glob() {
    let tokens = lex("echo file?.txt");
    // The ? should be tokenized somewhere in the output
    assert!(!tokens.is_empty());
}

#[test]
fn test_LEX_OP_COV_020_case_resume_double_semi_ampersand() {
    let tokens = lex(";;&");
    assert!(tokens
        .iter()
        .any(|t| matches!(t, Token::Identifier(s) if s == ";;&")));
}

#[test]
fn test_LEX_OP_COV_021_herestring() {
    let tokens = lex("cat <<< 'hello'");
    assert!(
        tokens
            .iter()
            .any(|t| matches!(t, Token::HereString(s) if s == "hello")),
        "Expected HereString(\"hello\"), got: {:?}",
        tokens
    );
}

#[test]
fn test_LEX_OP_COV_022_heredoc_indented() {
    let tokens = lex("cat <<-EOF\n\t\tline1\n\tEOF\n");
    assert!(
        tokens
            .iter()
            .any(|t| matches!(t, Token::Heredoc { delimiter, .. } if delimiter == "EOF")),
        "Expected Heredoc with delimiter EOF, got: {:?}",
        tokens
    );
}

#[test]
fn test_LEX_OP_COV_023_process_substitution_input() {
    let tokens = lex("diff <(ls dir1) file2");
    assert!(
        tokens
            .iter()
            .any(|t| matches!(t, Token::Identifier(s) if s.starts_with("<("))),
        "Expected process substitution <(...), got: {:?}",
        tokens
    );
}

#[test]
fn test_LEX_OP_COV_024_process_substitution_output() {
    let tokens = lex("tee >(grep foo)");
    assert!(
        tokens
            .iter()
            .any(|t| matches!(t, Token::Identifier(s) if s.starts_with(">("))),
        "Expected process substitution >(...), got: {:?}",
        tokens
    );
}

#[test]
fn test_LEX_OP_COV_025_case_fall_through_semicolon_ampersand() {
    let tokens = lex(";&");
    assert!(
        tokens
            .iter()
            .any(|t| matches!(t, Token::Identifier(s) if s == ";&")),
        "Expected ;& fall-through operator, got: {:?}",
        tokens
    );
}

#[test]
fn test_LEX_OP_COV_026_extended_glob_negation() {
    let tokens = lex("!(foo|bar)");
    assert!(
        tokens
            .iter()
            .any(|t| matches!(t, Token::Identifier(s) if s == "!(foo|bar)")),
        "Expected extended glob !(foo|bar), got: {:?}",
        tokens
    );
}

#[test]
fn test_LEX_OP_COV_027_eq_in_double_bracket() {
    let tokens = lex("[[ $x == y ]]");
    assert!(tokens.iter().any(|t| matches!(t, Token::DoubleLeftBracket)));
    assert!(tokens.iter().any(|t| matches!(t, Token::Eq)));
    assert!(tokens
        .iter()
        .any(|t| matches!(t, Token::DoubleRightBracket)));
}

#[test]
fn test_LEX_OP_COV_028_heredoc_basic_delimiter() {
    let tokens = lex("cat <<END\nhello world\nEND\n");
    assert!(
        tokens
            .iter()
            .any(|t| matches!(t, Token::Heredoc { delimiter, content }
                if delimiter == "END" && content == "hello world")),
        "Expected Heredoc with delimiter END and content 'hello world', got: {:?}",
        tokens
    );
}

#[test]
fn test_LEX_OP_COV_029_multiple_operators_and_or_sequence() {
    let tokens = lex("a && b || c");
    assert!(tokens.iter().any(|t| matches!(t, Token::And)));
    assert!(tokens.iter().any(|t| matches!(t, Token::Or)));
}

#[test]
fn test_LEX_OP_COV_030_fd_number_before_append_redirect() {
    let tokens = lex("cmd 2>>file");
    assert!(
        tokens.iter().any(|t| matches!(t, Token::GtGt)),
        "Expected >> append redirect, got: {:?}",
        tokens
    );
}

#[test]
fn test_LEX_OP_COV_031_noclobber_after_fd_number() {
    let tokens = lex("cmd 1>| file");
    assert!(
        tokens
            .iter()
            .any(|t| matches!(t, Token::Identifier(s) if s == ">|")),
        "Expected >| noclobber redirect, got: {:?}",
        tokens
    );
}

#[test]
fn test_LEX_OP_COV_032_readwrite_redirect_after_fd() {
    let tokens = lex("exec 3<> /dev/tty");
    assert!(
        tokens
            .iter()
            .any(|t| matches!(t, Token::Identifier(s) if s == "<>")),
        "Expected <> read-write redirect, got: {:?}",
        tokens
    );
}

#[test]
fn test_LEX_OP_COV_033_double_semi_vs_semi_amp_disambiguation() {
    // ;; is case terminator
    let tokens_dsemi = lex(";;");
    assert!(
        tokens_dsemi
            .iter()
            .any(|t| matches!(t, Token::Identifier(s) if s == ";;")),
        "Expected ;; case terminator, got: {:?}",
        tokens_dsemi
    );

    // ;& is case fall-through
    let tokens_samp = lex(";&");
    assert!(
        tokens_samp
            .iter()
            .any(|t| matches!(t, Token::Identifier(s) if s == ";&")),
        "Expected ;& fall-through, got: {:?}",
        tokens_samp
    );

    // ;;& is case resume
    let tokens_dsamp = lex(";;&");
    assert!(
        tokens_dsamp
            .iter()
            .any(|t| matches!(t, Token::Identifier(s) if s == ";;&")),
        "Expected ;;& case resume, got: {:?}",
        tokens_dsamp
    );
}

#[test]
fn test_LEX_OP_COV_034_plus_equals_different_lhs() {
    // Array append
    let tokens = lex("myarr+=(newval)");
    assert!(
        tokens
            .iter()
            .any(|t| matches!(t, Token::Identifier(s) if s == "+=")),
        "Expected += operator, got: {:?}",
        tokens
    );
}

#[test]
fn test_LEX_OP_COV_035_nested_extended_glob_with_inner_parens() {
    let tokens = lex("!(a|(b|c))");
    assert!(
        tokens
            .iter()
            .any(|t| matches!(t, Token::Identifier(s) if s == "!(a|(b|c))")),
        "Expected nested extended glob !(a|(b|c)), got: {:?}",
        tokens
    );
}

#[test]
fn test_LEX_OP_COV_036_not_before_command() {
    let tokens = lex("! grep foo file");
    assert!(
        tokens.iter().any(|t| matches!(t, Token::Not)),
        "Expected ! (Not) token, got: {:?}",
        tokens
    );
    assert!(
        tokens
            .iter()
            .any(|t| matches!(t, Token::Identifier(s) if s == "grep")),
        "Expected command identifier 'grep', got: {:?}",
        tokens
    );
}

#[test]
fn test_LEX_OP_COV_037_pipe_in_pipeline() {
    let tokens = lex("ls -la | sort | head -5");
    let pipe_count = tokens.iter().filter(|t| matches!(t, Token::Pipe)).count();
    assert_eq!(
        pipe_count, 2,
        "Expected 2 pipe tokens in pipeline, got {}: {:?}",
        pipe_count, tokens
    );
}

#[test]
fn test_LEX_OP_COV_038_semicolon_in_different_contexts() {
    // Semicolon as command separator
    let tokens = lex("echo a; echo b");
    let semi_count = tokens
        .iter()
        .filter(|t| matches!(t, Token::Semicolon))
        .count();
    assert_eq!(
        semi_count, 1,
        "Expected 1 semicolon, got {}: {:?}",
        semi_count, tokens
    );
}

#[test]
fn test_LEX_OP_COV_039_append_redirect_in_pipeline() {
    let tokens = lex("cmd1 | cmd2 >> outfile");
    assert!(
        tokens.iter().any(|t| matches!(t, Token::Pipe)),
        "Expected pipe, got: {:?}",
        tokens
    );
    assert!(
        tokens.iter().any(|t| matches!(t, Token::GtGt)),
        "Expected >> append redirect, got: {:?}",
        tokens
    );
}

#[test]
fn test_LEX_OP_COV_040_mixed_operators_conditional_and_or() {
    let tokens = lex("[[ $x == y ]] && echo yes || echo no");
    assert!(tokens.iter().any(|t| matches!(t, Token::DoubleLeftBracket)));
    assert!(tokens.iter().any(|t| matches!(t, Token::Eq)));
    assert!(tokens
        .iter()
        .any(|t| matches!(t, Token::DoubleRightBracket)));
    assert!(tokens.iter().any(|t| matches!(t, Token::And)));
    assert!(tokens.iter().any(|t| matches!(t, Token::Or)));
}