Skip to content

Commit dc90558

Browse files
committed
feat: add GitHub workflow
1 parent 1c91b8f commit dc90558

File tree

4 files changed

+175
-1
lines changed

4 files changed

+175
-1
lines changed

.github/workflows/check.yaml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Check
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
check:
11+
if: ${{ !github.event.repository.is_template }}
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout repository
16+
uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 0
19+
20+
- name: Setup pnpm
21+
uses: pnpm/action-setup@v4
22+
with:
23+
version: 10
24+
25+
- name: Setup Node.js
26+
uses: actions/setup-node@v4
27+
with:
28+
node-version: 24
29+
cache: 'pnpm'
30+
31+
- name: Install dependencies
32+
run: pnpm install --frozen-lockfile
33+
34+
- name: Run linter
35+
run: pnpm lint
36+
37+
- name: Check formatting
38+
run: pnpm format:check
39+
40+
- name: Get changed files
41+
id: changed-files
42+
run: |
43+
if [ "${{ github.event_name }}" == "pull_request" ]; then
44+
CHANGED=$(git diff --name-only origin/${{ github.base_ref }}...HEAD | tr '\n' ' ')
45+
else
46+
CHANGED=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | tr '\n' ' ')
47+
fi
48+
echo "files=$CHANGED" >> $GITHUB_OUTPUT
49+
50+
- name: Validate test files follow template
51+
run: bash scripts/validate-test-files.sh ${{ steps.changed-files.outputs.files }}

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,28 @@ After generating a day, you can:
7676
- `pnpm format` - Format code with Prettier
7777
- `pnpm format:check` - Check code formatting
7878

79+
## GitHub Actions
80+
81+
This repository includes a GitHub Actions workflow (`.github/workflows/check.yaml`) that automatically runs on pull requests and pushes to the `main` branch. The workflow:
82+
83+
- Runs the linter (`pnpm lint`)
84+
- Checks code formatting (`pnpm format:check`)
85+
- Validates that test files have not been changed to contain hardcoded answers
86+
87+
### Enabling Required Status Checks
88+
89+
To enforce that all checks pass before merging pull requests:
90+
91+
1. Go to your repository **Settings**
92+
1. Navigate to **Rules****Rulesets**
93+
1. Click **New ruleset****New branch ruleset**
94+
1. Configure the ruleset name and target branches (e.g., `main`)
95+
1. Enable **Require status checks to pass**
96+
1. Click **Show additional settings**
97+
1. Under **Status checks that are required**, click **Add checks**
98+
1. Type `check` and select the **check** action
99+
1. Click **Save changes**
100+
79101
## Excluded Files
80102

81103
- Input files (`input*.txt`)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "advent-of-code",
33
"description": "Advent of Code solutions",
4-
"version": "1.1.0",
4+
"version": "1.2.0",
55
"type": "module",
66
"scripts": {
77
"generate-day": "tsx scripts/generate-day.ts",

scripts/validate-test-files.sh

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)