diff --git a/.github/workflows/cpp-bmn.yml b/.github/workflows/cpp-bmn.yml index 066db1bdca96..a12ce1070e34 100644 --- a/.github/workflows/cpp-bmn.yml +++ b/.github/workflows/cpp-bmn.yml @@ -24,20 +24,6 @@ permissions: contents: read jobs: - cargo: - runs-on: ubuntu-latest - defaults: - run: - working-directory: cpp/extractor/bmn - steps: - - uses: actions/checkout@v5 - - name: Check formatting - run: cargo fmt --check - - name: Check - run: cargo check - - name: Test - run: cargo test - bazel: runs-on: ubuntu-latest steps: diff --git a/.github/workflows/ql-for-ql-tests.yml b/.github/workflows/ql-for-ql-tests.yml index 1942fd62939e..db2395dfc40b 100644 --- a/.github/workflows/ql-for-ql-tests.yml +++ b/.github/workflows/ql-for-ql-tests.yml @@ -39,8 +39,6 @@ jobs: ~/.cargo/git target key: ${{ runner.os }}-${{ steps.os_version.outputs.version }}-qltest-cargo-${{ hashFiles('rust-toolchain.toml', 'Cargo.lock') }} - - name: Check formatting - run: cd ql; cargo fmt -p codeql-extractor-ql -p buramu -p tree-sitter-blame -- --check - name: Build extractor run: | cd ql; diff --git a/.github/workflows/cargo-clippy.yml b/.github/workflows/rust-checks.yml similarity index 64% rename from .github/workflows/cargo-clippy.yml rename to .github/workflows/rust-checks.yml index d3b40974f6da..6b90cebf667c 100644 --- a/.github/workflows/cargo-clippy.yml +++ b/.github/workflows/rust-checks.yml @@ -1,6 +1,6 @@ -name: Run Clippy on changed Rust code +name: Rust checks -# Run on every PR; package selection below skips lint setup for unrelated changes. +# Run on every PR; package selection below skips checks for unrelated changes. on: pull_request: push: @@ -14,11 +14,11 @@ permissions: concurrency: # Cancel superseded PR checks; each push gets its own group so GitHub cannot # replace an already-pending push run with a newer one. - group: cargo-clippy-${{ github.event_name }}-${{ github.event.pull_request.number || github.run_id }} + group: rust-checks-${{ github.event_name }}-${{ github.event.pull_request.number || github.run_id }} cancel-in-progress: ${{ github.event_name == 'pull_request' }} jobs: - clippy: + rust-checks: runs-on: ubuntu-latest env: CARGO_TERM_COLOR: always @@ -28,15 +28,13 @@ jobs: # PRs compare the merge commit with its first parent; pushes need full # history to compare the event's before SHA across multi-commit pushes. fetch-depth: ${{ github.event_name == 'pull_request' && 2 || 0 }} - - name: Test package selection - run: python3 -m unittest discover -s misc/scripts/cargo-clippy - name: Select affected packages id: select env: # Use the PR merge base's actual first parent, or the push's before SHA. BASE: ${{ github.event_name == 'pull_request' && 'HEAD^1' || github.event.before }} run: | - packages=$(python3 misc/scripts/cargo-clippy/cargo_clippy.py select --base "$BASE") + packages=$(python3 misc/scripts/rust-checks/rust_checks.py select --base "$BASE") echo "packages=$packages" >> "$GITHUB_OUTPUT" echo "Selected packages: $packages" | tee -a "$GITHUB_STEP_SUMMARY" - name: Restore Cargo cache @@ -48,9 +46,9 @@ jobs: ~/.cargo/registry ~/.cargo/git target - key: cargo-clippy-${{ runner.os }}-${{ hashFiles('rust-toolchain.toml', 'Cargo.lock') }}-${{ github.sha }} + key: rust-checks-${{ runner.os }}-${{ hashFiles('rust-toolchain.toml', 'Cargo.lock') }}-${{ github.sha }} restore-keys: | - cargo-clippy-${{ runner.os }}-${{ hashFiles('rust-toolchain.toml', 'Cargo.lock') }}- + rust-checks-${{ runner.os }}-${{ hashFiles('rust-toolchain.toml', 'Cargo.lock') }}- - name: Show stable toolchain and cache if: steps.select.outputs.packages != '[]' env: @@ -62,11 +60,23 @@ jobs: - name: Inject ast-generator sources if: contains(fromJSON(steps.select.outputs.packages), 'ast-generator') run: bazel run //rust/ast-generator:inject-sources - - name: Clippy + - name: Check formatting + if: steps.select.outputs.packages != '[]' + env: + PACKAGES: ${{ steps.select.outputs.packages }} + run: python3 misc/scripts/rust-checks/rust_checks.py format "$PACKAGES" + - name: Run Clippy if: steps.select.outputs.packages != '[]' env: PACKAGES: ${{ steps.select.outputs.packages }} # For the unified extractor: skip linking the native Swift shim and # only check Rust code. SWIFT_SYNTAX_RS_CHECK_ONLY: "1" - run: python3 misc/scripts/cargo-clippy/cargo_clippy.py lint "$PACKAGES" + run: python3 misc/scripts/rust-checks/rust_checks.py lint "$PACKAGES" + - name: Run tests + if: steps.select.outputs.packages != '[]' + env: + PACKAGES: ${{ steps.select.outputs.packages }} + # Skips the unified extractor crates, whose tests need a Swift toolchain, + # and ast-generator, which has no tests of its own. + run: python3 misc/scripts/rust-checks/rust_checks.py test "$PACKAGES" diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 89d4d91f561c..4bb1cc829159 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -29,40 +29,6 @@ permissions: contents: read jobs: - rust-ast-generator: - runs-on: ubuntu-latest - defaults: - run: - working-directory: rust/ast-generator - steps: - - name: Checkout - uses: actions/checkout@v5 - - name: Inject sources - shell: bash - run: | - bazel run //rust/ast-generator:inject-sources - - name: Format - shell: bash - run: | - cargo fmt --check - - name: Compilation - shell: bash - run: cargo check - rust-code: - runs-on: ubuntu-latest - defaults: - run: - working-directory: rust/extractor - steps: - - name: Checkout - uses: actions/checkout@v5 - - name: Format - shell: bash - run: | - cargo fmt --check - - name: Compilation - shell: bash - run: cargo check rust-codegen: runs-on: ubuntu-latest steps: diff --git a/.github/workflows/tree-sitter-extractor-test.yml b/.github/workflows/tree-sitter-extractor-test.yml deleted file mode 100644 index 97ae51ff7c15..000000000000 --- a/.github/workflows/tree-sitter-extractor-test.yml +++ /dev/null @@ -1,45 +0,0 @@ -name: Test tree-sitter-extractor - -on: - push: - paths: &tree_sitter_paths - - "shared/extractor-common/**" - - "shared/tree-sitter-extractor/**" - - Cargo.toml - - Cargo.lock - - rust-toolchain.toml - - .github/workflows/tree-sitter-extractor-test.yml - branches: - - main - - "rc/*" - pull_request: - paths: *tree_sitter_paths - branches: - - main - - "rc/*" - -env: - CARGO_TERM_COLOR: always - -defaults: - run: - working-directory: shared/tree-sitter-extractor - -permissions: - contents: read - -jobs: - test: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v5 - - name: Check formatting - run: cargo fmt -p extractor-common -p codeql-extractor -- --check - - name: Run tests - run: cargo test -p extractor-common -p codeql-extractor --verbose - fmt: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v5 - - name: Check formatting - run: cargo fmt -p extractor-common -p codeql-extractor --check diff --git a/.github/workflows/yeast-test.yml b/.github/workflows/yeast-test.yml deleted file mode 100644 index 48e0ca01498f..000000000000 --- a/.github/workflows/yeast-test.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: Test Yeast - -on: - push: - paths: - - "shared/yeast/**" - - "shared/yeast-macros/**" - - "shared/yeast-schema/**" - - .github/workflows/yeast-test.yml - branches: - - main - - "rc/*" - pull_request: - paths: - - "shared/yeast/**" - - "shared/yeast-macros/**" - - "shared/yeast-schema/**" - - .github/workflows/yeast-test.yml - -env: - CARGO_TERM_COLOR: always - -permissions: - contents: read - -jobs: - test: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v5 - - name: Run tests - run: cargo test --verbose --package codeql-extractor --package yeast --package yeast-macros --package yeast-schema diff --git a/misc/scripts/cargo-clippy/test_cargo_clippy.py b/misc/scripts/cargo-clippy/test_cargo_clippy.py deleted file mode 100644 index 78f04fb02666..000000000000 --- a/misc/scripts/cargo-clippy/test_cargo_clippy.py +++ /dev/null @@ -1,52 +0,0 @@ -import unittest -from pathlib import Path - -from cargo_clippy import changed_files, select, workspace - - -ROOT = Path(__file__).resolve().parents[3] - - -class SelectionTests(unittest.TestCase): - def test_unrelated_changes_select_nothing(self): - self.assertEqual(select(ROOT, [ - "README.md", - "rust/ql/test/example/test.rs", - "python/extractor/tsg-python/src/main.rs", - ]), []) - - def test_nested_member_and_reverse_dependencies(self): - self.assertEqual(select(ROOT, ["rust/extractor/src/main.rs"]), ["codeql-rust"]) - self.assertEqual( - select(ROOT, ["ql/buramu/tree-sitter-blame/src/parser.c"]), - ["codeql-extractor-ql", "tree-sitter-blame"], - ) - self.assertTrue( - {"extractor-common", "codeql-extractor"} - <= set(select(ROOT, ["shared/extractor-common/src/lib.rs"])) - ) - - def test_external_inputs(self): - self.assertIn( - "yeast-python", - select(ROOT, ["python/extractor/tsg-python/tsp/src/parser.c"]), - ) - self.assertEqual(select(ROOT, ["MODULE.bazel"]), ["ast-generator"]) - - def test_global_inputs_select_everything(self): - packages, _ = workspace(ROOT) - self.assertEqual( - select(ROOT, ["Cargo.lock"]), - sorted(package["package"]["name"] for package in packages.values()), - ) - - def test_missing_base_selects_everything(self): - packages, _ = workspace(ROOT) - self.assertEqual( - select(ROOT, changed_files(ROOT, "f" * 40)), - sorted(package["package"]["name"] for package in packages.values()), - ) - - -if __name__ == "__main__": - unittest.main() diff --git a/misc/scripts/cargo-clippy/cargo_clippy.py b/misc/scripts/rust-checks/rust_checks.py similarity index 70% rename from misc/scripts/cargo-clippy/cargo_clippy.py rename to misc/scripts/rust-checks/rust_checks.py index 5221e9091db5..73ec2bab58f8 100644 --- a/misc/scripts/cargo-clippy/cargo_clippy.py +++ b/misc/scripts/rust-checks/rust_checks.py @@ -1,5 +1,9 @@ #!/usr/bin/env python3 -"""Select root-workspace packages affected by a PR or push, and lint them with Clippy.""" +"""Select root-workspace packages affected by a PR or push, then run Rust checks. + +When changing this script, run its tests with: + python3 -m unittest discover -s misc/scripts/rust-checks +""" import argparse import json @@ -16,9 +20,11 @@ "rust-toolchain.toml", "clippy.toml", ".clippy.toml", + "rustfmt.toml", + ".rustfmt.toml", ".cargo/", - ".github/workflows/cargo-clippy.yml", - "misc/scripts/cargo-clippy/", + ".github/workflows/rust-checks.yml", + "misc/scripts/rust-checks/", ) # Inputs read by build scripts outside their package's directory. @@ -30,6 +36,14 @@ ".bazelversion": "rust/ast-generator", } +# We skip running tests for: +# - The unified extractor and swift-syntax-rs: the tests link the swift-syntax +# parser, which needs a Swift toolchain that this workflow does not install. +# - The ast-generator: its only test comes from the injected rust-analyzer +# sources. They make no sense to run and expect the rust-analyzer repository +# layout. +UNTESTED_PACKAGES = {"codeql-extractor-unified", "swift-syntax-rs", "ast-generator"} + # Read workspace packages and map each package to its path dependents. def workspace(root): @@ -111,7 +125,28 @@ def lint(packages): return subprocess.run([*args, "--", "-D", "warnings"]).returncode -# Parse the command and dispatch selection or linting. +# Run rustfmt on the selected packages and return its exit code. +def check_format(packages): + if not packages: + return 0 + args = ["cargo", "fmt"] + for package in packages: + args.extend(["--package", package]) + return subprocess.run([*args, "--", "--check"]).returncode + + +# Run tests for the selected packages and return Cargo's exit code. +def test(packages): + packages = [package for package in packages if package not in UNTESTED_PACKAGES] + if not packages: + return 0 + args = ["cargo", "test", "--locked", "--no-fail-fast"] + for package in packages: + args.extend(["--package", package]) + return subprocess.run(args).returncode + + +# Parse the command and dispatch selection or checks. def main(): parser = argparse.ArgumentParser(description=__doc__) commands = parser.add_subparsers(dest="command", required=True) @@ -122,14 +157,27 @@ def main(): "--base", required=True, help="PR merge's first parent, or push event's before SHA (zero for a new branch)", ) + lint_parser = commands.add_parser("lint", help="run Clippy on the given packages") lint_parser.add_argument("packages", help="JSON list printed by the select command") + + format_parser = commands.add_parser("format", help="check formatting of the given packages") + format_parser.add_argument("packages", help="JSON list printed by the select command") + + test_parser = commands.add_parser("test", help="run tests for the given packages") + test_parser.add_argument("packages", help="JSON list printed by the select command") + args = parser.parse_args() root = Path(__file__).resolve().parents[3] + if args.command == "select": print(json.dumps(select(root, changed_files(root, args.base)))) - else: + elif args.command == "lint": raise SystemExit(lint(json.loads(args.packages))) + elif args.command == "format": + raise SystemExit(check_format(json.loads(args.packages))) + else: + raise SystemExit(test(json.loads(args.packages))) if __name__ == "__main__": main() diff --git a/misc/scripts/rust-checks/test_rust_checks.py b/misc/scripts/rust-checks/test_rust_checks.py new file mode 100644 index 000000000000..0cd6acc715bd --- /dev/null +++ b/misc/scripts/rust-checks/test_rust_checks.py @@ -0,0 +1,108 @@ +"""Tests for rust_checks.py. Run from the repository root with: + + python3 -m unittest discover -s misc/scripts/rust-checks +""" + +import unittest +from pathlib import Path +from unittest.mock import patch + +from rust_checks import ( + changed_files, + check_format, + select, + test, + workspace, +) + + +ROOT = Path(__file__).resolve().parents[3] + + +class SelectionTests(unittest.TestCase): + def test_unrelated_changes_select_nothing(self): + self.assertEqual(select(ROOT, [ + "README.md", + "rust/ql/test/example/test.rs", + "rust/ql/test/example/rustfmt.toml", + "python/extractor/tsg-python/src/main.rs", + "python/extractor/tsg-python/tsp/bindings/rust/lib.rs", + "python/extractor/tsg-python/.rustfmt.toml", + ]), []) + + def test_nested_member_and_reverse_dependencies(self): + self.assertEqual(select(ROOT, ["rust/extractor/src/main.rs"]), ["codeql-rust"]) + self.assertEqual(select(ROOT, ["rust/extractor/.rustfmt.toml"]), ["codeql-rust"]) + self.assertEqual( + select(ROOT, ["ql/buramu/tree-sitter-blame/src/parser.c"]), + ["codeql-extractor-ql", "tree-sitter-blame"], + ) + self.assertTrue( + {"extractor-common", "codeql-extractor"} + <= set(select(ROOT, ["shared/extractor-common/src/lib.rs"])) + ) + + def test_external_inputs(self): + self.assertIn( + "yeast-python", + select(ROOT, ["python/extractor/tsg-python/tsp/src/parser.c"]), + ) + self.assertEqual(select(ROOT, ["MODULE.bazel"]), ["ast-generator"]) + + def test_global_inputs_select_everything(self): + packages, _ = workspace(ROOT) + for path in ( + "Cargo.lock", + "rustfmt.toml", + ".rustfmt.toml", + ".github/workflows/rust-checks.yml", + "misc/scripts/rust-checks/rust_checks.py", + ): + with self.subTest(path=path): + self.assertEqual( + select(ROOT, [path]), + sorted(package["package"]["name"] for package in packages.values()), + ) + + def test_missing_base_selects_everything(self): + packages, _ = workspace(ROOT) + self.assertEqual( + select(ROOT, changed_files(ROOT, "f" * 40)), + sorted(package["package"]["name"] for package in packages.values()), + ) + + +class FormatTests(unittest.TestCase): + @patch("rust_checks.subprocess.run") + def test_checks_selected_packages_and_propagates_failure(self, run): + run.return_value.returncode = 1 + self.assertEqual(check_format(["one", "two"]), 1) + run.assert_called_once_with([ + "cargo", "fmt", "--package", "one", "--package", "two", "--", "--check", + ]) + + @patch("rust_checks.subprocess.run") + def test_empty_selection_skips_cargo(self, run): + self.assertEqual(check_format([]), 0) + run.assert_not_called() + + +class TestTests(unittest.TestCase): + @patch("rust_checks.subprocess.run") + def test_skips_untested_packages(self, run): + run.return_value.returncode = 1 + self.assertEqual( + test(["ast-generator", "codeql-extractor-unified", "swift-syntax-rs", "yeast"]), 1, + ) + run.assert_called_once_with( + ["cargo", "test", "--locked", "--no-fail-fast", "--package", "yeast"], + ) + + @patch("rust_checks.subprocess.run") + def test_only_untested_packages_skips_cargo(self, run): + self.assertEqual(test(["codeql-extractor-unified", "swift-syntax-rs"]), 0) + run.assert_not_called() + + +if __name__ == "__main__": + unittest.main()