Skip to content

Commit 84960a2

Browse files
committed
Add -h, --help to swift test and send argument to -XCTest directly
1 parent 989f373 commit 84960a2

File tree

5 files changed

+84
-24
lines changed

5 files changed

+84
-24
lines changed

Sources/Multitool/Error.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import libc
1515

1616
public enum CommandLineError: ErrorType {
1717
public enum UsageMode {
18-
case Print, Imply
18+
case Print, ImplySwiftBuild, ImplySwiftTest
1919
}
2020
case InvalidUsage(String, UsageMode)
2121
}
@@ -40,8 +40,10 @@ extension Error: CustomStringConvertible {
4040

4141
if isatty(fileno(libc.stdin)) {
4242
switch mode {
43-
case .Imply:
43+
case .ImplySwiftBuild:
4444
print("enter `swift build --help' for usage information", toStream: &stderr)
45+
case .ImplySwiftTest:
46+
print("enter `swift test --help' for usage information", toStream: &stderr)
4547
case .Print:
4648
print("", toStream: &stderr)
4749
usage { print($0, toStream: &stderr) }

Sources/swift-build/usage.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func parse(commandLineArguments args: [String]) throws -> (Mode, Options) {
9999
case (let ignoredArgument?, .Usage):
100100
throw CommandLineError.InvalidUsage("Both --help and \(ignoredArgument) specified", .Print)
101101
case (let oldMode?, let newMode):
102-
throw CommandLineError.InvalidUsage("Multiple modes specified: \(oldMode), \(newMode)", .Imply)
102+
throw CommandLineError.InvalidUsage("Multiple modes specified: \(oldMode), \(newMode)", .ImplySwiftBuild)
103103
case (nil, .Build):
104104
switch try cruncher.peek() {
105105
case .Name("debug")?:
@@ -109,7 +109,7 @@ func parse(commandLineArguments args: [String]) throws -> (Mode, Options) {
109109
mode = .Build(.Release)
110110
cruncher.postPeekPop()
111111
case .Name(let name)?:
112-
throw CommandLineError.InvalidUsage("Unknown build configuration: \(name)", .Imply)
112+
throw CommandLineError.InvalidUsage("Unknown build configuration: \(name)", .ImplySwiftBuild)
113113
default:
114114
break
115115
}
@@ -124,7 +124,7 @@ func parse(commandLineArguments args: [String]) throws -> (Mode, Options) {
124124
mode = .Init(.Library)
125125
cruncher.postPeekPop()
126126
case .Name(let name)?:
127-
throw CommandLineError.InvalidUsage("Unknown init mode: \(name)", .Imply)
127+
throw CommandLineError.InvalidUsage("Unknown init mode: \(name)", .ImplySwiftBuild)
128128
default:
129129
break
130130
}
@@ -137,7 +137,7 @@ func parse(commandLineArguments args: [String]) throws -> (Mode, Options) {
137137
mode = .Clean(.Dist)
138138
cruncher.postPeekPop()
139139
case .Name(let name)?:
140-
throw CommandLineError.InvalidUsage("Unknown clean mode: \(name)", .Imply)
140+
throw CommandLineError.InvalidUsage("Unknown clean mode: \(name)", .ImplySwiftBuild)
141141
default:
142142
break
143143
}
@@ -153,14 +153,14 @@ func parse(commandLineArguments args: [String]) throws -> (Mode, Options) {
153153
cruncher.postPeekPop()
154154
opts.chdir = name
155155
default:
156-
throw CommandLineError.InvalidUsage("Option `--chdir' requires subsequent directory argument", .Imply)
156+
throw CommandLineError.InvalidUsage("Option `--chdir' requires subsequent directory argument", .ImplySwiftBuild)
157157
}
158158

159159
case .Switch(.Verbose):
160160
opts.verbosity += 1
161161

162162
case .Name(let name):
163-
throw CommandLineError.InvalidUsage("Unknown argument: \(name)", .Imply)
163+
throw CommandLineError.InvalidUsage("Unknown argument: \(name)", .ImplySwiftBuild)
164164

165165
case .Switch(.Xcc):
166166
opts.Xcc.append(try cruncher.rawPop())
@@ -271,14 +271,14 @@ private struct Cruncher {
271271
}
272272

273273
guard !arg.hasPrefix("-") else {
274-
throw CommandLineError.InvalidUsage("unknown argument: \(arg)", .Imply)
274+
throw CommandLineError.InvalidUsage("unknown argument: \(arg)", .ImplySwiftBuild)
275275
}
276276

277277
return .Name(arg)
278278
}
279279

280280
mutating func rawPop() throws -> String {
281-
guard args.count > 0 else { throw CommandLineError.InvalidUsage("expected argument", .Imply) }
281+
guard args.count > 0 else { throw CommandLineError.InvalidUsage("expected argument", .ImplySwiftBuild) }
282282
return args.removeFirst()
283283
}
284284

Sources/swift-test/main.swift

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,26 @@ public var globalSymbolInMainBinary = 0
1818
Resources.initialize(&globalSymbolInMainBinary)
1919

2020
do {
21-
let dir = try directories()
22-
23-
//FIXME this is a hack for SwiftPM’s own tests
24-
setenv("SPM_INSTALL_PATH", dir.build, 0)
25-
26-
let yamlPath = Path.join(dir.build, "debug.yaml")
27-
guard yamlPath.exists else { throw Error.DebugYAMLNotFound }
21+
let args = Array(Process.arguments.dropFirst())
22+
let mode = try parse(commandLineArguments: args)
23+
24+
switch mode {
25+
case .Usage:
26+
usage()
27+
case .Run(let xctestArg):
28+
let dir = try directories()
29+
30+
//FIXME this is a hack for SwiftPM’s own tests
31+
setenv("SPM_INSTALL_PATH", dir.build, 0)
32+
33+
let yamlPath = Path.join(dir.build, "debug.yaml")
34+
guard yamlPath.exists else { throw Error.DebugYAMLNotFound }
35+
36+
try build(YAMLPath: yamlPath, target: "test")
37+
let success = try test(dir.build, "debug", xctestArg: xctestArg)
38+
exit(success ? 0 : 1)
39+
}
2840

29-
try build(YAMLPath: yamlPath, target: "test")
30-
let success = try test(dir.build, "debug")
31-
exit(success ? 0 : 1)
32-
3341
} catch {
34-
handleError(error, usage: { _ in })
42+
handleError(error, usage: usage)
3543
}

Sources/swift-test/test.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@
1111
import PackageType
1212
import Utility
1313

14-
func test(path: String..., args: String? = nil) throws -> Bool {
14+
func test(path: String..., xctestArg: String? = nil) throws -> Bool {
1515
let path = Path.join(path)
1616
var args: [String] = []
1717
let testsPath: String
1818

1919
#if os(OSX)
2020
testsPath = Path.join(path, "Package.xctest")
2121
args = ["xcrun", "xctest"]
22-
args += Process.arguments.dropFirst()
22+
if let xctestArg = xctestArg {
23+
args += ["-XCTest", xctestArg]
24+
}
2325
#else
26+
//FIXME: Pass xctestArg when swift-corelibs-xctest supports it
2427
testsPath = Path.join(path, "test-Package")
2528
#endif
2629

Sources/swift-test/usage.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright 2015 - 2016 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import enum Multitool.CommandLineError
12+
13+
func usage(print: (String) -> Void = { print($0) }) {
14+
//.........10.........20.........30.........40.........50.........60.........70..
15+
print("OVERVIEW: Build and run tests")
16+
print("")
17+
print("USAGE: swift test [options]")
18+
print("")
19+
print("OPTIONS:")
20+
print(" TestModule.TestCase Run a test case subclass")
21+
print(" TestModule.TestCase/test1 Run a specific test method")
22+
}
23+
24+
enum Mode {
25+
case Usage
26+
case Run(String)
27+
}
28+
29+
func parse(commandLineArguments args: [String]) throws -> Mode {
30+
31+
if args.count == 0 {
32+
return .Run("All")
33+
}
34+
35+
guard let argument = args.first where args.count == 1 else {
36+
throw CommandLineError.InvalidUsage("Unknown arguments: \(args)", .ImplySwiftTest)
37+
}
38+
39+
switch argument {
40+
case "--help", "-h":
41+
return .Usage
42+
case argument where argument.hasPrefix("-"):
43+
throw CommandLineError.InvalidUsage("Unknown argument: \(argument)", .ImplySwiftTest)
44+
default:
45+
return .Run(argument)
46+
}
47+
}

0 commit comments

Comments
 (0)