Skip to content

Commit d26d94b

Browse files
committed
Expose arguments to allow overriding them.
1 parent 1de5252 commit d26d94b

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

Sources/XCTest/Private/ArgumentParser.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ internal struct ArgumentParser {
5050

5151
private let arguments: [String]
5252

53-
init(arguments: [String] = CommandLine.arguments) {
53+
init(arguments: [String]) {
5454
self.arguments = arguments
5555
}
5656

Sources/XCTest/Public/XCTestMain.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,12 @@
6060
/// - Parameter testCases: An array of test cases run, each produced by a call to the `testCase` function
6161
/// - seealso: `testCase`
6262
public func XCTMain(_ testCases: [XCTestCaseEntry]) -> Never {
63+
XCTMain(testCases, arguments: CommandLine.arguments)
64+
}
65+
public func XCTMain(_ testCases: [XCTestCaseEntry], arguments: [String]) -> Never {
6366
let testBundle = Bundle.main
6467

65-
let executionMode = ArgumentParser().executionMode
68+
let executionMode = ArgumentParser(arguments: arguments).executionMode
6669

6770
// Apple XCTest behaves differently if tests have been filtered:
6871
// - The root `XCTestSuite` is named "Selected tests" instead of
@@ -96,7 +99,7 @@ public func XCTMain(_ testCases: [XCTestCaseEntry]) -> Never {
9699
let errMsg = "Error: Invalid option \"\(invalid)\"\n"
97100
FileHandle.standardError.write(errMsg.data(using: .utf8) ?? Data())
98101
}
99-
let exeName = URL(fileURLWithPath: CommandLine.arguments[0]).lastPathComponent
102+
let exeName = URL(fileURLWithPath: arguments[0]).lastPathComponent
100103
let sampleTest = rootTestSuite.list().first ?? "Tests.FooTestCase/testFoo"
101104
let sampleTests = sampleTest.prefix(while: { $0 != "/" })
102105
print("""
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %{swiftc} %s -o %T/ArgumentParser
2+
// RUN: %T/ArgumentParser
3+
4+
#if os(macOS)
5+
import SwiftXCTest
6+
#else
7+
import XCTest
8+
#endif
9+
10+
class ArgumentParsingTestCase: XCTestCase {
11+
static var allTests = {
12+
return [
13+
("testFail", testFail),
14+
("testSuccess", testSuccess),
15+
]
16+
}()
17+
func testFail() { XCTFail("failure") }
18+
func testSuccess() { }
19+
}
20+
21+
let arguments = ["main", "\(String(reflecting: ArgumentParsingTestCase.self))/testSuccess"]
22+
XCTMain([testCase(ArgumentParsingTestCase.allTests)], arguments: arguments)

0 commit comments

Comments
 (0)