diff --git a/.github/workflows/ci-selection.yml b/.github/workflows/ci-selection.yml new file mode 100644 index 0000000..abf134d --- /dev/null +++ b/.github/workflows/ci-selection.yml @@ -0,0 +1,43 @@ +name: Select relevant checks +on: + workflow_call: + outputs: + host: + value: ${{ jobs.select.outputs.host }} + families: + value: ${{ jobs.select.outputs.families }} + rendering: + value: ${{ jobs.select.outputs.rendering }} + scene: + value: ${{ jobs.select.outputs.scene }} + packages: + value: ${{ jobs.select.outputs.packages }} + compare: + value: ${{ jobs.select.outputs.compare }} + comparisons: + value: ${{ jobs.select.outputs.comparisons }} + plan: + value: ${{ jobs.select.outputs.plan }} +permissions: + contents: read +jobs: + select: + runs-on: ubuntu-24.04 + timeout-minutes: 5 + outputs: + host: ${{ steps.select.outputs.host }} + families: ${{ steps.select.outputs.families }} + rendering: ${{ steps.select.outputs.rendering }} + scene: ${{ steps.select.outputs.scene }} + packages: ${{ steps.select.outputs.packages }} + compare: ${{ steps.select.outputs.compare }} + comparisons: ${{ steps.select.outputs.comparisons }} + plan: ${{ steps.select.outputs.plan }} + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Test selection and required-check gating + run: python3 -m unittest discover -s tests -p test_ci_changes.py + - id: select + run: python3 tools/ci_changes.py diff --git a/.github/workflows/nuget-preview.yml b/.github/workflows/nuget-preview.yml index e911eb5..b91ebec 100644 --- a/.github/workflows/nuget-preview.yml +++ b/.github/workflows/nuget-preview.yml @@ -1,15 +1,9 @@ name: NuGet preview on: pull_request: - paths: - - 'src/**' - - 'eng/**' - - 'tests/nuget_consumer.py' - - 'Directory.Build.props' - - 'LICENSE' - - '.github/workflows/nuget-preview.yml' push: tags: ['nuget-preview-*'] + branches: [main] workflow_dispatch: inputs: publish: @@ -19,7 +13,11 @@ on: permissions: contents: read jobs: + changes: + uses: ./.github/workflows/ci-selection.yml verify: + needs: changes + if: needs.changes.outputs.packages == 'true' runs-on: ubuntu-24.04 timeout-minutes: 20 outputs: diff --git a/.github/workflows/progressive-textures.yml b/.github/workflows/progressive-textures.yml index f2aba39..3732be5 100644 --- a/.github/workflows/progressive-textures.yml +++ b/.github/workflows/progressive-textures.yml @@ -1,10 +1,19 @@ name: Progressive texture contracts -on: [push, pull_request, workflow_dispatch] +on: + push: + branches: [main] + pull_request: + workflow_dispatch: permissions: contents: read jobs: + changes: + uses: ./.github/workflows/ci-selection.yml contracts: + needs: changes + if: needs.changes.outputs.families == 'true' runs-on: ubuntu-24.04 + timeout-minutes: 10 steps: - uses: actions/checkout@v4 - uses: actions/setup-dotnet@v4 @@ -14,6 +23,5 @@ jobs: with: python-version: '3.12' - run: python3 tests/progressive_fixtures.py - - run: python3 -m unittest discover -s tests -p test_progressive_textures.py - run: dotnet run --project tests/TextureFamilies.Tests -c Debug - run: dotnet run --project tests/TextureFamilies.Tests -c Release diff --git a/.github/workflows/validation.yml b/.github/workflows/validation.yml index 3e622c7..d1f78ea 100644 --- a/.github/workflows/validation.yml +++ b/.github/workflows/validation.yml @@ -1,6 +1,7 @@ name: Packed scene in Dolphin on: push: + branches: [main] pull_request: workflow_dispatch: concurrency: @@ -9,19 +10,43 @@ concurrency: permissions: contents: read jobs: - validation: + changes: + uses: ./.github/workflows/ci-selection.yml + + host: + name: host checks + needs: changes + if: needs.changes.outputs.host == 'true' runs-on: ubuntu-24.04 - timeout-minutes: 50 + timeout-minutes: 10 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-dotnet@v4 + with: + dotnet-version: 8.0.x + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - run: python3 tools/test_host.py + + baseline-validation: + name: baseline validation (${{ matrix.configuration }}) + needs: [changes, host] + if: needs.changes.outputs.scene == 'true' + runs-on: ubuntu-24.04 + timeout-minutes: 30 strategy: fail-fast: false matrix: configuration: [Debug, Release] steps: - uses: actions/checkout@v4 + - id: sdk + run: echo "revision=$(cat .sdk-revision)" >> "$GITHUB_OUTPUT" - uses: actions/checkout@v4 with: repository: capz/DolphinDotNet - ref: efda89f3e1ad50fe4cac20124ad8a89f75b1c002 + ref: ${{ steps.sdk.outputs.revision }} path: vendor/sdk - uses: actions/setup-dotnet@v4 with: @@ -29,14 +54,11 @@ jobs: - uses: actions/setup-python@v5 with: python-version: '3.12' - - name: Test ordered mesh pass contracts - run: | - dotnet run --project tests/OrderedMeshPasses.Tests -c Release - dotnet run --project tests/OrderedMeshPasses.Tests -c Release -p:HypercubeNativePasses=false - - name: Build and test packed assets, scheduler and GameCube executable + - name: Build packed assets and GameCube executable env: CONFIGURATION: ${{ matrix.configuration }} FIXTURE_RESOLUTION: '48' + HYPERCUBE_SKIP_HOST_TESTS: '1' run: tools/build_validation.sh - name: Verify native resource API ranges and checksums run: | @@ -45,79 +67,129 @@ jobs: vendor/sdk/tests/native_buffer_crc_test - name: Build Dolphin validation container run: docker build -t hypercube-dolphin -f tools/Dockerfile.dolphin . - - name: Build shared rendering integration sample - env: - CONFIGURATION: ${{ matrix.configuration }} - run: tools/build_rendering.sh - - name: Validate bounded rendering, dirty uploads and cover states in Dolphin - run: >- - docker run --rm --init --shm-size=512m --user "$(id -u):$(id -g)" - --mount "type=bind,src=$PWD,dst=/work" -w /work hypercube-dolphin - python3 tools/rendering_validation.py out/native-rendering-${{ matrix.configuration }}/Game.dol - --logs out/dolphin-rendering-${{ matrix.configuration }} --seconds 35 - name: Boot the DOL and run all eleven scene comparisons run: >- docker run --rm --init --shm-size=512m --user "$(id -u):$(id -g)" --mount "type=bind,src=$PWD,dst=/work" -w /work hypercube-dolphin python3 tools/dolphin_validation.py out/native-${{ matrix.configuration }}/Game.dol --disc out/assets/world.iso --logs out/dolphin-${{ matrix.configuration }} --seconds 240 - - name: Build performance comparison executable - if: matrix.configuration == 'Release' - env: - PERFORMANCE_SUITE: '1' - run: tools/build_validation.sh - - name: Run nine performance comparisons and progress checks - if: matrix.configuration == 'Release' - run: >- - docker run --rm --init --shm-size=512m --user "$(id -u):$(id -g)" - --mount "type=bind,src=$PWD,dst=/work" -w /work hypercube-dolphin - python3 tools/dolphin_validation.py out/native-performance-Release/Game.dol - --suite performance --disc out/assets/world.iso - --logs out/dolphin-performance-Release --seconds 240 - - name: Build frame-phase profiling executable - if: matrix.configuration == 'Release' - env: - PROFILING_SUITE: '1' - run: tools/build_validation.sh - - name: Run sixteen engine/SDK throughput comparisons - if: matrix.configuration == 'Release' - run: >- - docker run --rm --init --shm-size=512m --user "$(id -u):$(id -g)" - --mount "type=bind,src=$PWD,dst=/work" -w /work hypercube-dolphin - python3 tools/dolphin_validation.py out/native-profiling-Release/Game.dol - --suite profiling --disc out/assets/world.iso - --logs out/dolphin-profiling-Release --seconds 360 - - name: Build SDK follow-up profiling executable - if: matrix.configuration == 'Release' + - name: Upload executable, assets, measurements and rendered evidence + if: always() + uses: actions/upload-artifact@v4 + with: + name: hypercube-validation-${{ matrix.configuration }} + path: | + out/native-*/Game.dol + out/native-*/Game.elf + out/assets/ + out/dolphin-*/*.csv + out/dolphin-*/*.md + out/dolphin-*/*.json + out/dolphin-*/*.png + out/dolphin-*/*.log + out/dolphin-*/user/Logs/ + out/dolphin-*/user/Dump/Audio/ + retention-days: 14 + + rendering: + name: rendering (${{ matrix.configuration }}) + needs: [changes, host] + if: needs.changes.outputs.rendering == 'true' + runs-on: ubuntu-24.04 + timeout-minutes: 20 + strategy: + fail-fast: false + matrix: + configuration: [Debug, Release] + steps: + - uses: actions/checkout@v4 + - id: sdk + run: echo "revision=$(cat .sdk-revision)" >> "$GITHUB_OUTPUT" + - uses: actions/checkout@v4 + with: + repository: capz/DolphinDotNet + ref: ${{ steps.sdk.outputs.revision }} + path: vendor/sdk + - uses: actions/setup-dotnet@v4 + with: + dotnet-version: 8.0.x + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - name: Build shared rendering integration sample env: - SDK_FOLLOWUP_SUITE: '1' - run: tools/build_validation.sh - - name: Run sixteen SDK wait and direct-read comparisons - if: matrix.configuration == 'Release' + CONFIGURATION: ${{ matrix.configuration }} + run: tools/build_rendering.sh + - name: Build Dolphin validation container + run: docker build -t hypercube-dolphin -f tools/Dockerfile.dolphin . + - name: Validate bounded rendering, dirty uploads and cover states in Dolphin run: >- docker run --rm --init --shm-size=512m --user "$(id -u):$(id -g)" --mount "type=bind,src=$PWD,dst=/work" -w /work hypercube-dolphin - python3 tools/dolphin_validation.py out/native-sdk-followup-Release/Game.dol - --suite sdk-followup --disc out/assets/world.iso - --logs out/dolphin-sdk-followup-Release --seconds 360 - - name: Build texture follow-up profiling executable - if: matrix.configuration == 'Release' + python3 tools/rendering_validation.py out/native-rendering-${{ matrix.configuration }}/Game.dol + --logs out/dolphin-rendering-${{ matrix.configuration }} --seconds 35 + - name: Upload executable, assets, measurements and rendered evidence + if: always() + uses: actions/upload-artifact@v4 + with: + name: hypercube-rendering-${{ matrix.configuration }} + path: | + out/native-*/Game.dol + out/native-*/Game.elf + out/assets/ + out/dolphin-*/*.csv + out/dolphin-*/*.md + out/dolphin-*/*.json + out/dolphin-*/*.png + out/dolphin-*/*.log + out/dolphin-*/user/Logs/ + out/dolphin-*/user/Dump/Audio/ + retention-days: 14 + + release-comparisons: + name: release comparisons (${{ matrix.suite }}) + needs: [changes, host] + if: needs.changes.outputs.compare == 'true' + runs-on: ubuntu-24.04 + timeout-minutes: 30 + strategy: + fail-fast: false + matrix: ${{ fromJSON(needs.changes.outputs.comparisons) }} + steps: + - uses: actions/checkout@v4 + - id: sdk + run: echo "revision=$(cat .sdk-revision)" >> "$GITHUB_OUTPUT" + - uses: actions/checkout@v4 + with: + repository: capz/DolphinDotNet + ref: ${{ steps.sdk.outputs.revision }} + path: vendor/sdk + - uses: actions/setup-dotnet@v4 + with: + dotnet-version: 8.0.x + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - name: Build Release comparison executable env: - TEXTURE_FOLLOWUP_SUITE: '1' - run: tools/build_validation.sh - - name: Run sixteen clear, wait and aligned-read comparisons - if: matrix.configuration == 'Release' + CONFIGURATION: Release + FIXTURE_RESOLUTION: '48' + HYPERCUBE_SKIP_HOST_TESTS: '1' + run: env "${{ matrix.build_flag }}=1" tools/build_validation.sh + - name: Build Dolphin validation container + run: docker build -t hypercube-dolphin -f tools/Dockerfile.dolphin . + - name: Run comparison and progress checks run: >- docker run --rm --init --shm-size=512m --user "$(id -u):$(id -g)" --mount "type=bind,src=$PWD,dst=/work" -w /work hypercube-dolphin - python3 tools/dolphin_validation.py out/native-texture-followup-Release/Game.dol - --suite texture-followup --disc out/assets/world.iso - --logs out/dolphin-texture-followup-Release --seconds 480 + python3 tools/dolphin_validation.py out/native-${{ matrix.suite }}-Release/Game.dol + --suite ${{ matrix.suite }} --disc out/assets/world.iso + --logs out/dolphin-${{ matrix.suite }}-Release --seconds ${{ matrix.seconds }} - name: Upload executable, assets, measurements and rendered evidence if: always() uses: actions/upload-artifact@v4 with: - name: hypercube-validation-${{ matrix.configuration }} + name: hypercube-${{ matrix.suite }}-Release path: | out/native-*/Game.dol out/native-*/Game.elf @@ -130,3 +202,20 @@ jobs: out/dolphin-*/user/Logs/ out/dolphin-*/user/Dump/Audio/ retention-days: 14 + + validation: + name: validation (${{ matrix.configuration }}) + needs: [changes, host, baseline-validation, rendering, release-comparisons] + if: always() + runs-on: ubuntu-24.04 + timeout-minutes: 5 + strategy: + fail-fast: false + matrix: + configuration: [Debug, Release] + steps: + - uses: actions/checkout@v4 + - name: Require every selected lane and allow only intentional skips + env: + CI_NEEDS: ${{ toJSON(needs) }} + run: python3 tools/ci_changes.py --check-results diff --git a/README.md b/README.md index 7a2f7da..c779cd6 100644 --- a/README.md +++ b/README.md @@ -99,3 +99,7 @@ namespaces remain unchanged. Hypercube is licensed under GPL-2.0-only, matching DolphinDotNet. GameCube package builds require a compatible published SDK package. Ordered transparent color/UV variants are available through [ordered mesh passes](docs/ordered-mesh-passes.md), with a portable published-SDK path and an opt-in native replay backend. + +CI selects relevant checks from the whole PR diff and runs the full suite for +shared or unknown changes. See [CI selection](docs/ci-selection.md) for routing, +required-check behavior and forcing a full run. diff --git a/docs/ci-selection.md b/docs/ci-selection.md new file mode 100644 index 0000000..f49f24f --- /dev/null +++ b/docs/ci-selection.md @@ -0,0 +1,65 @@ +# Selecting CI checks + +Pull requests select checks from the **entire PR diff**, using its actual target +branch and merge base. A documentation commit after a runtime commit cannot +hide the earlier runtime changes. Renames are examined as both a deletion and +an addition; filenames containing whitespace are preserved. + +The selector lives in `tools/ci_changes.py`. Its reusable workflow runs its own +regression tests before publishing a plan. Every workflow reports its selection +in the job summary. Selection does not use GitHub workflow `paths` filters, so +required checks still report for documentation-only PRs. + +| Change | Selected checks | +| --- | --- | +| README or documentation/evidence only | Selector tests and required-check reporting | +| Existing host unit tests | Fast host suite | +| Rendering helpers or rendering sample | Host, Debug/Release rendering capture, NuGet consumer | +| World streamer/cache | Host, Debug/Release packed scene, four Release comparisons, NuGet consumer | +| Texture family/LOD policy | Host, Debug/Release family contracts, NuGet consumer | +| Performance proof tool | Host and performance comparison | +| Profiling proof tool | Host and the three profiling comparisons | +| Common Dolphin decoder | Host and all emulator lanes | +| Package authoring/consumer/license | NuGet packaging and relocated native consumer | +| Shared core, SDK pin, project/build properties, CI plumbing, unknown path | Full suite | + +Rules combine across all changed paths. New runtime files and unclassified paths +run the full suite until their consumers are reviewed and a narrower rule is +added. Missing/malformed event data, unavailable commits or failed diff reads +also run the full suite. The selector tests cover these fallbacks and real Git +histories with multiple commits, changed target branches, deletes and renames. + +Fast host checks run once for executable changes: all Python contracts, the +portable engine tests in Debug/Release, the indexed-cache checks, and both native +and fallback ordered-pass contracts. Console builds depend on that job and skip +its duplicate host work. `tools/build_validation.sh` retains all original host +tests when run locally; its CI-only `HYPERCUBE_SKIP_HOST_TESTS=1` setting is used +only behind the required host job. Asset generation still runs for every console +build because each isolated job needs its own pack/disc. + +The four long Release comparisons use separate jobs with their original build +flags, capture durations and assertions. Only selected rows enter the comparison +matrix. Rendering and packed-scene runs are independent lanes, allowing a +rendering-only change to avoid world captures and profiling. + +The stable `validation (Debug)` and `validation (Release)` checks always run. +They require selection to succeed, every selected validation job to succeed and +every unselected job to be skipped. Failures, cancellations, missing results or +unexpected skips fail these gates. Family and package checks report separately. + +Feature branches are validated through their PR, avoiding duplicate push and PR +emulator runs. Pushes to `main` and manual workflow runs execute full validation; +NuGet release tags always run full package verification. Use **Run workflow** on +any workflow to force its full set of checks without changing selection rules. +No capture window or correctness assertion is reduced by selection. + +When changing routing, run: + +```sh +python3 -m unittest discover -s tests -p test_ci_changes.py +python3 tools/test_host.py +``` + +Keep the explicit dependency rules and required-check tests in sync with new +runtime consumers. A rule should be narrowed only when all affected consumers +are covered by the selected lanes. diff --git a/tests/test_ci_changes.py b/tests/test_ci_changes.py new file mode 100644 index 0000000..7469920 --- /dev/null +++ b/tests/test_ci_changes.py @@ -0,0 +1,181 @@ +import contextlib +import io +import itertools +import json +import os +from pathlib import Path +import subprocess +import sys +import tempfile +import unittest +from unittest.mock import patch + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tools')) +import ci_changes as ci + + +class Routing(unittest.TestCase): + def test_documentation_only_and_executable_files_under_docs(self): + self.assertEqual(ci.select(['README.md', 'docs/streaming-validation.md', + 'docs/measurements/scene.json']), set()) + self.assertEqual(ci.select(['docs/check.py']), ci.ALL) + + def test_rendering_does_not_schedule_world_comparisons(self): + for name in ci.RENDERING_FILES: + self.assertEqual(ci.select(['src/Hypercube.GameCube/' + name]), + {'host', 'rendering', 'packages'}) + self.assertEqual(ci.select(['samples/Rendering/Program.cs']), + {'host', 'rendering', 'packages'}) + + def test_shared_state_sdk_and_new_code_are_conservative(self): + for path in ('.sdk-revision', 'Directory.Build.props', 'eng/Package.props', + 'src/Hypercube.Core/ResourceScheduler.cs', + 'src/Hypercube.GameCube/NewHelper.cs', 'tests/NewTests/Program.cs', + '.github/workflows/validation.yml', '.github/actions/new/action.yml', + 'tools/ci_changes.py', 'tests/test_ci_changes.py', 'unrecognized.file'): + with self.subTest(path=path): + self.assertEqual(ci.select([path]), ci.ALL) + + def test_world_and_family_dependencies(self): + self.assertEqual(ci.select(['src/Hypercube.GameCube/WorldStreamer.cs']), + ci.WORLD | {'packages'}) + self.assertEqual(ci.select(['src/Hypercube.Core/TextureLodPolicy.cs']), + {'host', 'families', 'packages'}) + self.assertEqual(ci.select(['tools/hypercube/pack.py']), ci.WORLD | {'families'}) + self.assertEqual(ci.select(['tools/build_texture_pack.py']), {'host', 'families'}) + + def test_decoder_import_dependencies_and_specific_suites(self): + self.assertEqual(ci.select(['tools/dolphin_validation.py']), ci.WORLD | {'rendering'}) + self.assertEqual(ci.select(['tools/profiling_validation.py']), ci.PROFILING) + self.assertEqual(ci.select(['tools/performance_validation.py']), {'host', 'performance'}) + self.assertEqual(ci.select(['tools/rendering_validation.py']), {'host', 'rendering'}) + self.assertEqual(ci.select(['tests/test_profiling_validation.py']), {'host'}) + self.assertEqual(ci.select(['eng/pack.py', 'LICENSE']), {'packages'}) + + def test_union_and_native_lanes_always_have_host_prerequisite(self): + self.assertEqual(ci.select(['docs/a.md', 'tools/rendering_validation.py', + 'tools/performance_validation.py']), + {'host', 'rendering', 'performance'}) + paths = list(ci.RULES) + ['src/Hypercube.GameCube/' + n for n in ci.RENDERING_FILES | ci.WORLD_FILES] + for path in paths: + selected = ci.select([path]) + if selected & ({'scene', 'rendering'} | ci.SUITES): + self.assertIn('host', selected, path) + + def test_all_tracked_runtime_paths_are_intentionally_classified(self): + # Unknown paths still select everything; this catches accidental exclusions + # when adding suffix/prefix shortcuts to the classifier. + for directory in ('src', 'samples', 'tools', 'eng'): + for path in Path(directory).rglob('*'): + if path.is_file() and not any(p in path.parts for p in ('bin', 'obj', '__pycache__')): + self.assertTrue(ci.select([path.as_posix()]), path) + + +class DiffSelection(unittest.TestCase): + def git(self, *args): + return subprocess.check_output(['git', *args], cwd=self.root, text=True).strip() + + def commit(self, path, text): + file = self.root / path + file.parent.mkdir(parents=True, exist_ok=True) + file.write_text(text) + self.git('add', '-A') + self.git('commit', '-qm', 'fixture') + return self.git('rev-parse', 'HEAD') + + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.root = Path(self.temp.name) + self.git('init', '-q') + self.git('config', 'user.name', 'CI Test') + self.git('config', 'user.email', 'ci@example.invalid') + self.base = self.commit('README.md', 'base') + + def event(self, head, base=None): + return dict(pull_request=dict(base=dict(sha=base or self.base), head=dict(sha=head))) + + def test_entire_pr_not_only_last_commit(self): + self.commit('src/Hypercube.GameCube/WorldStreamer.cs', 'code') + head = self.commit('docs/a.md', 'last commit is docs') + selected, _ = ci.selection('pull_request', self.event(head), self.root) + self.assertEqual(selected, ci.plan(ci.WORLD | {'packages'})) + + def test_merge_base_excludes_unrelated_target_branch_changes(self): + self.git('checkout', '-qb', 'feature') + head = self.commit('docs/a.md', 'documentation') + self.git('checkout', '-qb', 'target', self.base) + target = self.commit('src/Hypercube.Core/ResourceScheduler.cs', 'unrelated target change') + selected, _ = ci.selection('pull_request', self.event(head, target), self.root) + self.assertEqual(selected, ci.plan(set())) + + def test_deletions_renames_and_whitespace_are_preserved(self): + self.base = self.commit('src/Hypercube.GameCube/WorldStreamer.cs', 'code') + (self.root / 'docs').mkdir() + self.git('mv', 'src/Hypercube.GameCube/WorldStreamer.cs', 'docs/moved.md') + self.git('commit', '-qm', 'rename') + head = self.commit('docs/space and\nnewline.md', 'documentation') + paths = ci.changed_paths(self.event(head), self.root) + self.assertIn('src/Hypercube.GameCube/WorldStreamer.cs', paths) + self.assertIn('docs/moved.md', paths) + self.assertIn('docs/space and\nnewline.md', paths) + self.assertEqual(ci.selection('pull_request', self.event(head), self.root)[0], + ci.plan(ci.WORLD | {'packages'})) + + def test_invalid_missing_and_unavailable_history_runs_full(self): + for event in ({}, [], self.event('bad'), self.event('f' * 40)): + with self.subTest(event=event): + self.assertEqual(ci.selection('pull_request', event, self.root)[0], ci.plan(ci.ALL)) + for event_name in ('push', 'workflow_dispatch', 'schedule', 'merge_group', ''): + self.assertEqual(ci.selection(event_name, {}, self.root)[0], ci.plan(ci.ALL)) + + +class RequiredGate(unittest.TestCase): + def needs(self, selected): + p = ci.plan(selected) + return dict(changes=dict(result='success', outputs=dict(plan=json.dumps(p))), + host=dict(result='success' if p['host'] else 'skipped'), + **{'baseline-validation': dict(result='success' if p['scene'] else 'skipped'), + 'rendering': dict(result='success' if p['rendering'] else 'skipped'), + 'release-comparisons': dict(result='success' if p['comparisons'] else 'skipped')}) + + def test_legitimate_skips_and_success(self): + for selected in (set(), ci.ALL, {'host'}, {'host', 'rendering'}, {'packages'}): + ci.check_results(self.needs(selected)) + + def test_selected_failures_cancellation_and_skips_cannot_pass(self): + for job, status in itertools.product(('host', 'baseline-validation', 'rendering', 'release-comparisons'), + ('failure', 'cancelled', 'skipped', None)): + needs = self.needs(ci.ALL) + needs[job]['result'] = status + with self.subTest(job=job, status=status), self.assertRaises(ValueError): + ci.check_results(needs) + for status in ('failure', 'cancelled', 'skipped', None): + needs = self.needs(set()) + needs['changes']['result'] = status + with self.assertRaises(ValueError): + ci.check_results(needs) + + def test_unexpected_execution_and_missing_result_fail(self): + needs = self.needs(set());needs['rendering']['result'] = 'success' + with self.assertRaises(ValueError):ci.check_results(needs) + needs = self.needs(ci.ALL);del needs['host'] + with self.assertRaises(ValueError):ci.check_results(needs) + + def test_cli_emits_valid_empty_matrix_and_single_line_outputs(self): + with tempfile.TemporaryDirectory() as folder: + root = Path(folder);event = root / 'event.json';output = root / 'output';summary = root / 'summary' + event.write_text('{}') + env = dict(os.environ, GITHUB_EVENT_NAME='pull_request', GITHUB_EVENT_PATH=str(event), + GITHUB_OUTPUT=str(output), GITHUB_STEP_SUMMARY=str(summary)) + with contextlib.redirect_stdout(io.StringIO()), patch.dict(os.environ, env), patch.object(sys, 'argv', ['ci_changes.py']), patch.object(ci, 'selection', return_value=(ci.plan(set()), 'Docs only')): + ci.main() + values = dict(line.split('=', 1) for line in output.read_text().splitlines()) + self.assertEqual(values['compare'], 'false') + self.assertEqual(json.loads(values['comparisons'])['include'][0]['suite'], 'not-selected') + self.assertEqual(json.loads(values['plan'])['comparisons'], []) + self.assertEqual(set(values), set(ci.FLAGS) | {'compare', 'comparisons', 'plan'}) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/build_validation.sh b/tools/build_validation.sh index 7cbea8f..df56372 100755 --- a/tools/build_validation.sh +++ b/tools/build_validation.sh @@ -39,10 +39,15 @@ sdk_root="${DOLPHIN_DOTNET_ROOT:-$PWD/vendor/sdk}" if [[ ! -d "$sdk_root" && -d "$PWD/../DolphinDotNet" ]]; then sdk_root="$PWD/../DolphinDotNet"; fi sdk_root="$(cd "$sdk_root" && pwd)" mkdir -p out -dotnet run --project tests/Hypercube.GameCube.Tests -c Release +# CI runs these host checks once in its required host job. Local builds retain them. +if [[ "${HYPERCUBE_SKIP_HOST_TESTS:-0}" != 1 ]]; then + dotnet run --project tests/Hypercube.GameCube.Tests -c Release +fi python3 tools/build_assets.py --resolution "${FIXTURE_RESOLUTION:-48}" -python3 -m unittest discover -s tests -p 'test_*.py' -dotnet run --project tests/Hypercube.Tests -c "$configuration" -- out/assets/world.hcp +if [[ "${HYPERCUBE_SKIP_HOST_TESTS:-0}" != 1 ]]; then + python3 -m unittest discover -s tests -p 'test_*.py' + dotnet run --project tests/Hypercube.Tests -c "$configuration" -- out/assets/world.hcp +fi dotnet build "$sdk_root/tools/DolphinDotNet.Compiler" -c Release -m:1 dotnet build samples/Validation/Validation.csproj -c "$configuration" -p:DndBuildNative=false -p:HypercubeCI=true -p:HypercubeOneMs="$execution" -p:HypercubePerformance="$performance" -p:HypercubeProfiling="$profiling" -p:HypercubeFollowup="$followup" -p:HypercubeTextureFollowup="$texture_followup" -p:DolphinDotNetRoot="$sdk_root" -m:1 dotnet "$sdk_root/tools/DolphinDotNet.Compiler/bin/Release/net8.0/DolphinDotNet.Compiler.dll" --bundle "samples/Validation/bin/$configuration/net8.0/Validation.dll" "$PWD/$native_output" diff --git a/tools/ci_changes.py b/tools/ci_changes.py new file mode 100644 index 0000000..ae0cc89 --- /dev/null +++ b/tools/ci_changes.py @@ -0,0 +1,170 @@ +#!/usr/bin/env python3 +"""Conservative CI selection from the whole PR diff; unknown changes run everything.""" +import argparse +import json +import os +from pathlib import Path +import re +import subprocess + +FLAGS = ('host', 'families', 'rendering', 'scene', 'packages') +COMPARISONS = ( + dict(suite='performance', build_flag='PERFORMANCE_SUITE', seconds=240), + dict(suite='profiling', build_flag='PROFILING_SUITE', seconds=360), + dict(suite='sdk-followup', build_flag='SDK_FOLLOWUP_SUITE', seconds=360), + dict(suite='texture-followup', build_flag='TEXTURE_FOLLOWUP_SUITE', seconds=480), +) +SUITES = {c['suite'] for c in COMPARISONS} +ALL = set(FLAGS) | SUITES +WORLD = {'host', 'scene'} | SUITES +PROFILING = {'host', 'profiling', 'sdk-followup', 'texture-followup'} +# Explicit names make new runtime files conservative until their consumers are reviewed. +WORLD_FILES = {'AramCache.cs', 'PackedWorld.cs', 'TexturePool.cs', 'WorldStreamer.cs'} +RENDERING_FILES = {'CloudShadowLayer.cs', 'DynamicMeshBuffer.cs', 'IndexedMeshCache.cs', + 'MeshBatch.cs', 'OrderedMeshPasses.cs', 'RenderTelemetry.cs', + 'ScrollingTextureLayer.cs', 'SharedGraphicsResources.cs'} +FAMILY_FILES = {'TextureFamilies.cs', 'TextureLodPolicy.cs'} +HOST_PROJECTS = ('Hypercube.Tests', 'Hypercube.GameCube.Tests', 'OrderedMeshPasses.Tests') +PYTHON_TESTS = {'test_assets.py', 'test_performance_validation.py', 'test_profiling_validation.py', + 'test_progressive_textures.py', 'test_rendering_validation.py', 'test_ci_changes.py'} +RULES = { + 'tools/build_assets.py': WORLD | {'families'}, + 'tools/hypercube/pack.py': WORLD | {'families'}, + 'tools/hypercube/mesh.py': WORLD, + 'tools/hypercube/disc.py': WORLD, + 'tools/hypercube/progressive.py': {'host', 'families'}, + 'tools/hypercube/__init__.py': WORLD | {'families'}, + 'tools/build_texture_pack.py': {'host', 'families'}, + 'tools/build_validation.sh': WORLD, + 'tools/build_rendering.sh': {'host', 'rendering'}, + # Rendering and performance/profiling tools import the common decoder/helpers. + 'tools/dolphin_validation.py': WORLD | {'rendering'}, + 'tools/rendering_validation.py': {'host', 'rendering'}, + 'tools/performance_validation.py': {'host', 'performance'}, + 'tools/profiling_validation.py': PROFILING, + 'tests/progressive_fixtures.py': {'host', 'families'}, + 'tests/nuget_consumer.py': {'packages'}, + 'eng/pack.py': {'packages'}, + 'eng/package-readme.md': {'packages'}, + 'LICENSE': {'packages'}, +} + + +def select(paths): + selected = set() + for path in paths: + # Config/code extensions always take priority over a documentation extension. + if path in RULES: + selected.update(RULES[path]) + elif path.startswith('.github/') or path in { + '.sdk-revision', 'Directory.Build.props', 'Directory.Build.targets', + 'global.json', 'NuGet.Config', 'nuget.config', 'Hypercube.sln', + 'tools/ci_changes.py', 'tools/test_host.py', 'tests/test_ci_changes.py', 'eng/Package.props', + } or path.endswith(('.csproj', '.props', '.targets', '.sln', '.slnx')): + selected.update(ALL) + elif path.startswith('src/Hypercube.Core/'): + name = path.removeprefix('src/Hypercube.Core/') + selected.update({'host', 'families', 'packages'} if name in FAMILY_FILES else ALL) + elif path.startswith('src/Hypercube.GameCube/'): + name = path.removeprefix('src/Hypercube.GameCube/') + if name in WORLD_FILES: + selected.update(WORLD | {'packages'}) + elif name in RENDERING_FILES: + selected.update({'host', 'rendering', 'packages'}) + else: + selected.update(ALL) + elif path.startswith('samples/Validation/'): + selected.update(WORLD) + elif path.startswith('samples/Rendering/'): + # The relocated NuGet consumer compiles this sample too. + selected.update({'host', 'rendering', 'packages'}) + elif path.startswith('tests/TextureFamilies.Tests/'): + selected.update({'host', 'families'}) + elif any(path.startswith('tests/' + name + '/') for name in HOST_PROJECTS): + selected.add('host') + elif path.startswith('tests/') and path.removeprefix('tests/') in PYTHON_TESTS: + selected.add('host') + elif path.startswith('docs/') or path in {'README.md', 'AGENTS.md', 'SECURITY.md'}: + # Executable/configuration files under docs are not silently exempt. + if path.endswith(('.md', '.txt', '.png', '.jpg', '.jpeg', '.svg')) or ( + path.startswith('docs/measurements/') and path.endswith(('.json', '.csv')) + ): + continue + selected.update(ALL) + else: + selected.update(ALL) + return selected + + +def plan(selected): + return {**{key: key in selected for key in FLAGS}, + 'comparisons': [dict(c) for c in COMPARISONS if c['suite'] in selected]} + + +def changed_paths(event, cwd=None): + pr = event['pull_request'] + base, head = pr['base']['sha'], pr['head']['sha'] + if not all(isinstance(sha, str) and re.fullmatch('[0-9a-fA-F]{40,64}', sha) for sha in (base, head)): + raise ValueError('Invalid comparison revision') + # --no-renames includes BOTH sides of a rename; NUL preserves whitespace/newlines. + raw = subprocess.check_output(['git', 'diff', '--name-only', '--no-renames', '-z', + base + '...' + head, '--'], cwd=cwd, stderr=subprocess.PIPE) + return [path.decode('utf-8') for path in raw.split(b'\0') if path] + + +def selection(event_name, event, cwd=None): + if event_name != 'pull_request': + return plan(ALL), 'Full validation for main, tags and manual runs' + try: + paths = changed_paths(event, cwd) + return plan(select(paths)), f'Selected from {len(paths)} changed PR paths' + except (KeyError, TypeError, ValueError, OSError, subprocess.SubprocessError) as error: + return plan(ALL), f'Diff unavailable; full validation ({type(error).__name__})' + + +def check_results(needs): + """Skipped is valid only for an unselected lane, never for a selected one.""" + if needs.get('changes', {}).get('result') != 'success': + raise ValueError('CI selection did not succeed') + selected = json.loads(needs['changes']['outputs']['plan']) + expected = {'host': selected['host'], 'baseline-validation': selected['scene'], + 'rendering': selected['rendering'], 'release-comparisons': bool(selected['comparisons'])} + for job, required in expected.items(): + actual = needs.get(job, {}).get('result') + if actual != ('success' if required else 'skipped'): + raise ValueError(f'{job}: expected {"success" if required else "skipped"}, got {actual}') + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--check-results', action='store_true') + args = parser.parse_args() + if args.check_results: + check_results(json.loads(os.environ['CI_NEEDS'])) + print('Every selected validation lane passed; unselected lanes were skipped.') + return + try: + event = json.loads(Path(os.environ['GITHUB_EVENT_PATH']).read_text()) + result, reason = selection(os.environ.get('GITHUB_EVENT_NAME', ''), event) + except (KeyError, ValueError, OSError): + result, reason = plan(ALL), 'Event unavailable; full validation' + outputs = {key: str(result[key]).lower() for key in FLAGS} + outputs['compare'] = str(bool(result['comparisons'])).lower() + # A sentinel keeps the matrix valid even when a skipped job is expanded by tooling. + matrix = result['comparisons'] or [dict(suite='not-selected', build_flag='CI_NOT_SELECTED', seconds=0)] + outputs['comparisons'] = json.dumps({'include': matrix}, separators=(',', ':')) + outputs['plan'] = json.dumps(result, separators=(',', ':')) + with Path(os.environ['GITHUB_OUTPUT']).open('a') as output: + for name, value in outputs.items(): + output.write(f'{name}={value}\n') + summary = reason + '\n\n' + '\n'.join(f'- {key}: {value}' for key, value in outputs.items() + if key not in {'plan', 'comparisons'}) + summary += '\n- comparisons: ' + ', '.join(c['suite'] for c in result['comparisons']) + '\n' + if os.environ.get('GITHUB_STEP_SUMMARY'): + with Path(os.environ['GITHUB_STEP_SUMMARY']).open('a') as output: + output.write(summary) + print(summary) + + +if __name__ == '__main__': + main() diff --git a/tools/test_host.py b/tools/test_host.py new file mode 100644 index 0000000..7454f7f --- /dev/null +++ b/tools/test_host.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +"""Fast SDK-free checks, run once before selected console builds.""" +import subprocess + + +def run(*args): + print('+', ' '.join(args), flush=True) + subprocess.run(args, check=True) + + +def main(): + run('python3', '-m', 'unittest', 'discover', '-s', 'tests', '-p', 'test_*.py') + run('python3', 'tools/build_assets.py', '--resolution', '48') + for config in ('Debug', 'Release'): + run('dotnet', 'run', '--project', 'tests/Hypercube.Tests', '-c', config, '--', 'out/assets/world.hcp') + run('dotnet', 'run', '--project', 'tests/Hypercube.GameCube.Tests', '-c', 'Release') + for native in ('true', 'false'): + run('dotnet', 'run', '--project', 'tests/OrderedMeshPasses.Tests', '-c', 'Release', + '-p:HypercubeNativePasses=' + native) + + +if __name__ == '__main__': + main()