Skip to content

Commit 4d0e4fc

Browse files
committed
Add -h, --help to swift test and send argument to -XCTest directly
1 parent 987ea09 commit 4d0e4fc

File tree

5 files changed

+81
-21
lines changed

5 files changed

+81
-21
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: 7 additions & 7 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
}
@@ -126,7 +126,7 @@ func parse(commandLineArguments args: [String]) throws -> (Mode, Options) {
126126
mode = .Clean(.Dist)
127127
cruncher.postPeekPop()
128128
case .Name(let name)?:
129-
throw CommandLineError.InvalidUsage("Unknown clean mode: \(name)", .Imply)
129+
throw CommandLineError.InvalidUsage("Unknown clean mode: \(name)", .ImplySwiftBuild)
130130
default:
131131
break
132132
}
@@ -142,14 +142,14 @@ func parse(commandLineArguments args: [String]) throws -> (Mode, Options) {
142142
cruncher.postPeekPop()
143143
opts.chdir = name
144144
default:
145-
throw CommandLineError.InvalidUsage("Option `--chdir' requires subsequent directory argument", .Imply)
145+
throw CommandLineError.InvalidUsage("Option `--chdir' requires subsequent directory argument", .ImplySwiftBuild)
146146
}
147147

148148
case .Switch(.Verbose):
149149
opts.verbosity += 1
150150

151151
case .Name(let name):
152-
throw CommandLineError.InvalidUsage("Unknown argument: \(name)", .Imply)
152+
throw CommandLineError.InvalidUsage("Unknown argument: \(name)", .ImplySwiftBuild)
153153

154154
case .Switch(.Xcc):
155155
opts.Xcc.append(try cruncher.rawPop())
@@ -260,14 +260,14 @@ private struct Cruncher {
260260
}
261261

262262
guard !arg.hasPrefix("-") else {
263-
throw CommandLineError.InvalidUsage("unknown argument: \(arg)", .Imply)
263+
throw CommandLineError.InvalidUsage("unknown argument: \(arg)", .ImplySwiftBuild)
264264
}
265265

266266
return .Name(arg)
267267
}
268268

269269
mutating func rawPop() throws -> String {
270-
guard args.count > 0 else { throw CommandLineError.InvalidUsage("expected argument", .Imply) }
270+
guard args.count > 0 else { throw CommandLineError.InvalidUsage("expected argument", .ImplySwiftBuild) }
271271
return args.removeFirst()
272272
}
273273

Sources/swift-test/main.swift

Lines changed: 18 additions & 10 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()
21+
let args = Array(Process.arguments.dropFirst())
22+
let mode = try parse(commandLineArguments: args)
2223

23-
//FIXME this is a hack for SwiftPM’s own tests
24-
setenv("SPM_INSTALL_PATH", dir.build, 0)
24+
switch mode {
25+
case .Usage:
26+
usage()
27+
case .Run(let xctestArg):
28+
let dir = try directories()
2529

26-
let yamlPath = Path.join(dir.build, "debug.yaml")
27-
guard yamlPath.exists else { throw Error.DebugYAMLNotFound }
28-
29-
try build(YAMLPath: yamlPath, target: "test")
30-
let success = test(dir.build, "debug")
31-
exit(success ? 0 : 1)
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 = test(dir.build, "debug", xctestArg: xctestArg)
38+
exit(success ? 0 : 1)
39+
}
3240

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,15 +11,18 @@
1111
import PackageType
1212
import Utility
1313

14-
func test(path: String..., args: String? = nil) -> Bool {
14+
func test(path: String..., xctestArg: String? = nil) -> Bool {
1515
let path = Path.join(path)
1616
let result: Void?
1717
#if os(OSX)
1818
var args = ["xcrun", "xctest"]
19-
args += Process.arguments.dropFirst()
19+
if let xctestArg = xctestArg {
20+
args += ["-XCTest", xctestArg]
21+
}
2022
args += [Path.join(path, "Package.xctest")]
2123
result = try? system(args)
2224
#else
25+
//FIXME: Pass xctestArg when swift-corelibs-xctest supports it
2326
result = try? system(Path.join(path, "test-Package"))
2427
#endif
2528
return result != nil

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)