Skip to content

Commit f86bbb5

Browse files
committed
A touch of error handling
Previously, failing to read the file would cause the entire test to be abandoned. Now we print an error and keep going.
1 parent 0370acb commit f86bbb5

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

Sources/LiteSupport/RunLineParser.swift

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,26 @@ enum RunLineParser {
7676
pattern: "\(escapedPrefix)\\s*([\\w-]+):(.*)$",
7777
options: [.anchorsMatchLines])
7878
var lines = [RunLine]()
79-
let contents = try String(contentsOf: file, encoding: .utf8)
80-
let nsString = NSString(string: contents)
81-
let range = NSRange(location: 0, length: nsString.length)
82-
for match in regex.matches(in: contents, range: range) {
83-
let command = nsString.substring(with: match.range(at: 1))
84-
let runLine = nsString.substring(with: match.range(at: 2))
85-
.trimmingCharacters(in: .whitespaces)
86-
if runLine.isEmpty { continue }
87-
let mode: RunLine.Mode
88-
switch command {
89-
case "RUN": mode = .run
90-
case "RUN-NOT": mode = .runNot
91-
case "RUN-XFAIL": mode = .runXfail
92-
default: continue
79+
switch Result(catching: { try String(contentsOf: file, encoding: .utf8) }) {
80+
case .failure(_):
81+
print("⚠️ could not open '\(file.path)'; skipping test")
82+
case let .success(contents):
83+
let nsString = NSString(string: contents)
84+
let range = NSRange(location: 0, length: nsString.length)
85+
for match in regex.matches(in: contents, range: range) {
86+
let command = nsString.substring(with: match.range(at: 1))
87+
let runLine = nsString.substring(with: match.range(at: 2))
88+
.trimmingCharacters(in: .whitespaces)
89+
if runLine.isEmpty { continue }
90+
let mode: RunLine.Mode
91+
switch command {
92+
case "RUN": mode = .run
93+
case "RUN-NOT": mode = .runNot
94+
case "RUN-XFAIL": mode = .runXfail
95+
default: continue
96+
}
97+
lines.append(RunLine(mode: mode, commandLine: runLine))
9398
}
94-
lines.append(RunLine(mode: mode, commandLine: runLine))
9599
}
96100
return lines
97101
}

0 commit comments

Comments
 (0)