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
#![allow(deprecated)]
#![allow(clippy::unwrap_used)] // Tests can use unwrap() for simplicity
#![allow(clippy::expect_used)]
//! EXTREME TDD: Fix Safety Taxonomy Validation Tests
//!
//! RED PHASE: These tests WILL FAIL until we classify existing rules.
//!
//! Test Structure:
//! 1. SAFE fixes - Applied by default `--fix`
//! 2. SAFE-WITH-ASSUMPTIONS - Require `--fix --fix-assumptions`
//! 3. UNSAFE - Never auto-applied, suggestions only
//! 4. Property tests - Verify semantic preservation
//! 5. Integration tests - Mixed safety levels
use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::TempDir;
/// Helper to create bashrs command
#[allow(deprecated)]
fn rash_cmd() -> Command {
assert_cmd::cargo_bin_cmd!("bashrs")
}
// ============================================================================
// SAFE FIXES - Applied by default with `--fix`
// ============================================================================
#[test]
fn test_safe_sc2086_unquoted_variable_autofix() {
// RED: SC2086 should be classified as SAFE
// Quoting variables is semantically safe transformation
let temp = TempDir::new().unwrap();
let script = temp.path().join("unquoted.sh");
fs::write(
&script,
r#"#!/bin/bash
echo $variable
echo $file
"#,
)
.unwrap();
// Without --fix: Should report diagnostics (exit code 1 for warnings)
rash_cmd()
.arg("lint")
.arg(&script)
.assert()
.code(1)
.stdout(predicate::str::contains("SC2086"))
.stdout(predicate::str::contains("Double quote"));
// With --fix: Should apply SAFE fixes
let fixed = temp.path().join("fixed.sh");
rash_cmd()
.arg("lint")
.arg(&script)
.arg("--fix")
.arg("--output")
.arg(&fixed)
.assert()
.success();
// Verify fix applied
let content = fs::read_to_string(&fixed).unwrap();
assert!(
content.contains(r#"echo "$variable""#),
"Should quote $variable"
);
assert!(content.contains(r#"echo "$file""#), "Should quote $file");
}
#[test]
fn test_safe_sc2046_unquoted_command_substitution_autofix() {
// RED: SC2046 should be classified as SAFE
let temp = TempDir::new().unwrap();
let script = temp.path().join("cmd_sub.sh");
fs::write(
&script,
r#"#!/bin/bash
for file in $(ls *.txt); do
echo $file
done
"#,
)
.unwrap();
// With --fix: Should apply SAFE fixes
let fixed = temp.path().join("fixed.sh");
rash_cmd()
.arg("lint")
.arg(&script)
.arg("--fix")
.arg("--output")
.arg(&fixed)
.assert()
.success();
// Verify fix applied (quoting command substitution)
let content = fs::read_to_string(&fixed).unwrap();
assert!(
content.contains(r#""$(ls *.txt)""#) || content.contains("*.txt"),
"Should quote command substitution or suggest glob"
);
}
#[test]
fn test_safe_sc2116_useless_echo_autofix() {
// RED: SC2116 should be classified as SAFE
let temp = TempDir::new().unwrap();
let script = temp.path().join("useless_echo.sh");
fs::write(
&script,
r#"#!/bin/bash
var=$(echo $other_var)
result=$(echo "hello world")
"#,
)
.unwrap();
// With --fix: Should apply SAFE fixes
let fixed = temp.path().join("fixed.sh");
rash_cmd()
.arg("lint")
.arg(&script)
.arg("--fix")
.arg("--output")
.arg(&fixed)
.assert()
.success();
// Verify fix applied (removed useless echo)
let content = fs::read_to_string(&fixed).unwrap();
assert!(
content.contains(r#"var="$other_var""#) || content.contains("var=$other_var"),
"Should remove useless echo"
);
}
// ============================================================================
// SAFE-WITH-ASSUMPTIONS - Require `--fix --fix-assumptions`
// ============================================================================
#[test]
fn test_safe_with_assumptions_idem001_mkdir_not_applied_by_default() {
// RED: IDEM001 should be SAFE-WITH-ASSUMPTIONS
// Assumption: Directory doesn't need strict error checking
let temp = TempDir::new().unwrap();
let script = temp.path().join("mkdir.sh");
fs::write(
&script,
r#"#!/bin/bash
mkdir /tmp/mydir
mkdir /app/releases
"#,
)
.unwrap();
// Without --fix-assumptions: Should NOT apply fix
let fixed = temp.path().join("fixed.sh");
rash_cmd()
.arg("lint")
.arg(&script)
.arg("--fix")
.arg("--output")
.arg(&fixed)
.assert()
.success();
let content = fs::read_to_string(&fixed).unwrap();
// Should NOT have -p flag (only SAFE fixes applied)
assert!(
!content.contains("mkdir -p"),
"Should NOT apply SAFE-WITH-ASSUMPTIONS fix without --fix-assumptions"
);
}
#[test]
fn test_safe_with_assumptions_idem001_mkdir_with_flag() {
// RED: With --fix-assumptions, should apply IDEM001
let temp = TempDir::new().unwrap();
let script = temp.path().join("mkdir.sh");
fs::write(
&script,
r#"#!/bin/bash
mkdir /tmp/mydir
"#,
)
.unwrap();
// With --fix-assumptions: Should apply fix
let fixed = temp.path().join("fixed.sh");
rash_cmd()
.arg("lint")
.arg(&script)
.arg("--fix")
.arg("--fix-assumptions")
.arg("--output")
.arg(&fixed)
.assert()
.success();
let content = fs::read_to_string(&fixed).unwrap();
assert!(
content.contains("mkdir -p"),
"Should apply mkdir -p with --fix-assumptions"
);
}
#[test]
fn test_safe_with_assumptions_idem002_rm_not_applied_by_default() {
// RED: IDEM002 should be SAFE-WITH-ASSUMPTIONS
// Assumption: Missing file is not an error condition
let temp = TempDir::new().unwrap();
let script = temp.path().join("rm.sh");
fs::write(
&script,
r#"#!/bin/bash
rm /tmp/cache.txt
rm /app/current
"#,
)
.unwrap();
// Without --fix-assumptions: Should NOT apply fix
let fixed = temp.path().join("fixed.sh");
rash_cmd()
.arg("lint")
.arg(&script)
.arg("--fix")
.arg("--output")
.arg(&fixed)
.assert()
.success();
let content = fs::read_to_string(&fixed).unwrap();
assert!(
!content.contains("rm -f"),
"Should NOT apply SAFE-WITH-ASSUMPTIONS fix without --fix-assumptions"
);
}
#[test]
fn test_safe_with_assumptions_idem002_rm_with_flag() {
// RED: With --fix-assumptions, should apply IDEM002
let temp = TempDir::new().unwrap();
let script = temp.path().join("rm.sh");
fs::write(
&script,
r#"#!/bin/bash
rm /tmp/cache.txt
"#,
)
.unwrap();
// With --fix-assumptions: Should apply fix
let fixed = temp.path().join("fixed.sh");
rash_cmd()
.arg("lint")
.arg(&script)
.arg("--fix")
.arg("--fix-assumptions")
.arg("--output")
.arg(&fixed)
.assert()
.success();
let content = fs::read_to_string(&fixed).unwrap();
assert!(
content.contains("rm -f"),
"Should apply rm -f with --fix-assumptions"
);
}
// ============================================================================
// UNSAFE FIXES - Never auto-applied, suggestions only
// ============================================================================
#[test]
fn test_unsafe_idem003_symlink_never_autofix() {
// RED: IDEM003 should be UNSAFE
// Semantic transformation: ln -s → (remove existing + ln -s)
let temp = TempDir::new().unwrap();
let script = temp.path().join("symlink.sh");
fs::write(
&script,
r#"#!/bin/bash
ln -s /app/releases/v1.0.0 /app/current
"#,
)
.unwrap();
// Even with --fix --fix-assumptions: Should NOT apply UNSAFE fix (exit code 0 when using --output)
let fixed = temp.path().join("fixed.sh");
rash_cmd()
.arg("lint")
.arg(&script)
.arg("--fix")
.arg("--fix-assumptions")
.arg("--output")
.arg(&fixed)
.assert()
.success();
let content = fs::read_to_string(&fixed).unwrap();
// Should NOT have ln -sf or rm before ln
assert!(!content.contains("ln -sf"), "Should NOT apply UNSAFE fix");
assert!(
!content.contains("rm") || !content.contains("ln -s"),
"Should NOT add rm before ln -s (semantic change)"
);
// Should provide diagnostic output (exit code 1 for warnings)
rash_cmd()
.arg("lint")
.arg(&script)
.assert()
.code(1)
.stdout(predicate::str::contains("IDEM003"));
}
#[test]
fn test_unsafe_det001_random_never_autofix() {
// RED: DET001 should be UNSAFE
// Semantic transformation: $RANDOM → version-based ID
let temp = TempDir::new().unwrap();
let script = temp.path().join("random.sh");
fs::write(
&script,
r#"#!/bin/bash
SESSION_ID=$RANDOM
echo "Session: $SESSION_ID"
"#,
)
.unwrap();
// Even with --fix --fix-assumptions: Should NOT apply UNSAFE fix (exit code 0 when using --output)
let fixed = temp.path().join("fixed.sh");
rash_cmd()
.arg("lint")
.arg(&script)
.arg("--fix")
.arg("--fix-assumptions")
.arg("--output")
.arg(&fixed)
.assert()
.success();
let content = fs::read_to_string(&fixed).unwrap();
// Should still contain $RANDOM (no automatic transformation)
assert!(
content.contains("$RANDOM"),
"Should NOT automatically replace $RANDOM (UNSAFE)"
);
// Should provide diagnostic output (exit code 2 for errors including UNSAFE)
rash_cmd()
.arg("lint")
.arg(&script)
.assert()
.code(2)
.stdout(predicate::str::contains("DET001"));
}
#[test]
include!("test_fix_safety_taxonomy_tests_cont.rs");