|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# run-pkl-recipes.sh |
| 4 | +# |
| 5 | +# This script serves as a test runner for our Pkl templates. It iterates |
| 6 | +# through all recipes, evaluates them, and compares the output to the |
| 7 | +# expected golden file (.result). |
| 8 | + |
| 9 | +set -euo pipefail |
| 10 | + |
| 11 | +# 1. Check if the 'pkl' command is available |
| 12 | +if ! command -v pkl &> /dev/null; then |
| 13 | + echo "Error: The 'pkl' command-line tool is not installed or not in your PATH." >&2 |
| 14 | + echo "Please install it by following the instructions at: https://pkl-lang.org/main/current/pkl-cli/index.html" >&2 |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +echo "Pkl command found. Starting recipe validation..." |
| 19 | +echo "------------------------------------------------" |
| 20 | + |
| 21 | +EXIT_CODE=0 |
| 22 | +RECIPES_DIR="docs/recipes" |
| 23 | + |
| 24 | +# 2. Iterate through all Pkl template files |
| 25 | +for pkl_template in "${RECIPES_DIR}"/*.pkl; do |
| 26 | + base_name=$(basename "$pkl_template" .pkl) |
| 27 | + recipe_name=$(basename "$base_name") |
| 28 | + |
| 29 | + # Define path for the golden file |
| 30 | + golden_file="${RECIPES_DIR}/${base_name}.result" |
| 31 | + |
| 32 | + echo -n "-> Processing recipe: ${recipe_name}... " |
| 33 | + |
| 34 | + # 3. Check if corresponding result file exists |
| 35 | + if [ ! -f "$golden_file" ]; then |
| 36 | + echo "[SKIP] Missing golden file: ${golden_file}" |
| 37 | + continue |
| 38 | + fi |
| 39 | + |
| 40 | + # 4. Evaluate the Pkl template and capture the output |
| 41 | + # - The --module-path flag is CRITICAL. It tells Pkl where to resolve |
| 42 | + # the `modulepath:/` URIs used in the templates. |
| 43 | + # - `-f raw` outputs the raw string value of the 'output.text' property. |
| 44 | + actual_output=$(pkl eval --module-path "${RECIPES_DIR}" "${pkl_template}") |
| 45 | + expected_output=$(cat "$golden_file") |
| 46 | + |
| 47 | + # Normalize line endings for cross-platform compatibility |
| 48 | + actual_normalized=$(echo "$actual_output" | tr -d '\r') |
| 49 | + expected_normalized=$(echo "$expected_output" | tr -d '\r') |
| 50 | + |
| 51 | + # 5. Compare the actual output with the expected golden file |
| 52 | + if [ "$actual_normalized" == "$expected_normalized" ]; then |
| 53 | + echo "[OK]" |
| 54 | + else |
| 55 | + echo "[FAIL]" |
| 56 | + echo " Output did not match the golden file." |
| 57 | + echo " --- DIFF ---" |
| 58 | + diff --unified <(echo "$expected_normalized") <(echo "$actual_normalized") || true |
| 59 | + echo " ------------" |
| 60 | + EXIT_CODE=1 |
| 61 | + fi |
| 62 | +done |
| 63 | + |
| 64 | +echo "------------------------------------------------" |
| 65 | +if [ $EXIT_CODE -eq 0 ]; then |
| 66 | + echo "All Pkl recipes validated successfully." |
| 67 | +else |
| 68 | + echo "One or more Pkl recipes failed validation." |
| 69 | +fi |
| 70 | + |
| 71 | +exit $EXIT_CODE |
0 commit comments