Skip to content
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
46 changes: 28 additions & 18 deletions Sources/Testing/Running/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,38 +118,48 @@ public struct Configuration: Sendable {
/// - Returns: A Boolean value representing if the test satisfied the filter.
public typealias TestFilter = @Sendable (Test) -> Bool

/// Storage for ``testFilter``.
private var _testFilter: TestFilter = { !$0.isHidden }

/// The test filter to which tests should be filtered when run.
public var testFilter: TestFilter?
public var testFilter: TestFilter {
get {
_testFilter
}
set {
// By default, the test filter should always filter out hidden tests. This
// is the appropriate behavior for external clients of this SPI. If the
// testing library needs to enable hidden tests in its own test targets,
// it should use setTestFilter(toMatch:includeHiddenTests:) instead.
_testFilter = { test in
!test.isHidden && newValue(test)
}
}
}

/// Filter tests to run to those specified via a set of test IDs.
///
/// - Parameters:
/// - selection: A set of test IDs to be filtered. If `nil`, the current
/// selection is cleared.
/// - selection: A set of test IDs to be filtered.
///
/// By default, all tests are run and no filter is set.
public mutating func setTestFilter(toMatch selection: Set<Test.ID>?) {
setTestFilter(toMatch: selection.map(Test.ID.Selection.init), includeHiddenTests: false)
public mutating func setTestFilter(toMatch selection: Set<Test.ID>) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By making the argument here non-optional, we simplify the logic quite a bit. If a caller wants to reset the test filter, they can set it to { _ in true }, but in practice I don't think mutating a test filter in-flight is a concern for us.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay!

setTestFilter(toMatch: Test.ID.Selection(testIDs: selection), includeHiddenTests: false)
}

/// Filter tests to run to those specified via a Test.ID.Selection instance.
/// Filter tests to run to those specified by a selection of test IDs.
///
/// - Parameters:
/// - selection: A selection of test IDs to be filtered. If `nil`, the
/// current selection is cleared.
/// - includeHiddenTests: If false, a test annotated with the `.hidden` trait will not be included, even if its ID is present in `selection`.
/// - selection: A selection of test IDs to be filtered.
/// - includeHiddenTests: If false, a test annotated with the `.hidden`
/// trait will not be included, even if its ID is present in `selection`.
///
/// By default, all tests are run and no filter is set.
mutating func setTestFilter(toMatch selection: Test.ID.Selection?, includeHiddenTests: Bool) {
if let selection {
testFilter = { test in
if includeHiddenTests || !test.isHidden {
return selection.contains(test)
}
return false
}
mutating func setTestFilter(toMatch selection: Test.ID.Selection, includeHiddenTests: Bool) {
if includeHiddenTests {
_testFilter = selection.contains
} else {
testFilter = nil
testFilter = selection.contains
}
}
}
4 changes: 0 additions & 4 deletions Sources/Testing/Running/EntryPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,6 @@ func configurationForSwiftPMEntryPoint(withArguments args: [String]) throws -> C
}
}
}
filters.append { test in
// Don't run the fixture tests in the testing library's own test targets.
!test.isHidden
}
configuration.testFilter = { [filters] test in
filters.allSatisfy { filter in
filter(test)
Expand Down
19 changes: 1 addition & 18 deletions Sources/Testing/Running/Runner.Plan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,6 @@ extension Runner.Plan {
}
}

/// Determine if a test is included in the selected test IDs, if any are
/// configured.
///
/// - Parameters:
/// - test: The test to query.
/// - filter: The filter to decide if the test is included.
///
/// - Returns: Whether or not the specified test is selected. If
/// `selectedTests` is `nil`, `test` is considered selected if it is not
/// hidden.
private static func _isTestIncluded(_ test: Test, using filter: Configuration.TestFilter?) -> Bool {
guard let filter else {
return !test.isHidden
}
return filter(test)
}

/// Construct a graph of runner plan steps for the specified tests.
///
/// - Parameters:
Expand Down Expand Up @@ -176,7 +159,7 @@ extension Runner.Plan {
// zip() near the end of the function.
testGraph = testGraph.mapValues { test in
test.flatMap { test in
_isTestIncluded(test, using: configuration.testFilter) ? test : nil
configuration.testFilter(test) ? test : nil
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Testing/Running/XCTestScaffold.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public enum XCTestScaffold: Sendable {
if let tags {
// Check if the test's tags intersect the set of selected tags. If there
// was a previous filter function, it must also pass.
let oldTestFilter = configuration.testFilter ?? { _ in true }
let oldTestFilter = configuration.testFilter
configuration.testFilter = { test in
!tags.isDisjoint(with: test.tags) && oldTestFilter(test)
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/TestingTests/RunnerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct NeverRunTests {
final class RunnerTests: XCTestCase {
func testDefaultInit() async throws {
let runner = await Runner()
XCTAssertFalse(runner.tests.contains { $0.isHidden })
XCTAssertFalse(runner.tests.contains(where: \.isHidden))
}

func testTestsProperty() async throws {
Expand Down