Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sharding and regex-based --test_filter in swift_test #1410

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

brentleyjones
Copy link
Collaborator

swift_test targets can now shard their execution using Bazel's standard shard_count test attribute. When Bazel runs a sharded test, it spawns N separate actions in parallel where each shard runs a distinct subset of the tests.

On both Apple and Linux, this is achieved by looking at the shard count and filtering out a subset of tests. This is straightforward on Linux where we generate the test runner. Making this work with Apple's xctest bundle runner would not have been feasible, so we've abandoned that approach and now treat swift_test like any other binary rule: we generate a plain executable (wrapped as an .xctest "bundle" on macOS, but not a Mach-O bundle type binary) and use XCTestSuite.default to access the dynamically discovered tests. From that object, we can recurse down the test suite/test case hierarchy and select the shard's subset to run.

This means we also can no longer use xctest's test filtering, so this change reimplements it manually. This turns out to be an improvement, because we can now use regular expressions to perform the filtering instead of being limited to XCTest's less flexible syntax. Tests will be represented with names of the form ClassName/MethodName, and --test_filter can be a regular expression that matches those.

PiperOrigin-RevId: 606703344
(cherry picked from commit 80cdccc, 1b1f348, 51b1d1c, and bf90194)

`swift_test` targets can now shard their execution using Bazel's standard `shard_count` test attribute. When Bazel runs a sharded test, it spawns _N_ separate actions in parallel where each shard runs a distinct subset of the tests.

On both Apple and Linux, this is achieved by looking at the shard count and filtering out a subset of tests. This is straightforward on Linux where we generate the test runner. Making this work with Apple's `xctest` bundle runner would not have been feasible, so we've abandoned that approach and now treat `swift_test` like any other binary rule: we generate a plain executable (wrapped as an `.xctest` "bundle" on macOS, but not a Mach-O bundle type binary) and use `XCTestSuite.default` to access the dynamically discovered tests. From that object, we can recurse down the test suite/test case hierarchy and select the shard's subset to run.

This means we also can no longer use `xctest`'s test filtering, so this change reimplements it manually. This turns out to be an improvement, because we can now use regular expressions to perform the filtering instead of being limited to XCTest's less flexible syntax. Tests will be represented with names of the form `ClassName/MethodName`, and `--test_filter` can be a regular expression that matches those.

PiperOrigin-RevId: 606703344
(cherry picked from commit 80cdccc, 1b1f348, 51b1d1c, and bf90194)
Signed-off-by: Brentley Jones <[email protected]>
brentleyjones referenced this pull request Oct 15, 2024
`swift_test` targets can now shard their execution using Bazel's standard `shard_count` test attribute. When Bazel runs a sharded test, it spawns _N_ separate actions in parallel where each shard runs a distinct subset of the tests.

On both Apple and Linux, this is achieved by looking at the shard count and filtering out a subset of tests. This is straightforward on Linux where we generate the test runner. Making this work with Apple's `xctest` bundle runner would not have been feasible, so we've abandoned that approach and now treat `swift_test` like any other binary rule: we generate a plain executable (wrapped as an `.xctest` "bundle" on macOS, but not a Mach-O bundle type binary) and use `XCTestSuite.default` to access the dynamically discovered tests. From that object, we can recurse down the test suite/test case hierarchy and select the shard's subset to run.

This means we also can no longer use `xctest`'s test filtering, so this change reimplements it manually. This turns out to be an improvement, because we can now use regular expressions to perform the filtering instead of being limited to XCTest's less flexible syntax. Tests will be represented with names of the form `ClassName/MethodName`, and `--test_filter` can be a regular expression that matches those.

PiperOrigin-RevId: 606703344
brentleyjones referenced this pull request Oct 15, 2024
…used.

Also allow the test filter to match any part of the test name, not just the whole string. Users can explicitly write `^...$` to force a whole string match.

PiperOrigin-RevId: 607074089
brentleyjones referenced this pull request Oct 15, 2024
…l to support sharding/filtering.

1) `XCTestSuite.default.run` doesn't automatically exit with a non-zero exit code when tests fail, so even failing tests were counted as passing (!). Handle this manually.
2) If Xcode is installed on the build machine at a different location than the test execution machine, the test binary will fail to load because it has the wrong machine's Xcode path in its rpaths. Weakly link to XCTest and `dlopen` the framework and support dylib at runtime to address this.

PiperOrigin-RevId: 609333464
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants