Skip to content

Commit

Permalink
Added support for ksdiff
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrandonw committed Jul 7, 2017
1 parent 83581db commit a19175e
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions Sources/ScreenshotAssertions/ScreenshotAssertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ public func assertScreenshot(
-> XCTAttachment? {

let fileURL = URL(fileURLWithPath: String(describing: file))
let screenshotsURL = fileURL.deletingLastPathComponent().appendingPathComponent("__Screenshots__")
let screenshotsDirectoryURL = fileURL.deletingLastPathComponent()
.appendingPathComponent("__Screenshots__")
let fileManager = FileManager.default

try! fileManager.createDirectory(
at: screenshotsURL,
at: screenshotsDirectoryURL,
withIntermediateDirectories: true,
attributes: nil
)
Expand All @@ -25,8 +26,8 @@ public func assertScreenshot(
UIGraphicsEndImageContext()
let data = UIImagePNGRepresentation(image)!

let screenshotURL = screenshotsURL
.appendingPathComponent("\(fileURL.deletingPathExtension().lastPathComponent).\(function).png")
let screenshotName = "\(fileURL.deletingPathExtension().lastPathComponent).\(function).png"
let screenshotURL = URL(string: screenshotName, relativeTo: screenshotsDirectoryURL)!

guard fileManager.fileExists(atPath: screenshotURL.path) else {
try! data.write(to: screenshotURL)
Expand All @@ -40,9 +41,17 @@ public func assertScreenshot(
let attachment = XCTAttachment(image: imageDiff)
attachment.lifetime = .deleteOnSuccess

let failedScreenshotUrl = URL.init(fileURLWithPath: NSTemporaryDirectory())
.appendingPathComponent(screenshotName)
try! data.write(to: failedScreenshotUrl)

let ksdiff = """
ksdiff "\(trimFileProtocol(screenshotURL))" "\(trimFileProtocol(failedScreenshotUrl))"
"""

XCTAssert(
false,
"\(screenshotURL.debugDescription) does not match screenshot",
"\(screenshotURL.debugDescription) does not match screenshot\n\n\(ksdiff)\n",
file: file,
line: line
)
Expand All @@ -69,3 +78,7 @@ func diff(_ a: UIImage, _ b: UIImage) -> UIImage {
UIGraphicsEndImageContext()
return image
}

private func trimFileProtocol(_ url: URL) -> String {
return String(url.absoluteString.suffix(url.absoluteString.count - 7))
}

0 comments on commit a19175e

Please sign in to comment.