1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
//===----------------------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift Argument Parser open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
import XCTest
import ArgumentParserTestHelpers
import ArgumentParser
final class SubcommandEndToEndTests: XCTestCase {
}
// MARK: Single value String
fileprivate struct Foo: ParsableCommand {
static var configuration =
CommandConfiguration(subcommands: [CommandA.self, CommandB.self])
@Option() var name: String
}
fileprivate struct CommandA: ParsableCommand {
static var configuration = CommandConfiguration(commandName: "a")
@OptionGroup() var foo: Foo
@Option() var bar: Int
}
fileprivate struct CommandB: ParsableCommand {
static var configuration = CommandConfiguration(commandName: "b")
@OptionGroup() var foo: Foo
@Option() var baz: String
}
extension SubcommandEndToEndTests {
func testParsing_SubCommand() throws {
AssertParseCommand(Foo.self, CommandA.self, ["--name", "Foo", "a", "--bar", "42"]) { a in
XCTAssertEqual(a.bar, 42)
XCTAssertEqual(a.foo.name, "Foo")
}
AssertParseCommand(Foo.self, CommandB.self, ["--name", "A", "b", "--baz", "abc"]) { b in
XCTAssertEqual(b.baz, "abc")
XCTAssertEqual(b.foo.name, "A")
}
}
func testParsing_SubCommand_manual() throws {
AssertParseCommand(Foo.self, CommandA.self, ["--name", "Foo", "a", "--bar", "42"]) { a in
XCTAssertEqual(a.bar, 42)
XCTAssertEqual(a.foo.name, "Foo")
}
AssertParseCommand(Foo.self, Foo.self, ["--name", "Foo"]) { foo in
XCTAssertEqual(foo.name, "Foo")
}
}
func testParsing_SubCommand_help() throws {
let helpFoo = Foo.message(for: CleanExit.helpRequest())
let helpA = Foo.message(for: CleanExit.helpRequest(CommandA.self))
let helpB = Foo.message(for: CleanExit.helpRequest(CommandB.self))
AssertEqualStrings(actual: helpFoo, expected: """
USAGE: foo --name <name> <subcommand>
OPTIONS:
--name <name>
-h, --help Show help information.
SUBCOMMANDS:
a
b
See 'foo help <subcommand>' for detailed help.
""")
AssertEqualStrings(actual: helpA, expected: """
USAGE: foo a --name <name> --bar <bar>
OPTIONS:
--name <name>
--bar <bar>
-h, --help Show help information.
""")
AssertEqualStrings(actual: helpB, expected: """
USAGE: foo b --name <name> --baz <baz>
OPTIONS:
--name <name>
--baz <baz>
-h, --help Show help information.
""")
}
func testParsing_SubCommand_fails() throws {
XCTAssertThrowsError(try Foo.parse(["--name", "Foo", "a", "--baz", "42"]), "'baz' is not an option for the 'a' subcommand.")
XCTAssertThrowsError(try Foo.parse(["--name", "Foo", "b", "--bar", "42"]), "'bar' is not an option for the 'b' subcommand.")
}
}
fileprivate var mathDidRun = false
fileprivate struct Math: ParsableCommand {
enum Operation: String, ExpressibleByArgument {
case add
case multiply
}
@Option(help: "The operation to perform")
var operation: Operation = .add
@Flag(name: [.short, .long])
var verbose: Bool = false
@Argument(help: "The first operand")
var operands: [Int] = []
mutating func run() {
XCTAssertEqual(operation, .multiply)
XCTAssertTrue(verbose)
XCTAssertEqual(operands, [5, 11])
mathDidRun = true
}
}
extension SubcommandEndToEndTests {
func testParsing_SingleCommand() throws {
var mathCommand = try Math.parseAsRoot(["--operation", "multiply", "-v", "5", "11"])
XCTAssertFalse(mathDidRun)
try mathCommand.run()
XCTAssertTrue(mathDidRun)
}
}
// MARK: Nested Command Arguments Validated
struct BaseCommand: ParsableCommand {
enum BaseCommandError: Error {
case baseCommandFailure
case subCommandFailure
}
static let baseFlagValue = "base"
static var configuration = CommandConfiguration(
commandName: "base",
subcommands: [SubCommand.self]
)
@Option()
var baseFlag: String
mutating func validate() throws {
guard baseFlag == BaseCommand.baseFlagValue else {
throw BaseCommandError.baseCommandFailure
}
}
}
extension BaseCommand {
struct SubCommand : ParsableCommand {
static let subFlagValue = "sub"
static var configuration = CommandConfiguration(
commandName: "sub",
subcommands: [SubSubCommand.self]
)
@Option()
var subFlag: String
mutating func validate() throws {
guard subFlag == SubCommand.subFlagValue else {
throw BaseCommandError.subCommandFailure
}
}
}
}
extension BaseCommand.SubCommand {
struct SubSubCommand : ParsableCommand, TestableParsableArguments {
let didValidateExpectation = XCTestExpectation(singleExpectation: "did validate subcommand")
static var configuration = CommandConfiguration(
commandName: "subsub"
)
@Flag
var subSubFlag: Bool = false
private enum CodingKeys: CodingKey {
case subSubFlag
}
}
}
extension SubcommandEndToEndTests {
func testValidate_subcommands() {
// provide a value to base-flag that will throw
AssertErrorMessage(
BaseCommand.self,
["--base-flag", "foo", "sub", "--sub-flag", "foo", "subsub"],
"baseCommandFailure"
)
// provide a value to sub-flag that will throw
AssertErrorMessage(
BaseCommand.self,
["--base-flag", BaseCommand.baseFlagValue, "sub", "--sub-flag", "foo", "subsub"],
"subCommandFailure"
)
// provide a valid command and make sure both validates succeed
AssertParseCommand(BaseCommand.self,
BaseCommand.SubCommand.SubSubCommand.self,
["--base-flag", BaseCommand.baseFlagValue, "sub", "--sub-flag", BaseCommand.SubCommand.subFlagValue, "subsub", "--sub-sub-flag"]) { cmd in
XCTAssertTrue(cmd.subSubFlag)
// make sure that the instance of SubSubCommand provided
// had its validate method called, not just that any instance of SubSubCommand was validated
wait(for: [cmd.didValidateExpectation], timeout: 0.1)
}
}
}
// MARK: Version flags
private struct A: ParsableCommand {
static var configuration = CommandConfiguration(
version: "1.0.0",
subcommands: [HasVersionFlag.self, NoVersionFlag.self])
struct HasVersionFlag: ParsableCommand {
@Flag var version: Bool = false
}
struct NoVersionFlag: ParsableCommand {
@Flag var hello: Bool = false
}
}
extension SubcommandEndToEndTests {
func testParsingVersionFlags() throws {
AssertErrorMessage(A.self, ["--version"], "1.0.0")
AssertErrorMessage(A.self, ["no-version-flag", "--version"], "1.0.0")
AssertParseCommand(A.self, A.HasVersionFlag.self, ["has-version-flag", "--version"]) { cmd in
XCTAssertTrue(cmd.version)
}
}
}
|