File: DefaultSubcommandEndToEndTests.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (218 lines) | stat: -rw-r--r-- 7,955 bytes parent folder | download
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
//===----------------------------------------------------------*- 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
@testable import ArgumentParser

final class DefaultSubcommandEndToEndTests: XCTestCase {
}

// MARK: -

private struct Main: ParsableCommand {
  static let configuration = CommandConfiguration(
    subcommands: [Default.self, Foo.self, Bar.self],
    defaultSubcommand: Default.self
  )
}

private struct Default: ParsableCommand {
  enum Mode: String, CaseIterable, ExpressibleByArgument {
    case foo, bar, baz
  }

  @Option var mode: Mode = .foo
}

private struct Foo: ParsableCommand {}
private struct Bar: ParsableCommand {}

extension DefaultSubcommandEndToEndTests {
  func testDefaultSubcommand() {
    AssertParseCommand(Main.self, Default.self, []) { def in
      XCTAssertEqual(.foo, def.mode)
    }

    AssertParseCommand(Main.self, Default.self, ["--mode=bar"]) { def in
      XCTAssertEqual(.bar, def.mode)
    }

    AssertParseCommand(Main.self, Default.self, ["--mode", "bar"]) { def in
      XCTAssertEqual(.bar, def.mode)
    }

    AssertParseCommand(Main.self, Default.self, ["--mode", "baz"]) { def in
      XCTAssertEqual(.baz, def.mode)
    }
  }

  func testNonDefaultSubcommand() {
    AssertParseCommand(Main.self, Foo.self, ["foo"]) { _ in }
    AssertParseCommand(Main.self, Bar.self, ["bar"]) { _ in }

    AssertParseCommand(Main.self, Default.self, ["default", "--mode", "bar"]) { def in
      XCTAssertEqual(.bar, def.mode)
    }
  }

  func testParsingFailure() {
    XCTAssertThrowsError(try Main.parseAsRoot(["--mode", "qux"]))
    XCTAssertThrowsError(try Main.parseAsRoot(["qux"]))
  }
}

extension DefaultSubcommandEndToEndTests {
  fileprivate struct MyCommand: ParsableCommand {
    static let configuration = CommandConfiguration(
      subcommands: [Plugin.self, NonDefault.self, Other.self],
      defaultSubcommand: Plugin.self
    )
    
    @OptionGroup
    var options: CommonOptions
  }
  
  fileprivate struct CommonOptions: ParsableArguments {
    @Flag(name: [.customLong("verbose"), .customShort("v")],
          help: "Enable verbose aoutput.")
    var verbose = false
  }
  
  fileprivate struct Plugin: ParsableCommand {
    @OptionGroup var options: CommonOptions
    @Argument var pluginName: String
    
    @Argument(parsing: .captureForPassthrough)
    var pluginArguments: [String] = []
  }
  
  fileprivate struct NonDefault: ParsableCommand {
    @OptionGroup var options: CommonOptions
    @Argument var pluginName: String
    
    @Argument(parsing: .captureForPassthrough)
    var pluginArguments: [String] = []
  }
  
  fileprivate struct Other: ParsableCommand {
    @OptionGroup var options: CommonOptions
  }
  
  func testRemainingDefaultImplicit() throws {
    AssertParseCommand(MyCommand.self, Plugin.self, ["my-plugin"]) { plugin in
      XCTAssertEqual(plugin.pluginName, "my-plugin")
      XCTAssertEqual(plugin.pluginArguments, [])
      XCTAssertEqual(plugin.options.verbose, false)
    }
    AssertParseCommand(MyCommand.self, Plugin.self, ["my-plugin", "--verbose"]) { plugin in
      XCTAssertEqual(plugin.pluginName, "my-plugin")
      XCTAssertEqual(plugin.pluginArguments, ["--verbose"])
      XCTAssertEqual(plugin.options.verbose, false)
    }
    AssertParseCommand(MyCommand.self, Plugin.self, ["--verbose", "my-plugin", "--verbose"]) { plugin in
      XCTAssertEqual(plugin.pluginName, "my-plugin")
      XCTAssertEqual(plugin.pluginArguments, ["--verbose"])
      XCTAssertEqual(plugin.options.verbose, true)
    }
    AssertParseCommand(MyCommand.self, Plugin.self, ["my-plugin", "--help"]) { plugin in
      XCTAssertEqual(plugin.pluginName, "my-plugin")
      XCTAssertEqual(plugin.pluginArguments, ["--help"])
      XCTAssertEqual(plugin.options.verbose, false)
    }
  }

  func testRemainingDefaultExplicit() throws {
    AssertParseCommand(MyCommand.self, Plugin.self, ["plugin", "my-plugin"]) { plugin in
      XCTAssertEqual(plugin.pluginName, "my-plugin")
      XCTAssertEqual(plugin.pluginArguments, [])
      XCTAssertEqual(plugin.options.verbose, false)
    }
    AssertParseCommand(MyCommand.self, Plugin.self, ["plugin", "my-plugin", "--verbose"]) { plugin in
      XCTAssertEqual(plugin.pluginName, "my-plugin")
      XCTAssertEqual(plugin.pluginArguments, ["--verbose"])
      XCTAssertEqual(plugin.options.verbose, false)
    }
    AssertParseCommand(MyCommand.self, Plugin.self, ["--verbose", "plugin", "my-plugin", "--verbose"]) { plugin in
      XCTAssertEqual(plugin.pluginName, "my-plugin")
      XCTAssertEqual(plugin.pluginArguments, ["--verbose"])
      XCTAssertEqual(plugin.options.verbose, true)
    }
    AssertParseCommand(MyCommand.self, Plugin.self, ["--verbose", "plugin", "my-plugin", "--help"]) { plugin in
      XCTAssertEqual(plugin.pluginName, "my-plugin")
      XCTAssertEqual(plugin.pluginArguments, ["--help"])
      XCTAssertEqual(plugin.options.verbose, true)
    }
  }

  func testRemainingNonDefault() throws {
    AssertParseCommand(MyCommand.self, NonDefault.self, ["non-default", "my-plugin"]) { nondef in
      XCTAssertEqual(nondef.pluginName, "my-plugin")
      XCTAssertEqual(nondef.pluginArguments, [])
      XCTAssertEqual(nondef.options.verbose, false)
    }
    AssertParseCommand(MyCommand.self, NonDefault.self, ["non-default", "my-plugin", "--verbose"]) { nondef in
      XCTAssertEqual(nondef.pluginName, "my-plugin")
      XCTAssertEqual(nondef.pluginArguments, ["--verbose"])
      XCTAssertEqual(nondef.options.verbose, false)
    }
    AssertParseCommand(MyCommand.self, NonDefault.self, ["--verbose", "non-default", "my-plugin", "--verbose"]) { nondef in
      XCTAssertEqual(nondef.pluginName, "my-plugin")
      XCTAssertEqual(nondef.pluginArguments, ["--verbose"])
      XCTAssertEqual(nondef.options.verbose, true)
    }
    AssertParseCommand(MyCommand.self, NonDefault.self, ["--verbose", "non-default", "my-plugin", "--help"]) { nondef in
      XCTAssertEqual(nondef.pluginName, "my-plugin")
      XCTAssertEqual(nondef.pluginArguments, ["--help"])
      XCTAssertEqual(nondef.options.verbose, true)
    }
  }

  func testRemainingDefaultOther() throws {
    AssertParseCommand(MyCommand.self, Other.self, ["other"]) { other in
      XCTAssertEqual(other.options.verbose, false)
    }
    AssertParseCommand(MyCommand.self, Other.self, ["other", "--verbose"]) { other in
      XCTAssertEqual(other.options.verbose, true)
    }
  }
  
  func testRemainingDefaultFailure() {
    XCTAssertThrowsError(try MyCommand.parseAsRoot([]))
    XCTAssertThrowsError(try MyCommand.parseAsRoot(["--verbose"]))
    XCTAssertThrowsError(try MyCommand.parseAsRoot(["plugin", "--verbose", "my-plugin"]))
  }
}

extension DefaultSubcommandEndToEndTests {
  struct RootWithPassthroughDefault: ParsableCommand {
    static let configuration = CommandConfiguration(
      subcommands: [PassthroughDefault.self],
      defaultSubcommand: PassthroughDefault.self,
      helpNames: [.short, .long, .customLong("help", withSingleDash: true)]
    )
  }
  
  struct PassthroughDefault: ParsableCommand {
    @Argument(parsing: .captureForPassthrough)
    var remaining: [String] = []
  }

  // Test fix for https://github.com/apple/swift-package-manager/issues/7218
  func testHelpWithPassthroughDefault() throws {
    AssertParseCommand(
      RootWithPassthroughDefault.self, HelpCommand.self, ["-h"]) { _ in }
    AssertParseCommand(
      RootWithPassthroughDefault.self, HelpCommand.self, ["-help"]) { _ in }
    AssertParseCommand(
      RootWithPassthroughDefault.self, HelpCommand.self, ["--help"]) { _ in }
  }
}