|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Tests for validate-frontmatter.sh |
| 5 | +# Runs validation logic against fixture files without git |
| 6 | + |
| 7 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 8 | +FIXTURES_DIR="$SCRIPT_DIR/test-fixtures/frontmatter" |
| 9 | +PASSED=0 |
| 10 | +FAILED=0 |
| 11 | + |
| 12 | +# Create fixtures directory |
| 13 | +mkdir -p "$FIXTURES_DIR" |
| 14 | + |
| 15 | +# Helper to run validation on a single file |
| 16 | +validate_file() { |
| 17 | + local file="$1" |
| 18 | + local ERRORS=0 |
| 19 | + |
| 20 | + FRONTMATTER=$(awk '/^---$/{if(++c==2)exit; next}c==1' "$file") |
| 21 | + |
| 22 | + if [ -z "$FRONTMATTER" ]; then |
| 23 | + echo "no-frontmatter" |
| 24 | + return |
| 25 | + fi |
| 26 | + |
| 27 | + if ! echo "$FRONTMATTER" | grep -qE '^title:'; then |
| 28 | + ERRORS=$((ERRORS + 1)) |
| 29 | + fi |
| 30 | + |
| 31 | + if ! echo "$FRONTMATTER" | grep -qE '^description:'; then |
| 32 | + ERRORS=$((ERRORS + 1)) |
| 33 | + fi |
| 34 | + |
| 35 | + if ! echo "$FRONTMATTER" | grep -qE '^keywords:'; then |
| 36 | + ERRORS=$((ERRORS + 1)) |
| 37 | + else |
| 38 | + KEYWORDS_COUNT=$(echo "$FRONTMATTER" | awk '/^keywords:/{f=1;next} f && /^[a-z]/{exit} f && /^ *-/' | wc -l | tr -d ' ') |
| 39 | + if [ "$KEYWORDS_COUNT" -lt 1 ]; then |
| 40 | + ERRORS=$((ERRORS + 1)) |
| 41 | + elif [ "$KEYWORDS_COUNT" -gt 3 ]; then |
| 42 | + ERRORS=$((ERRORS + 1)) |
| 43 | + fi |
| 44 | + fi |
| 45 | + |
| 46 | + echo "$ERRORS" |
| 47 | +} |
| 48 | + |
| 49 | +# Test helper |
| 50 | +test_case() { |
| 51 | + local name="$1" |
| 52 | + local expected="$2" |
| 53 | + local file="$3" |
| 54 | + |
| 55 | + result=$(validate_file "$file") |
| 56 | + if [ "$result" = "$expected" ]; then |
| 57 | + echo "✓ $name" |
| 58 | + PASSED=$((PASSED + 1)) |
| 59 | + else |
| 60 | + echo "✗ $name (expected: $expected, got: $result)" |
| 61 | + FAILED=$((FAILED + 1)) |
| 62 | + fi |
| 63 | +} |
| 64 | + |
| 65 | +echo "Creating test fixtures..." |
| 66 | + |
| 67 | +# Valid file with all fields |
| 68 | +cat > "$FIXTURES_DIR/valid.md" << 'EOF' |
| 69 | +--- |
| 70 | +title: Valid Page |
| 71 | +description: This is a valid page with all required fields |
| 72 | +keywords: |
| 73 | + - test |
| 74 | + - valid |
| 75 | +--- |
| 76 | +
|
| 77 | +# Content |
| 78 | +EOF |
| 79 | + |
| 80 | +# Valid with 1 keyword |
| 81 | +cat > "$FIXTURES_DIR/valid-1-keyword.md" << 'EOF' |
| 82 | +--- |
| 83 | +title: Valid Page |
| 84 | +description: This is valid |
| 85 | +keywords: |
| 86 | + - single |
| 87 | +--- |
| 88 | +
|
| 89 | +# Content |
| 90 | +EOF |
| 91 | + |
| 92 | +# Valid with 3 keywords |
| 93 | +cat > "$FIXTURES_DIR/valid-3-keywords.md" << 'EOF' |
| 94 | +--- |
| 95 | +title: Valid Page |
| 96 | +description: This is valid |
| 97 | +keywords: |
| 98 | + - one |
| 99 | + - two |
| 100 | + - three |
| 101 | +--- |
| 102 | +
|
| 103 | +# Content |
| 104 | +EOF |
| 105 | + |
| 106 | +# Missing description |
| 107 | +cat > "$FIXTURES_DIR/missing-description.md" << 'EOF' |
| 108 | +--- |
| 109 | +title: Missing Description |
| 110 | +keywords: |
| 111 | + - test |
| 112 | +--- |
| 113 | +
|
| 114 | +# Content |
| 115 | +EOF |
| 116 | + |
| 117 | +# Missing keywords |
| 118 | +cat > "$FIXTURES_DIR/missing-keywords.md" << 'EOF' |
| 119 | +--- |
| 120 | +title: Missing Keywords |
| 121 | +description: No keywords here |
| 122 | +--- |
| 123 | +
|
| 124 | +# Content |
| 125 | +EOF |
| 126 | + |
| 127 | +# Missing title |
| 128 | +cat > "$FIXTURES_DIR/missing-title.md" << 'EOF' |
| 129 | +--- |
| 130 | +description: No title here |
| 131 | +keywords: |
| 132 | + - test |
| 133 | +--- |
| 134 | +
|
| 135 | +# Content |
| 136 | +EOF |
| 137 | + |
| 138 | +# Too many keywords (4) |
| 139 | +cat > "$FIXTURES_DIR/too-many-keywords.md" << 'EOF' |
| 140 | +--- |
| 141 | +title: Too Many Keywords |
| 142 | +description: Has 4 keywords |
| 143 | +keywords: |
| 144 | + - one |
| 145 | + - two |
| 146 | + - three |
| 147 | + - four |
| 148 | +--- |
| 149 | +
|
| 150 | +# Content |
| 151 | +EOF |
| 152 | + |
| 153 | +# Empty keywords array |
| 154 | +cat > "$FIXTURES_DIR/empty-keywords.md" << 'EOF' |
| 155 | +--- |
| 156 | +title: Empty Keywords |
| 157 | +description: Keywords field exists but empty |
| 158 | +keywords: |
| 159 | +--- |
| 160 | +
|
| 161 | +# Content |
| 162 | +EOF |
| 163 | + |
| 164 | +# No frontmatter |
| 165 | +cat > "$FIXTURES_DIR/no-frontmatter.md" << 'EOF' |
| 166 | +# Just Content |
| 167 | +
|
| 168 | +No frontmatter here |
| 169 | +EOF |
| 170 | + |
| 171 | +# All fields missing |
| 172 | +cat > "$FIXTURES_DIR/all-missing.md" << 'EOF' |
| 173 | +--- |
| 174 | +layout: page |
| 175 | +--- |
| 176 | +
|
| 177 | +# Content |
| 178 | +EOF |
| 179 | + |
| 180 | +echo "" |
| 181 | +echo "Running tests..." |
| 182 | +echo "" |
| 183 | + |
| 184 | +# Run tests |
| 185 | +test_case "valid file with all fields" "0" "$FIXTURES_DIR/valid.md" |
| 186 | +test_case "valid with 1 keyword" "0" "$FIXTURES_DIR/valid-1-keyword.md" |
| 187 | +test_case "valid with 3 keywords" "0" "$FIXTURES_DIR/valid-3-keywords.md" |
| 188 | +test_case "missing description" "1" "$FIXTURES_DIR/missing-description.md" |
| 189 | +test_case "missing keywords" "1" "$FIXTURES_DIR/missing-keywords.md" |
| 190 | +test_case "missing title" "1" "$FIXTURES_DIR/missing-title.md" |
| 191 | +test_case "too many keywords (4)" "1" "$FIXTURES_DIR/too-many-keywords.md" |
| 192 | +test_case "empty keywords array" "1" "$FIXTURES_DIR/empty-keywords.md" |
| 193 | +test_case "no frontmatter" "no-frontmatter" "$FIXTURES_DIR/no-frontmatter.md" |
| 194 | +test_case "all fields missing" "3" "$FIXTURES_DIR/all-missing.md" |
| 195 | + |
| 196 | +echo "" |
| 197 | +echo "Results: $PASSED passed, $FAILED failed" |
| 198 | + |
| 199 | +# Cleanup |
| 200 | +rm -rf "$FIXTURES_DIR" |
| 201 | + |
| 202 | +if [ $FAILED -gt 0 ]; then |
| 203 | + exit 1 |
| 204 | +fi |
0 commit comments