-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCIAssertSnapshot.swift
75 lines (68 loc) · 2.71 KB
/
CIAssertSnapshot.swift
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
import Foundation
@_exported import SnapshotTesting
import XCTest
/// Returns a valid snapshot directory under the project’s `ci_scripts`.
///
/// - Parameter file: A `StaticString` representing the current test’s filename.
/// - Returns: A directory for the snapshots.
/// - Note: It makes strong assumptions about the structure of the project; namely,
/// it expects the project to consist of a single package located at the root.
func snapshotDirectory(
for file: StaticString,
testsPathComponent: String = "Tests",
packagesPathComponent: String = "Packages",
ciScriptsPathComponent: String = "ci_scripts",
snapshotsPathComponent: String = "__Snapshots__"
) -> String {
let fileURL = URL(fileURLWithPath: "\(file)", isDirectory: false)
let packageRootPath = fileURL
.pathComponents
.prefix(while: { $0 != testsPathComponent && $0 != packagesPathComponent })
let testsPath = packageRootPath + [testsPathComponent]
let relativePath = fileURL
.deletingPathExtension()
.pathComponents
.dropFirst(testsPath.count)
let snapshotDirectoryPath = packageRootPath + [ciScriptsPathComponent, snapshotsPathComponent] +
relativePath
return snapshotDirectoryPath.joined(separator: "/")
}
/// Asserts that a given value matches references on disk.
///
/// - Parameters:
/// - value: A value to compare against a reference.
/// - snapshotting: An array of strategies for serializing, deserializing, and comparing values.
/// - recording: Whether or not to record a new reference.
/// - timeout: The amount of time a snapshot must be generated in.
/// - file: The file in which failure occurred. Defaults to the file name of the test case in
/// which this function was called.
/// - testName: The name of the test in which failure occurred. Defaults to the function name of
/// the test case in which this function was called.
/// - line: The line number on which failure occurred. Defaults to the line number on which this
/// function was called.
/// - testsPathComponent: The name of the tests directory. Defaults to “Tests”.
@MainActor
public func ciAssertSnapshot<Value>(
matching value: @autoclosure () throws -> Value,
as snapshotting: Snapshotting<Value, some Any>,
named name: String? = nil,
record recording: Bool = false,
timeout: TimeInterval = 5,
file: StaticString = #file,
testName: String = #function,
line: UInt = #line,
testsPathComponent: String = "Tests"
) {
let failure = verifySnapshot(
matching: try value(),
as: snapshotting,
named: name,
record: recording,
snapshotDirectory: snapshotDirectory(for: file, testsPathComponent: testsPathComponent),
timeout: timeout,
file: file,
testName: testName
)
guard let message = failure else { return }
XCTFail(message, file: file, line: line)
}