diff --git a/Sources/XCTest/Private/ArgumentParser.swift b/Sources/XCTest/Private/ArgumentParser.swift index d30f86fb..a1a348c9 100644 --- a/Sources/XCTest/Private/ArgumentParser.swift +++ b/Sources/XCTest/Private/ArgumentParser.swift @@ -50,8 +50,37 @@ internal struct ArgumentParser { private let arguments: [String] + /// Filter out arguments from an arguments array that XCTest doesn't care about. + /// + /// - Parameters: + /// - arguments: The arguments array to filter. + /// + /// - Returns: A copy of `arguments` with ignored arguments removed. + private static func removeIgnoredArguments(from arguments: [String]) -> [String] { + var result = [String]() + result.reserveCapacity(arguments.count) + + var iterator = arguments.makeIterator() + while let argument = iterator.next() { + // Filter out any arguments of the form "--testing-library=xctest". + if argument.starts(with: "--testing-library=") { + continue + } + + // Next, filter out any split arguments ("--testing-library xctest") + if argument == "--testing-library" { + _ = iterator.next() // skip subsequent value + continue + } + + result.append(argument) + } + + return result + } + init(arguments: [String]) { - self.arguments = arguments + self.arguments = Self.removeIgnoredArguments(from: arguments) } var executionMode: ExecutionMode { diff --git a/Tests/Functional/ArgumentParserWithIgnoredArgs/main.swift b/Tests/Functional/ArgumentParserWithIgnoredArgs/main.swift new file mode 100644 index 00000000..8c0fbfe7 --- /dev/null +++ b/Tests/Functional/ArgumentParserWithIgnoredArgs/main.swift @@ -0,0 +1,22 @@ +// RUN: %{swiftc} %s -o %T/ArgumentParserWithIgnoredArgs +// RUN: %T/ArgumentParserWithIgnoredArgs + +#if os(macOS) + import SwiftXCTest +#else + import XCTest +#endif + +class ArgumentParsingWithIgnoredArgsTestCase: XCTestCase { + static var allTests = { + return [ + ("testFail", testFail), + ("testSuccess", testSuccess), + ] + }() + func testFail() { XCTFail("failure") } + func testSuccess() { } +} + +let arguments = ["main", "--testing-library=xctest", "--testing-library", "xctest", "\(String(reflecting: ArgumentParsingWithIgnoredArgsTestCase.self))/testSuccess", "--testing-library",] +XCTMain([testCase(ArgumentParsingWithIgnoredArgsTestCase.allTests)], arguments: arguments)