/ tests / test_keep_going.sh
test_keep_going.sh
  1  #!/usr/bin/env bash
  2  # Test keep-going functionality
  3  
  4  set -euo pipefail
  5  
  6  SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  7  REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
  8  
  9  # Create a test Barf file for keep-going tests
 10  TEST_DIR=$(mktemp -d)
 11  trap 'rm -rf "$TEST_DIR"' EXIT
 12  
 13  cd "$TEST_DIR"
 14  mkdir -p Bar.d  # Create Bar.d so bar recognizes this as the toplevel
 15  # Symlink necessary Bar.d modules
 16  ln -s "$REPO_ROOT/Bar.d/std_lib" Bar.d/std_lib
 17  ln -s "$REPO_ROOT/Bar.d/rule_lib" Bar.d/rule_lib
 18  ln -s "$REPO_ROOT/Bar.d/tty_lib" Bar.d/tty_lib
 19  # Create dummy _rules file to satisfy glob expansion
 20  touch Bar.d/dummy_rules
 21  
 22  # Test 1: Keep-going disabled (default) - stops at first failure
 23  test_keep_going_disabled() {
 24      echo "Test 1: Keep-going disabled (default)"
 25      
 26      cat > Barf <<'EOF'
 27  rule test1: --pure -- 'echo "Task 1"; true'
 28  rule test1: --pure -- 'echo "Task 2"; false'
 29  rule test1: --pure -- 'echo "Task 3"; true'
 30  EOF
 31      
 32      local output
 33      if output=$("$REPO_ROOT/bar" --bare test1 2>&1); then
 34          echo "ERROR: test1 should have failed"
 35          return 1
 36      fi
 37      
 38      # Should only see task1 and task2 (stops at task2 failure)
 39      if ! echo "$output" | grep -q "Task 1"; then
 40          echo "ERROR: Task 1 should have executed"
 41          return 1
 42      fi
 43      if ! echo "$output" | grep -q "Task 2"; then
 44          echo "ERROR: Task 2 should have executed"
 45          return 1
 46      fi
 47      if echo "$output" | grep -q "Task 3"; then
 48          echo "ERROR: Task 3 should NOT have executed (stopped at failure)"
 49          return 1
 50      fi
 51      
 52      echo "✓ Keep-going disabled works correctly"
 53      rm Barf
 54  }
 55  
 56  # Test 2: Keep-going enabled - continues through pure failures
 57  test_keep_going_enabled() {
 58      echo "Test 2: Keep-going enabled with pure clauses"
 59      
 60      cat > Barf <<'EOF'
 61  rule test2: --pure -- 'echo "Task 1"; true'
 62  rule test2: --pure -- 'echo "Task 2"; false'
 63  rule test2: --pure -- 'echo "Task 3"; true'
 64  EOF
 65      
 66      local output
 67      if output=$(BAR_KEEP_GOING=yes "$REPO_ROOT/bar" --bare test2 2>&1); then
 68          echo "ERROR: test2 should have failed (keep-going propagates failure)"
 69          return 1
 70      fi
 71      
 72      # All three tasks should execute
 73      if ! echo "$output" | grep -q "Task 1"; then
 74          echo "ERROR: Task 1 should have executed"
 75          return 1
 76      fi
 77      if ! echo "$output" | grep -q "Task 2"; then
 78          echo "ERROR: Task 2 should have executed"
 79          return 1
 80      fi
 81      if ! echo "$output" | grep -q "Task 3"; then
 82          echo "ERROR: Task 3 should have executed (keep-going)"
 83          return 1
 84      fi
 85      
 86      echo "✓ Keep-going enabled works correctly"
 87      rm Barf
 88  }
 89  
 90  # Test 3: Non-pure clause stops keep-going
 91  test_nonpure_stops_keepgoing() {
 92      echo "Test 3: Non-pure clause halts keep-going"
 93      
 94      cat > Barf <<'EOF'
 95  rule test3: --pure -- 'echo "Task 1"; true'
 96  rule test3: --pure -- 'echo "Task 2"; false'
 97  rule test3: -- 'echo "Task 3"; true'
 98  EOF
 99      
100      local output
101      if output=$(BAR_KEEP_GOING=yes "$REPO_ROOT/bar" --bare test3 2>&1); then
102          echo "ERROR: test3 should have failed"
103          return 1
104      fi
105      
106      # Tasks 1 and 2 execute, but 3 does not (non-pure)
107      if ! echo "$output" | grep -q "Task 1"; then
108          echo "ERROR: Task 1 should have executed"
109          return 1
110      fi
111      if ! echo "$output" | grep -q "Task 2"; then
112          echo "ERROR: Task 2 should have executed"
113          return 1
114      fi
115      if echo "$output" | grep -q "Task 3"; then
116          echo "ERROR: Task 3 should NOT have executed (non-pure stops keep-going)"
117          return 1
118      fi
119      
120      echo "✓ Non-pure clause stops keep-going correctly"
121      rm Barf
122  }
123  
124  # Test 4: --conclusive takes precedence over keep-going
125  test_conclusive_precedence() {
126      echo "Test 4: --conclusive takes precedence"
127      
128      cat > Barf <<'EOF'
129  rule test4: --pure -- 'echo "Task 1"; true'
130  rule test4: --pure --conclusive -- 'echo "Task 2"; false'
131  rule test4: --pure -- 'echo "Task 3"; true'
132  EOF
133      
134      local output
135      if output=$(BAR_KEEP_GOING=yes "$REPO_ROOT/bar" --bare test4 2>&1); then
136          echo "ERROR: test4 should have failed"
137          return 1
138      fi
139      
140      # Tasks 1 and 2 execute, but 3 does not (conclusive)
141      if ! echo "$output" | grep -q "Task 1"; then
142          echo "ERROR: Task 1 should have executed"
143          return 1
144      fi
145      if ! echo "$output" | grep -q "Task 2"; then
146          echo "ERROR: Task 2 should have executed"
147          return 1
148      fi
149      if echo "$output" | grep -q "Task 3"; then
150          echo "ERROR: Task 3 should NOT have executed (conclusive failure)"
151          return 1
152      fi
153      
154      echo "✓ --conclusive precedence works correctly"
155      rm Barf
156  }
157  
158  # Test 5: Mixed scenario - realistic use case
159  test_mixed_realistic() {
160      echo "Test 5: Mixed realistic scenario"
161      
162      cat > Barf <<'EOF'
163  rule test5: --pure -- 'echo "A"; true'
164  rule test5: --pure -- 'echo "B"; false'
165  rule test5: --pure -- 'echo "C"; true'
166  rule test5: --pure -- 'echo "D"; false'
167  rule test5: --pure -- 'echo "E"; true'
168  EOF
169      
170      local output
171      if output=$(BAR_KEEP_GOING=yes "$REPO_ROOT/bar" --bare test5 2>&1); then
172          echo "ERROR: test5 should have failed"
173          return 1
174      fi
175      
176      # All tasks should execute
177      for task in A B C D E; do
178          if ! echo "$output" | grep -q "$task"; then
179              echo "ERROR: Task $task should have executed"
180              echo "Output: $output"
181              return 1
182          fi
183      done
184      
185      echo "✓ Mixed realistic scenario works correctly"
186      rm Barf
187  }
188  
189  # Run all tests
190  main() {
191      echo "=== Testing Keep-Going Functionality ==="
192      echo
193      
194      test_keep_going_disabled
195      echo
196      
197      test_keep_going_enabled
198      echo
199      
200      test_nonpure_stops_keepgoing
201      echo
202      
203      test_conclusive_precedence
204      echo
205      
206      test_mixed_realistic
207      echo
208      
209      echo "=== All Keep-Going Tests Passed ==="
210  }
211  
212  main "$@"