diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a5d09d581..bbb8b3b84 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,6 +27,13 @@ jobs: - uses: actions/setup-python@v5 with: python-version: '3.12' + - name: Does this pull request need Rust checks? + if: github.event_name == 'pull_request' + id: rust + env: + BASE_SHA: ${{ github.event.pull_request.base.sha }} + HEAD_SHA: ${{ github.event.pull_request.head.sha }} + run: python3 tools/ci/rust_changes.py "$BASE_SHA" "$HEAD_SHA" >> "$GITHUB_OUTPUT" - name: Keep game artifacts out of Git run: | python tools/check_public_tree.py @@ -34,19 +41,23 @@ jobs: - name: Test offline research tooling run: python -m unittest discover -s tests -v - name: Install Rust toolchain and the ALSA and udev headers the client links against + if: steps.rust.outputs.required != 'false' run: | sudo apt-get update && sudo apt-get install -y --no-install-recommends libasound2-dev libudev-dev rustup toolchain install stable --profile minimal --component rustfmt --component clippy --target wasm32-unknown-unknown # Dependencies and the workspace's own build, saved from main's runs (pull requests read main's cache). - uses: Swatinem/rust-cache@v2 + if: steps.rust.outputs.required != 'false' with: shared-key: verify save-if: ${{ github.ref == 'refs/heads/main' }} - name: Check formatting and lint + if: steps.rust.outputs.required != 'false' run: | cargo +stable fmt --all -- --check cargo +stable clippy --workspace --all-targets -- -D warnings - name: Test Rust core and voxel kernel correctness + if: steps.rust.outputs.required != 'false' run: cargo +stable test --workspace parity: if: >- diff --git a/tests/test_ci_rust_changes.py b/tests/test_ci_rust_changes.py new file mode 100644 index 000000000..6fd7621da --- /dev/null +++ b/tests/test_ci_rust_changes.py @@ -0,0 +1,53 @@ +import os +from pathlib import Path +import subprocess +import tempfile +import unittest + +from tools.ci.rust_changes import changed_paths, needs_rust + + +class RustChangesTests(unittest.TestCase): + def test_only_content_changes_skip_rust(self): + self.assertFalse(needs_rust([])) + self.assertFalse(needs_rust([ + "website/src/index.html", "website/src/main.ts", "docs/setup.md", "README.md", + ])) + for path in [ + "crates/horizon-client/src/game.rs", "xtask/src/main.rs", "Cargo.lock", + ".cargo/config.toml", ".github/workflows/ci.yml", "tools/ci/rust_changes.py", + "research-lock.json", "website/src/tokens.css", "unknown-file", + ]: + with self.subTest(path=path): + self.assertTrue(needs_rust(["README.md", path])) + + def test_moving_source_into_docs_still_requires_rust(self): + with tempfile.TemporaryDirectory() as directory: + def git(*args): + return subprocess.check_output(["git", "-C", directory, *args]) + + git("init", "--quiet") + source = Path(directory, "source.rs") + source.write_text("fn main() {}\n") + git("add", ".") + git("-c", "user.name=CI test", "-c", "user.email=ci@example.invalid", + "-c", "commit.gpgsign=false", "commit", "--quiet", "-m", "base") + base = git("rev-parse", "HEAD").decode().strip() + Path(directory, "docs").mkdir() + source.rename(Path(directory, "docs/example.rs")) + git("add", "-A") + git("-c", "user.name=CI test", "-c", "user.email=ci@example.invalid", + "-c", "commit.gpgsign=false", "commit", "--quiet", "-m", "move") + # Exercise the same diff command as CI in the temporary repository. + previous = Path.cwd() + try: + os.chdir(directory) + paths = changed_paths(base, "HEAD") + finally: + os.chdir(previous) + self.assertIn("source.rs", paths) + self.assertTrue(needs_rust(paths)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/ci/rust_changes.py b/tools/ci/rust_changes.py new file mode 100644 index 000000000..d2bf41713 --- /dev/null +++ b/tools/ci/rust_changes.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 +"""Keep verify's Rust steps for any change beyond website content or documentation.""" + +import argparse +import subprocess + + +def needs_rust(paths): + # horizon-theme's Rust tests check that the generated website tokens match. + return any(path == "website/src/tokens.css" or not ( + path.startswith(("website/", "docs/")) or path.endswith(".md") + ) for path in paths) + + +def changed_paths(base, head): + # Renames must include deletions too: moving source into docs still affects Rust. + output = subprocess.check_output([ + "git", "diff", "--name-only", "--no-renames", "-z", f"{base}...{head}", "--", + ]) + return [path for path in output.decode("utf-8").split("\0") if path] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("base") + parser.add_argument("head") + args = parser.parse_args() + print(f"required={str(needs_rust(changed_paths(args.base, args.head))).lower()}")