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
use super::*;
use crate::bash_parser::ast::{ArithExpr, BashExpr, BashStmt, Redirect, Span, TestExpr};
use crate::bash_parser::parser_arith::ArithToken;
#[test]
fn test_V_TEST_001_variable_set() {
    let input = "if [[ -v MYVAR ]]; then\n    echo set\nfi";
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(
        ast.is_ok(),
        "-v test operator should parse: {:?}",
        ast.err()
    );
}

#[test]
fn test_ENV_PREFIX_001_while_ifs() {
    // IFS='=' before read — env prefix, not assignment condition
    let input = "while IFS='=' read -r key value; do\n    echo \"$key=$value\"\ndone < input.txt";
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(
        ast.is_ok(),
        "IFS= env prefix in while should parse: {:?}",
        ast.err()
    );
}

#[test]
fn test_REGEX_POSIX_CLASS_001_bracket_depth() {
    // =~ with POSIX char class [[:space:]] should not break on ]] inside
    let input = "if [[ \"$key\" =~ ^[[:space:]]*# ]]; then\n    echo comment\nfi";
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(
        ast.is_ok(),
        "=~ with [[:space:]] should parse: {:?}",
        ast.err()
    );
}

#[test]
fn test_COMBINED_REDIR_001_if_condition() {
    // &>/dev/null in if command condition
    let input = "if command -v git &>/dev/null; then\n    echo found\nfi";
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(
        ast.is_ok(),
        "&>/dev/null in if condition should parse: {:?}",
        ast.err()
    );
}

#[test]
fn test_COMBINED_REDIR_002_negated_condition() {
    // ! command -v ... &>/dev/null
    let input = "if ! command -v git &>/dev/null; then\n    echo missing\nfi";
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(
        ast.is_ok(),
        "negated &>/dev/null in condition should parse: {:?}",
        ast.err()
    );
}

#[test]
fn test_COMBINED_REDIR_003_in_command() {
    // &> in regular command (already tested but verify no regression)
    let input = "echo hello &> output.log";
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(ast.is_ok(), "&> in command should parse: {:?}", ast.err());
    if let BashStmt::Command { redirects, .. } = &ast.expect("ok").statements[0] {
        assert_eq!(redirects.len(), 1, "Should have one Combined redirect");
        assert!(matches!(&redirects[0], Redirect::Combined { .. }));
    }
}

#[test]
fn test_DOGFOOD_022_assoc_arrays_and_arithmetic() {
    // Full dogfood_22 constructs
    let input = r#"declare -A config
config[host]="localhost"
config[port]="8080"
for key in "${!config[@]}"; do
printf "%s = %s\n" "$key" "${config[$key]}"
done
arr=(zero one two three four five)
echo "Elements 2-4: ${arr[@]:2:3}"
echo "Last element: ${arr[-1]}"
a=10; b=3
echo "Add: $((a + b))"
echo "Mul: $((a * b))"
max=$((a > b ? a : b))
echo "Max: $max"
"#;
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(
        ast.is_ok(),
        "dogfood_22 constructs should parse: {:?}",
        ast.err()
    );
}

#[test]
fn test_DOGFOOD_023_deployment_script() {
    // Key constructs from dogfood_23
    let input = r#"set -euo pipefail
readonly LOG_FILE="/var/log/deploy.log"
readonly TIMESTAMP_FMT="+%Y-%m-%d %H:%M:%S"

log() {
local level="$1"
shift
local msg="$*"
echo "[$level] $msg" >&2
}

info()  { log "INFO"  "$@"; }

health_check() {
local url="$1"
local max_retries="${2:-10}"
local attempt=0
while (( attempt < max_retries )); do
    if curl -sf -o /dev/null "$url" 2>/dev/null; then
        return 0
    fi
    attempt=$((attempt + 1))
    sleep 5
done
return 1
}

deploy_service() {
local service_name="$1"
for cmd in docker curl jq; do
    if ! command -v "$cmd" &>/dev/null; then
        return 1
    fi
done
if ! docker pull "$service_name" 2>/dev/null; then
    return 1
fi
}

main() {
info "Starting deployment"
deploy_service "${SERVICE_NAME:-myapp}"
health_check "${HEALTH_URL:-http://localhost:8080/health}"
}

main "$@"
"#;
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(
        ast.is_ok(),
        "dogfood_23 key constructs should parse: {:?}",
        ast.err()
    );
}

// --- Batch 3: $'...' ANSI-C quoting, heredoc on done, -L test op ---

#[test]
fn test_ANSI_C_QUOTE_001_tab() {
    let input = "IFS=$'\\t' read -r a b";
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(
        ast.is_ok(),
        "$'\\t' ANSI-C quoting should parse: {:?}",
        ast.err()
    );
}

#[test]
fn test_ANSI_C_QUOTE_002_newline() {
    let input = "echo $'hello\\nworld'";
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(
        ast.is_ok(),
        "$'\\n' ANSI-C quoting should parse: {:?}",
        ast.err()
    );
}

#[test]
fn test_HEREDOC_COMPOUND_001_done_heredoc() {
    let input = "while read -r line; do\n    echo \"$line\"\ndone <<EOF\napple\nbanana\nEOF";
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(ast.is_ok(), "done <<EOF should parse: {:?}", ast.err());
}

#[test]
fn test_FILE_TEST_001_symlink() {
    let input = "if [ -L /tmp/link ]; then echo symlink; fi";
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(ast.is_ok(), "-L test should parse: {:?}", ast.err());
}

#[test]
fn test_FILE_TEST_002_all_operators() {
    // Test all file test operators
    for op in [
        "-f", "-e", "-s", "-d", "-r", "-w", "-x", "-L", "-h", "-p", "-b", "-c", "-g", "-k", "-u",
        "-t", "-O", "-G", "-N", "-v", "-n", "-z",
    ] {
        let input = format!("[ {} /tmp/test ]", op);
        let mut parser = BashParser::new(&input).expect("parser");
        let ast = parser.parse();
        assert!(ast.is_ok(), "{} test should parse: {:?}", op, ast.err());
    }
}

#[test]
fn test_TRIPLE_ELIF_001_with_else() {
    let input = "if [ -f x ]; then\n    echo a\nelif [ -d x ]; then\n    echo b\nelif [ -L x ]; then\n    echo c\nelse\n    echo d\nfi";
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(ast.is_ok(), "triple elif should parse: {:?}", ast.err());
    if let BashStmt::If {
        elif_blocks,
        else_block,
        ..
    } = &ast.expect("ok").statements[0]
    {
        assert_eq!(elif_blocks.len(), 2, "Should have 2 elif blocks");
        assert!(else_block.is_some(), "Should have else block");
    }
}

#[test]
fn test_DOGFOOD_024_traps_and_ansi_c() {
    let input = r#"set -euo pipefail
TMPDIR=$(mktemp -d)
cleanup() {
local exit_code=$?
rm -rf "$TMPDIR"
exit "$exit_code"
}
trap cleanup EXIT
trap 'echo "Caught SIGINT" >&2; cleanup' INT
exec 200>"$LOCKFILE"
flock -n 200 || { echo "Already running" >&2; exit 1; }
"#;
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(
        ast.is_ok(),
        "dogfood_24 traps should parse: {:?}",
        ast.err()
    );
}

#[test]
fn test_DOGFOOD_026_git_and_find() {
    let input = r#"current_branch=$(git branch --show-current)
default_branch=$(git symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null | sed 's|origin/||' || echo "main")
if [[ "$current_branch" != "$default_branch" ]]; then
echo "Not on $default_branch branch"
fi
find /var/log -type f -name "*.log" -exec gzip {} \;
find . -name "*.txt" -print0 | xargs -0 grep -l "pattern" 2>/dev/null || true
"#;
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(
        ast.is_ok(),
        "dogfood_26 git/find should parse: {:?}",
        ast.err()
    );
}

#[test]
fn test_DOGFOOD_027_detect_os_and_install() {
    let input = r#"detect_os() {
if [[ -f /etc/os-release ]]; then
    . /etc/os-release
    echo "$ID"
elif [[ -f /etc/redhat-release ]]; then
    echo "rhel"
elif command -v sw_vers &>/dev/null; then
    echo "macos"
else
    echo "unknown"
fi
}

install_package() {
local pkg="$1"
case "$(detect_os)" in
    ubuntu|debian)
        sudo apt-get install -y "$pkg"
        ;;
    centos|rhel|fedora)
        sudo yum install -y "$pkg"
        ;;
    *)
        echo "Unknown OS" >&2
        return 1
        ;;
esac
}
"#;
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(
        ast.is_ok(),
        "dogfood_27 detect_os should parse: {:?}",
        ast.err()
    );
}

// --- Batch 4: && || inside [[ ]], -a -o inside [ ] ---

#[test]
fn test_TEST_AND_001_double_bracket() {
    let input = r#"if [[ "$a" == "1" && "$b" == "2" ]]; then echo ok; fi"#;
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(ast.is_ok(), "&& inside [[ ]] should parse: {:?}", ast.err());
}

#[test]
fn test_TEST_OR_001_double_bracket() {
    let input = r#"if [[ "$a" == "1" || "$b" == "2" ]]; then echo ok; fi"#;
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(ast.is_ok(), "|| inside [[ ]] should parse: {:?}", ast.err());
}

#[test]
fn test_TEST_AND_002_single_bracket() {
    let input = "if [ -f /etc/passwd -a -r /etc/passwd ]; then echo ok; fi";
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(ast.is_ok(), "-a inside [ ] should parse: {:?}", ast.err());
}

#[test]
fn test_TEST_OR_002_single_bracket() {
    let input = "if [ -f /tmp/a -o -f /tmp/b ]; then echo ok; fi";
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(ast.is_ok(), "-o inside [ ] should parse: {:?}", ast.err());
}

#[test]
fn test_TEST_COMPOUND_001_triple_and() {
    let input = r#"[[ "$a" == "1" && "$b" == "2" && "$c" == "3" ]]"#;
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(
        ast.is_ok(),
        "triple && inside [[ ]] should parse: {:?}",
        ast.err()
    );
}

#[test]
fn test_DOGFOOD_029_edge_cases() {
    let input = r#"result=$(echo "$(basename "$(dirname "$(pwd)")")")
echo "Grandparent: $result"
echo "${UNDEFINED:-default value with spaces}"
outer="hello"
echo "${outer:-${inner:-deep_default}}"
x=10
(( x += 5 ))
echo "x=$x"
for i in 1 2 3; do
for j in a b c; do
    if [[ "$j" == "b" ]]; then
        continue
    fi
    if [[ "$i" == "2" && "$j" == "c" ]]; then
        break 2
    fi
    echo "$i-$j"
done
done
n=5
until [[ $n -le 0 ]]; do
echo "Countdown: $n"
n=$((n - 1))
done
if (( age >= 18 && age < 65 )); then
echo "Working age"
fi
if [ -f /etc/passwd -a -r /etc/passwd ]; then
echo "readable"
fi
"#;
    let mut parser = BashParser::new(input).expect("parser");
    let ast = parser.parse();
    assert!(
        ast.is_ok(),
        "dogfood_29 edge cases should parse: {:?}",
        ast.err()
    );
}