-
-
Notifications
You must be signed in to change notification settings - Fork 63
BridgeJS: show unified diff on snapshot mismatch #600
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,17 +22,26 @@ func assertSnapshot( | |
|
|
||
| if FileManager.default.fileExists(atPath: snapshotPath.path) { | ||
| let existingSnapshot = try String(contentsOf: snapshotPath, encoding: .utf8) | ||
| let ok = existingSnapshot == String(data: input, encoding: .utf8)! | ||
| let actual = String(data: input, encoding: .utf8)! | ||
| let ok = existingSnapshot == actual | ||
| let actualFilePath = snapshotPath.path + ".actual" | ||
| let updateSnapshots = ProcessInfo.processInfo.environment["UPDATE_SNAPSHOTS"] != nil | ||
| func buildComment() -> Comment { | ||
| "Snapshot mismatch: \(actualFilePath) \(snapshotPath.path)" | ||
| } | ||
|
|
||
| if !updateSnapshots { | ||
| #expect(ok, buildComment(), sourceLocation: sourceLocation) | ||
| if !ok { | ||
| try input.write(to: URL(fileURLWithPath: actualFilePath)) | ||
| try actual.write(toFile: actualFilePath, atomically: true, encoding: .utf8) | ||
| } | ||
|
|
||
| let diff = ok ? nil : unifiedDiff(expectedPath: snapshotPath.path, actualPath: actualFilePath) | ||
| func buildComment() -> Comment { | ||
| var message = "Snapshot mismatch: \(actualFilePath) \(snapshotPath.path)" | ||
| if let diff { | ||
| message.append("\n\n" + diff) | ||
| } | ||
| return Comment(rawValue: message) | ||
| } | ||
|
|
||
| #expect(ok, buildComment(), sourceLocation: sourceLocation) | ||
| } else { | ||
| try input.write(to: snapshotPath) | ||
| } | ||
|
|
@@ -41,3 +50,23 @@ func assertSnapshot( | |
| #expect(Bool(false), "Snapshot created at \(snapshotPath.path)", sourceLocation: sourceLocation) | ||
| } | ||
| } | ||
|
|
||
| private func unifiedDiff(expectedPath: String, actualPath: String) -> String? { | ||
| let process = Process() | ||
| process.executableURL = URL(fileURLWithPath: "/usr/bin/env") | ||
| process.arguments = ["diff", "-u", expectedPath, actualPath] | ||
| let output = Pipe() | ||
| process.standardOutput = output | ||
| process.standardError = Pipe() | ||
|
|
||
| do { | ||
| try process.run() | ||
| } catch { | ||
| return nil | ||
| } | ||
| process.waitUntilExit() | ||
|
|
||
| let data = output.fileHandleForReading.readDataToEndOfFile() | ||
|
Comment on lines
+55
to
+69
|
||
| guard !data.isEmpty else { return nil } | ||
| return String(data: data, encoding: .utf8) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
process.standardErroris set to aPipe()but never read. This can both drop useful error output (e.g., diff errors) and has the same pipe-buffer deadlock risk as stdout. Either read stderr as well, or redirect stderr to stdout / a file, and consider checkingterminationStatus(0=same, 1=different, 2=error) to decide what to return.