chore/run only tests where changes where #27
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Advent of Code Tests | |
| on: | |
| push: | |
| branches: | |
| - main | |
| - 'chore/**' | |
| pull_request: | |
| branches: | |
| - main | |
| jobs: | |
| detect_changes: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| rust_projects: ${{ steps.filter.outputs.rust_projects }} | |
| go_projects: ${{ steps.filter.outputs.go_projects }} | |
| csharp_projects: ${{ steps.filter.outputs.csharp_projects }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Detect changed projects | |
| id: filter | |
| run: | | |
| # Get changed files | |
| if [ "${{ github.event_name }}" == "pull_request" ]; then | |
| CHANGED_FILES=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }}) | |
| else | |
| CHANGED_FILES=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }}) | |
| fi | |
| echo "Changed files:" | |
| echo "$CHANGED_FILES" | |
| # Define all projects | |
| ALL_RUST_PROJECTS=("2015" "2018" "2019/aoc_rust" "2020/01" "2021/_1" "2021/_2" "2021/_3" "2021/_4" "2021/_5" "2021/_6" "2021/_7" "2022") | |
| ALL_GO_PROJECTS=("2017" "2019/go" "2020/02" "2024/golang") | |
| ALL_CSHARP_PROJECTS=("2021/cs/1" "2021/cs/2") | |
| # Check for workflow changes - if workflow changed, run all tests | |
| if echo "$CHANGED_FILES" | grep -q "^.github/workflows/tests.yml"; then | |
| echo "Workflow changed, running all tests" | |
| RUST_PROJECTS=$(printf '%s\n' "${ALL_RUST_PROJECTS[@]}" | jq -R . | jq -s -c .) | |
| GO_PROJECTS=$(printf '%s\n' "${ALL_GO_PROJECTS[@]}" | jq -R . | jq -s -c .) | |
| CSHARP_PROJECTS=$(printf '%s\n' "${ALL_CSHARP_PROJECTS[@]}" | jq -R . | jq -s -c .) | |
| else | |
| # Detect Rust projects | |
| RUST_PROJECTS=() | |
| for project in "${ALL_RUST_PROJECTS[@]}"; do | |
| if echo "$CHANGED_FILES" | grep -q "^${project}/"; then | |
| RUST_PROJECTS+=("$project") | |
| fi | |
| done | |
| # Detect Go projects | |
| GO_PROJECTS=() | |
| for project in "${ALL_GO_PROJECTS[@]}"; do | |
| if echo "$CHANGED_FILES" | grep -q "^${project}/"; then | |
| GO_PROJECTS+=("$project") | |
| fi | |
| done | |
| # Detect C# projects | |
| CSHARP_PROJECTS=() | |
| for project in "${ALL_CSHARP_PROJECTS[@]}"; do | |
| if echo "$CHANGED_FILES" | grep -q "^${project}/"; then | |
| CSHARP_PROJECTS+=("$project") | |
| fi | |
| done | |
| RUST_PROJECTS=$(printf '%s\n' "${RUST_PROJECTS[@]}" | jq -R . | jq -s -c .) | |
| GO_PROJECTS=$(printf '%s\n' "${GO_PROJECTS[@]}" | jq -R . | jq -s -c .) | |
| CSHARP_PROJECTS=$(printf '%s\n' "${CSHARP_PROJECTS[@]}" | jq -R . | jq -s -c .) | |
| fi | |
| echo "rust_projects=$RUST_PROJECTS" >> $GITHUB_OUTPUT | |
| echo "go_projects=$GO_PROJECTS" >> $GITHUB_OUTPUT | |
| echo "csharp_projects=$CSHARP_PROJECTS" >> $GITHUB_OUTPUT | |
| echo "Rust projects to test: $RUST_PROJECTS" | |
| echo "Go projects to test: $GO_PROJECTS" | |
| echo "C# projects to test: $CSHARP_PROJECTS" | |
| rust_tests: | |
| needs: detect_changes | |
| if: needs.detect_changes.outputs.rust_projects != '[]' | |
| runs-on: ubuntu-latest | |
| continue-on-error: true | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| project: ${{ fromJson(needs.detect_changes.outputs.rust_projects) }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Install Rust Stable | |
| uses: dtolnay/rust-toolchain@stable | |
| if: ${{ matrix.project != '2021/_4' }} | |
| - name: Install Rust Nightly for 2021/_4 | |
| uses: dtolnay/rust-toolchain@nightly | |
| if: ${{ matrix.project == '2021/_4' }} | |
| - name: Set up cargo-nextest | |
| uses: taiki-e/install-action@nextest | |
| - name: Build 2015 binary | |
| if: ${{ matrix.project == '2015' }} | |
| run: cargo build --release | |
| working-directory: ${{ matrix.project }} | |
| - name: Run Rust tests in 2015 with cargo-nextest | |
| if: ${{ matrix.project == '2015' }} | |
| run: cargo nextest run | |
| working-directory: ${{ matrix.project }} | |
| - name: Run Rust tests in ${{ matrix.project }} | |
| if: ${{ matrix.project != '2015' }} | |
| run: cargo test | |
| working-directory: ${{ matrix.project }} | |
| go_tests: | |
| needs: detect_changes | |
| if: needs.detect_changes.outputs.go_projects != '[]' | |
| runs-on: ubuntu-latest | |
| continue-on-error: true | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| project: ${{ fromJson(needs.detect_changes.outputs.go_projects) }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Go | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version: '^1.18' | |
| - name: Run Go tests in 2017 | |
| if: ${{ matrix.project == '2017' }} | |
| run: go test ./... | |
| working-directory: 2017 | |
| - name: Run Go tests in 2019/go | |
| if: ${{ matrix.project == '2019/go' }} | |
| run: go test ./cmd/... | |
| working-directory: 2019/go | |
| - name: Run Go tests in 2020/02 | |
| if: ${{ matrix.project == '2020/02' }} | |
| run: go test ./... | |
| working-directory: 2020/02 | |
| - name: Run Go tests in 2024/golang | |
| if: ${{ matrix.project == '2024/golang' }} | |
| run: go test ./cmd/... | |
| working-directory: 2024/golang | |
| csharp_tests: | |
| needs: detect_changes | |
| if: needs.detect_changes.outputs.csharp_projects != '[]' | |
| runs-on: ubuntu-latest | |
| continue-on-error: true | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| project: ${{ fromJson(needs.detect_changes.outputs.csharp_projects) }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up .NET | |
| uses: actions/setup-dotnet@v4 | |
| with: | |
| dotnet-version: '6.0.x' | |
| - name: Restore dependencies for ${{ matrix.project }} | |
| run: dotnet restore | |
| working-directory: ${{ matrix.project }} | |
| - name: Run C# tests in ${{ matrix.project }} | |
| run: dotnet test | |
| working-directory: ${{ matrix.project }} |