Skip to content

Commit b5a8093

Browse files
authored
Merge pull request #286 from gmittert/OHGODHELP
Add Help Printing to XCTest executables
2 parents aada646 + 6437a69 commit b5a8093

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

Sources/XCTest/Private/ArgumentParser.swift

+7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ internal struct ArgumentParser {
3636
/// Print a list of all the tests in the suite.
3737
case list(type: ListType)
3838

39+
/// Print Help
40+
case help(invalidOption: String?)
41+
3942
var selectedTestName: String? {
4043
if case .run(let name) = self {
4144
return name
@@ -58,6 +61,10 @@ internal struct ArgumentParser {
5861
return .list(type: .humanReadable)
5962
} else if arguments[1] == "--dump-tests-json" {
6063
return .list(type: .json)
64+
} else if arguments[1] == "--help" || arguments[1] == "-h" {
65+
return .help(invalidOption: nil)
66+
} else if let fst = arguments[1].first, fst == "-" {
67+
return .help(invalidOption: arguments[1])
6168
} else {
6269
return .run(selectedTestName: arguments[1])
6370
}

Sources/XCTest/Public/XCTestMain.swift

+35-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
/// Starts a test run for the specified test cases.
3030
///
31-
/// This function will not return. If the test cases pass, then it will call `exit(0)`. If there is a failure, then it will call `exit(1)`.
31+
/// This function will not return. If the test cases pass, then it will call `exit(EXIT_SUCCESS)`. If there is a failure, then it will call `exit(EXIT_FAILURE)`.
3232
/// Example usage:
3333
///
3434
/// class TestFoo: XCTestCase {
@@ -83,10 +83,41 @@ public func XCTMain(_ testCases: [XCTestCaseEntry]) -> Never {
8383
switch executionMode {
8484
case .list(type: .humanReadable):
8585
TestListing(testSuite: rootTestSuite).printTestList()
86-
exit(0)
86+
exit(EXIT_SUCCESS)
8787
case .list(type: .json):
8888
TestListing(testSuite: rootTestSuite).printTestJSON()
89-
exit(0)
89+
exit(EXIT_SUCCESS)
90+
case let .help(invalidOption):
91+
if let invalid = invalidOption {
92+
let errMsg = "Error: Invalid option \"\(invalid)\"\n"
93+
FileHandle.standardError.write(errMsg.data(using: .utf8) ?? Data())
94+
}
95+
let exeName = CommandLine.arguments[0].lastPathComponent
96+
let sampleTest = rootTestSuite.list().first ?? "Tests.FooTestCase/testFoo"
97+
let sampleTests = sampleTest.prefix(while: { $0 != "/" })
98+
print("""
99+
Usage: \(exeName) [OPTION]
100+
\(exeName) [TESTCASE]
101+
Run and report results of test cases.
102+
103+
With no OPTION or TESTCASE, runs all test cases.
104+
105+
OPTIONS:
106+
107+
-l, --list-test List tests line by line to standard output
108+
--dump-tests-json List tests in JSON to standard output
109+
110+
TESTCASES:
111+
112+
Run a single test
113+
114+
> \(exeName) \(sampleTest)
115+
116+
Run all the tests in \(sampleTests)
117+
118+
> \(exeName) \(sampleTests)
119+
""")
120+
exit(invalidOption == nil ? EXIT_SUCCESS : EXIT_FAILURE)
90121
case .run(selectedTestName: _):
91122
// Add a test observer that prints test progress to stdout.
92123
let observationCenter = XCTestObservationCenter.shared
@@ -96,6 +127,6 @@ public func XCTMain(_ testCases: [XCTestCaseEntry]) -> Never {
96127
rootTestSuite.run()
97128
observationCenter.testBundleDidFinish(testBundle)
98129

99-
exit(rootTestSuite.testRun!.totalFailureCount == 0 ? 0 : 1)
130+
exit(rootTestSuite.testRun!.totalFailureCount == 0 ? EXIT_SUCCESS : EXIT_FAILURE)
100131
}
101132
}

0 commit comments

Comments
 (0)