Skip to content

Commit 0f195fb

Browse files
authored
Merge pull request #153 from cashapp/entin/extract-images-script
Add script for extracting snapshot images from test results
2 parents 8c11d82 + 786757a commit 0f195fb

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/swift
2+
3+
// Usage:
4+
//
5+
// 1. Download the Test Results bundle from the CI build. From your PR, go to
6+
// Checks > CI, then click on "Test Results" in the Artifacts section.
7+
//
8+
// 2. Run this script with the path to this download.
9+
//
10+
// ./Scripts/ExtractImagesFromTestResults.sh /path/to/Test\ Results
11+
//
12+
// 3. This will create a directory called "tmp" that contains all of the
13+
// extracted images (reference, failed, and diff) organized by OS version,
14+
// test class name, and test name.
15+
16+
import Foundation
17+
18+
enum TaskError: Error {
19+
case code(Int32)
20+
}
21+
22+
func execute(commandPath: String, arguments: [String]) throws {
23+
let task = Process()
24+
task.launchPath = commandPath
25+
task.arguments = arguments
26+
27+
task.launch()
28+
29+
task.waitUntilExit()
30+
31+
guard task.terminationStatus == 0 else {
32+
throw TaskError.code(task.terminationStatus)
33+
}
34+
}
35+
36+
if CommandLine.arguments.count < 2 || CommandLine.arguments[1] == "--help" || CommandLine.arguments[1] == "-h" {
37+
print("usage: CompareRenamedSnapshots SNAPSHOT_LIST_FILE OLD_REPO_PATH NEW_DEVICE_ID OLD_DEVICE_ID")
38+
exit(0)
39+
}
40+
41+
let testResultsContainerPath = CommandLine.arguments[1]
42+
let testResultsContainerURL = URL(filePath: testResultsContainerPath)
43+
44+
let fileManager = FileManager.default
45+
guard let testResultsEnumerator = fileManager.enumerator(at: testResultsContainerURL, includingPropertiesForKeys: [URLResourceKey.nameKey], options: .skipsHiddenFiles) else {
46+
exit(1)
47+
}
48+
49+
func parseResults(testResultsPath: String, outputPath: String) throws {
50+
try execute(
51+
commandPath: "/opt/homebrew/bin/xcparse",
52+
arguments: ["screenshots", "--os", "--test", testResultsPath, outputPath]
53+
)
54+
}
55+
56+
for case let fileURL as URL in testResultsEnumerator {
57+
if fileURL.path.hasSuffix(".xcresult") {
58+
try parseResults(testResultsPath: fileURL.path, outputPath: "tmp")
59+
}
60+
}
61+
62+
/*
63+
64+
As a future enhancement, this script could also rename the failed snapshot images. One complicating
65+
factor of this is the tests with multiple associated reference images (e.g. when using identifiers)
66+
don't differentiate the resulting images in the test result bundle. We'll also need to handle
67+
renaming based on which snapshot engine was used:
68+
69+
FBSnapshotTestCase
70+
71+
/Example/SnapshotTests/ReferenceImages/_64/<TestClass>/
72+
73+
74+
75+
76+
SnapshotTesting
77+
78+
/Example/SnapshotTests/__Snapshots__/<TestClass>/
79+
80+
<TestName>.390x844-14-5-3x.png
81+
<TestName>.375x812-13-7-3x.png
82+
83+
*/
84+

0 commit comments

Comments
 (0)