|
| 1 | +#!/bin/bash |
| 2 | +cd $(dirname $0) |
| 3 | +source "../utils.sh" |
| 4 | +cd ../../testrepo |
| 5 | + |
| 6 | +bold "Test: Watcher keeps watching a folder when an overlapping source folder is removed" |
| 7 | + |
| 8 | +error_output=$(rewatch clean 2>&1) |
| 9 | +if [ $? -eq 0 ]; |
| 10 | +then |
| 11 | + success "Repo Cleaned" |
| 12 | +else |
| 13 | + error "Error Cleaning Repo" |
| 14 | + printf "%s\n" "$error_output" >&2 |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +DEP01_CONFIG=packages/dep01/rescript.json |
| 19 | +RETAINED=packages/dep01/src/retained |
| 20 | + |
| 21 | +cleanup() { |
| 22 | + git restore --worktree -- "$DEP01_CONFIG" |
| 23 | + rm -rf "$RETAINED" |
| 24 | + rm -f rewatch.log |
| 25 | +} |
| 26 | + |
| 27 | +# src/retained is watched both through the recursive src folder and as a |
| 28 | +# source folder of its own |
| 29 | +mkdir -p "$RETAINED" |
| 30 | +echo 'let value = 1' > "$RETAINED/Retained.res" |
| 31 | +node -e " |
| 32 | + const fs = require('fs'); |
| 33 | + const config = JSON.parse(fs.readFileSync('$DEP01_CONFIG', 'utf8')); |
| 34 | + config.sources = [{dir: 'src', subdirs: true}, 'src/retained']; |
| 35 | + fs.writeFileSync('$DEP01_CONFIG', JSON.stringify(config, null, 2) + '\n'); |
| 36 | +" |
| 37 | + |
| 38 | +retained_output() { |
| 39 | + find "$RETAINED" -name 'Retained.*js' | head -n 1 |
| 40 | +} |
| 41 | + |
| 42 | +rewatch_bg watch > rewatch.log 2>&1 & |
| 43 | +success "Watcher Started" |
| 44 | + |
| 45 | +wait_for_retained_value() { |
| 46 | + local value=$1 |
| 47 | + local timeout=30 |
| 48 | + while [ "$timeout" -gt 0 ]; do |
| 49 | + output=$(retained_output) |
| 50 | + if [ -n "$output" ] && grep -q "$value" "$output"; then |
| 51 | + return 0 |
| 52 | + fi |
| 53 | + sleep 1 |
| 54 | + timeout=$((timeout - 1)) |
| 55 | + done |
| 56 | + return 1 |
| 57 | +} |
| 58 | + |
| 59 | +if ! wait_for_retained_value "= 1"; then |
| 60 | + error "Initial build did not compile the retained folder" |
| 61 | + cat rewatch.log |
| 62 | + exit_watcher |
| 63 | + cleanup |
| 64 | + exit 1 |
| 65 | +fi |
| 66 | +success "Initial build completed" |
| 67 | + |
| 68 | +completed_builds=$(grep -c "Finished .*compilation" rewatch.log 2>/dev/null || true) |
| 69 | + |
| 70 | +# Remove the recursive src folder while keeping src itself (non-recursive) |
| 71 | +# and src/retained |
| 72 | +node -e " |
| 73 | + const fs = require('fs'); |
| 74 | + const config = JSON.parse(fs.readFileSync('$DEP01_CONFIG', 'utf8')); |
| 75 | + config.sources = ['src', 'src/retained']; |
| 76 | + fs.writeFileSync('$DEP01_CONFIG', JSON.stringify(config, null, 2) + '\n'); |
| 77 | +" |
| 78 | +if ! wait_for_pattern_count rewatch.log "Finished .*compilation" \ |
| 79 | + $((completed_builds + 1)) 30; then |
| 80 | + error "No rebuild after the configuration change" |
| 81 | + cat rewatch.log |
| 82 | + exit_watcher |
| 83 | + cleanup |
| 84 | + exit 1 |
| 85 | +fi |
| 86 | + |
| 87 | +echo 'let value = 2' > "$RETAINED/Retained.res" |
| 88 | +if wait_for_retained_value "= 2"; then |
| 89 | + success "Edit in the retained folder was compiled" |
| 90 | +else |
| 91 | + error "Edit in the retained folder was not picked up" |
| 92 | + cat rewatch.log |
| 93 | + exit_watcher |
| 94 | + cleanup |
| 95 | + exit 1 |
| 96 | +fi |
| 97 | + |
| 98 | +if ! exit_watcher; then |
| 99 | + cleanup |
| 100 | + exit 1 |
| 101 | +fi |
| 102 | +cleanup |
| 103 | + |
| 104 | +# Rebuild to regenerate the outputs removed by `rewatch clean` |
| 105 | +rewatch build > /dev/null 2>&1 |
| 106 | + |
| 107 | +if git diff --exit-code . > /dev/null 2>&1 && [ -z "$(git ls-files --others --exclude-standard .)" ]; |
| 108 | +then |
| 109 | + success "No leftover changes" |
| 110 | +else |
| 111 | + error "Leftover changes detected" |
| 112 | + git diff . |
| 113 | + git ls-files --others --exclude-standard . |
| 114 | + exit 1 |
| 115 | +fi |
0 commit comments