/
CompletionScriptTests.swift
381 lines (314 loc) · 10.7 KB
/
CompletionScriptTests.swift
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//===----------------------------------------------------------*- 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 CompletionScriptTests: XCTestCase {
}
extension CompletionScriptTests {
struct Path: ExpressibleByArgument {
var path: String
init?(argument: String) {
self.path = argument
}
static var defaultCompletionKind: CompletionKind {
.file()
}
}
enum Kind: String, ExpressibleByArgument, CaseIterable {
case one, two, three = "custom-three"
}
struct Base: ParsableCommand {
@Option(help: "The user's name.") var name: String
@Option() var kind: Kind
@Option(completion: .list(["1", "2", "3"])) var otherKind: Kind
@Option() var path1: Path
@Option() var path2: Path?
@Option(completion: .list(["a", "b", "c"])) var path3: Path
@Flag(help: .hidden) var verbose = false
}
func testBase_Zsh() throws {
let script1 = try CompletionsGenerator(command: Base.self, shell: .zsh)
.generateCompletionScript()
XCTAssertEqual(zshBaseCompletions, script1)
let script2 = try CompletionsGenerator(command: Base.self, shellName: "zsh")
.generateCompletionScript()
XCTAssertEqual(zshBaseCompletions, script2)
let script3 = Base.completionScript(for: .zsh)
XCTAssertEqual(zshBaseCompletions, script3)
}
func testBase_Bash() throws {
let script1 = try CompletionsGenerator(command: Base.self, shell: .bash)
.generateCompletionScript()
XCTAssertEqual(bashBaseCompletions, script1)
let script2 = try CompletionsGenerator(command: Base.self, shellName: "bash")
.generateCompletionScript()
XCTAssertEqual(bashBaseCompletions, script2)
let script3 = Base.completionScript(for: .bash)
XCTAssertEqual(bashBaseCompletions, script3)
}
func testBase_Fish() throws {
let script1 = try CompletionsGenerator(command: Base.self, shell: .fish)
.generateCompletionScript()
XCTAssertEqual(fishBaseCompletions, script1)
let script2 = try CompletionsGenerator(command: Base.self, shellName: "fish")
.generateCompletionScript()
XCTAssertEqual(fishBaseCompletions, script2)
let script3 = Base.completionScript(for: .fish)
XCTAssertEqual(fishBaseCompletions, script3)
}
}
extension CompletionScriptTests {
struct Custom: ParsableCommand {
@Option(name: .shortAndLong, completion: .custom { _ in ["a", "b", "c"] })
var one: String
@Argument(completion: .custom { _ in ["d", "e", "f"] })
var two: String
@Option(name: .customShort("z"), completion: .custom { _ in ["x", "y", "z"] })
var three: String
}
func verifyCustomOutput(
_ arg: String,
expectedOutput: String,
file: StaticString = #file, line: UInt = #line
) throws {
do {
_ = try Custom.parse(["---completion", "--", arg])
XCTFail("Didn't error as expected", file: (file), line: line)
} catch let error as CommandError {
guard case .completionScriptCustomResponse(let output) = error.parserError else {
throw error
}
XCTAssertEqual(expectedOutput, output, file: (file), line: line)
}
}
func testCustomCompletions() throws {
try verifyCustomOutput("-o", expectedOutput: "a\nb\nc")
try verifyCustomOutput("--one", expectedOutput: "a\nb\nc")
try verifyCustomOutput("two", expectedOutput: "d\ne\nf")
try verifyCustomOutput("-z", expectedOutput: "x\ny\nz")
XCTAssertThrowsError(try verifyCustomOutput("--bad", expectedOutput: ""))
}
}
extension CompletionScriptTests {
struct EscapedCommand: ParsableCommand {
@Option(help: #"Escaped chars: '[]\."#)
var one: String
@Argument(completion: .custom { _ in ["d", "e", "f"] })
var two: String
}
func testEscaped_Zsh() throws {
XCTAssertEqual(zshEscapedCompletion, EscapedCommand.completionScript(for: .zsh))
}
}
private let zshBaseCompletions = """
#compdef base
local context state state_descr line
_base_commandname=$words[1]
typeset -A opt_args
_base() {
integer ret=1
local -a args
args+=(
'--name[The user'"'"'s name.]:name:'
'--kind:kind:(one two custom-three)'
'--other-kind:other-kind:(1 2 3)'
'--path1:path1:_files'
'--path2:path2:_files'
'--path3:path3:(a b c)'
'(-h --help)'{-h,--help}'[Show help information.]'
)
_arguments -w -s -S $args[@] && ret=0
return ret
}
_custom_completion() {
local completions=("${(@f)$($*)}")
_describe '' completions
}
_base
"""
private let bashBaseCompletions = """
#!/bin/bash
_base() {
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
COMPREPLY=()
opts="--name --kind --other-kind --path1 --path2 --path3 -h --help"
if [[ $COMP_CWORD == "1" ]]; then
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
return
fi
case $prev in
--name)
return
;;
--kind)
COMPREPLY=( $(compgen -W "one two custom-three" -- "$cur") )
return
;;
--other-kind)
COMPREPLY=( $(compgen -W "1 2 3" -- "$cur") )
return
;;
--path1)
COMPREPLY=( $(compgen -f -- "$cur") )
return
;;
--path2)
COMPREPLY=( $(compgen -f -- "$cur") )
return
;;
--path3)
COMPREPLY=( $(compgen -W "a b c" -- "$cur") )
return
;;
esac
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
complete -F _base base
"""
private let zshEscapedCompletion = """
#compdef escaped-command
local context state state_descr line
_escaped_command_commandname=$words[1]
typeset -A opt_args
_escaped-command() {
integer ret=1
local -a args
args+=(
'--one[Escaped chars: '"'"'\\[\\]\\\\.]:one:'
':two:{_custom_completion $_escaped_command_commandname ---completion -- two $words}'
'(-h --help)'{-h,--help}'[Show help information.]'
)
_arguments -w -s -S $args[@] && ret=0
return ret
}
_custom_completion() {
local completions=("${(@f)$($*)}")
_describe '' completions
}
_escaped-command
"""
private let fishBaseCompletions = """
function _swift_base_using_command
set -l cmd (commandline -opc)
if [ (count $cmd) -eq (count $argv) ]
for i in (seq (count $argv))
if [ $cmd[$i] != $argv[$i] ]
return 1
end
end
return 0
end
return 1
end
complete -c base -n '_swift_base_using_command base' -f -r -l name -d 'The user\\'s name.'
complete -c base -n '_swift_base_using_command base' -f -r -l kind
complete -c base -n '_swift_base_using_command base --kind' -f -k -a 'one two custom-three'
complete -c base -n '_swift_base_using_command base' -f -r -l other-kind
complete -c base -n '_swift_base_using_command base --other-kind' -f -k -a '1 2 3'
complete -c base -n '_swift_base_using_command base' -f -r -l path1
complete -c base -n '_swift_base_using_command base --path1' -f -a '(for i in *.{}; echo $i;end)'
complete -c base -n '_swift_base_using_command base' -f -r -l path2
complete -c base -n '_swift_base_using_command base --path2' -f -a '(for i in *.{}; echo $i;end)'
complete -c base -n '_swift_base_using_command base' -f -r -l path3
complete -c base -n '_swift_base_using_command base --path3' -f -k -a 'a b c'
complete -c base -n '_swift_base_using_command base' -f -s h -l help -d 'Show help information.'
"""
// MARK: - Test Hidden Subcommand
struct Parent: ParsableCommand {
static var configuration = CommandConfiguration(subcommands: [HiddenChild.self])
}
struct HiddenChild: ParsableCommand {
static var configuration = CommandConfiguration(shouldDisplay: false)
}
extension CompletionScriptTests {
func testHiddenSubcommand_Zsh() throws {
let script1 = try CompletionsGenerator(command: Parent.self, shell: .zsh)
.generateCompletionScript()
XCTAssertEqual(zshHiddenCompletion, script1)
let script2 = try CompletionsGenerator(command: Parent.self, shellName: "zsh")
.generateCompletionScript()
XCTAssertEqual(zshHiddenCompletion, script2)
let script3 = Parent.completionScript(for: .zsh)
XCTAssertEqual(zshHiddenCompletion, script3)
}
func testHiddenSubcommand_Bash() throws {
let script1 = try CompletionsGenerator(command: Parent.self, shell: .bash)
.generateCompletionScript()
XCTAssertEqual(bashHiddenCompletion, script1)
let script2 = try CompletionsGenerator(command: Parent.self, shellName: "bash")
.generateCompletionScript()
XCTAssertEqual(bashHiddenCompletion, script2)
let script3 = Parent.completionScript(for: .bash)
XCTAssertEqual(bashHiddenCompletion, script3)
}
func testHiddenSubcommand_Fish() throws {
let script1 = try CompletionsGenerator(command: Parent.self, shell: .fish)
.generateCompletionScript()
XCTAssertEqual(fishHiddenCompletion, script1)
let script2 = try CompletionsGenerator(command: Parent.self, shellName: "fish")
.generateCompletionScript()
XCTAssertEqual(fishHiddenCompletion, script2)
let script3 = Parent.completionScript(for: .fish)
XCTAssertEqual(fishHiddenCompletion, script3)
}
}
let zshHiddenCompletion = """
#compdef parent
local context state state_descr line
_parent_commandname=$words[1]
typeset -A opt_args
_parent() {
integer ret=1
local -a args
args+=(
'(-h --help)'{-h,--help}'[Show help information.]'
)
_arguments -w -s -S $args[@] && ret=0
return ret
}
_custom_completion() {
local completions=("${(@f)$($*)}")
_describe '' completions
}
_parent
"""
let bashHiddenCompletion = """
#!/bin/bash
_parent() {
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
COMPREPLY=()
opts="-h --help"
if [[ $COMP_CWORD == "1" ]]; then
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
return
fi
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
complete -F _parent parent
"""
let fishHiddenCompletion = """
function _swift_parent_using_command
set -l cmd (commandline -opc)
if [ (count $cmd) -eq (count $argv) ]
for i in (seq (count $argv))
if [ $cmd[$i] != $argv[$i] ]
return 1
end
end
return 0
end
return 1
end
complete -c parent -n '_swift_parent_using_command parent' -f -s h -l help -d 'Show help information.'
"""