-
Notifications
You must be signed in to change notification settings - Fork 22
feat(snapshots): Add image-name manifest mode #267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cameroncooke
wants to merge
2
commits into
main
Choose a base branch
from
cameroncooke/EME-1182-selective_snapshots
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
Sources/SnapshottingTests/AllSnapshotImageNamesWriter.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import Foundation | ||
|
|
||
| final class AllSnapshotImageNamesWriter { | ||
| static let envKey = "SNAPSHOTS_ALL_IMAGE_NAMES_FILE" | ||
|
|
||
| private let outputURL: URL | ||
|
|
||
| static func createFromEnvironment( | ||
| environment: [String: String] = ProcessInfo.processInfo.environment, | ||
| fileManager: FileManager = .default | ||
| ) -> AllSnapshotImageNamesWriter? { | ||
| guard let outputPath = environment[envKey] else { | ||
| return nil | ||
| } | ||
|
|
||
| let trimmed = outputPath.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| guard !trimmed.isEmpty else { | ||
| preconditionFailure("\(envKey) is set but empty. Provide a valid file path.") | ||
| } | ||
|
|
||
| let outputURL: URL | ||
| if trimmed.hasPrefix("/") { | ||
| outputURL = URL(fileURLWithPath: trimmed).standardizedFileURL | ||
| } else { | ||
| outputURL = URL(fileURLWithPath: fileManager.currentDirectoryPath) | ||
| .appendingPathComponent(trimmed) | ||
| .standardizedFileURL | ||
| } | ||
|
|
||
| return Self(outputURL: outputURL, fileManager: fileManager) | ||
| } | ||
|
|
||
| init(outputURL: URL, fileManager: FileManager = .default) { | ||
| self.outputURL = outputURL | ||
|
|
||
| do { | ||
| try fileManager.createDirectory( | ||
| at: outputURL.deletingLastPathComponent(), | ||
| withIntermediateDirectories: true | ||
| ) | ||
| } catch { | ||
| preconditionFailure("Failed to create all snapshot image names directory at \(outputURL.deletingLastPathComponent().path): \(error)") | ||
| } | ||
| } | ||
|
|
||
| func write(imageNames: [String]) { | ||
| let sortedImageNames = Set(imageNames).sorted() | ||
| let contents = sortedImageNames.isEmpty ? "" : "\(sortedImageNames.joined(separator: "\n"))\n" | ||
| guard let data = contents.data(using: .utf8) else { | ||
| preconditionFailure("Failed to encode all snapshot image names file at \(outputURL.path)") | ||
| } | ||
|
|
||
| do { | ||
| try data.write(to: outputURL, options: .atomic) | ||
| } catch { | ||
| preconditionFailure("Failed to write all snapshot image names file at \(outputURL.path): \(error)") | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import Foundation | ||
|
|
||
| struct SnapshotPreviewDestination { | ||
| static func currentDeviceName( | ||
| environment: [String: String] = ProcessInfo.processInfo.environment | ||
| ) -> String? { | ||
| environment["SIMULATOR_DEVICE_NAME"] ?? environment["SIMULATOR_MODEL_IDENTIFIER"] | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
Sources/SnapshottingTests/SnapshotPreviewDeviceFilter.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| struct SnapshotPreviewDeviceFilter { | ||
| static func shouldInclude( | ||
| discoveredPreview: DiscoveredPreview, | ||
| index: Int, | ||
| currentDestinationDeviceName: String? | ||
| ) -> Bool { | ||
| let requestedDevice = discoveredPreview.devices.indices.contains(index) ? discoveredPreview.devices[index] : nil | ||
| return shouldInclude(requestedDeviceName: requestedDevice, currentDestinationDeviceName: currentDestinationDeviceName) | ||
| } | ||
|
|
||
| static func shouldInclude( | ||
| requestedDeviceName: String?, | ||
| currentDestinationDeviceName: String? | ||
| ) -> Bool { | ||
| guard let currentDestinationDeviceName else { | ||
| return true | ||
| } | ||
|
|
||
| guard let requestedDeviceName, !requestedDeviceName.isEmpty else { | ||
| return true | ||
| } | ||
|
|
||
| return requestedDeviceName == currentDestinationDeviceName | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
Tests/SnapshottingTestsTests/AllSnapshotImageNamesTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import Foundation | ||
| import SwiftUI | ||
| @testable import SnapshotPreviewsCore | ||
| @testable import SnapshottingTests | ||
| import XCTest | ||
|
|
||
| @MainActor | ||
| final class AllSnapshotImageNamesTests: XCTestCase { | ||
| private var tempDir: URL! | ||
|
|
||
| override func setUp() { | ||
| super.setUp() | ||
| tempDir = FileManager.default.temporaryDirectory | ||
| .appendingPathComponent("AllSnapshotImageNamesTests-\(UUID().uuidString)") | ||
| } | ||
|
|
||
| override func tearDown() { | ||
| try? FileManager.default.removeItem(at: tempDir) | ||
| super.tearDown() | ||
| } | ||
|
|
||
| func testCurrentDestinationDeviceNamePrefersSimulatorDeviceName() { | ||
| let deviceName = SnapshotPreviewDestination.currentDeviceName( | ||
| environment: [ | ||
| "SIMULATOR_DEVICE_NAME": "iPhone 15", | ||
| "SIMULATOR_MODEL_IDENTIFIER": "iPhone16,1", | ||
| ] | ||
| ) | ||
|
|
||
| XCTAssertEqual(deviceName, "iPhone 15") | ||
| } | ||
|
|
||
| func testCurrentDestinationDeviceNameFallsBackToSimulatorModelIdentifier() { | ||
| let deviceName = SnapshotPreviewDestination.currentDeviceName( | ||
| environment: ["SIMULATOR_MODEL_IDENTIFIER": "iPhone16,1"] | ||
| ) | ||
|
|
||
| XCTAssertEqual(deviceName, "iPhone16,1") | ||
| } | ||
|
|
||
| func testDiscoveredPreviewDeviceFilterIncludesUndeclaredAndMatchingDevices() { | ||
| let preview = DiscoveredPreview( | ||
| typeName: "Module.TestView_Previews", | ||
| displayName: "Test View", | ||
| devices: ["", "iPhone 15", "iPhone 14"], | ||
| orientations: ["portrait", "portrait", "portrait"], | ||
| numberOfPreviews: 3 | ||
| ) | ||
|
|
||
| XCTAssertTrue( | ||
| SnapshotPreviewDeviceFilter.shouldInclude( | ||
| discoveredPreview: preview, | ||
| index: 0, | ||
| currentDestinationDeviceName: "iPhone 15" | ||
| ) | ||
| ) | ||
| XCTAssertTrue( | ||
| SnapshotPreviewDeviceFilter.shouldInclude( | ||
| discoveredPreview: preview, | ||
| index: 1, | ||
| currentDestinationDeviceName: "iPhone 15" | ||
| ) | ||
| ) | ||
| XCTAssertFalse( | ||
| SnapshotPreviewDeviceFilter.shouldInclude( | ||
| discoveredPreview: preview, | ||
| index: 2, | ||
| currentDestinationDeviceName: "iPhone 15" | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| func testWriterReplacesStaleOutputWithSortedDeduplicatedNames() throws { | ||
| let outputURL = tempDir.appendingPathComponent("all-image-names.txt") | ||
| try FileManager.default.createDirectory(at: tempDir, withIntermediateDirectories: true) | ||
| try "stale.png\n".write(to: outputURL, atomically: true, encoding: .utf8) | ||
|
|
||
| let writer = try XCTUnwrap( | ||
| AllSnapshotImageNamesWriter.createFromEnvironment( | ||
| environment: [AllSnapshotImageNamesWriter.envKey: outputURL.path] | ||
| ) | ||
| ) | ||
|
|
||
| writer.write(imageNames: ["Beta.png", "Alpha.png", "Beta.png"]) | ||
|
|
||
| XCTAssertEqual(try readAllSnapshotImageNamesFile(at: outputURL), "Alpha.png\nBeta.png\n") | ||
| } | ||
|
|
||
| func testWriterWritesEmptyFileWhenThereAreNoImageNames() throws { | ||
| let outputURL = tempDir.appendingPathComponent("all-image-names.txt") | ||
| let writer = AllSnapshotImageNamesWriter(outputURL: outputURL) | ||
|
|
||
| writer.write(imageNames: []) | ||
|
|
||
| XCTAssertEqual(try readAllSnapshotImageNamesFile(at: outputURL), "") | ||
| } | ||
|
|
||
| func testLogicalImageNamesAreSanitizedPngNamesAndPreserveDuplicateOrdinalsAfterDeviceFiltering() { | ||
| let previewType = PreviewType( | ||
| typeName: "TestModule.AllSnapshotImageNamesTestProvider", | ||
| previewProvider: AllSnapshotImageNamesTestProvider.self | ||
| ) | ||
| let resolver = SnapshotTest.FileNameResolver(previews: [previewType]) | ||
|
|
||
| let imageNames = SnapshotTest.logicalImageNames( | ||
| previews: [previewType], | ||
| fileNameResolver: resolver, | ||
| environment: ["SIMULATOR_DEVICE_NAME": "iPhone 14"] | ||
| ) | ||
|
|
||
| XCTAssertEqual( | ||
| imageNames, | ||
| [ | ||
| "All_Snapshot_Image_Names_Test_Provider_Duplicate_1.png", | ||
| "All_Snapshot_Image_Names_Test_Provider_Other.png", | ||
| ] | ||
| ) | ||
| } | ||
|
|
||
| func testLogicalImageNamesUsePreviewTypeDeviceWhenDestinationMatches() { | ||
| let previewType = PreviewType( | ||
| typeName: "TestModule.AllSnapshotImageNamesTestProvider", | ||
| previewProvider: AllSnapshotImageNamesTestProvider.self | ||
| ) | ||
| let resolver = SnapshotTest.FileNameResolver(previews: [previewType]) | ||
|
|
||
| let imageNames = SnapshotTest.logicalImageNames( | ||
| previews: [previewType], | ||
| fileNameResolver: resolver, | ||
| environment: ["SIMULATOR_DEVICE_NAME": "iPhone 15"] | ||
| ) | ||
|
|
||
| XCTAssertEqual( | ||
| imageNames, | ||
| [ | ||
| "All_Snapshot_Image_Names_Test_Provider_Duplicate_1.png", | ||
| "All_Snapshot_Image_Names_Test_Provider_Duplicate_2.png", | ||
| "All_Snapshot_Image_Names_Test_Provider_Other.png", | ||
| ] | ||
| ) | ||
| } | ||
|
|
||
| private func readAllSnapshotImageNamesFile(at url: URL) throws -> String { | ||
| try String(contentsOf: url, encoding: .utf8) | ||
| } | ||
| } | ||
|
|
||
| private struct AllSnapshotImageNamesTestProvider: PreviewProvider { | ||
| static var previews: some View { | ||
| Group { | ||
| Text("One") | ||
| .previewDisplayName("Duplicate") | ||
| Text("Two") | ||
| .previewDisplayName("Duplicate") | ||
| .previewDevice("iPhone 15") | ||
| Text("Three") | ||
| .previewDisplayName("Other") | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice, im assuming this exactly matches the
image_file_namevalue generated for a given snapshot 👏