|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# This script validates that solution.test.ts files follow the expected template. |
| 4 | +# This ensures that test files don't contain hardcoded answers. |
| 5 | +# |
| 6 | +# Usage: |
| 7 | +# bash scripts/validate-test-files.sh [files...] |
| 8 | +# |
| 9 | +# If no files are provided, validates all src/day*/solution.test.ts files. |
| 10 | +# If files are provided, only validates those that match solution.test.ts pattern. |
| 11 | + |
| 12 | +FAILED=0 |
| 13 | +CHECKED=0 |
| 14 | +TEMPLATE='import { readFile } from '\''node:fs/promises'\'' |
| 15 | +import { dirname, join } from '\''node:path'\'' |
| 16 | +import { fileURLToPath } from '\''node:url'\'' |
| 17 | +import { describe, expect, it } from '\''vitest'\'' |
| 18 | +import { part1, part2 } from '\''./solution'\'' |
| 19 | +
|
| 20 | +const __filename = fileURLToPath(import.meta.url) |
| 21 | +const __dirname = dirname(__filename) |
| 22 | +
|
| 23 | +describe('\''Day DAY_NUMBER'\'', () => { |
| 24 | + it('\''should solve part 1'\'', async () => { |
| 25 | + const input = await readFile(join(__dirname, '\''input1.txt'\''), '\''utf-8'\'') |
| 26 | + const expectation = await readFile(join(__dirname, '\''expectation1.txt'\''), '\''utf-8'\'') |
| 27 | +
|
| 28 | + expect(part1(input.trim())).toBe(expectation.trim()) |
| 29 | + }) |
| 30 | +
|
| 31 | + it('\''should solve part 2'\'', async () => { |
| 32 | + const input = await readFile(join(__dirname, '\''input2.txt'\''), '\''utf-8'\'') |
| 33 | + const expectation = await readFile(join(__dirname, '\''expectation2.txt'\''), '\''utf-8'\'') |
| 34 | +
|
| 35 | + expect(part2(input.trim())).toBe(expectation.trim()) |
| 36 | + }) |
| 37 | +}) |
| 38 | +' |
| 39 | + |
| 40 | +validate_file() { |
| 41 | + local file="$1" |
| 42 | + |
| 43 | + # Skip if not a solution.test.ts file |
| 44 | + if [[ ! "$file" =~ solution\.test\.ts$ ]]; then |
| 45 | + return |
| 46 | + fi |
| 47 | + |
| 48 | + # Skip if file doesn't exist (e.g., deleted in the diff) |
| 49 | + if [ ! -f "$file" ]; then |
| 50 | + return |
| 51 | + fi |
| 52 | + |
| 53 | + CHECKED=$((CHECKED + 1)) |
| 54 | + |
| 55 | + # Extract day number from directory name (e.g., day01 -> 1, day12 -> 12) |
| 56 | + DAY_NUM=$(echo "$file" | sed -E 's/.*day0*([0-9]+).*/\1/') |
| 57 | + |
| 58 | + # Create expected content for this day |
| 59 | + EXPECTED=$(echo "$TEMPLATE" | sed "s/DAY_NUMBER/$DAY_NUM/g") |
| 60 | + |
| 61 | + # Get actual file content |
| 62 | + ACTUAL=$(cat "$file") |
| 63 | + |
| 64 | + if [ "$EXPECTED" != "$ACTUAL" ]; then |
| 65 | + echo "❌ $file does not match the expected template" |
| 66 | + echo "This check ensures test files don't contain hardcoded answers." |
| 67 | + echo "" |
| 68 | + echo "Expected content:" |
| 69 | + echo "$EXPECTED" |
| 70 | + echo "" |
| 71 | + echo "Actual content:" |
| 72 | + echo "$ACTUAL" |
| 73 | + FAILED=1 |
| 74 | + else |
| 75 | + echo "✅ $file matches template" |
| 76 | + fi |
| 77 | +} |
| 78 | + |
| 79 | +# If arguments provided, validate only those files |
| 80 | +# Otherwise, validate all solution.test.ts files |
| 81 | +if [ $# -gt 0 ]; then |
| 82 | + for file in "$@"; do |
| 83 | + validate_file "$file" |
| 84 | + done |
| 85 | +else |
| 86 | + for file in src/day*/solution.test.ts; do |
| 87 | + validate_file "$file" |
| 88 | + done |
| 89 | +fi |
| 90 | + |
| 91 | +if [ $CHECKED -eq 0 ]; then |
| 92 | + echo "No solution.test.ts files to validate" |
| 93 | + exit 0 |
| 94 | +fi |
| 95 | + |
| 96 | +if [ $FAILED -eq 1 ]; then |
| 97 | + echo "" |
| 98 | + echo "Some test files don't match the expected template." |
| 99 | + echo "Please ensure test files only contain the standard template without modifications." |
| 100 | + exit 1 |
| 101 | +fi |
0 commit comments