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

Implement --test_filter support #999

Merged
merged 4 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,12 @@ have the paths made absolute via swizzling by enabling the
set the `BUILD_WORKSPACE_DIRECTORY` environment variable in your scheme to the
root of your workspace (i.e. `$(SRCROOT)`).

A subset of tests for a given target can be executed via the `--test_filter` parameter:

```
bazel test //:Tests --test_filter=TestModuleName.TestClassName/testMethodName
```


**ATTRIBUTES**

Expand Down
51 changes: 51 additions & 0 deletions examples/apple/test_filter/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
load(
"//swift:swift.bzl",
"swift_library",
"swift_test",
)

swift_library(
name = "test_filter_lib",
testonly = True,
srcs = [
"FailTests.swift",
"PassFailTests.swift",
"PassTests.swift",
"TestHelper.swift",
],
module_name = "test_filter",
target_compatible_with = ["@platforms//os:macos"],
)

# Verify that tests fail as expected without test filtering.
swift_test(
name = "test_filter__baseline",
env = {
"EXPECT_FAILURE": "TRUE",
},
module_name = "test_filter",
target_compatible_with = ["@platforms//os:macos"],
deps = [":test_filter_lib"],
)

# Verify that test scope is filtered by TARGET.TEST_CLASS as expected.
swift_test(
name = "test_filter__feature__target_class",
env = {
"TESTBRIDGE_TEST_ONLY": "test_filter.PassTests",
},
module_name = "test_filter",
target_compatible_with = ["@platforms//os:macos"],
deps = [":test_filter_lib"],
)

# Verify that test scope is filtered by TARGET.TEST_CLASS.TEST_METHOD as expected.
swift_test(
name = "test_filter__feature__target_class_method",
env = {
"TESTBRIDGE_TEST_ONLY": "test_filter.PassFailTests/test_pass",
},
module_name = "test_filter",
target_compatible_with = ["@platforms//os:macos"],
deps = [":test_filter_lib"],
)
8 changes: 8 additions & 0 deletions examples/apple/test_filter/FailTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import XCTest

class FailTests: XCTestCase {

func test_fail() {
TestHelper.ExpectFailureIfNeeded()
}
}
12 changes: 12 additions & 0 deletions examples/apple/test_filter/PassFailTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import XCTest

class PassFailTests: XCTestCase {

func test_pass() {
TestHelper.Pass()
}

func test_fail() {
TestHelper.ExpectFailureIfNeeded()
}
}
8 changes: 8 additions & 0 deletions examples/apple/test_filter/PassTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import XCTest

class PassTests: XCTestCase {

func test_pass() {
TestHelper.Pass()
}
}
20 changes: 20 additions & 0 deletions examples/apple/test_filter/TestHelper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import XCTest

enum TestHelper {

static func ExpectFailureIfNeeded() {
let options: XCTExpectedFailure.Options = .init()
options.isEnabled = ProcessInfo.processInfo.environment["EXPECT_FAILURE"] == "TRUE"
XCTExpectFailure("Expected failure", options: options) {
Fail()
}
}

static func Pass() {
XCTAssertTrue(true)
}

private static func Fail() {
XCTFail("Fail")
}
}
6 changes: 6 additions & 0 deletions swift/internal/swift_binary_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,12 @@ have the paths made absolute via swizzling by enabling the
`"apple.swizzle_absolute_xcttestsourcelocation"` feature. You'll also need to
set the `BUILD_WORKSPACE_DIRECTORY` environment variable in your scheme to the
root of your workspace (i.e. `$(SRCROOT)`).

A subset of tests for a given target can be executed via the `--test_filter` parameter:

```
bazel test //:Tests --test_filter=TestModuleName.TestClassName/testMethodName
```
""",
executable = True,
fragments = ["cpp"],
Expand Down
4 changes: 2 additions & 2 deletions tools/xctest_runner/xctest_runner.sh.template
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ fi

exit_status=0
xctest=$(xcrun -f xctest)
# TODO(allevato): Support Bazel's --test_filter.
DYLD_INSERT_LIBRARIES=$sanitizer_dyld_env LLVM_PROFILE_FILE="$profraw" $xctest -XCTest All "$bundle_path" || exit_status=$?
test_filter=${TESTBRIDGE_TEST_ONLY:-All}
DYLD_INSERT_LIBRARIES=$sanitizer_dyld_env LLVM_PROFILE_FILE="$profraw" $xctest -XCTest "$test_filter" "$bundle_path" || exit_status=$?

if [[ "$exit_status" -ne 0 ]]; then
exit "$exit_status"
Expand Down