Skip to content

Commit

Permalink
Add ExcludedSnapshotPreviews support (#69)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolas Hinderling <[email protected]>
  • Loading branch information
NicoHinderling and Nicolas Hinderling authored Nov 13, 2023
1 parent bf1f1ac commit f210b1d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
4 changes: 4 additions & 0 deletions DemoApp/DemoAppUITests/MyPreviewTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class MyPreviewTest: PreviewTest {
override func snapshotPreviews() -> [String]? {
return nil
}

override func excludedSnapshotPreviews() -> [String]? {
return nil
}

override func enableAccessibilityAudit() -> Bool {
true
Expand Down
31 changes: 27 additions & 4 deletions Sources/SnapshottingSwift/Snapshots.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,39 @@ class Snapshots {
try! FileManager.default.createDirectory(at: Self.resultsDir, withIntermediateDirectories: true)

let snapshotPreviews = ProcessInfo.processInfo.environment["SNAPSHOT_PREVIEWS"];
let excludedSnapshotPreviews = ProcessInfo.processInfo.environment["EXCLUDED_SNAPSHOT_PREVIEWS"];

var previewsSet: Set<String>? = nil
if let snapshotPreviews {
let previewsList = try! JSONDecoder().decode([String].self, from: snapshotPreviews.data(using: .utf8)!)
previewsSet = Set(previewsList)
}
let previewTypes = findPreviews { name in
guard let previewsSet else { return true }

return previewsSet.contains(name)
var excludedPreviewsSet: Set<String>? = nil
if let excludedSnapshotPreviews {
let excludedPreviewsList = try! JSONDecoder().decode([String].self, from: excludedSnapshotPreviews.data(using: .utf8)!)
excludedPreviewsSet = Set(excludedPreviewsList)
}

let previewTypes = findPreviews { name in
if let previewsSet {
return previewsSet.contains(name)
} else if let excludedPreviewsSet {
if #available(iOS 16.0, *) {
for excludedPreview in excludedPreviewsSet {
do {
let regex = try Regex(excludedPreview)
if name.firstMatch(of: regex) != nil {
return false
}
} catch {
print("Error trying to unwrap regex for excludedSnapshotPreview (\(excludedPreview)): \(error)")
}
}
}

}
return true
}
let json = previewTypes.map { preview in
[
"typeName": preview.typeName,
Expand Down
11 changes: 11 additions & 0 deletions Sources/SnapshottingTests/PreviewTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ - (XCUIApplication *)getApp {
return NULL;
}

- (NSArray<NSString *> *)excludedSnapshotPreviews {
return NULL;
}

- (BOOL)enableAccessibilityAudit {
return YES;
}
Expand Down Expand Up @@ -82,6 +86,13 @@ - (void)generateSnapshots {
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:previews options:nil error:nil];
launchEnvironment[@"SNAPSHOT_PREVIEWS"] = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

NSArray *excludedPreviews = [self excludedSnapshotPreviews];
if (excludedPreviews) {
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:excludedPreviews options:nil error:nil];
launchEnvironment[@"EXCLUDED_SNAPSHOT_PREVIEWS"] = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

app.launchEnvironment = launchEnvironment;
[app launch];

Expand Down
2 changes: 2 additions & 0 deletions Sources/SnapshottingTests/include/PreviewTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
// Elements should be the type name of the preview, like "MyModule.MyView_Previews"
- (nullable NSArray<NSString *> *)snapshotPreviews;

- (nullable NSArray<NSString *> *)excludedSnapshotPreviews;

- (BOOL)enableAccessibilityAudit;

- (XCUIAccessibilityAuditType)auditType API_AVAILABLE(ios(17.0));
Expand Down

0 comments on commit f210b1d

Please sign in to comment.