Repository navigation
Expand file tree
/
Copy pathSnapshotTesting.swift
More file actions
95 lines (87 loc) · 2.7 KB
/
Copy pathSnapshotTesting.swift
File metadata and controls
95 lines (87 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// SnapshotTesting.swift
// Shared
import Foundation
import SnapshotTesting
import Testing
let defaultSize = CGSize(width: 200, height: 200)
#if OPENSWIFTUI
let shouldRecord: SnapshotTestingConfiguration.Record? = nil
#else
let shouldRecord: SnapshotTestingConfiguration.Record? = .all
#endif
let diffTool: SnapshotTestingConfiguration.DiffTool = .odiff
extension SnapshotTestingConfiguration.DiffTool {
static let odiff = Self {
"odiff \"\($0)\" \"\($1)\""
}
}
private let defaultSnapshotReferenceRoot: String = {
URL(fileURLWithPath: #filePath)
.deletingLastPathComponent()
.deletingLastPathComponent()
.appendingPathComponent("ReferenceImages")
.path
}()
let snapshotReferenceDirectory: String = {
#if os(macOS)
let os = "macOS"
#elseif os(iOS)
#if targetEnvironment(simulator)
let os = "iOS_Simulator"
#else
let os = "iOS"
#endif
#else
#error("Unsupported UI test platform")
#endif
let version = ProcessInfo.processInfo.operatingSystemVersion
let osVersion = "\(version.majorVersion).\(version.minorVersion).\(version.patchVersion)"
let configuredRoot = ProcessInfo.processInfo.environment["SNAPSHOT_REFERENCE_DIR"]
let referenceRoot: String
if let configuredRoot, !configuredRoot.isEmpty, !configuredRoot.contains("$(") {
referenceRoot = configuredRoot
} else {
referenceRoot = defaultSnapshotReferenceRoot
}
let directory = referenceRoot + "/\(os)/\(osVersion)"
print("SNAPSHOT_REFERENCE_DIR: \(directory)")
return directory
}()
func openSwiftUIAssertSnapshotValue<Value, Format>(
of value: @autoclosure () -> Value,
as snapshotting: Snapshotting<Value, Format>,
named name: String? = nil,
record recording: SnapshotTestingConfiguration.Record? = shouldRecord,
timeout: TimeInterval = 5,
fileID: StaticString = #fileID,
file filePath: StaticString = #filePath,
testName: String = #function,
line: UInt = #line,
column: UInt = #column
) {
let snapshotDirectory = snapshotReferenceDirectory + "/" + fileID.description
let failure = verifySnapshot(
of: value(),
as: snapshotting,
named: name,
record: recording,
snapshotDirectory: snapshotDirectory,
timeout: timeout,
fileID: fileID,
file: filePath,
testName: testName,
line: line,
column: column
)
guard let message = failure else { return }
Issue.record(
Comment(rawValue: message),
sourceLocation: SourceLocation(
fileID: fileID.description,
filePath: filePath.description,
line: Int(line),
column: Int(column)
)
)
}