Skip to content

Add Help Printing to XCTest executables #286

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
Oct 26, 2019
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
7 changes: 7 additions & 0 deletions Sources/XCTest/Private/ArgumentParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ internal struct ArgumentParser {
/// Print a list of all the tests in the suite.
case list(type: ListType)

/// Print Help
case help(invalidOption: String?)

var selectedTestName: String? {
if case .run(let name) = self {
return name
Expand All @@ -58,6 +61,10 @@ internal struct ArgumentParser {
return .list(type: .humanReadable)
} else if arguments[1] == "--dump-tests-json" {
return .list(type: .json)
} else if arguments[1] == "--help" || arguments[1] == "-h" {
return .help(invalidOption: nil)
} else if let fst = arguments[1].first, fst == "-" {
return .help(invalidOption: arguments[1])
} else {
return .run(selectedTestName: arguments[1])
}
Expand Down
39 changes: 35 additions & 4 deletions Sources/XCTest/Public/XCTestMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

/// Starts a test run for the specified test cases.
///
/// 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)`.
/// 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)`.
/// Example usage:
///
/// class TestFoo: XCTestCase {
Expand Down Expand Up @@ -83,10 +83,41 @@ public func XCTMain(_ testCases: [XCTestCaseEntry]) -> Never {
switch executionMode {
case .list(type: .humanReadable):
TestListing(testSuite: rootTestSuite).printTestList()
exit(0)
exit(EXIT_SUCCESS)
case .list(type: .json):
TestListing(testSuite: rootTestSuite).printTestJSON()
exit(0)
exit(EXIT_SUCCESS)
case let .help(invalidOption):
if let invalid = invalidOption {
let errMsg = "Error: Invalid option \"\(invalid)\"\n"
FileHandle.standardError.write(errMsg.data(using: .utf8) ?? Data())
}
let exeName = CommandLine.arguments[0].lastPathComponent
let sampleTest = rootTestSuite.list().first ?? "Tests.FooTestCase/testFoo"
let sampleTests = sampleTest.prefix(while: { $0 != "/" })
print("""
Usage: \(exeName) [OPTION]
\(exeName) [TESTCASE]
Run and report results of test cases.

With no OPTION or TESTCASE, runs all test cases.

OPTIONS:

-l, --list-test List tests line by line to standard output
--dump-tests-json List tests in JSON to standard output

TESTCASES:

Run a single test

> \(exeName) \(sampleTest)

Run all the tests in \(sampleTests)

> \(exeName) \(sampleTests)
""")
exit(invalidOption == nil ? EXIT_SUCCESS : EXIT_FAILURE)
case .run(selectedTestName: _):
// Add a test observer that prints test progress to stdout.
let observationCenter = XCTestObservationCenter.shared
Expand All @@ -96,6 +127,6 @@ public func XCTMain(_ testCases: [XCTestCaseEntry]) -> Never {
rootTestSuite.run()
observationCenter.testBundleDidFinish(testBundle)

exit(rootTestSuite.testRun!.totalFailureCount == 0 ? 0 : 1)
exit(rootTestSuite.testRun!.totalFailureCount == 0 ? EXIT_SUCCESS : EXIT_FAILURE)
}
}