Merge pull request #2503 from aiongg/ci-test-select

Workspace: pull requests run only the tests their change reaches
This commit is contained in:
aiongg
2026-11-20 13:01:27 -05:00
committed by GitHub
6 changed files with 104 additions and 29 deletions
+3 -3
View File
@@ -111,7 +111,7 @@ workflows:
'.github/workflows/workspace.yml':
- 'actions/checkout@v7.0.1'
- 'step-security/harden-runner@v2.22.4'
- 'storytold/craft-common-actions@d65d611f0ef67eaa94039351be5afce61102dbb0'
- 'storytold/craft-common-actions@0bef64f155fcea81edbd9542381777358bfbdcf4'
dependencies:
'actions/cache@v6.1.0':
ref: 'v6.1.0'
@@ -153,8 +153,8 @@ dependencies:
commit: 'sha1-76747d22a7567cc4438546a84f11bdf18e670ab8'
owner_id: 88700172
repo_id: 1382753260
'storytold/craft-common-actions@d65d611f0ef67eaa94039351be5afce61102dbb0':
'storytold/craft-common-actions@0bef64f155fcea81edbd9542381777358bfbdcf4':
ref: 'main'
commit: 'sha1-d65d611f0ef67eaa94039351be5afce61102dbb0'
commit: 'sha1-0bef64f155fcea81edbd9542381777358bfbdcf4'
owner_id: 76897702
repo_id: 1414429108
+34 -11
View File
@@ -24,16 +24,23 @@ concurrency:
cancel-in-progress: ${{ github.event_name == 'pull_request' || github.event_name == 'push' && github.ref == 'refs/heads/main' }}
jobs:
# Whether this run needs the macOS and Windows legs. On a pull request they run only when it can
# change platform behaviour: a crate with platform cfg code or one that depends on it, the build
# setup, or a file outside the crates (craft-common-actions' select-platforms). The full-ci label
# runs them anyway from the next push or manual run; every other event runs them.
# Whether this run needs the macOS and Windows legs, and which tests every leg runs
# (craft-common-actions' select-platforms). On a pull request the macOS and Windows legs run only
# when it can change platform behaviour: a crate with platform cfg code or one that depends on
# it, the build setup, or a file outside the crates. The tests are those of the crates a change
# reaches: the crates that own a changed file, the crates whose Cargo.toml declares a read of it
# (`[package.metadata.craft] reads`, which this job checks is complete), and every crate that
# depends on those. The build setup, a crate's own Cargo.toml or a file outside the crates other
# than documentation runs every test; documentation alone runs none. The full-ci label runs
# everything from the next push or manual run; every other event runs everything.
platforms:
name: select platforms
runs-on: ubuntu-latest
timeout-minutes: 5
outputs:
platform: ${{ steps.select.outputs.platform }}
tests: ${{ steps.select.outputs.tests }}
filter: ${{ steps.select.outputs.filter }}
steps:
- name: Harden runner
uses: step-security/harden-runner@v2.22.4
@@ -46,10 +53,11 @@ jobs:
persist-credentials: false
fetch-depth: 2
- id: select
uses: storytold/craft-common-actions/actions/select-platforms@d65d611f0ef67eaa94039351be5afce61102dbb0
uses: storytold/craft-common-actions/actions/select-platforms@0bef64f155fcea81edbd9542381777358bfbdcf4
with:
os: '["ubuntu-latest","windows-latest","macos-latest"]'
select-os: '["windows-latest","macos-latest"]'
select-tests: true
label: full-ci
# The Linux leg keeps the compiled workspace crates between runs (`cache-workspace-crates`): each
@@ -61,6 +69,7 @@ jobs:
# cold lint leg is still shorter than its gate leg.
native:
name: Workspace
needs: platforms
uses: storytold/craft-common-actions/.github/workflows/gate.yml@main
with:
os: '["ubuntu-latest"]'
@@ -79,10 +88,13 @@ jobs:
# The fuzz-style tests run a fraction of their cases here: every hostile-input hammer and
# sweep 1/8 (craft-testkit's CRAFT_HOSTILE_DIVISOR), every proptest without its own case
# count 64 cases instead of 256. nightly.yml runs them all in full.
# CRAFT_TESTS and CRAFT_TEST_FILTER are the `platforms` job's test selection.
env: &env |
CRAFT_PRINT_REQUIRE_QUEUE=0
CRAFT_HOSTILE_DIVISOR=8
PROPTEST_CASES=64
CRAFT_TESTS=${{ needs.platforms.outputs.tests }}
CRAFT_TEST_FILTER=${{ needs.platforms.outputs.filter }}
setup-command: &setup bash scripts/ci-test-inputs.sh
# The PhotoCraft corpora, keyed on the script that pins them: a hit skips the PhotoCraft
# checkout and its xtask build (1:51 on Windows).
@@ -109,10 +121,17 @@ jobs:
# tests run (stubicc, h264_h264enc_bench); clippy in the lint leg covers the rest. The default-feature compile is the lint leg's
# first clippy run; the last command runs the crates whose tests check behaviour with a
# feature off, which an all-features build cannot reach.
cargo nextest run --workspace --all-features --locked --profile ci --lib --bins --tests
cargo nextest run -p craft-aep -p craft-video --all-features --locked --profile ci --example stubicc --example h264_h264enc_bench
# Every build is the whole workspace's, as on main; the filter narrows only the run. The
# doctests run for the whole workspace: `cargo test -p <selected>` would resolve features
# over the selected crates only and rebuild the dependencies whose features differ.
if [ "$CRAFT_TESTS" = none ]; then
echo "No crate changed: no tests to run."
exit 0
fi
cargo nextest run --workspace --all-features --locked --profile ci -E "$CRAFT_TEST_FILTER" --no-tests=warn --lib --bins --tests
cargo nextest run -p craft-aep -p craft-video --all-features --locked --profile ci -E "$CRAFT_TEST_FILTER" --no-tests=warn --example stubicc --example h264_h264enc_bench
cargo test --workspace --doc --all-features --locked
cargo nextest run -p craft-image-codecs -p craft-text --locked --profile ci --lib --bins --tests
cargo nextest run -p craft-image-codecs -p craft-text --locked --profile ci -E "$CRAFT_TEST_FILTER" --no-tests=warn --lib --bins --tests
# The macOS and Windows legs, when `platforms` says this run needs them, with the Linux leg's
# inputs.
@@ -142,11 +161,15 @@ jobs:
lint-command: *lint
command: *command
windows-command: |
if [ "$CRAFT_TESTS" = none ]; then
echo "No crate changed: no tests to run."
exit 0
fi
powershell.exe -NoProfile -Command '$ErrorActionPreference = "Stop"; Start-Service Spooler; Add-PrinterDriver -Name "Generic / Text Only"'
cargo nextest run --workspace --all-features --locked --profile ci --lib --bins --tests
cargo nextest run -p craft-aep -p craft-video --all-features --locked --profile ci --example stubicc --example h264_h264enc_bench
cargo nextest run --workspace --all-features --locked --profile ci -E "$CRAFT_TEST_FILTER" --no-tests=warn --lib --bins --tests
cargo nextest run -p craft-aep -p craft-video --all-features --locked --profile ci -E "$CRAFT_TEST_FILTER" --no-tests=warn --example stubicc --example h264_h264enc_bench
cargo test --workspace --doc --all-features --locked
cargo nextest run -p craft-image-codecs -p craft-text --locked --profile ci --lib --bins --tests
cargo nextest run -p craft-image-codecs -p craft-text --locked --profile ci -E "$CRAFT_TEST_FILTER" --no-tests=warn --lib --bins --tests
# Each main run's Linux leg saves a rolling cache entry of its own (`cache-workspace-crates`), and
# every run restores only the newest. This deletes the older ones as soon as the Linux leg is
@@ -16,3 +16,10 @@ craft-fonts-build = { path = "../.." }
[lints]
workspace = true
# Files of other crates that this crate's build or tests read. CI selects this crate's tests
# when one of them changes (craft-common-actions' select-platforms).
[package.metadata.craft]
reads = [
"crates/fonts-build/tests/fixture/craft-fonts",
]
+9
View File
@@ -10,6 +10,15 @@ rust-version.workspace = true
[lints]
workspace = true
# Files of other crates that this crate's build or tests read. CI selects this crate's tests
# when one of them changes (craft-common-actions' select-platforms).
[package.metadata.craft]
reads = [
"crates/cms/profiles/craft-coated-fogra39.icc",
"crates/cms/profiles/photocraft-coated-cmyk.icc",
"crates/fonts-build/tests/fixture/subset/Tiny-glyf.ttf",
]
[features]
default = []
read = ["dep:hayro-syntax", "dep:thiserror"]
+7
View File
@@ -10,6 +10,13 @@ repository.workspace = true
[lints]
workspace = true
# Files of other crates that this crate's build or tests read. CI selects this crate's tests
# when one of them changes (craft-common-actions' select-platforms).
[package.metadata.craft]
reads = [
"crates/ui/tests/fonts/NotoSans-test.ttf",
]
[dependencies]
craft-xml = { workspace = true }
quick-xml = { version = "0.41", default-features = false }
+44 -15
View File
@@ -33,17 +33,25 @@ fi
cargo clippy --workspace --all-targets --locked -- -D warnings
cargo clippy --workspace --all-targets --all-features --locked -- -D warnings
"""
TEST_SOURCE = """
cargo nextest run --workspace --all-features --locked --profile ci --lib --bins --tests
cargo nextest run -p craft-aep -p craft-video --all-features --locked --profile ci --example stubicc --example h264_h264enc_bench
cargo test --workspace --doc --all-features --locked
cargo nextest run -p craft-image-codecs -p craft-text --locked --profile ci --lib --bins --tests
# The `platforms` job's selection: no tests when no crate changed, otherwise a nextest filter.
SELECT_GUARD = """
if [ "$CRAFT_TESTS" = none ]; then
echo "No crate changed: no tests to run."
exit 0
fi
"""
TEST_RUN = """
cargo nextest run --workspace --all-features --locked --profile ci -E "$CRAFT_TEST_FILTER" --no-tests=warn --lib --bins --tests
cargo nextest run -p craft-aep -p craft-video --all-features --locked --profile ci -E "$CRAFT_TEST_FILTER" --no-tests=warn --example stubicc --example h264_h264enc_bench
cargo test --workspace --doc --all-features --locked
cargo nextest run -p craft-image-codecs -p craft-text --locked --profile ci -E "$CRAFT_TEST_FILTER" --no-tests=warn --lib --bins --tests
"""
TEST_SOURCE = SELECT_GUARD + TEST_RUN
WINDOWS_SETUP = """powershell.exe -NoProfile -Command '$ErrorActionPreference = "Stop"; Start-Service Spooler; Add-PrinterDriver -Name "Generic / Text Only"'"""
EXPECTED_SOURCE = {
"lint-command": LINT_SOURCE,
"command": TEST_SOURCE,
"windows-command": WINDOWS_SETUP + "\n" + TEST_SOURCE,
"windows-command": SELECT_GUARD + WINDOWS_SETUP + "\n" + TEST_RUN,
}
@@ -78,11 +86,12 @@ esac
"""
def commands(os_name, fail="", name="command", source=None):
def commands(os_name, fail="", name="command", source=None, tests="all", test_filter="all()"):
validate_source(WORKFLOW.read_text() if source is None else source, name)
return subprocess.run(
["bash", "-euo", "pipefail", "-c", HARNESS],
env={**os.environ, "RUNNER_OS": os_name, "FAIL": fail, "BLOCK": name},
env={**os.environ, "RUNNER_OS": os_name, "FAIL": fail, "BLOCK": name,
"CRAFT_TESTS": tests, "CRAFT_TEST_FILTER": test_filter},
text=True, capture_output=True, check=False,
)
@@ -92,12 +101,12 @@ COMMON = [
"clippy --workspace --all-targets --locked -- -D warnings",
"clippy --workspace --all-targets --all-features --locked -- -D warnings",
]
TESTS = [
"nextest run --workspace --all-features --locked --profile ci --lib --bins --tests",
"nextest run -p craft-aep -p craft-video --all-features --locked --profile ci --example stubicc --example h264_h264enc_bench",
"test --workspace --doc --all-features --locked",
"nextest run -p craft-image-codecs -p craft-text --locked --profile ci --lib --bins --tests",
]
def test_commands(test_filter="all()"):
return [line.strip()[len("cargo "):].replace('"$CRAFT_TEST_FILTER"', test_filter)
for line in TEST_RUN.splitlines() if line.strip()]
TESTS = test_commands()
class WorkspaceTests(unittest.TestCase):
@@ -118,6 +127,20 @@ class WorkspaceTests(unittest.TestCase):
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(result.stdout.splitlines(), TESTS)
def test_a_selection_filters_every_nextest_run_and_none_runs_nothing(self):
selected = "package(=craft-geom) | package(=craft-svg)"
result = commands("Linux", tests="some", test_filter=selected)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(result.stdout.splitlines(), test_commands(selected))
# The doctests stay whole-workspace; every nextest run carries the filter.
self.assertIn("test --workspace --doc --all-features --locked", result.stdout.splitlines())
for line in result.stdout.splitlines():
if line.startswith("nextest"):
self.assertIn(f"-E {selected} --no-tests=warn", line)
result = commands("Linux", tests="none", test_filter="none()")
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(result.stdout.splitlines(), ["No crate changed: no tests to run."])
def test_each_stage_failure_remains_fatal(self):
for name, stages in (("lint-command", COMMON), ("command", TESTS)):
for index, command in enumerate(stages):
@@ -130,7 +153,7 @@ class WorkspaceTests(unittest.TestCase):
validate_source(WORKFLOW.read_text(), "windows-command")
source = block(WORKFLOW.read_text(), "windows-command")
cargo = [line.strip() for line in source.splitlines() if line.strip().startswith("cargo ")]
self.assertEqual(cargo, ["cargo " + command for command in TESTS])
self.assertEqual(cargo, [line.strip() for line in TEST_RUN.splitlines() if line.strip()])
def test_workflow_source_is_rejected_before_starting_any_process(self):
source = WORKFLOW.read_text()
@@ -179,6 +202,12 @@ class WorkspaceTests(unittest.TestCase):
self.assertIn('os: \'["ubuntu-latest"]\'', source)
self.assertIn('os: \'["windows-latest","macos-latest"]\'', source)
self.assertIn("if: needs.platforms.outputs.platform == 'true'", source)
# Every leg reads the test selection of the platforms job.
self.assertIn(" native:\n name: Workspace\n needs: platforms\n", source)
self.assertIn(" select-tests: true\n", source)
self.assertIn(" CRAFT_TESTS=${{ needs.platforms.outputs.tests }}\n"
" CRAFT_TEST_FILTER=${{ needs.platforms.outputs.filter }}\n", source)
self.assertIn(" env: *env\n", source)
# The macOS and Windows legs run the Linux leg's blocks, not copies of them.
self.assertIn(" lint-command: &lint |\n", source)
self.assertIn(" command: &command |\n", source)