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)]
#![allow(clippy::expect_used)]
// Tests can use unwrap() for simplicity
// CLI Integration Tests for bashrs make purify --with-tests Command
// EXTREME TDD: GREEN phase - Feature implemented and tests passing
//
// Test Naming Convention: test_MAKE_WITH_TESTS_<ID>_<feature>_<scenario>
//
// Task IDs:
// - MAKE_WITH_TESTS_001: Basic test generation
// - MAKE_WITH_TESTS_002: Determinism test generation
// - MAKE_WITH_TESTS_003: Idempotency test generation
// - MAKE_WITH_TESTS_004: POSIX compliance test generation
// - MAKE_WITH_TESTS_005: Property-based test generation
// - MAKE_WITH_TESTS_006: Test execution verification
#![allow(non_snake_case)] // Test naming convention: test_<TASK_ID>_<feature>_<scenario>
use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use std::io::Write;
use tempfile::{NamedTempFile, TempDir};
// ============================================================================
// Helper Functions
// ============================================================================
/// Create a bashrs command (MANDATORY pattern per CLAUDE.md)
#[allow(deprecated)]
fn bashrs_cmd() -> Command {
assert_cmd::cargo_bin_cmd!("bashrs")
}
/// Create a temporary Makefile with given content
fn create_temp_makefile(content: &str) -> NamedTempFile {
let mut file = NamedTempFile::new().expect("Failed to create temp file");
file.write_all(content.as_bytes())
.expect("Failed to write to temp file");
file
}
// ============================================================================
// Test: MAKE_WITH_TESTS_001 - Basic Test Generation
// ============================================================================
#[test]
fn test_MAKE_WITH_TESTS_001_generates_test_file() {
let makefile = r#"# Simple Makefile
.PHONY: all
all:
echo "Building"
"#;
let input_file = create_temp_makefile(makefile);
let output_dir = TempDir::new().expect("Failed to create temp dir");
let output_file = output_dir.path().join("Makefile");
let test_file = output_dir.path().join("Makefile.test.sh");
// RED: This will fail until --with-tests is implemented
bashrs_cmd()
.arg("make")
.arg("purify")
.arg(input_file.path())
.arg("--with-tests")
.arg("-o")
.arg(&output_file)
.assert()
.success();
// Verify test file was created
assert!(
test_file.exists(),
"Test file should be generated at {}",
test_file.display()
);
// Verify test file has POSIX shebang
let test_content = fs::read_to_string(&test_file).expect("Failed to read test file");
assert!(
test_content.starts_with("#!/bin/sh"),
"Test file should have POSIX shebang"
);
}
#[test]
fn test_MAKE_WITH_TESTS_001_test_file_naming_convention() {
let makefile = "all:\n\techo test";
let input_file = create_temp_makefile(makefile);
let output_dir = TempDir::new().expect("Failed to create temp dir");
let output_file = output_dir.path().join("MyMakefile");
bashrs_cmd()
.arg("make")
.arg("purify")
.arg(input_file.path())
.arg("--with-tests")
.arg("-o")
.arg(&output_file)
.assert()
.success();
// Test file should be named <makefile>.test.sh
let test_file = output_dir.path().join("MyMakefile.test.sh");
assert!(
test_file.exists(),
"Test file should follow <makefile>.test.sh naming"
);
}
// ============================================================================
// Test: MAKE_WITH_TESTS_002 - Determinism Test Generation
// ============================================================================
#[test]
fn test_MAKE_WITH_TESTS_002_generates_determinism_test() {
let makefile = r#".PHONY: build
build:
echo "Deterministic build"
"#;
let input_file = create_temp_makefile(makefile);
let output_dir = TempDir::new().expect("Failed to create temp dir");
let output_file = output_dir.path().join("Makefile");
let test_file = output_dir.path().join("Makefile.test.sh");
bashrs_cmd()
.arg("make")
.arg("purify")
.arg(input_file.path())
.arg("--with-tests")
.arg("-o")
.arg(&output_file)
.assert()
.success();
let test_content = fs::read_to_string(&test_file).expect("Failed to read test file");
// Verify test contains determinism test
assert!(
test_content.contains("test_determinism") || test_content.contains("determinism"),
"Test file should contain determinism test"
);
// Verify test runs make twice to compare outputs
assert!(
test_content.contains("make") && test_content.matches("make").count() >= 2,
"Determinism test should run make multiple times"
);
}
// DELETED: test_MAKE_WITH_TESTS_002_determinism_test_passes
// Reason: Integration test (executes generated scripts with make), not unit test
// - Flaky under extreme parallelism (7187 tests) despite retry logic
// - Core functionality (test generation) already covered by test_MAKE_WITH_TESTS_002_generates_determinism_test
// - Five Whys: ROOT CAUSE = Tests depend on external system state (make timing, load)
// - User requirement: "flakey tests are not allowed. use five-whys and fix or delete"
// ============================================================================
// Test: MAKE_WITH_TESTS_003 - Idempotency Test Generation
// ============================================================================
#[test]
fn test_MAKE_WITH_TESTS_003_generates_idempotency_test() {
let makefile = r#".PHONY: install
install:
mkdir -p /tmp/test_dir
"#;
let input_file = create_temp_makefile(makefile);
let output_dir = TempDir::new().expect("Failed to create temp dir");
let output_file = output_dir.path().join("Makefile");
let test_file = output_dir.path().join("Makefile.test.sh");
bashrs_cmd()
.arg("make")
.arg("purify")
.arg(input_file.path())
.arg("--with-tests")
.arg("-o")
.arg(&output_file)
.assert()
.success();
let test_content = fs::read_to_string(&test_file).expect("Failed to read test file");
// Verify test contains idempotency test
assert!(
test_content.contains("test_idempotency") || test_content.contains("idempotent"),
"Test file should contain idempotency test"
);
// Verify test runs make multiple times
assert!(
test_content.matches("make").count() >= 2,
"Idempotency test should run make multiple times"
);
}
// ============================================================================
// Test: MAKE_WITH_TESTS_004 - POSIX Compliance Test Generation
// ============================================================================
#[test]
fn test_MAKE_WITH_TESTS_004_generates_posix_compliance_test() {
let makefile = r#".PHONY: test
test:
echo "test"
"#;
let input_file = create_temp_makefile(makefile);
let output_dir = TempDir::new().expect("Failed to create temp dir");
let output_file = output_dir.path().join("Makefile");
let test_file = output_dir.path().join("Makefile.test.sh");
bashrs_cmd()
.arg("make")
.arg("purify")
.arg(input_file.path())
.arg("--with-tests")
.arg("-o")
.arg(&output_file)
.assert()
.success();
let test_content = fs::read_to_string(&test_file).expect("Failed to read test file");
// Verify test contains POSIX compliance check
assert!(
test_content.contains("make") && test_content.contains("POSIX"),
"Test file should contain POSIX compliance test"
);
}
// ============================================================================
// Test: MAKE_WITH_TESTS_005 - Property-Based Test Generation
// ============================================================================
#[test]
fn test_MAKE_WITH_TESTS_005_property_tests_flag() {
let makefile = r#".PHONY: build
build:
@echo "Build"
"#;
let input_file = create_temp_makefile(makefile);
let output_dir = TempDir::new().expect("Failed to create temp dir");
let output_file = output_dir.path().join("Makefile");
let test_file = output_dir.path().join("Makefile.test.sh");
bashrs_cmd()
.arg("make")
.arg("purify")
.arg(input_file.path())
.arg("--with-tests")
.arg("--property-tests")
.arg("-o")
.arg(&output_file)
.assert()
.success();
let test_content = fs::read_to_string(&test_file).expect("Failed to read test file");
// Verify test contains property testing logic
assert!(
test_content.contains("for")
|| test_content.contains("while")
|| test_content.contains("seq"),
"Property tests should iterate over multiple test cases"
);
// Verify test runs many cases
assert!(
test_content.contains("100") || test_content.contains("50"),
"Property tests should run many cases"
);
}
// ============================================================================
// Test: MAKE_WITH_TESTS_006 - Test Execution Verification
// ============================================================================
#[test]
fn test_MAKE_WITH_TESTS_006_generated_tests_are_executable() {
let makefile = r#".PHONY: all
all:
@echo "Hello"
"#;
let input_file = create_temp_makefile(makefile);
let output_dir = TempDir::new().expect("Failed to create temp dir");
let output_file = output_dir.path().join("Makefile");
let test_file = output_dir.path().join("Makefile.test.sh");
bashrs_cmd()
.arg("make")
.arg("purify")
.arg(input_file.path())
.arg("--with-tests")
.arg("-o")
.arg(&output_file)
.assert()
.success();
// Make test file executable
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mut perms = fs::metadata(&test_file).unwrap().permissions();
perms.set_mode(0o755);
fs::set_permissions(&test_file, perms).unwrap();
}
// Verify test file is valid sh
let output = std::process::Command::new("sh")
.arg("-n") // Syntax check only
.arg(&test_file)
.output()
.expect("Failed to check test file syntax");
assert!(
output.status.success(),
"Generated test file should have valid sh syntax"
);
}
// DELETED: test_MAKE_WITH_TESTS_006_all_tests_pass_for_valid_makefile
// Reason: Integration test (executes generated scripts with make), not unit test
// - Flaky under extreme parallelism (7187 tests) despite retry logic
// - Core functionality (test generation) already covered by test_MAKE_WITH_TESTS_006_generated_tests_are_executable
// - Five Whys: ROOT CAUSE = Tests depend on external system state (make timing, load)
// - User requirement: "flakey tests are not allowed. use five-whys and fix or delete"
// ============================================================================
// Test: Error Handling
// ============================================================================
#[test]
fn test_MAKE_WITH_TESTS_error_missing_output() {
let makefile = "all:\n\techo test";
let input_file = create_temp_makefile(makefile);
// Should fail: --with-tests requires -o flag
bashrs_cmd()
.arg("make")
.arg("purify")
.arg(input_file.path())
.arg("--with-tests")
.assert()
.failure()
.stderr(predicate::str::contains("output").or(predicate::str::contains("-o")));
}
// ============================================================================
// Test: Help and Documentation
// ============================================================================
#[test]
fn test_MAKE_WITH_TESTS_help_flag() {
bashrs_cmd()
.arg("make")
.arg("purify")
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("--with-tests"))
.stdout(predicate::str::contains("Generate test suite"));
}