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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
//! Test Generation for Purified Makefiles
//!
//! Generates comprehensive test suites for purified Makefiles to ensure:
//! - Determinism: Same inputs always produce same outputs
//! - Idempotency: Safe to re-run multiple times
//! - POSIX Compliance: Generated shell commands pass validation
//!
//! EXTREME TDD: This module implements Phase 2 (GREEN) to make CLI tests pass.

use std::path::Path;

/// Test generation options for Makefiles
#[derive(Debug, Clone)]
pub struct MakefileTestGeneratorOptions {
    /// Generate property-based tests (100+ cases)
    pub property_tests: bool,

    /// Number of property test cases to generate
    pub property_test_count: usize,
}

impl Default for MakefileTestGeneratorOptions {
    fn default() -> Self {
        Self {
            property_tests: false,
            property_test_count: 100,
        }
    }
}

/// Test generator for purified Makefiles
pub struct MakefileTestGenerator {
    options: MakefileTestGeneratorOptions,
}

impl MakefileTestGenerator {
    /// Create a new test generator with given options
    pub fn new(options: MakefileTestGeneratorOptions) -> Self {
        Self { options }
    }

    /// Extract Makefile name from path
    #[allow(clippy::expect_used)]
    fn get_makefile_name(makefile_path: &Path) -> &str {
        makefile_path
            .file_name()
            .expect("Makefile path should have a file name")
            .to_str()
            .expect("File name should be valid UTF-8")
    }

    /// Generate test suite for a purified Makefile
    ///
    /// # Arguments
    /// * `makefile_path` - Path to the purified Makefile
    /// * `_purified_content` - Content of the purified Makefile (for future analysis)
    ///
    /// # Returns
    /// Generated test suite as a String
    pub fn generate_tests(&self, makefile_path: &Path, _purified_content: &str) -> String {
        let mut test_suite = String::new();

        // Shebang
        test_suite.push_str("#!/bin/sh\n");
        test_suite.push_str("# Test Suite for ");
        test_suite.push_str(Self::get_makefile_name(makefile_path));
        test_suite.push('\n');
        test_suite.push_str("# Generated by bashrs make purify --with-tests\n\n");
        test_suite.push_str("set -e  # Exit on first failure\n\n");

        // Test 1: Determinism
        test_suite.push_str(&self.generate_determinism_test(makefile_path));
        test_suite.push('\n');

        // Test 2: Idempotency
        test_suite.push_str(&self.generate_idempotency_test(makefile_path));
        test_suite.push('\n');

        // Test 3: POSIX Compliance
        test_suite.push_str(&self.generate_posix_compliance_test(makefile_path));
        test_suite.push('\n');

        // Test 4: Property-based tests (if enabled)
        if self.options.property_tests {
            test_suite.push_str(&self.generate_property_determinism_test(makefile_path));
            test_suite.push('\n');
        }

        // Test Runner
        test_suite.push_str(&self.generate_test_runner());

        test_suite
    }

    /// Generate determinism test
    ///
    /// Tests that running make twice produces same outputs
    fn generate_determinism_test(&self, makefile_path: &Path) -> String {
        let makefile_name = Self::get_makefile_name(makefile_path);

        format!(
            r#"# Test: Determinism - same make invocation produces same output
test_determinism() {{
    printf "Testing determinism for {}...\n"

    # Run make twice and capture output
    make -f "{}" > /tmp/output1.txt 2>&1 || true
    make -f "{}" > /tmp/output2.txt 2>&1 || true

    # Sort outputs before comparing (handles make's parallel execution)
    sort /tmp/output1.txt > /tmp/output1_sorted.txt
    sort /tmp/output2.txt > /tmp/output2_sorted.txt

    # Compare sorted outputs
    if diff /tmp/output1_sorted.txt /tmp/output2_sorted.txt > /dev/null; then
        printf "✓ Determinism test passed\n"
        rm -f /tmp/output1.txt /tmp/output2.txt /tmp/output1_sorted.txt /tmp/output2_sorted.txt
        return 0
    else
        printf "✗ Determinism test failed - outputs differ\n"
        printf "First run (sorted):\n"
        cat /tmp/output1_sorted.txt
        printf "\nSecond run (sorted):\n"
        cat /tmp/output2_sorted.txt
        rm -f /tmp/output1.txt /tmp/output2.txt /tmp/output1_sorted.txt /tmp/output2_sorted.txt
        return 1
    fi
}}
"#,
            makefile_name, makefile_name, makefile_name
        )
    }

    /// Generate idempotency test
    ///
    /// Tests that running make multiple times is safe
    fn generate_idempotency_test(&self, makefile_path: &Path) -> String {
        let makefile_name = Self::get_makefile_name(makefile_path);

        format!(
            r#"# Test: Idempotency - safe to re-run multiple times
test_idempotency() {{
    printf "Testing idempotency for {}...\n"

    # Run make three times
    make -f "{}" > /dev/null 2>&1 || true
    make -f "{}" > /dev/null 2>&1 || exit_code1=$?
    make -f "{}" > /dev/null 2>&1 || exit_code2=$?

    # Second and third runs should succeed (exit code 0)
    if [ "${{exit_code1:-0}}" -eq 0 ] && [ "${{exit_code2:-0}}" -eq 0 ]; then
        printf "✓ Idempotency test passed\n"
        return 0
    else
        printf "✗ Idempotency test failed - not safe to re-run\n"
        return 1
    fi
}}
"#,
            makefile_name, makefile_name, makefile_name, makefile_name
        )
    }

    /// Generate POSIX compliance test
    ///
    /// Tests that Makefile follows POSIX conventions
    fn generate_posix_compliance_test(&self, makefile_path: &Path) -> String {
        let makefile_name = Self::get_makefile_name(makefile_path);

        format!(
            r#"# Test: POSIX Compliance - Makefile is POSIX-compatible
test_posix_compliance() {{
    printf "Testing POSIX compliance for {}...\n"

    # Check if Makefile can be parsed by POSIX make
    # (Most systems have GNU make, so we just verify it doesn't error)
    if make -f "{}" --version > /dev/null 2>&1; then
        printf "✓ POSIX compliance test passed\n"
        return 0
    else
        printf "⚠ Could not verify POSIX compliance (make may not be available)\n"
        return 0  # Don't fail if make is not available
    fi
}}
"#,
            makefile_name, makefile_name
        )
    }

    /// Generate property-based determinism tests
    ///
    /// Tests determinism across multiple randomized scenarios
    fn generate_property_determinism_test(&self, makefile_path: &Path) -> String {
        let makefile_name = Self::get_makefile_name(makefile_path);
        let count = self.options.property_test_count;

        format!(
            r#"# Test: Property-Based Determinism - {count} test cases
test_property_determinism() {{
    printf "Testing property-based determinism ({count} cases) for {}...\n"

    failed=0
    passed=0

    # Run make multiple times and verify determinism
    i=1
    while [ "$i" -le {count} ]; do
        make -f "{}" > "/tmp/prop_output_${{i}}.txt" 2>&1 || true
        sort "/tmp/prop_output_${{i}}.txt" > "/tmp/prop_output_${{i}}_sorted.txt"

        if [ "$i" -gt 1 ]; then
            if diff "/tmp/prop_output_1_sorted.txt" "/tmp/prop_output_${{i}}_sorted.txt" > /dev/null 2>&1; then
                passed=$((passed + 1))
            else
                failed=$((failed + 1))
            fi
        fi

        i=$((i + 1))
    done

    # Cleanup
    rm -f /tmp/prop_output_*.txt /tmp/prop_output_*_sorted.txt

    if [ "$failed" -eq 0 ]; then
        printf "✓ Property-based determinism test passed ($passed/{count} cases)\n"
        return 0
    else
        printf "✗ Property-based determinism test failed ($failed/{count} cases)\n"
        return 1
    fi
}}
"#,
            makefile_name, makefile_name
        )
    }

    /// Generate test runner
    ///
    /// Runs all tests and reports results
    fn generate_test_runner(&self) -> String {
        let mut script = String::from("# Test Runner\nrun_all_tests() {\n");
        script.push_str("    printf \"\\n=== Running Test Suite ===\\n\\n\"\n\n");
        script.push_str("    failed=0\n    passed=0\n\n");

        // Add core tests
        script.push_str(&self.generate_test_invocation("determinism"));
        script.push_str(&self.generate_test_invocation("idempotency"));
        script.push_str(&self.generate_test_invocation("posix_compliance"));

        // Add property tests if enabled
        if self.options.property_tests {
            script.push_str(&self.generate_test_invocation("property_determinism"));
        }

        // Add summary
        script.push_str(&self.generate_test_summary());
        script.push_str("}\n\n# Run tests\nrun_all_tests\n");

        script
    }

    fn generate_test_invocation(&self, test_name: &str) -> String {
        format!(
            r#"    # Run {0} test
    if test_{0}; then
        passed=$((passed + 1))
    else
        failed=$((failed + 1))
    fi

    printf "\n"

"#,
            test_name
        )
    }

    fn generate_test_summary(&self) -> String {
        r#"    printf "\n=== Test Summary ===\n"
    printf "Passed: %d\n" "$passed"
    printf "Failed: %d\n" "$failed"

    if [ "$failed" -eq 0 ]; then
        printf "\n✓ All tests passed!\n"
        return 0
    else
        printf "\n✗ Some tests failed\n"
        return 1
    fi
"#
        .to_string()
    }
}

// Tests in make_parser test modules

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]
    #![allow(clippy::expect_used)]
    use super::*;
    use std::path::PathBuf;

    // -----------------------------------------------------------------------
    // MakefileTestGeneratorOptions — default values
    // -----------------------------------------------------------------------

    #[test]
    fn test_options_default_property_tests_disabled() {
        let opts = MakefileTestGeneratorOptions::default();
        assert!(!opts.property_tests);
    }

    #[test]
    fn test_options_default_property_test_count() {
        let opts = MakefileTestGeneratorOptions::default();
        assert_eq!(opts.property_test_count, 100);
    }

    #[test]
    fn test_options_custom_values() {
        let opts = MakefileTestGeneratorOptions {
            property_tests: true,
            property_test_count: 50,
        };
        assert!(opts.property_tests);
        assert_eq!(opts.property_test_count, 50);
    }

    #[test]
    fn test_options_clone() {
        let opts = MakefileTestGeneratorOptions {
            property_tests: true,
            property_test_count: 200,
        };
        let cloned = opts.clone();
        assert_eq!(cloned.property_tests, opts.property_tests);
        assert_eq!(cloned.property_test_count, opts.property_test_count);
    }

    #[test]
    fn test_options_debug() {
        let opts = MakefileTestGeneratorOptions::default();
        let dbg = format!("{opts:?}");
        assert!(dbg.contains("MakefileTestGeneratorOptions"));
    }

    // -----------------------------------------------------------------------
    // MakefileTestGenerator::new
    // -----------------------------------------------------------------------

    #[test]
    fn test_generator_new() {
        let opts = MakefileTestGeneratorOptions::default();
        let gen = MakefileTestGenerator::new(opts);
        assert!(!gen.options.property_tests);
    }

    // -----------------------------------------------------------------------
    // get_makefile_name — extracts filename from path
    // -----------------------------------------------------------------------

    #[test]
    fn test_get_makefile_name_simple() {
        let path = PathBuf::from("Makefile");
        assert_eq!(MakefileTestGenerator::get_makefile_name(&path), "Makefile");
    }

    #[test]
    fn test_get_makefile_name_with_directory() {
        let path = PathBuf::from("/home/user/project/Makefile.purified");
        assert_eq!(
            MakefileTestGenerator::get_makefile_name(&path),
            "Makefile.purified"
        );
    }

    #[test]
    fn test_get_makefile_name_nested() {
        let path = PathBuf::from("a/b/c/GNUmakefile");
        assert_eq!(
            MakefileTestGenerator::get_makefile_name(&path),
            "GNUmakefile"
        );
    }

    // -----------------------------------------------------------------------
    // generate_tests — without property tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_generate_tests_contains_shebang() {
        let gen = MakefileTestGenerator::new(MakefileTestGeneratorOptions::default());
        let path = PathBuf::from("Makefile");
        let output = gen.generate_tests(&path, "all:\n\techo hello");
        assert!(output.starts_with("#!/bin/sh"));
    }

    #[test]
    fn test_generate_tests_contains_header() {
        let gen = MakefileTestGenerator::new(MakefileTestGeneratorOptions::default());
        let path = PathBuf::from("Makefile");
        let output = gen.generate_tests(&path, "");
        assert!(output.contains("Test Suite for Makefile"));
        assert!(output.contains("Generated by bashrs"));
    }

    #[test]
    fn test_generate_tests_contains_set_e() {
        let gen = MakefileTestGenerator::new(MakefileTestGeneratorOptions::default());
        let path = PathBuf::from("Makefile");
        let output = gen.generate_tests(&path, "");
        assert!(output.contains("set -e"));
    }

    #[test]
    fn test_generate_tests_contains_determinism_test() {
        let gen = MakefileTestGenerator::new(MakefileTestGeneratorOptions::default());
        let path = PathBuf::from("Makefile");
        let output = gen.generate_tests(&path, "");
        assert!(output.contains("test_determinism"));
    }

    #[test]
    fn test_generate_tests_contains_idempotency_test() {
        let gen = MakefileTestGenerator::new(MakefileTestGeneratorOptions::default());
        let path = PathBuf::from("Makefile");
        let output = gen.generate_tests(&path, "");
        assert!(output.contains("test_idempotency"));
    }

    #[test]
    fn test_generate_tests_contains_posix_compliance_test() {
        let gen = MakefileTestGenerator::new(MakefileTestGeneratorOptions::default());
        let path = PathBuf::from("Makefile");
        let output = gen.generate_tests(&path, "");
        assert!(output.contains("test_posix_compliance"));
    }

    #[test]
    fn test_generate_tests_no_property_tests_by_default() {
        let gen = MakefileTestGenerator::new(MakefileTestGeneratorOptions::default());
        let path = PathBuf::from("Makefile");
        let output = gen.generate_tests(&path, "");
        assert!(
            !output.contains("test_property_determinism"),
            "Property tests should not be included by default"
        );
    }

    #[test]
    fn test_generate_tests_contains_test_runner() {
        let gen = MakefileTestGenerator::new(MakefileTestGeneratorOptions::default());
        let path = PathBuf::from("Makefile");
        let output = gen.generate_tests(&path, "");
        assert!(output.contains("run_all_tests"));
    }

    // -----------------------------------------------------------------------
    // generate_tests — with property tests enabled
    // -----------------------------------------------------------------------

    #[test]
    fn test_generate_tests_with_property_tests() {
        let opts = MakefileTestGeneratorOptions {
            property_tests: true,
            property_test_count: 50,
        };
        let gen = MakefileTestGenerator::new(opts);
        let path = PathBuf::from("Makefile");
        let output = gen.generate_tests(&path, "");
        assert!(
            output.contains("test_property_determinism"),
            "Property tests should be included when enabled"
        );
        assert!(output.contains("50"), "Should use custom test count");
    }

    #[test]
    fn test_generate_tests_property_tests_in_runner() {
        let opts = MakefileTestGeneratorOptions {
            property_tests: true,
            property_test_count: 100,
        };
        let gen = MakefileTestGenerator::new(opts);
        let path = PathBuf::from("Makefile");
        let output = gen.generate_tests(&path, "");
        // The test runner should invoke property_determinism
        let runner_portion = output.split("run_all_tests").collect::<Vec<_>>();
        assert!(
            runner_portion.len() >= 2,
            "Should have run_all_tests section"
        );
        assert!(
            runner_portion[1].contains("property_determinism"),
            "Runner should include property test"
        );
    }

    // -----------------------------------------------------------------------
    // generate_determinism_test
    // -----------------------------------------------------------------------

    #[test]
    fn test_determinism_test_references_makefile() {
        let gen = MakefileTestGenerator::new(MakefileTestGeneratorOptions::default());
        let path = PathBuf::from("MyMakefile");
        let output = gen.generate_determinism_test(&path);
        assert!(output.contains("MyMakefile"));
        assert!(output.contains("Testing determinism"));
        assert!(output.contains("make -f"));
        assert!(output.contains("diff"));
    }

    #[test]
    fn test_determinism_test_cleanup() {
        let gen = MakefileTestGenerator::new(MakefileTestGeneratorOptions::default());
        let path = PathBuf::from("Makefile");
        let output = gen.generate_determinism_test(&path);
        assert!(output.contains("rm -f"), "Should clean up temp files");
    }

    // -----------------------------------------------------------------------
    // generate_idempotency_test
    // -----------------------------------------------------------------------

    #[test]
    fn test_idempotency_test_runs_three_times() {
        let gen = MakefileTestGenerator::new(MakefileTestGeneratorOptions::default());
        let path = PathBuf::from("Makefile");
        let output = gen.generate_idempotency_test(&path);
        // Should run make three times
        let make_count = output.matches("make -f").count();
        assert_eq!(make_count, 3, "Should run make 3 times");
    }

    #[test]
    fn test_idempotency_test_checks_exit_codes() {
        let gen = MakefileTestGenerator::new(MakefileTestGeneratorOptions::default());
        let path = PathBuf::from("Makefile");
        let output = gen.generate_idempotency_test(&path);
        assert!(output.contains("exit_code1"));
        assert!(output.contains("exit_code2"));
    }

    // -----------------------------------------------------------------------
    // generate_posix_compliance_test
    // -----------------------------------------------------------------------

    #[test]
    fn test_posix_compliance_test_content() {
        let gen = MakefileTestGenerator::new(MakefileTestGeneratorOptions::default());
        let path = PathBuf::from("Makefile");
        let output = gen.generate_posix_compliance_test(&path);
        assert!(output.contains("POSIX compliance"));
        assert!(output.contains("make -f"));
    }

    #[test]
    fn test_posix_compliance_test_graceful_fallback() {
        let gen = MakefileTestGenerator::new(MakefileTestGeneratorOptions::default());
        let path = PathBuf::from("Makefile");
        let output = gen.generate_posix_compliance_test(&path);
        // Should not fail if make is not available
        assert!(
            output.contains("return 0"),
            "Should return 0 even if make is unavailable"
        );
    }

    // -----------------------------------------------------------------------
    // generate_property_determinism_test
    // -----------------------------------------------------------------------

    #[test]
    fn test_property_determinism_test_count() {
        let opts = MakefileTestGeneratorOptions {
            property_tests: true,
            property_test_count: 75,
        };
        let gen = MakefileTestGenerator::new(opts);
        let path = PathBuf::from("Makefile");
        let output = gen.generate_property_determinism_test(&path);
        assert!(output.contains("75"), "Should use count 75");
        assert!(output.contains("test_property_determinism"));
        assert!(output.contains("while"));
    }

    #[test]
    fn test_property_determinism_test_cleanup() {
        let opts = MakefileTestGeneratorOptions {
            property_tests: true,
            property_test_count: 10,
        };
        let gen = MakefileTestGenerator::new(opts);
        let path = PathBuf::from("Makefile");
        let output = gen.generate_property_determinism_test(&path);
        assert!(output.contains("rm -f"), "Should clean up temp files");
    }

    // -----------------------------------------------------------------------
    // generate_test_runner
    // -----------------------------------------------------------------------

    #[test]
    fn test_runner_structure() {
        let gen = MakefileTestGenerator::new(MakefileTestGeneratorOptions::default());
        let output = gen.generate_test_runner();
        assert!(output.contains("run_all_tests"));
        assert!(output.contains("Test Suite"));
        assert!(output.contains("passed=0"));
        assert!(output.contains("failed=0"));
    }

    #[test]
    fn test_runner_includes_core_tests() {
        let gen = MakefileTestGenerator::new(MakefileTestGeneratorOptions::default());
        let output = gen.generate_test_runner();
        assert!(output.contains("test_determinism"));
        assert!(output.contains("test_idempotency"));
        assert!(output.contains("test_posix_compliance"));
    }

    #[test]
    fn test_runner_with_property_tests() {
        let opts = MakefileTestGeneratorOptions {
            property_tests: true,
            property_test_count: 100,
        };
        let gen = MakefileTestGenerator::new(opts);
        let output = gen.generate_test_runner();
        assert!(output.contains("test_property_determinism"));
    }

    #[test]
    fn test_runner_without_property_tests() {
        let gen = MakefileTestGenerator::new(MakefileTestGeneratorOptions::default());
        let output = gen.generate_test_runner();
        assert!(!output.contains("test_property_determinism"));
    }

    // -----------------------------------------------------------------------
    // generate_test_invocation
    // -----------------------------------------------------------------------

    #[test]
    fn test_invocation_format() {
        let gen = MakefileTestGenerator::new(MakefileTestGeneratorOptions::default());
        let output = gen.generate_test_invocation("my_test");
        assert!(output.contains("test_my_test"));
        assert!(output.contains("passed=$((passed + 1))"));
        assert!(output.contains("failed=$((failed + 1))"));
    }

    // -----------------------------------------------------------------------
    // generate_test_summary
    // -----------------------------------------------------------------------

    #[test]
    fn test_summary_content() {
        let gen = MakefileTestGenerator::new(MakefileTestGeneratorOptions::default());
        let output = gen.generate_test_summary();
        assert!(output.contains("Test Summary"));
        assert!(output.contains("Passed"));
        assert!(output.contains("Failed"));
        assert!(output.contains("All tests passed"));
        assert!(output.contains("Some tests failed"));
    }

    // -----------------------------------------------------------------------
    // Full integration test — valid shell script output
    // -----------------------------------------------------------------------

    #[test]
    fn test_full_output_is_valid_shell() {
        let opts = MakefileTestGeneratorOptions {
            property_tests: true,
            property_test_count: 5,
        };
        let gen = MakefileTestGenerator::new(opts);
        let path = PathBuf::from("/tmp/Makefile.purified");
        let content = "all:\n\techo hello\nclean:\n\trm -f *.o";
        let output = gen.generate_tests(&path, content);

        // Verify it's a well-formed shell script
        assert!(output.starts_with("#!/bin/sh"));
        assert!(output.contains("set -e"));
        assert!(output.ends_with("run_all_tests\n"));

        // All test functions should be present
        assert!(output.contains("test_determinism()"));
        assert!(output.contains("test_idempotency()"));
        assert!(output.contains("test_posix_compliance()"));
        assert!(output.contains("test_property_determinism()"));

        // Makefile name should appear
        assert!(output.contains("Makefile.purified"));
    }

    #[test]
    fn test_different_makefile_names() {
        let gen = MakefileTestGenerator::new(MakefileTestGeneratorOptions::default());

        for name in &["Makefile", "GNUmakefile", "makefile.mk", "build.mk"] {
            let path = PathBuf::from(name);
            let output = gen.generate_tests(&path, "");
            assert!(
                output.contains(name),
                "Output should reference makefile name '{name}'"
            );
        }
    }
}