Snapshot tests cleanup

This commit is contained in:
pkurchatov
2023-10-31 14:11:38 +03:00
parent 8deba3ab1b
commit bc5eb14acb
3 changed files with 14 additions and 66 deletions
@@ -1,12 +1,7 @@
import Foundation
public enum ReferenceSet {
public static var path: String {
// GN build support
if let legacyPath = legacyPath {
return legacyPath
}
enum ReferenceSet {
static var path: String {
let plistPath = Bundle.main.path(forResource: "Info", ofType: "plist")!
let plistContents = try! PropertyListDecoder().decode(
PlistContents.self,
@@ -16,25 +11,6 @@ public enum ReferenceSet {
}
}
private var legacyPath: String? {
guard let plistPath = Bundle.main.path(forResource: "SnapshotsInfo", ofType: "plist") else {
return nil
}
let plistContents = try! PropertyListDecoder().decode(
LegacyPlistContents.self,
from: Data(contentsOf: URL(fileURLWithPath: plistPath))
)
return plistContents.projectPath + "/DivKit/reference_snapshots"
}
private struct LegacyPlistContents: Decodable {
let projectPath: String
enum CodingKeys: String, CodingKey {
case projectPath = "PROJECT_DIR_PATH"
}
}
private struct PlistContents: Decodable {
let referenceSnapshotsPath: String
@@ -1,12 +1,7 @@
import Foundation
public enum ReferenceSet {
public static var path: String {
// GN build support
if let legacyPath = legacyPath {
return legacyPath
}
enum ReferenceSet {
static var path: String {
let plistPath = Bundle.main.path(forResource: "Info", ofType: "plist")!
let plistContents = try! PropertyListDecoder().decode(
PlistContents.self,
@@ -16,25 +11,6 @@ public enum ReferenceSet {
}
}
private var legacyPath: String? {
guard let plistPath = Bundle.main.path(forResource: "SnapshotsInfo", ofType: "plist") else {
return nil
}
let plistContents = try! PropertyListDecoder().decode(
LegacyPlistContents.self,
from: Data(contentsOf: URL(fileURLWithPath: plistPath))
)
return plistContents.projectPath + "/DivKit/reference_snapshots"
}
private struct LegacyPlistContents: Decodable {
let projectPath: String
enum CodingKeys: String, CodingKey {
case projectPath = "PROJECT_DIR_PATH"
}
}
private struct PlistContents: Decodable {
let referenceSnapshotsPath: String
@@ -6,7 +6,6 @@ import XCTest
import CommonCorePublic
public enum TestMode {
case record
case update
case verify
}
@@ -54,23 +53,20 @@ public enum SnapshotTestKit {
do {
switch mode {
case .record:
case .update:
if let reference = try? UIImage.makeWith(url: referenceURL),
snapshot.compare(with: reference) {
return
}
let snapshotsDir = referenceURL.deletingLastPathComponent()
let fileManager = FileManager.default
if !fileManager.fileExists(atPath: snapshotsDir.path) {
try fileManager.createDirectory(at: snapshotsDir, withIntermediateDirectories: true)
}
try snapshot.makePNGData().write(to: referenceURL)
throw SnapshotTestError.recordModeEnabled
case .update:
let reference = try UIImage.makeWith(url: referenceURL)
if !snapshot.compare(with: reference) {
testSnapshot(
snapshot,
referenceURL: referenceURL,
mode: .record
)
}
throw SnapshotTestError.updateModeEnabled
case .verify:
let data = try Data(contentsOf: referenceURL)
if data == snapshot.pngData() {
@@ -114,14 +110,14 @@ extension UIImage {
}
private enum SnapshotTestError: LocalizedError {
case recordModeEnabled
case updateModeEnabled
case comparisonFailed
case nilPNGData
case imageCouldNotBeCreated
var errorDescription: String? {
switch self {
case .recordModeEnabled:
case .updateModeEnabled:
return "Snapshot saved. Don't forget to change mode back to `verify`!"
case .comparisonFailed:
return "View snapshot is not equal to reference. Diff is attached in test result"