Skip to content

Commit 937977a

Browse files
committed
Replace zsh END_MARKER pseudo-completion with a space to ease migration.
Document why & how this pseudo-completion is used. Do not trim whitespace in testing, as that breaks with the space pseudo-completion. Testing should be as exact as possible; trimming whitespace makes it less exact. Signed-off-by: Ross Goldberg <[email protected]>
1 parent 8ffd363 commit 937977a

File tree

17 files changed

+74
-22
lines changed

17 files changed

+74
-22
lines changed

Sources/ArgumentParser/Completions/CompletionsGenerator.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ public struct CompletionShell: RawRepresentable, Hashable, CaseIterable {
102102
func format(completions: [String]) -> String {
103103
var completions = completions
104104
if self == .zsh {
105-
completions.append("END_MARKER")
105+
// This pseudo-completion is removed by the zsh completion script.
106+
// It allows trailing empty string completions to work in zsh.
107+
// zsh completion scripts generated by older SAP versions ignore spaces.
108+
completions.append(" ")
106109
}
107110
return completions.joined(separator: "\n")
108111
}

Sources/ArgumentParserTestHelpers/TestHelpers.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,9 @@ extension XCTest {
379379

380380
let outputData = output.fileHandleForReading.readDataToEndOfFile()
381381
let outputActual = String(data: outputData, encoding: .utf8)!
382-
.trimmingCharacters(in: .whitespacesAndNewlines)
383382

384383
let errorData = error.fileHandleForReading.readDataToEndOfFile()
385384
let errorActual = String(data: errorData, encoding: .utf8)!
386-
.trimmingCharacters(in: .whitespacesAndNewlines)
387385

388386
if let expected = expected {
389387
AssertEqualStrings(
@@ -407,8 +405,8 @@ extension XCTest {
407405
file: StaticString = #filePath, line: UInt = #line
408406
) throws {
409407
AssertEqualStrings(
410-
actual: actual.trimmingCharacters(in: .whitespacesAndNewlines),
411-
expected: expected.trimmingCharacters(in: .whitespacesAndNewlines),
408+
actual: actual,
409+
expected: expected,
412410
file: file,
413411
line: line)
414412

@@ -463,7 +461,7 @@ extension XCTest {
463461
let expected = try String(contentsOf: snapshotFileURL, encoding: .utf8)
464462
AssertEqualStrings(
465463
actual: actual,
466-
expected: expected.trimmingCharacters(in: .newlines),
464+
expected: expected,
467465
file: file,
468466
line: line)
469467
return expected

Tests/ArgumentParserExampleTests/CountLinesExampleTests.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ final class CountLinesExampleTests: XCTestCase {
2626
let testFile = try XCTUnwrap(
2727
Bundle.module.url(forResource: "CountLinesTest", withExtension: "txt"))
2828
try AssertExecuteCommand(
29-
command: "count-lines \(testFile.path)", expected: "20")
29+
command: "count-lines \(testFile.path)", expected: "20\n")
3030
try AssertExecuteCommand(
31-
command: "count-lines \(testFile.path) --prefix al", expected: "4")
31+
command: "count-lines \(testFile.path) --prefix al", expected: "4\n")
3232
}
3333

3434
func testCountLinesHelp() throws {
@@ -44,6 +44,8 @@ final class CountLinesExampleTests: XCTestCase {
4444
--prefix <prefix> Only count lines with this prefix.
4545
--verbose Include extra information in the output.
4646
-h, --help Show help information.
47+
48+
4749
"""
4850
try AssertExecuteCommand(command: "count-lines -h", expected: helpText)
4951
}

Tests/ArgumentParserExampleTests/MathExampleTests.swift

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ final class MathExampleTests: XCTestCase {
2727
}
2828

2929
func testMath_Simple() throws {
30-
try AssertExecuteCommand(command: "math 1 2 3 4 5", expected: "15")
30+
try AssertExecuteCommand(command: "math 1 2 3 4 5", expected: "15\n")
3131
try AssertExecuteCommand(
32-
command: "math multiply 1 2 3 4 5", expected: "120")
32+
command: "math multiply 1 2 3 4 5", expected: "120\n")
3333
}
3434

3535
func testMath_Help() throws {
@@ -48,6 +48,7 @@ final class MathExampleTests: XCTestCase {
4848
stats Calculate descriptive statistics.
4949
5050
See 'math help <subcommand>' for detailed help.
51+
5152
"""
5253

5354
try AssertExecuteCommand(command: "math -h", expected: helpText)
@@ -68,6 +69,8 @@ final class MathExampleTests: XCTestCase {
6869
-x, --hex-output Use hexadecimal notation for the result.
6970
--version Show the version.
7071
-h, --help Show help information.
72+
73+
7174
"""
7275

7376
try AssertExecuteCommand(command: "math add -h", expected: helpText)
@@ -95,6 +98,8 @@ final class MathExampleTests: XCTestCase {
9598
median, mode; default: mean)
9699
--version Show the version.
97100
-h, --help Show help information.
101+
102+
98103
"""
99104

100105
try AssertExecuteCommand(
@@ -123,6 +128,8 @@ final class MathExampleTests: XCTestCase {
123128
--custom <custom>
124129
--version Show the version.
125130
-h, --help Show help information.
131+
132+
126133
"""
127134

128135
// The "quantiles" subcommand's run() method is unimplemented, so it
@@ -145,20 +152,21 @@ final class MathExampleTests: XCTestCase {
145152
Error: Please provide at least one value to calculate the mode.
146153
Usage: math stats average [--kind <kind>] [<values> ...]
147154
See 'math stats average --help' for more information.
155+
148156
""",
149157
exitCode: .validationFailure)
150158
}
151159

152160
func testMath_Versions() throws {
153161
try AssertExecuteCommand(
154162
command: "math --version",
155-
expected: "1.0.0")
163+
expected: "1.0.0\n")
156164
try AssertExecuteCommand(
157165
command: "math stats --version",
158-
expected: "1.0.0")
166+
expected: "1.0.0\n")
159167
try AssertExecuteCommand(
160168
command: "math stats average --version",
161-
expected: "1.5.0-alpha")
169+
expected: "1.5.0-alpha\n")
162170
}
163171

164172
func testMath_ExitCodes() throws {
@@ -187,6 +195,7 @@ final class MathExampleTests: XCTestCase {
187195
Error: Unknown option '--foo'
188196
Usage: math add [--hex-output] [<values> ...]
189197
See 'math add --help' for more information.
198+
190199
""",
191200
exitCode: .validationFailure)
192201

@@ -197,6 +206,7 @@ final class MathExampleTests: XCTestCase {
197206
Help: <values> A group of integers to operate on.
198207
Usage: math add [--hex-output] [<values> ...]
199208
See 'math add --help' for more information.
209+
200210
""",
201211
exitCode: .validationFailure)
202212
}
@@ -246,7 +256,7 @@ extension MathExampleTests {
246256
"hello",
247257
"helicopter",
248258
"heliotrope",
249-
]),
259+
]) + "\n",
250260
environment: [
251261
CompletionShell.shellEnvironmentVariableName: shell.rawValue
252262
]
@@ -258,7 +268,7 @@ extension MathExampleTests {
258268
"hello",
259269
"helicopter",
260270
"heliotrope",
261-
]),
271+
]) + "\n",
262272
environment: [
263273
CompletionShell.shellEnvironmentVariableName: shell.rawValue
264274
]
@@ -269,7 +279,7 @@ extension MathExampleTests {
269279
expected: shell.format(completions: [
270280
"aardvark",
271281
"aaaaalbert",
272-
]),
282+
]) + "\n",
273283
environment: [
274284
CompletionShell.shellEnvironmentVariableName: shell.rawValue
275285
]

Tests/ArgumentParserExampleTests/RepeatExampleTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ final class RepeatExampleTests: XCTestCase {
2525
expected: """
2626
hello
2727
hello
28+
2829
""")
2930
}
3031

@@ -34,6 +35,7 @@ final class RepeatExampleTests: XCTestCase {
3435
expected: """
3536
1: hello
3637
2: hello
38+
3739
""")
3840
}
3941

@@ -47,6 +49,7 @@ final class RepeatExampleTests: XCTestCase {
4749
hello
4850
hello
4951
hello
52+
5053
""")
5154
}
5255

@@ -61,6 +64,8 @@ final class RepeatExampleTests: XCTestCase {
6164
--count <count> The number of times to repeat 'phrase'.
6265
--include-counter Include a counter with each repetition.
6366
-h, --help Show help information.
67+
68+
6469
"""
6570

6671
try AssertExecuteCommand(command: "repeat -h", expected: helpText)
@@ -82,6 +87,8 @@ final class RepeatExampleTests: XCTestCase {
8287
--count <count> The number of times to repeat 'phrase'.
8388
--include-counter Include a counter with each repetition.
8489
-h, --help Show help information.
90+
91+
8592
""",
8693
exitCode: .validationFailure)
8794

@@ -92,6 +99,7 @@ final class RepeatExampleTests: XCTestCase {
9299
Help: --count <count> The number of times to repeat 'phrase'.
93100
Usage: repeat [--count <count>] [--include-counter] <phrase>
94101
See 'repeat --help' for more information.
102+
95103
""",
96104
exitCode: .validationFailure)
97105

@@ -102,6 +110,7 @@ final class RepeatExampleTests: XCTestCase {
102110
Help: --count <count> The number of times to repeat 'phrase'.
103111
Usage: repeat [--count <count>] [--include-counter] <phrase>
104112
See 'repeat --help' for more information.
113+
105114
""",
106115
exitCode: .validationFailure)
107116

@@ -111,6 +120,7 @@ final class RepeatExampleTests: XCTestCase {
111120
Error: Unknown option '--version'
112121
Usage: repeat [--count <count>] [--include-counter] <phrase>
113122
See 'repeat --help' for more information.
123+
114124
""",
115125
exitCode: .validationFailure)
116126
}

Tests/ArgumentParserExampleTests/RollDiceExampleTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ final class RollDiceExampleTests: XCTestCase {
3434
--seed <seed> A seed to use for repeatable random generation.
3535
-v, --verbose Show all roll results.
3636
-h, --help Show help information.
37+
38+
3739
"""
3840

3941
try AssertExecuteCommand(command: "roll -h", expected: helpText)
@@ -48,6 +50,7 @@ final class RollDiceExampleTests: XCTestCase {
4850
Help: --times <n> Rolls the dice <n> times.
4951
Usage: roll [--times <n>] [--sides <m>] [--seed <seed>] [--verbose]
5052
See 'roll --help' for more information.
53+
5154
""",
5255
exitCode: .validationFailure)
5356

@@ -58,6 +61,7 @@ final class RollDiceExampleTests: XCTestCase {
5861
Help: --times <n> Rolls the dice <n> times.
5962
Usage: roll [--times <n>] [--sides <m>] [--seed <seed>] [--verbose]
6063
See 'roll --help' for more information.
64+
6165
""",
6266
exitCode: .validationFailure)
6367
}

Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testColorDoccReference().md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ color help [<subcommands>...]
3232
```
3333

3434
**subcommands:**
35+
36+
37+
38+
39+

Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testCountLinesDoccReference().md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,8 @@ count-lines help [<subcommands>...]
3535
```
3636

3737
**subcommands:**
38+
39+
40+
41+
42+

Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testMathDoccReference().md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,8 @@ math help [<subcommands>...]
205205
```
206206

207207
**subcommands:**
208+
209+
210+
211+
212+

Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testRepeatDoccReference().md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,8 @@ repeat help [<subcommands>...]
3535
```
3636

3737
**subcommands:**
38+
39+
40+
41+
42+

0 commit comments

Comments
 (0)