Skip to content

Report an error when attempting to use --filter or --skip in unsupported environments #163

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

Merged
merged 1 commit into from
Dec 19, 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
57 changes: 41 additions & 16 deletions Sources/Testing/Running/EntryPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,26 +172,31 @@ func configurationForSwiftPMEntryPoint(withArguments args: [String]) throws -> C
// constructed solely from a string, they are safe to send across isolation
// boundaries.
var filters = [Configuration.TestFilter]()
if #available(_regexAPI, *) {
if let filterArgIndex = args.firstIndex(of: "--filter"), filterArgIndex < args.endIndex {
let filterArg = args[args.index(after: filterArgIndex)]

let regex = try UncheckedSendable(rawValue: Regex(filterArg))
filters.append { test in
let id = String(describing: test.id)
return id.contains(regex.rawValue)
}
if let filterArgIndex = args.firstIndex(of: "--filter"), filterArgIndex < args.endIndex {
guard #available(_regexAPI, *) else {
throw _EntryPointError.featureUnavailable("The '--filter' option is not supported on this OS version.")
}
if let skipArgIndex = args.firstIndex(of: "--skip"), skipArgIndex < args.endIndex {
let skipArg = args[args.index(after: skipArgIndex)]

let regex = try UncheckedSendable(rawValue: Regex(skipArg))
filters.append { test in
let id = String(describing: test.id)
return !id.contains(regex.rawValue)
}
let filterArg = args[args.index(after: filterArgIndex)]
let regex = try UncheckedSendable(rawValue: Regex(filterArg))
filters.append { test in
let id = String(describing: test.id)
return id.contains(regex.rawValue)
}
}
if let skipArgIndex = args.firstIndex(of: "--skip"), skipArgIndex < args.endIndex {
guard #available(_regexAPI, *) else {
throw _EntryPointError.featureUnavailable("The '--skip' option is not supported on this OS version.")
}

let skipArg = args[args.index(after: skipArgIndex)]
let regex = try UncheckedSendable(rawValue: Regex(skipArg))
filters.append { test in
let id = String(describing: test.id)
return !id.contains(regex.rawValue)
}
}

configuration.testFilter = { [filters] test in
filters.allSatisfy { filter in
filter(test)
Expand Down Expand Up @@ -329,3 +334,23 @@ extension [Event.ConsoleOutputRecorder.Option] {
}
#endif
}

// MARK: - Error reporting

/// A type describing an error encountered in the entry point.
private enum _EntryPointError: Error {
/// A feature is unavailable.
///
/// - Parameters:
/// - explanation: An explanation of the problem.
case featureUnavailable(_ explanation: String)
}

extension _EntryPointError: CustomStringConvertible {
var description: String {
switch self {
case let .featureUnavailable(explanation):
explanation
}
}
}
3 changes: 3 additions & 0 deletions Tests/TestingTests/SwiftPMTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct SwiftPMTests {
}

@Test("--filter argument")
@available(_regexAPI, *)
func filter() throws {
let configuration = try configurationForSwiftPMEntryPoint(withArguments: ["PATH", "--filter", "hello"])
let testFilter = try #require(configuration.testFilter)
Expand All @@ -62,6 +63,7 @@ struct SwiftPMTests {
}

@Test("--skip argument")
@available(_regexAPI, *)
func skip() throws {
let configuration = try configurationForSwiftPMEntryPoint(withArguments: ["PATH", "--skip", "hello"])
let testFilter = try #require(configuration.testFilter)
Expand All @@ -82,6 +84,7 @@ struct SwiftPMTests {
}

@Test("--filter/--skip arguments and .hidden trait")
@available(_regexAPI, *)
func filterAndSkipAndHidden() throws {
let configuration = try configurationForSwiftPMEntryPoint(withArguments: ["PATH", "--filter", "hello", "--skip", "hello2"])
let testFilter = try #require(configuration.testFilter)
Expand Down