|
| 1 | +#!/bin/sh |
| 2 | +# Test for source database refresh optimization. |
| 3 | +# |
| 4 | +# This test directory has a catch-all default.do that handles build/* |
| 5 | +# targets and errors on everything else. The test verifies that |
| 6 | +# initializeSourceDatabase is skipped for unchanged source files |
| 7 | +# (the DB directory is not deleted and recreated unnecessarily), |
| 8 | +# while still refreshing when source files actually change. |
| 9 | + |
| 10 | +set -e |
| 11 | + |
| 12 | +############################################################################## |
| 13 | +# Helpers |
| 14 | +############################################################################## |
| 15 | +get_db_dir() { |
| 16 | + DB_KEY=$(printf '%s' "$1" | md5sum | awk '{print toupper($1)}') |
| 17 | + echo "$HOME/.redo/database/$(echo $DB_KEY | cut -c1-3)/$(echo $DB_KEY | cut -c4-9)/$(echo $DB_KEY | cut -c10-21)/$(echo $DB_KEY | cut -c22-)" |
| 18 | +} |
| 19 | +get_stamp_dir() { |
| 20 | + DB_KEY=$(printf '%s' "$1" | md5sum | awk '{print toupper($1)}') |
| 21 | + echo "$HOME/.redo/stamps/$(echo $DB_KEY | cut -c1-3)/$(echo $DB_KEY | cut -c4-9)/$(echo $DB_KEY | cut -c10-21)/$(echo $DB_KEY | cut -c22-)" |
| 22 | +} |
| 23 | + |
| 24 | +############################################################################## |
| 25 | +# Setup |
| 26 | +############################################################################## |
| 27 | +mkdir -p src build |
| 28 | +SRC_PATH="$(cd src && pwd)/data.txt" |
| 29 | +BUILD_PATH="$(cd build && pwd)/data.txt" |
| 30 | +rm -rf "$(get_db_dir "$SRC_PATH")" "$(get_stamp_dir "$SRC_PATH")" |
| 31 | +rm -rf "$(get_db_dir "$BUILD_PATH")" "$(get_stamp_dir "$BUILD_PATH")" |
| 32 | +../flush-cache |
| 33 | + |
| 34 | +echo "content v1" > src/data.txt |
| 35 | + |
| 36 | +############################################################################## |
| 37 | +# Initial build — creates source DB with stamp |
| 38 | +############################################################################## |
| 39 | +redo-ifchange build/data.txt |
| 40 | +test "$(cat build/data.txt)" = "built from: content v1" || exit 1 |
| 41 | + |
| 42 | +SRC_DB=$(get_db_dir "$SRC_PATH") |
| 43 | +test -d "$SRC_DB/y" || exit 2 |
| 44 | + |
| 45 | +# Record the DB directory inode to detect if it gets recreated |
| 46 | +INODE_BEFORE=$(stat -c %i "$SRC_DB" 2>/dev/null || stat -f %i "$SRC_DB" 2>/dev/null) |
| 47 | + |
| 48 | +############################################################################## |
| 49 | +# Rebuild with no changes — DB should NOT be recreated |
| 50 | +############################################################################## |
| 51 | +../flush-cache |
| 52 | +redo-ifchange build/data.txt |
| 53 | + |
| 54 | +test -d "$SRC_DB/y" || exit 3 |
| 55 | + |
| 56 | +INODE_AFTER=$(stat -c %i "$SRC_DB" 2>/dev/null || stat -f %i "$SRC_DB" 2>/dev/null) |
| 57 | +if [ "$INODE_BEFORE" != "$INODE_AFTER" ]; then |
| 58 | + echo "FAIL: source DB was recreated on unchanged rebuild (inode $INODE_BEFORE -> $INODE_AFTER)" >&2 |
| 59 | + exit 4 |
| 60 | +fi |
| 61 | + |
| 62 | +############################################################################## |
| 63 | +# Modify source — DB should refresh and target should rebuild |
| 64 | +############################################################################## |
| 65 | +../sleep |
| 66 | +echo "content v2" > src/data.txt |
| 67 | +../flush-cache |
| 68 | + |
| 69 | +redo-ifchange build/data.txt |
| 70 | +test "$(cat build/data.txt)" = "built from: content v2" || exit 5 |
| 71 | +test -d "$SRC_DB/y" || exit 6 |
| 72 | + |
| 73 | +echo "PASS: source DB skip optimization" >&2 |
| 74 | +rm -rf src build |
0 commit comments