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
#![allow(clippy::unwrap_used)]
#![allow(clippy::expect_used)]

use super::config::*;
use super::discovery::*;
use super::rules::*;
use super::runner;
use super::scoring::*;
use std::path::PathBuf;

#[test]
fn test_quoting_escaped_quotes_no_false_positive() {
    // echo "echo \"Line $i: Hello\"" — $i is inside double quotes (escaped inner quotes)
    let content = "#!/bin/sh\necho \"echo \\\"Line $i: Hello\\\"\"\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Quoting, content, &artifact);
    assert!(
        result.passed,
        "Escaped quotes should not cause false positive: {:?}",
        result.violations
    );
}

#[test]
fn test_quoting_subshell_no_false_positive() {
    // SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
    let content = "#!/bin/sh\nSCRIPT_DIR=\"$(cd \"$(dirname \"${BASH_SOURCE[0]}\")\" && pwd)\"\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Quoting, content, &artifact);
    assert!(
        result.passed,
        "Subshell with nested quotes should not flag: {:?}",
        result.violations
    );
}

#[test]
fn test_quoting_simple_subshell_not_flagged() {
    // OUTPUT=$(date +%Y-%m-%d) — inside $() is a separate context
    let content = "#!/bin/sh\nOUTPUT=\"$(date +%Y-%m-%d)\"\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Quoting, content, &artifact);
    assert!(result.passed, "Variable in subshell should not be flagged");
}

#[test]
fn test_quoting_unquoted_still_detected() {
    // Plain unquoted $VAR should still be detected
    let content = "#!/bin/sh\necho $UNQUOTED\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Quoting, content, &artifact);
    assert!(
        !result.passed,
        "Unquoted $UNQUOTED should still be detected"
    );
}

#[test]
fn test_quoting_backslash_dollar_not_flagged() {
    // \$VAR is literal, not an expansion
    let content = "#!/bin/sh\necho \\$NOTAVAR\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Quoting, content, &artifact);
    assert!(
        result.passed,
        "Escaped \\$VAR should not be flagged: {:?}",
        result.violations
    );
}

// ─── COMPLY-001 Bashism Detection Expansion ───

#[test]
fn test_posix_function_keyword_detected() {
    let content = "#!/bin/sh\nfunction greet {\n  echo hello\n}\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(
        !result.passed,
        "function keyword should be detected as bashism"
    );
    assert!(result
        .violations
        .iter()
        .any(|v| v.message.contains("function keyword")));
}

#[test]
fn test_posix_function_keyword_with_parens_detected() {
    let content = "#!/bin/sh\nfunction greet() {\n  echo hello\n}\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(
        !result.passed,
        "function greet() should be detected as bashism"
    );
}

#[test]
fn test_posix_name_parens_no_false_positive() {
    // POSIX-valid function definition: name() { ... }
    let content = "#!/bin/sh\ngreet() {\n  echo hello\n}\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(
        result.passed,
        "POSIX name() should not be flagged: {:?}",
        result.violations
    );
}

#[test]
fn test_posix_standalone_arithmetic_detected() {
    let content = "#!/bin/sh\n(( i++ ))\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(!result.passed, "(( )) should be detected as bashism");
    assert!(result
        .violations
        .iter()
        .any(|v| v.message.contains("(( ))")));
}

#[test]
fn test_posix_dollar_arithmetic_no_false_positive() {
    // $(( )) is POSIX arithmetic expansion
    let content = "#!/bin/sh\nresult=$(( 1 + 2 ))\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(
        result.passed,
        "$(( )) should not be flagged: {:?}",
        result.violations
    );
}

#[test]
fn test_posix_arithmetic_after_semicolon() {
    let content = "#!/bin/sh\necho start; (( count++ ))\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(!result.passed, "(( )) after semicolon should be detected");
}

#[test]
fn test_posix_herestring_detected() {
    let content = "#!/bin/sh\nread x <<< \"hello\"\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(
        !result.passed,
        "<<< here-string should be detected as bashism"
    );
    assert!(result
        .violations
        .iter()
        .any(|v| v.message.contains("here-string")));
}

#[test]
fn test_posix_heredoc_no_false_positive() {
    // << heredoc is POSIX
    let content = "#!/bin/sh\ncat << EOF\nhello\nEOF\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(
        result.passed,
        "<< heredoc should not be flagged: {:?}",
        result.violations
    );
}

#[test]
fn test_posix_select_statement_detected() {
    let content = "#!/bin/sh\nselect opt in a b c; do echo $opt; done\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(
        !result.passed,
        "select statement should be detected as bashism"
    );
    assert!(result
        .violations
        .iter()
        .any(|v| v.message.contains("select")));
}

#[test]
fn test_posix_pattern_substitution_detected() {
    let content = "#!/bin/sh\necho ${var//old/new}\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(!result.passed, "pattern substitution should be detected");
    assert!(result
        .violations
        .iter()
        .any(|v| v.message.contains("pattern substitution")));
}

#[test]
fn test_posix_single_pattern_substitution_detected() {
    let content = "#!/bin/sh\necho ${var/old/new}\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(
        !result.passed,
        "single pattern substitution should be detected"
    );
}

#[test]
fn test_posix_default_expansion_no_false_positive() {
    // ${var:-default} is POSIX
    let content = "#!/bin/sh\necho ${var:-default}\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(
        result.passed,
        "POSIX default expansion should not be flagged: {:?}",
        result.violations
    );
}

#[test]
fn test_posix_default_with_path_no_false_positive() {
    // ${TMPDIR:-/tmp} is POSIX default expansion containing a path — NOT pattern substitution
    let content = "#!/bin/sh\ntrap 'rm -rf \"${TMPDIR:-/tmp}/rash\"' EXIT\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(
        result.passed,
        "POSIX default with path value should not be flagged: {:?}",
        result.violations
    );
}

#[test]
fn test_posix_prefix_removal_no_false_positive() {
    // ${var#*/} is POSIX prefix removal — NOT pattern substitution
    let content = "#!/bin/sh\necho ${path#*/}\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(
        result.passed,
        "POSIX prefix removal should not be flagged: {:?}",
        result.violations
    );
}

#[test]
fn test_posix_suffix_removal_no_false_positive() {
    // ${var%/*} is POSIX suffix removal — NOT pattern substitution
    let content = "#!/bin/sh\necho ${path%/*}\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(
        result.passed,
        "POSIX suffix removal should not be flagged: {:?}",
        result.violations
    );
}

#[test]
fn test_posix_error_expansion_no_false_positive() {
    // ${var:?error} is POSIX
    let content = "#!/bin/sh\necho ${var:?error}\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(
        result.passed,
        "POSIX error expansion should not be flagged: {:?}",
        result.violations
    );
}

#[test]
fn test_posix_case_modification_lower_detected() {
    let content = "#!/bin/sh\necho ${var,,}\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(
        !result.passed,
        "lowercase case modification should be detected"
    );
    assert!(result
        .violations
        .iter()
        .any(|v| v.message.contains("case modification")));
}

#[test]
fn test_posix_case_modification_upper_detected() {
    let content = "#!/bin/sh\necho ${var^^}\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(
        !result.passed,
        "uppercase case modification should be detected"
    );
}

#[test]
fn test_posix_pipefail_detected() {
    let content = "#!/bin/sh\nset -o pipefail\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(
        !result.passed,
        "set -o pipefail should be detected as bashism"
    );
    assert!(result
        .violations
        .iter()
        .any(|v| v.message.contains("pipefail")));
}

#[test]
fn test_posix_euo_pipefail_detected() {
    let content = "#!/bin/sh\nset -euo pipefail\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(
        !result.passed,
        "set -euo pipefail should be detected as bashism"
    );
}

#[test]
fn test_posix_set_e_no_false_positive() {
    // set -e is POSIX
    let content = "#!/bin/sh\nset -e\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(
        result.passed,
        "set -e should not be flagged: {:?}",
        result.violations
    );
}

#[test]
fn test_posix_ampersand_redirect_detected() {
    let content = "#!/bin/sh\ncommand &>/dev/null\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(!result.passed, "&> redirect should be detected as bashism");
    assert!(result
        .violations
        .iter()
        .any(|v| v.message.contains("&> redirect")));
}

#[test]
fn test_posix_fd_redirect_no_false_positive() {
    // >&2 is POSIX file descriptor redirect
    let content = "#!/bin/sh\necho error >&2\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Posix, content, &artifact);
    assert!(
        result.passed,
        ">&2 should not be flagged: {:?}",
        result.violations
    );
}


include!("tests_part2_incl2.rs");