Skip to content

Commit 571b29b

Browse files
authored
SwiftSyntaxTest: refactor command-line argument parsing into a test utility module. NFC (#18418)
1 parent ba180c5 commit 571b29b

File tree

10 files changed

+60
-44
lines changed

10 files changed

+60
-44
lines changed

test/IDE/coloring.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// REQUIRES: OS=macosx
33
// RUN: %target-swift-ide-test -syntax-coloring -source-filename %s | %FileCheck %s
44
// RUN: %target-swift-ide-test -syntax-coloring -typecheck -source-filename %s | %FileCheck %s -check-prefixes CHECK,CHECK-OLD
5-
// RUN: %swift-swiftsyntax-test --classify-syntax --source-file %s | %FileCheck %s --check-prefixes CHECK,CHECK-NEW
5+
// RUN: %swift-swiftsyntax-test -classify-syntax -source-file %s | %FileCheck %s --check-prefixes CHECK,CHECK-NEW
66
// XFAIL: broken_std_regex
77

88
#line 17 "abc.swift"

test/IDE/coloring_configs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// We need to require macOS because swiftSyntax currently doesn't build on Linux
22
// REQUIRES: OS=macosx
33
// RUN: %target-swift-ide-test -syntax-coloring -source-filename %s -D CONF | %FileCheck %s
4-
// RUN: %swift-swiftsyntax-test --classify-syntax --source-file %s | %FileCheck %s
4+
// RUN: %swift-swiftsyntax-test -classify-syntax -source-file %s | %FileCheck %s
55

66
// CHECK: <kw>var</kw> f : <type>Int</type>
77
var f : Int

test/IDE/coloring_keywords.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// REQUIRES: OS=macosx
33
// RUN: %target-swift-ide-test -syntax-coloring -source-filename %s | %FileCheck %s
44
// RUN: %target-swift-ide-test -syntax-coloring -typecheck -source-filename %s | %FileCheck %s
5-
// RUN: %swift-swiftsyntax-test --classify-syntax --source-file %s | %FileCheck %s
5+
// RUN: %swift-swiftsyntax-test -classify-syntax -source-file %s | %FileCheck %s
66

77
// CHECK: <kw>return</kw> c.return
88

test/IDE/coloring_unclosed_hash_if.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// REQUIRES: OS=macosx
33
// RUN: %target-swift-ide-test -syntax-coloring -source-filename %s | %FileCheck %s
44
// RUN: %target-swift-ide-test -syntax-coloring -typecheck -source-filename %s | %FileCheck %s
5-
// RUN: %swift-swiftsyntax-test --classify-syntax --source-file %s | %FileCheck %s
5+
// RUN: %swift-swiftsyntax-test -classify-syntax -source-file %s | %FileCheck %s
66

77
// CHECK: <#kw>#if</#kw> <#id>d</#id>
88
// CHECK-NEXT: <kw>func</kw> bar() {

tools/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ if(SWIFT_HOST_VARIANT STREQUAL "macosx")
5454
# Ensure we only build SwiftSyntax when we're building both.
5555
if(BUILD_FOUNDATION)
5656
add_subdirectory(SwiftSyntax)
57+
add_subdirectory(swift-test-utils)
5758
add_swift_tool_subdirectory(swift-swiftsyntax-test)
5859
endif()
5960
endif()
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
macro(add_swift_module_depends results target)
2+
list(APPEND ${results} "${target}-macosx")
3+
list(APPEND ${results} "${target}-macosx-x86_64")
4+
list(APPEND ${results} "${target}-swiftmodule-macosx-x86_64")
5+
endmacro()
6+
7+
add_swift_module_depends(all_depends swiftTestUtils)
8+
add_swift_module_depends(all_depends swiftSwiftSyntax)
9+
110
add_swift_host_tool(swift-swiftsyntax-test
211
main.swift
312
ClassifiedSyntaxTreePrinter.swift
4-
CommandLineArguments.swift
513
empty.c # FIXME: If there is no C file in the target Xcode skips the linking phase and doesn't create the executable
614
COMPILE_FLAGS "-module-name" "main"
7-
DEPENDS
8-
swiftSwiftSyntax-macosx
9-
swiftSwiftSyntax-macosx-x86_64
10-
swiftSwiftSyntax-swiftmodule-macosx-x86_64
15+
DEPENDS ${all_depends}
1116
)
Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import SwiftSyntax
22
import Foundation
3+
import TestUtils
34

45
/// Print the given message to stderr
56
func printerr(_ message: String, terminator: String = "\n") {
@@ -12,35 +13,35 @@ func printHelp() {
1213
Utility to test SwiftSyntax syntax tree deserialization.
1314
1415
Actions (must specify one):
15-
--deserialize-incremental
16-
Deserialize a full pre-edit syntax tree (--pre-edit-tree), parse an
17-
incrementally transferred post-edit syntax tree (--incr-tree) and
16+
-deserialize-incremental
17+
Deserialize a full pre-edit syntax tree (-pre-edit-tree), parse an
18+
incrementally transferred post-edit syntax tree (-incr-tree) and
1819
write the source representation of the post-edit syntax tree to an
19-
out file (--out).
20-
--classify-syntax
21-
Parse the given source file (--source-file) and output it with
20+
out file (-out).
21+
-classify-syntax
22+
Parse the given source file (-source-file) and output it with
2223
tokens classified for syntax colouring.
23-
--help
24+
-help
2425
Print this help message
2526
2627
Arguments:
27-
--source-file FILENAME
28+
-source-file FILENAME
2829
The path to a Swift source file to parse
29-
--pre-edit-tree FILENAME
30+
-pre-edit-tree FILENAME
3031
The path to a JSON serialized pre-edit syntax tree
31-
--incr-tree FILENAME
32+
-incr-tree FILENAME
3233
The path to a JSON serialized incrementally transferred post-edit
3334
syntax tree
34-
--out FILENAME
35+
-out FILENAME
3536
The file to which the source representation of the post-edit syntax
3637
tree shall be written.
3738
""")
3839
}
3940

4041
func performRoundTrip(args: CommandLineArguments) throws {
41-
let preEditTreeURL = URL(fileURLWithPath: try args.getRequired("--pre-edit-tree"))
42-
let incrTreeURL = URL(fileURLWithPath: try args.getRequired("--incr-tree"))
43-
let outURL = URL(fileURLWithPath: try args.getRequired("--out"))
42+
let preEditTreeURL = URL(fileURLWithPath: try args.getRequired("-pre-edit-tree"))
43+
let incrTreeURL = URL(fileURLWithPath: try args.getRequired("-incr-tree"))
44+
let outURL = URL(fileURLWithPath: try args.getRequired("-out"))
4445

4546
let preEditTreeData = try Data(contentsOf: preEditTreeURL)
4647
let incrTreeData = try Data(contentsOf: incrTreeURL)
@@ -53,14 +54,14 @@ func performRoundTrip(args: CommandLineArguments) throws {
5354
}
5455

5556
func performClassifySyntax(args: CommandLineArguments) throws {
56-
let treeURL = URL(fileURLWithPath: try args.getRequired("--source-file"))
57+
let treeURL = URL(fileURLWithPath: try args.getRequired("-source-file"))
5758

5859
let tree = try SyntaxTreeParser.parse(treeURL)
5960
let classifications = SyntaxClassifier.classifyTokensInTree(tree)
6061
let printer = ClassifiedSyntaxTreePrinter(classifications: classifications)
6162
let result = printer.print(tree: tree)
6263

63-
if let outURL = args["--out"].map(URL.init(fileURLWithPath:)) {
64+
if let outURL = args["-out"].map(URL.init(fileURLWithPath:)) {
6465
try result.write(to: outURL, atomically: false, encoding: .utf8)
6566
} else {
6667
print(result)
@@ -70,22 +71,22 @@ func performClassifySyntax(args: CommandLineArguments) throws {
7071
do {
7172
let args = try CommandLineArguments.parse(CommandLine.arguments.dropFirst())
7273

73-
if args.has("--deserialize-incremental") {
74+
if args.has("-deserialize-incremental") {
7475
try performRoundTrip(args: args)
75-
} else if args.has("--classify-syntax") {
76+
} else if args.has("-classify-syntax") {
7677
try performClassifySyntax(args: args)
77-
} else if args.has("--help") {
78+
} else if args.has("-help") {
7879
printHelp()
7980
} else {
8081
printerr("""
8182
No action specified.
82-
See --help for information about available actions
83+
See -help for information about available actions
8384
""")
8485
exit(1)
8586
}
8687
exit(0)
8788
} catch {
8889
printerr(error.localizedDescription)
89-
printerr("Run swift-swiftsyntax-test --help for more help.")
90+
printerr("Run swift-swiftsyntax-test -help for more help.")
9091
exit(1)
9192
}

tools/swift-test-utils/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
add_swift_library(swiftTestUtils SHARED
2+
# This file should be listed the first. Module name is inferred from the
3+
# filename.
4+
SwiftTestUtils.swift
5+
6+
SWIFT_MODULE_DEPENDS Foundation
7+
INSTALL_IN_COMPONENT swift-syntax
8+
TARGET_SDKS OSX
9+
IS_STDLIB)

tools/swift-swiftsyntax-test/CommandLineArguments.swift renamed to tools/swift-test-utils/SwiftTestUtils.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
import Foundation
22

3-
struct CommandLineArguments {
4-
struct MissingArgumentError: LocalizedError {
3+
public struct CommandLineArguments {
4+
public struct MissingArgumentError: LocalizedError {
55
let argName: String
66

7-
var errorDescription: String? {
7+
public var errorDescription: String? {
88
return "Missing required argument: \(argName)"
99
}
1010
}
11-
struct UnkeyedArgumentError: LocalizedError {
11+
public struct UnkeyedArgumentError: LocalizedError {
1212
let argName: String
1313

14-
var errorDescription: String? {
14+
public var errorDescription: String? {
1515
return "Unexpectedly found command line argument \(argName) without a key"
1616
}
1717
}
1818

1919
private let args: [String: String]
2020

21-
static func parse<T: Sequence>(_ args: T) throws -> CommandLineArguments
21+
public static func parse<T: Sequence>(_ args: T) throws -> CommandLineArguments
2222
where T.Element == String {
2323
var parsedArgs: [String: String] = [:]
2424
var currentKey: String? = nil
2525
for arg in args {
26-
if arg.hasPrefix("--") {
26+
if arg.hasPrefix("-") {
2727
// Parse a new key
2828
if let currentKey = currentKey {
2929
// The last key didn't have a value. Just add it with an empty string as
@@ -48,19 +48,19 @@ struct CommandLineArguments {
4848
return CommandLineArguments(args: parsedArgs)
4949
}
5050

51-
subscript(key: String) -> String? {
51+
public subscript(key: String) -> String? {
5252
return args[key]
5353
}
5454

55-
func getRequired(_ key: String) throws -> String {
55+
public func getRequired(_ key: String) throws -> String {
5656
if let value = args[key] {
5757
return value
5858
} else {
5959
throw MissingArgumentError(argName: key)
6060
}
6161
}
6262

63-
func has(_ key: String) -> Bool {
63+
public func has(_ key: String) -> Bool {
6464
return args[key] != nil
6565
}
6666
}

utils/incrparse/incr_transfer_round_trip.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ def main():
9898
sys.exit(1)
9999

100100
try:
101-
run_command([swift_swiftsyntax_test, '--deserialize-incremental'] +
102-
['--pre-edit-tree', pre_edit_tree_file] +
103-
['--incr-tree', incremental_tree_file] +
104-
['--out', after_roundtrip_source_file])
101+
run_command([swift_swiftsyntax_test, '-deserialize-incremental'] +
102+
['-pre-edit-tree', pre_edit_tree_file] +
103+
['-incr-tree', incremental_tree_file] +
104+
['-out', after_roundtrip_source_file])
105105
except subprocess.CalledProcessError as e:
106106
print('Test case "%s" of %s FAILed' % (test_case, test_file),
107107
file=sys.stderr)

0 commit comments

Comments
 (0)