From f210b1d57397ad62502becc8a5a90791e39384a4 Mon Sep 17 00:00:00 2001 From: Nico Hinderling Date: Mon, 13 Nov 2023 11:51:35 -0800 Subject: [PATCH] Add ExcludedSnapshotPreviews support (#69) Co-authored-by: Nicolas Hinderling --- DemoApp/DemoAppUITests/MyPreviewTest.swift | 4 +++ Sources/SnapshottingSwift/Snapshots.swift | 31 ++++++++++++++++--- Sources/SnapshottingTests/PreviewTest.m | 11 +++++++ .../SnapshottingTests/include/PreviewTest.h | 2 ++ 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/DemoApp/DemoAppUITests/MyPreviewTest.swift b/DemoApp/DemoAppUITests/MyPreviewTest.swift index 309a12c..531ba7e 100644 --- a/DemoApp/DemoAppUITests/MyPreviewTest.swift +++ b/DemoApp/DemoAppUITests/MyPreviewTest.swift @@ -19,6 +19,10 @@ class MyPreviewTest: PreviewTest { override func snapshotPreviews() -> [String]? { return nil } + + override func excludedSnapshotPreviews() -> [String]? { + return nil + } override func enableAccessibilityAudit() -> Bool { true diff --git a/Sources/SnapshottingSwift/Snapshots.swift b/Sources/SnapshottingSwift/Snapshots.swift index eacdce8..cd1fb06 100644 --- a/Sources/SnapshottingSwift/Snapshots.swift +++ b/Sources/SnapshottingSwift/Snapshots.swift @@ -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? = 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? = 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, diff --git a/Sources/SnapshottingTests/PreviewTest.m b/Sources/SnapshottingTests/PreviewTest.m index d954c2c..7cdcbbb 100644 --- a/Sources/SnapshottingTests/PreviewTest.m +++ b/Sources/SnapshottingTests/PreviewTest.m @@ -48,6 +48,10 @@ - (XCUIApplication *)getApp { return NULL; } +- (NSArray *)excludedSnapshotPreviews { + return NULL; +} + - (BOOL)enableAccessibilityAudit { return YES; } @@ -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]; diff --git a/Sources/SnapshottingTests/include/PreviewTest.h b/Sources/SnapshottingTests/include/PreviewTest.h index be8131b..bbe5abd 100644 --- a/Sources/SnapshottingTests/include/PreviewTest.h +++ b/Sources/SnapshottingTests/include/PreviewTest.h @@ -19,6 +19,8 @@ // Elements should be the type name of the preview, like "MyModule.MyView_Previews" - (nullable NSArray *)snapshotPreviews; +- (nullable NSArray *)excludedSnapshotPreviews; + - (BOOL)enableAccessibilityAudit; - (XCUIAccessibilityAuditType)auditType API_AVAILABLE(ios(17.0));