|
1 | | -import Foundation |
2 | | -import TSCBasic |
3 | | -import TSCUtility |
| 1 | +import ArgumentParser |
4 | 2 | import FileCheck |
| 3 | +import Foundation |
| 4 | + |
| 5 | +private extension FileCheckOptions { |
| 6 | + init(_ command: FileCheckCommand) { |
| 7 | + var options = FileCheckOptions() |
| 8 | + if command.disableColors { |
| 9 | + options.insert(.disableColors) |
| 10 | + } |
| 11 | + |
| 12 | + if command.useStrictWhitespace { |
| 13 | + options.insert(.strictWhitespace) |
| 14 | + } |
| 15 | + |
| 16 | + if command.allowEmptyInput { |
| 17 | + options.insert(.allowEmptyInput) |
| 18 | + } |
| 19 | + |
| 20 | + if command.matchFullLines { |
| 21 | + options.insert(.matchFullLines) |
| 22 | + } |
| 23 | + |
| 24 | + self = options |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +struct FileCheckCommand: ParsableCommand { |
| 29 | + static var configuration = CommandConfiguration(commandName: "filecheck") |
| 30 | + |
| 31 | + @Flag(help: "Disable colorized diagnostics.") |
| 32 | + var disableColors = false |
| 33 | + |
| 34 | + @Flag(help: "Do not treat all horizontal whitespace as equivalent.") |
| 35 | + var useStrictWhitespace = false |
| 36 | + |
| 37 | + @Flag( |
| 38 | + name: [.customShort("e"), .long], |
| 39 | + help: """ |
| 40 | + Allow the input file to be empty. This is useful when \ |
| 41 | + making checks that some error message does not occur, \ |
| 42 | + for example. |
| 43 | + """ |
| 44 | + ) |
| 45 | + var allowEmptyInput = false |
| 46 | + |
| 47 | + @Flag(help: """ |
| 48 | + Require all positive matches to cover an entire input line. \ |
| 49 | + Allows leading and trailing whitespace if \ |
| 50 | + --strict-whitespace is not also used. |
| 51 | + """) |
| 52 | + var matchFullLines = false |
| 53 | + |
| 54 | + @Option( |
| 55 | + help: """ |
| 56 | + Specifies one or more prefixes to match. By default these \ |
| 57 | + patterns are prefixed with “CHECK”. |
| 58 | + """ |
| 59 | + ) |
| 60 | + var prefixes: [String] = [] |
| 61 | + |
| 62 | + @Option( |
| 63 | + name: .shortAndLong, |
| 64 | + help: "The file to use for checked input. Defaults to stdin." |
| 65 | + ) |
| 66 | + var inputFile: String? |
| 67 | + |
| 68 | + @Argument |
| 69 | + var file: String |
| 70 | + |
| 71 | + mutating func run() throws { |
| 72 | + let fileHandle: FileHandle |
| 73 | + if let input = inputFile { |
| 74 | + guard let handle = FileHandle(forReadingAtPath: input) else { |
| 75 | + throw ValidationError("unable to open check file at path \(input).") |
| 76 | + } |
| 77 | + |
| 78 | + fileHandle = handle |
| 79 | + } else { |
| 80 | + fileHandle = .standardInput |
| 81 | + } |
| 82 | + |
| 83 | + let checkPrefixes = prefixes + ["CHECK"] |
| 84 | + let matchedAll = fileCheckOutput(of: .stdout, |
| 85 | + withPrefixes: checkPrefixes, |
| 86 | + checkNot: [], |
| 87 | + against: .filePath(file), |
| 88 | + options: FileCheckOptions(self)) { |
| 89 | + |
| 90 | + // FIXME: Better way to stream this data? |
| 91 | + FileHandle.standardOutput.write(fileHandle.readDataToEndOfFile()) |
| 92 | + } |
| 93 | + |
5 | 94 |
|
6 | | -func run() -> Int { |
7 | | - let cli = ArgumentParser(usage: "FileCheck", overview: "") |
8 | | - let binder = ArgumentBinder<FileCheckOptions>() |
9 | | - //swiftlint:disable statement_position |
10 | | - binder.bind(option: |
11 | | - cli.add(option: "--disable-colors", kind: Bool.self, |
12 | | - usage: "Disable colorized diagnostics"), |
13 | | - to: { if $1 { $0.insert(.disableColors) } |
14 | | - else { $0.remove(.disableColors) } }) |
15 | | - binder.bind(option: |
16 | | - cli.add(option: "--use-strict-whitespace", |
17 | | - kind: Bool.self, |
18 | | - usage: "Do not treat all horizontal whitespace as equivalent"), |
19 | | - to: { if $1 { $0.insert(.strictWhitespace) } |
20 | | - else { $0.remove(.strictWhitespace) } }) |
21 | | - binder.bind(option: |
22 | | - cli.add(option: "--allow-empty-input", shortName: "-e", |
23 | | - kind: Bool.self, |
24 | | - usage: """ |
25 | | - Allow the input file to be empty. This is useful when \ |
26 | | - making checks that some error message does not occur, \ |
27 | | - for example. |
28 | | - """), |
29 | | - to: { if $1 { $0.insert(.allowEmptyInput) } |
30 | | - else { $0.remove(.allowEmptyInput) } }) |
31 | | - binder.bind(option: |
32 | | - cli.add(option: "--match-full-lines", |
33 | | - kind: Bool.self, |
34 | | - usage: """ |
35 | | - Require all positive matches to cover an entire input line. \ |
36 | | - Allows leading and trailing whitespace if \ |
37 | | - --strict-whitespace is not also used. |
38 | | - """), |
39 | | - to: { if $1 { $0.insert(.matchFullLines) } |
40 | | - else { $0.remove(.matchFullLines) } }) |
41 | | - let prefixes = |
42 | | - cli.add(option: "--prefixes", kind: [String].self, |
43 | | - usage: """ |
44 | | - Specifies one or more prefixes to match. By default these \ |
45 | | - patterns are prefixed with “CHECK”. |
46 | | - """) |
47 | | - |
48 | | - let inputFile = |
49 | | - cli.add(option: "--input-file", shortName: "-i", |
50 | | - kind: String.self, |
51 | | - usage: "The file to use for checked input. Defaults to stdin.") |
52 | | - |
53 | | - let file = |
54 | | - cli.add(positional: "", kind: String.self, |
55 | | - usage: "") |
56 | | - |
57 | | - let args = Array(CommandLine.arguments.dropFirst()) |
58 | | - guard let results = try? cli.parse(args) else { |
59 | | - cli.printUsage(on: stderrStream) |
60 | | - return -1 |
61 | | - } |
62 | | - |
63 | | - guard let filePath = results.get(file) else { |
64 | | - print("FileCheck error: No input file was provided.") |
65 | | - return -1 |
66 | | - } |
67 | | - |
68 | | - var options = FileCheckOptions() |
69 | | - do { |
70 | | - try binder.fill(parseResult: results, into: &options) |
71 | | - } catch { |
72 | | - cli.printUsage(on: stderrStream) |
73 | | - return -1 |
74 | | - } |
75 | | - |
76 | | - let fileHandle: FileHandle |
77 | | - if let input = results.get(inputFile) { |
78 | | - guard let handle = FileHandle(forReadingAtPath: input) else { |
79 | | - print("FileCheck error: unable to open check file at path \(inputFile).") |
80 | | - return -1 |
| 95 | + if !matchedAll { |
| 96 | + throw ExitCode.failure |
| 97 | + } |
81 | 98 | } |
82 | | - fileHandle = handle |
83 | | - } else { |
84 | | - fileHandle = .standardInput |
85 | | - } |
86 | | - var checkPrefixes = results.get(prefixes) ?? [] |
87 | | - checkPrefixes.append("CHECK") |
88 | | - |
89 | | - let matchedAll = fileCheckOutput(of: .stdout, |
90 | | - withPrefixes: checkPrefixes, |
91 | | - checkNot: [], |
92 | | - against: .filePath(filePath), |
93 | | - options: options) { |
94 | | - // FIXME: Better way to stream this data? |
95 | | - FileHandle.standardOutput.write(fileHandle.readDataToEndOfFile()) |
96 | | - } |
97 | | - |
98 | | - return matchedAll ? 0 : -1 |
99 | 99 | } |
100 | 100 |
|
101 | | -exit(Int32(run())) |
| 101 | +FileCheckCommand.main() |
0 commit comments