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
#![allow(deprecated)]
#![allow(clippy::unwrap_used)] // Tests can use unwrap() for simplicity
#![allow(clippy::expect_used)]
// Exit code tests for bashrs lint command (Issue #6)
//
// Expected behavior (aligned with make lint):
// - Exit 0: No issues found
// - Exit 1: Warnings found (no errors)
// - Exit 2: Errors found
// - Exit 2: Tool failure (invalid arguments, file not found)
//
// EXTREME TDD: Test-driven development for Issue #6
// https://github.com/paiml/bashrs/issues/6
use assert_cmd::Command;
use std::io::Write;
use tempfile::NamedTempFile;
/// Helper function to create bashrs command
#[allow(deprecated)]
fn bashrs_cmd() -> Command {
assert_cmd::cargo_bin_cmd!("bashrs")
}
// ============================================================================
// RED Phase: Test_Issue_006_* - Exit Code Tests
// ============================================================================
/// Test: Exit 0 when no issues found
#[test]
fn test_issue_006_exit_0_no_issues() {
// ARRANGE: Clean bash script with no issues
let bash_code = r#"#!/bin/bash
# Clean script
echo "Hello, World"
"#;
let mut file = NamedTempFile::new().unwrap();
file.write_all(bash_code.as_bytes()).unwrap();
// ACT & ASSERT: Should exit 0 (success)
bashrs_cmd().arg("lint").arg(file.path()).assert().success(); // success() checks exit code 0
}
/// Test: Exit 1 when only warnings (no errors)
#[test]
fn test_issue_006_exit_0_warnings_only() {
// ARRANGE: Script with warning (SC2086 - unquoted variable)
// This should produce WARNING, not ERROR
let bash_code = r#"#!/bin/bash
var="test"
echo $var
"#;
let mut file = NamedTempFile::new().unwrap();
file.write_all(bash_code.as_bytes()).unwrap();
// ACT & ASSERT: Should exit 1 (warnings found)
bashrs_cmd().arg("lint").arg(file.path()).assert().code(1); // Exit 1 for warnings
}
/// Test: Exit 0 when only info messages (no errors)
#[test]
fn test_issue_006_exit_0_info_only() {
// ARRANGE: Script that might produce INFO-level diagnostics
let bash_code = r#"#!/bin/bash
# Script with potential style issues
echo "test"
"#;
let mut file = NamedTempFile::new().unwrap();
file.write_all(bash_code.as_bytes()).unwrap();
// ACT & ASSERT: Should exit 0 (info is non-blocking)
bashrs_cmd().arg("lint").arg(file.path()).assert().success(); // Exit 0 for info only
}
/// Test: Exit 1 when errors found
#[test]
fn test_issue_006_exit_1_errors_found() {
// ARRANGE: Script with actual ERROR (SC2188: Redirection without command)
let bash_code = r#"#!/bin/bash
# SC2188: Redirection without command (ERROR severity)
> output.txt
"#;
let mut file = NamedTempFile::new().unwrap();
file.write_all(bash_code.as_bytes()).unwrap();
// ACT & ASSERT: Should exit 2 (errors found)
bashrs_cmd()
.arg("lint")
.arg(file.path())
.assert()
.failure() // Exit non-zero
.code(2); // Exit code 2 for errors
}
/// Test: Exit 2 when multiple errors found
#[test]
fn test_issue_006_exit_1_multiple_errors() {
// ARRANGE: Script with multiple errors (SC2188)
let bash_code = r#"#!/bin/bash
# Multiple redirection errors
> output1.txt
> output2.txt
echo $y # WARNING (unquoted variable)
"#;
let mut file = NamedTempFile::new().unwrap();
file.write_all(bash_code.as_bytes()).unwrap();
// ACT & ASSERT: Should exit 2 (errors found, even with warnings)
bashrs_cmd()
.arg("lint")
.arg(file.path())
.assert()
.failure()
.code(2); // Exit code 2 for errors
}
/// Test: Exit 2 when errors AND warnings (errors take precedence)
#[test]
fn test_issue_006_exit_1_errors_and_warnings() {
// ARRANGE: Script with both errors and warnings
let bash_code = r#"#!/bin/bash
var="test"
echo $var # WARNING (unquoted variable)
> error.log # ERROR (SC2188: Redirection without command)
"#;
let mut file = NamedTempFile::new().unwrap();
file.write_all(bash_code.as_bytes()).unwrap();
// ACT & ASSERT: Should exit 2 (errors present)
bashrs_cmd()
.arg("lint")
.arg(file.path())
.assert()
.failure()
.code(2); // Exit code 2 for errors
}
/// Test: Exit 2 for tool failure (file not found)
#[test]
fn test_issue_006_exit_2_file_not_found() {
// ARRANGE: Non-existent file
// ACT & ASSERT: Should exit 2 (tool failure)
bashrs_cmd()
.arg("lint")
.arg("/nonexistent/path/to/file.sh")
.assert()
.failure()
.code(2); // Exit code 2 for tool failure
}
/// Test: Exit 2 for tool failure (invalid format argument)
#[test]
fn test_issue_006_exit_2_invalid_format() {
// ARRANGE: Create a valid file
let bash_code = "#!/bin/bash\necho 'test'\n";
let mut file = NamedTempFile::new().unwrap();
file.write_all(bash_code.as_bytes()).unwrap();
// ACT & ASSERT: Invalid --format argument should be tool failure
bashrs_cmd()
.arg("lint")
.arg("--format")
.arg("invalid-format")
.arg(file.path())
.assert()
.failure()
.code(2); // Exit code 2 for invalid arguments
}
// ============================================================================
// CI/CD Integration Tests
// ============================================================================
/// Test: CI/CD pipeline with warnings should exit 1
/// Updated behavior: warnings exit with code 1 (non-zero for CI/CD failure)
#[test]
fn test_issue_006_ci_cd_warnings_pass() {
// ARRANGE: Typical CI/CD script with minor warnings
let bash_code = r#"#!/bin/bash
# CI/CD deployment script
VERSION="1.0.0"
echo $VERSION # WARNING: unquoted variable
deploy_to_production
"#;
let mut file = NamedTempFile::new().unwrap();
file.write_all(bash_code.as_bytes()).unwrap();
// ACT & ASSERT: Should exit 1 for warnings
let output = bashrs_cmd().arg("lint").arg(file.path()).output().unwrap();
// Should exit 1 (warnings found)
assert_eq!(
output.status.code(),
Some(1),
"Should exit 1 with warnings. Exit code should be 1, got: {:?}",
output.status.code()
);
}
/// Test: CI/CD pipeline with errors should fail (exit 2)
#[test]
fn test_issue_006_ci_cd_errors_fail() {
// ARRANGE: CI/CD script with actual errors
let bash_code = r#"#!/bin/bash
# Broken deployment with ERROR
VERSION="1.0.0"
> deploy.log # ERROR (SC2188: Redirection without command)
"#;
let mut file = NamedTempFile::new().unwrap();
file.write_all(bash_code.as_bytes()).unwrap();
// ACT & ASSERT: CI/CD should fail with errors
let output = bashrs_cmd().arg("lint").arg(file.path()).output().unwrap();
// Should exit 2 (errors found)
assert_eq!(
output.status.code(),
Some(2),
"CI/CD should fail with errors. Exit code should be 2, got: {:?}",
output.status.code()
);
}
// ============================================================================
// Property Tests (EXTREME TDD)
// ============================================================================
/// Property: Any script without errors should exit 0
#[test]
fn test_issue_006_property_no_errors_means_exit_0() {
// Test multiple clean scripts
let clean_scripts = [
"#!/bin/bash\necho 'hello'\n",
"#!/bin/bash\ntrue\n",
"#!/bin/bash\n# Just a comment\n",
"#!/bin/bash\nVAR=\"test\"\necho \"$VAR\"\n", // Properly quoted
];
for (idx, script) in clean_scripts.iter().enumerate() {
let mut file = NamedTempFile::new().unwrap();
file.write_all(script.as_bytes()).unwrap();
let output = bashrs_cmd().arg("lint").arg(file.path()).output().unwrap();
assert_eq!(
output.status.code(),
Some(0),
"Clean script {} should exit 0, got: {:?}",
idx,
output.status.code()
);
}
}
/// Property: File not found should always exit 2
#[test]
fn test_issue_006_property_file_not_found_exit_2() {
let nonexistent_paths = vec![
"/tmp/nonexistent_file_12345.sh",
"/does/not/exist.bash",
"~/fake_script.sh",
];
for path in nonexistent_paths {
let output = bashrs_cmd().arg("lint").arg(path).output().unwrap();
assert_eq!(
output.status.code(),
Some(2),
"File not found should exit 2 for path: {}",
path
);
}
}