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