Skip to content

SwiftSyntaxTest: refactor command-line argument parsing into a test utility module. NFC #18418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/IDE/coloring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// REQUIRES: OS=macosx
// RUN: %target-swift-ide-test -syntax-coloring -source-filename %s | %FileCheck %s
// RUN: %target-swift-ide-test -syntax-coloring -typecheck -source-filename %s | %FileCheck %s -check-prefixes CHECK,CHECK-OLD
// RUN: %swift-swiftsyntax-test --classify-syntax --source-file %s | %FileCheck %s --check-prefixes CHECK,CHECK-NEW
// RUN: %swift-swiftsyntax-test -classify-syntax -source-file %s | %FileCheck %s --check-prefixes CHECK,CHECK-NEW
// XFAIL: broken_std_regex

#line 17 "abc.swift"
Expand Down
2 changes: 1 addition & 1 deletion test/IDE/coloring_configs.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// We need to require macOS because swiftSyntax currently doesn't build on Linux
// REQUIRES: OS=macosx
// RUN: %target-swift-ide-test -syntax-coloring -source-filename %s -D CONF | %FileCheck %s
// RUN: %swift-swiftsyntax-test --classify-syntax --source-file %s | %FileCheck %s
// RUN: %swift-swiftsyntax-test -classify-syntax -source-file %s | %FileCheck %s

// CHECK: <kw>var</kw> f : <type>Int</type>
var f : Int
Expand Down
2 changes: 1 addition & 1 deletion test/IDE/coloring_keywords.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// REQUIRES: OS=macosx
// RUN: %target-swift-ide-test -syntax-coloring -source-filename %s | %FileCheck %s
// RUN: %target-swift-ide-test -syntax-coloring -typecheck -source-filename %s | %FileCheck %s
// RUN: %swift-swiftsyntax-test --classify-syntax --source-file %s | %FileCheck %s
// RUN: %swift-swiftsyntax-test -classify-syntax -source-file %s | %FileCheck %s

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

Expand Down
2 changes: 1 addition & 1 deletion test/IDE/coloring_unclosed_hash_if.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// REQUIRES: OS=macosx
// RUN: %target-swift-ide-test -syntax-coloring -source-filename %s | %FileCheck %s
// RUN: %target-swift-ide-test -syntax-coloring -typecheck -source-filename %s | %FileCheck %s
// RUN: %swift-swiftsyntax-test --classify-syntax --source-file %s | %FileCheck %s
// RUN: %swift-swiftsyntax-test -classify-syntax -source-file %s | %FileCheck %s

// CHECK: <#kw>#if</#kw> <#id>d</#id>
// CHECK-NEXT: <kw>func</kw> bar() {
Expand Down
1 change: 1 addition & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ if(SWIFT_HOST_VARIANT STREQUAL "macosx")
# Ensure we only build SwiftSyntax when we're building both.
if(BUILD_FOUNDATION)
add_subdirectory(SwiftSyntax)
add_subdirectory(swift-test-utils)
add_swift_tool_subdirectory(swift-swiftsyntax-test)
endif()
endif()
Expand Down
15 changes: 10 additions & 5 deletions tools/swift-swiftsyntax-test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
macro(add_swift_module_depends results target)
list(APPEND ${results} "${target}-macosx")
list(APPEND ${results} "${target}-macosx-x86_64")
list(APPEND ${results} "${target}-swiftmodule-macosx-x86_64")
endmacro()

add_swift_module_depends(all_depends swiftTestUtils)
add_swift_module_depends(all_depends swiftSwiftSyntax)

add_swift_host_tool(swift-swiftsyntax-test
main.swift
ClassifiedSyntaxTreePrinter.swift
CommandLineArguments.swift
empty.c # FIXME: If there is no C file in the target Xcode skips the linking phase and doesn't create the executable
COMPILE_FLAGS "-module-name" "main"
DEPENDS
swiftSwiftSyntax-macosx
swiftSwiftSyntax-macosx-x86_64
swiftSwiftSyntax-swiftmodule-macosx-x86_64
DEPENDS ${all_depends}
)
43 changes: 22 additions & 21 deletions tools/swift-swiftsyntax-test/main.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import SwiftSyntax
import Foundation
import TestUtils

/// Print the given message to stderr
func printerr(_ message: String, terminator: String = "\n") {
Expand All @@ -12,35 +13,35 @@ func printHelp() {
Utility to test SwiftSyntax syntax tree deserialization.

Actions (must specify one):
--deserialize-incremental
Deserialize a full pre-edit syntax tree (--pre-edit-tree), parse an
incrementally transferred post-edit syntax tree (--incr-tree) and
-deserialize-incremental
Deserialize a full pre-edit syntax tree (-pre-edit-tree), parse an
incrementally transferred post-edit syntax tree (-incr-tree) and
write the source representation of the post-edit syntax tree to an
out file (--out).
--classify-syntax
Parse the given source file (--source-file) and output it with
out file (-out).
-classify-syntax
Parse the given source file (-source-file) and output it with
tokens classified for syntax colouring.
--help
-help
Print this help message

Arguments:
--source-file FILENAME
-source-file FILENAME
The path to a Swift source file to parse
--pre-edit-tree FILENAME
-pre-edit-tree FILENAME
The path to a JSON serialized pre-edit syntax tree
--incr-tree FILENAME
-incr-tree FILENAME
The path to a JSON serialized incrementally transferred post-edit
syntax tree
--out FILENAME
-out FILENAME
The file to which the source representation of the post-edit syntax
tree shall be written.
""")
}

func performRoundTrip(args: CommandLineArguments) throws {
let preEditTreeURL = URL(fileURLWithPath: try args.getRequired("--pre-edit-tree"))
let incrTreeURL = URL(fileURLWithPath: try args.getRequired("--incr-tree"))
let outURL = URL(fileURLWithPath: try args.getRequired("--out"))
let preEditTreeURL = URL(fileURLWithPath: try args.getRequired("-pre-edit-tree"))
let incrTreeURL = URL(fileURLWithPath: try args.getRequired("-incr-tree"))
let outURL = URL(fileURLWithPath: try args.getRequired("-out"))

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

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

let tree = try SyntaxTreeParser.parse(treeURL)
let classifications = SyntaxClassifier.classifyTokensInTree(tree)
let printer = ClassifiedSyntaxTreePrinter(classifications: classifications)
let result = printer.print(tree: tree)

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

if args.has("--deserialize-incremental") {
if args.has("-deserialize-incremental") {
try performRoundTrip(args: args)
} else if args.has("--classify-syntax") {
} else if args.has("-classify-syntax") {
try performClassifySyntax(args: args)
} else if args.has("--help") {
} else if args.has("-help") {
printHelp()
} else {
printerr("""
No action specified.
See --help for information about available actions
See -help for information about available actions
""")
exit(1)
}
exit(0)
} catch {
printerr(error.localizedDescription)
printerr("Run swift-swiftsyntax-test --help for more help.")
printerr("Run swift-swiftsyntax-test -help for more help.")
exit(1)
}
9 changes: 9 additions & 0 deletions tools/swift-test-utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
add_swift_library(swiftTestUtils SHARED
# This file should be listed the first. Module name is inferred from the
# filename.
SwiftTestUtils.swift

SWIFT_MODULE_DEPENDS Foundation
INSTALL_IN_COMPONENT swift-syntax
TARGET_SDKS OSX
IS_STDLIB)
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import Foundation

struct CommandLineArguments {
struct MissingArgumentError: LocalizedError {
public struct CommandLineArguments {
public struct MissingArgumentError: LocalizedError {
let argName: String

var errorDescription: String? {
public var errorDescription: String? {
return "Missing required argument: \(argName)"
}
}
struct UnkeyedArgumentError: LocalizedError {
public struct UnkeyedArgumentError: LocalizedError {
let argName: String

var errorDescription: String? {
public var errorDescription: String? {
return "Unexpectedly found command line argument \(argName) without a key"
}
}

private let args: [String: String]

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

subscript(key: String) -> String? {
public subscript(key: String) -> String? {
return args[key]
}

func getRequired(_ key: String) throws -> String {
public func getRequired(_ key: String) throws -> String {
if let value = args[key] {
return value
} else {
throw MissingArgumentError(argName: key)
}
}

func has(_ key: String) -> Bool {
public func has(_ key: String) -> Bool {
return args[key] != nil
}
}
8 changes: 4 additions & 4 deletions utils/incrparse/incr_transfer_round_trip.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ def main():
sys.exit(1)

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