|
| 1 | +#!/bin/bash |
| 2 | +cd $(dirname $0) |
| 3 | +source "../utils.sh" |
| 4 | +cd ../../testrepo |
| 5 | + |
| 6 | +bold "Test: Watcher recovers from an invalid config change" |
| 7 | + |
| 8 | +wait_for_pattern() { |
| 9 | + local file="$1" pattern="$2" timeout="${3:-30}" |
| 10 | + while [ "$timeout" -gt 0 ]; do |
| 11 | + grep -q "$pattern" "$file" 2> /dev/null && return 0 |
| 12 | + sleep 1 |
| 13 | + timeout=$((timeout - 1)) |
| 14 | + done |
| 15 | + return 1 |
| 16 | +} |
| 17 | + |
| 18 | +wait_for_pattern_count() { |
| 19 | + local file="$1" pattern="$2" expected="$3" timeout="${4:-30}" |
| 20 | + while [ "$timeout" -gt 0 ]; do |
| 21 | + local count |
| 22 | + count=$(grep -c "$pattern" "$file" 2> /dev/null || true) |
| 23 | + [ "$count" -ge "$expected" ] && return 0 |
| 24 | + sleep 1 |
| 25 | + timeout=$((timeout - 1)) |
| 26 | + done |
| 27 | + return 1 |
| 28 | +} |
| 29 | + |
| 30 | +rewatch clean > /dev/null 2>&1 |
| 31 | +cp rescript.json rescript.json.watch-recovery.bak |
| 32 | +rewatch_bg watch > rewatch.log 2>&1 & |
| 33 | + |
| 34 | +if ! wait_for_file "./src/Test.mjs" 20 || ! wait_for_file "lib/watch.lock" 20; then |
| 35 | + error "Initial watch build did not complete" |
| 36 | + cat rewatch.log |
| 37 | + mv rescript.json.watch-recovery.bak rescript.json |
| 38 | + exit_watcher |
| 39 | + exit 1 |
| 40 | +fi |
| 41 | + |
| 42 | +initial_completed=$(grep -c "Finished compilation" rewatch.log 2> /dev/null || true) |
| 43 | +printf '{"name":' > rescript.json |
| 44 | + |
| 45 | +if ! wait_for_pattern rewatch.log "Could not initialize build" 30; then |
| 46 | + error "Watcher did not report the invalid config" |
| 47 | + cat rewatch.log |
| 48 | + mv rescript.json.watch-recovery.bak rescript.json |
| 49 | + exit_watcher |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | + |
| 53 | +watcher_pid=$(cat lib/watch.lock) |
| 54 | +if ! kill -0 "$watcher_pid" 2> /dev/null; then |
| 55 | + error "Watcher exited after the invalid config" |
| 56 | + cat rewatch.log |
| 57 | + mv rescript.json.watch-recovery.bak rescript.json |
| 58 | + exit 1 |
| 59 | +fi |
| 60 | + |
| 61 | +node -e ' |
| 62 | + const fs = require("fs"); |
| 63 | + const config = JSON.parse(fs.readFileSync(process.argv[1], "utf8")); |
| 64 | + config.suffix = ".recovered.mjs"; |
| 65 | + fs.writeFileSync(process.argv[2], `${JSON.stringify(config, null, 2)}\n`); |
| 66 | +' rescript.json.watch-recovery.bak rescript.json |
| 67 | + |
| 68 | +expected_completed=$((initial_completed + 1)) |
| 69 | +if ! wait_for_pattern_count rewatch.log "Finished compilation" "$expected_completed" 30; then |
| 70 | + error "Watcher did not recover after restoring a valid config" |
| 71 | + cat rewatch.log |
| 72 | + mv rescript.json.watch-recovery.bak rescript.json |
| 73 | + exit_watcher |
| 74 | + exit 1 |
| 75 | +fi |
| 76 | + |
| 77 | +echo '// watch-recovery-test' >> src/Test.res |
| 78 | +if ! wait_for_file "./src/Test.recovered.mjs" 20; then |
| 79 | + error "Recovered watcher did not compile with the restored config" |
| 80 | + cat rewatch.log |
| 81 | + mv rescript.json.watch-recovery.bak rescript.json |
| 82 | + git checkout -- src/Test.res |
| 83 | + exit_watcher |
| 84 | + exit 1 |
| 85 | +fi |
| 86 | + |
| 87 | +exit_watcher |
| 88 | +rewatch clean > /dev/null 2>&1 |
| 89 | +mv rescript.json.watch-recovery.bak rescript.json |
| 90 | +git checkout -- src/Test.res |
| 91 | +rm -f rewatch.log |
| 92 | +rewatch build > /dev/null 2>&1 |
| 93 | + |
| 94 | +if git diff --exit-code . > /dev/null 2>&1 && [ -z "$(git ls-files --others --exclude-standard .)" ]; then |
| 95 | + success "Watcher recovered from invalid config" |
| 96 | +else |
| 97 | + error "Watcher recovery test left repository changes" |
| 98 | + git diff . |
| 99 | + git ls-files --others --exclude-standard . |
| 100 | + exit 1 |
| 101 | +fi |
0 commit comments