Skip to content

Commit

Permalink
Merge branch 'main' into custom-dump
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis authored Jan 25, 2025
2 parents b1bc5c1 + 008509a commit c203f9d
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 28 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,17 @@ jobs:
- uses: actions/checkout@v4
- run: swift build
- run: swift test

android:
strategy:
matrix:
swift:
- "6.0.2"
name: Android
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: skiptools/swift-android-action@v2
with:
swift-version: ${{ matrix.swift }}
copy-files: ${GITHUB_WORKSPACE}/Tests/SnapshotTestingTests/__Snapshots__
7 changes: 2 additions & 5 deletions Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import Foundation
of value: @autoclosure () throws -> Value?,
as snapshotting: Snapshotting<Value, String>,
message: @autoclosure () -> String = "",
record isRecording: Bool? = nil,
record: SnapshotTestingConfiguration.Record? = nil,
timeout: TimeInterval = 5,
syntaxDescriptor: InlineSnapshotSyntaxDescriptor = InlineSnapshotSyntaxDescriptor(),
matches expected: (() -> String)? = nil,
Expand All @@ -47,10 +47,7 @@ import Foundation
line: UInt = #line,
column: UInt = #column
) {
let record =
(isRecording == true ? .all : isRecording == false ? .missing : nil)
?? SnapshotTestingConfiguration.current?.record
?? _record
let record = record ?? SnapshotTestingConfiguration.current?.record ?? _record
withSnapshotTesting(record: record) {
let _: Void = installTestObserver
do {
Expand Down
55 changes: 34 additions & 21 deletions Sources/SnapshotTesting/AssertSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,17 @@ public func verifySnapshot<Value, Format>(
let fileUrl = URL(fileURLWithPath: "\(filePath)", isDirectory: false)
let fileName = fileUrl.deletingPathExtension().lastPathComponent

#if os(Android)
// When running tests on Android, the CI script copies the Tests/SnapshotTestingTests/__Snapshots__ up to the temporary folder
let snapshotsBaseUrl = URL(
fileURLWithPath: "/data/local/tmp/android-xctest", isDirectory: true)
#else
let snapshotsBaseUrl = fileUrl.deletingLastPathComponent()
#endif

let snapshotDirectoryUrl =
snapshotDirectory.map { URL(fileURLWithPath: $0, isDirectory: true) }
?? fileUrl
.deletingLastPathComponent()
.appendingPathComponent("__Snapshots__")
.appendingPathComponent(fileName)
?? snapshotsBaseUrl.appendingPathComponent("__Snapshots__").appendingPathComponent(fileName)

let identifier: String
if let name = name {
Expand All @@ -316,10 +321,12 @@ public func verifySnapshot<Value, Format>(
}

let testName = sanitizePathComponent(testName)
let snapshotFileUrl =
var snapshotFileUrl =
snapshotDirectoryUrl
.appendingPathComponent("\(testName).\(identifier)")
.appendingPathExtension(snapshotting.pathExtension ?? "")
if let ext = snapshotting.pathExtension {
snapshotFileUrl = snapshotFileUrl.appendingPathExtension(ext)
}
let fileManager = FileManager.default
try fileManager.createDirectory(at: snapshotDirectoryUrl, withIntermediateDirectories: true)

Expand Down Expand Up @@ -359,7 +366,7 @@ public func verifySnapshot<Value, Format>(
try snapshotData.write(to: snapshotFileUrl)
}

#if !os(Linux) && !os(Windows)
#if !os(Android) && !os(Linux) && !os(Windows)
if !isSwiftTesting,
ProcessInfo.processInfo.environment.keys.contains("__XCODE_BUILT_PRODUCTS_DIR_PATHS")
{
Expand Down Expand Up @@ -446,7 +453,7 @@ public func verifySnapshot<Value, Format>(
try snapshotting.diffing.toData(diffable).write(to: failedSnapshotFileUrl)

if !attachments.isEmpty {
#if !os(Linux) && !os(Windows)
#if !os(Linux) && !os(Android) && !os(Windows)
if ProcessInfo.processInfo.environment.keys.contains("__XCODE_BUILT_PRODUCTS_DIR_PATHS"),
!isSwiftTesting
{
Expand Down Expand Up @@ -501,7 +508,7 @@ func sanitizePathComponent(_ string: String) -> String {
.replacingOccurrences(of: "^-|-$", with: "", options: .regularExpression)
}

#if !os(Linux) && !os(Windows)
#if !os(Android) && !os(Linux) && !os(Windows)
import CoreServices

func uniformTypeIdentifier(fromExtension pathExtension: String) -> String? {
Expand All @@ -516,27 +523,33 @@ func sanitizePathComponent(_ string: String) -> String {
}
#endif

extension DispatchQueue {
private static let key = DispatchSpecificKey<UInt8>()
private static let value: UInt8 = 0

fileprivate static func mainSync<R>(execute block: () -> R) -> R {
Self.main.setSpecific(key: key, value: value)
if getSpecific(key: key) == value {
return block()
} else {
return main.sync(execute: block)
}
}
}

// We need to clean counter between tests executions in order to support test-iterations.
private class CleanCounterBetweenTestCases: NSObject, XCTestObservation {
private static var registered = false

static func registerIfNeeded() {
if Thread.isMainThread {
doRegisterIfNeeded()
} else {
DispatchQueue.main.sync {
doRegisterIfNeeded()
DispatchQueue.mainSync {
if !registered {
registered = true
XCTestObservationCenter.shared.addTestObserver(CleanCounterBetweenTestCases())
}
}
}

private static func doRegisterIfNeeded() {
if !registered {
registered = true
XCTestObservationCenter.shared.addTestObserver(CleanCounterBetweenTestCases())
}
}

func testCaseDidFinish(_ testCase: XCTestCase) {
counterQueue.sync {
counterMap = [:]
Expand Down
2 changes: 1 addition & 1 deletion Sources/SnapshotTesting/Common/XCTAttachment.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if os(Linux) || os(Windows)
#if os(Linux) || os(Android) || os(Windows)
import Foundation

public struct XCTAttachment {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SnapshotTesting/Internal/Deprecations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public func _verifyInlineSnapshot<Value>(

/// Did not successfully record, so we will fail.
if !attachments.isEmpty {
#if !os(Linux) && !os(Windows)
#if !os(Linux) && !os(Android) && !os(Windows)
if ProcessInfo.processInfo.environment.keys.contains("__XCODE_BUILT_PRODUCTS_DIR_PATHS") {
XCTContext.runActivity(named: "Attached Failure Diff") { activity in
attachments.forEach {
Expand Down
14 changes: 14 additions & 0 deletions Tests/SnapshotTestingTests/AssertSnapshotSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,18 @@
assertSnapshot(of: user, as: .dump)
}
}

@MainActor
@Suite(
.snapshots(
record: .missing
)
)
struct MainActorTests {
@Test func dump() {
struct User { let id: Int, name: String, bio: String }
let user = User(id: 1, name: "Blobby", bio: "Blobbed around the world.")
assertSnapshot(of: user, as: .dump)
}
}
#endif
3 changes: 3 additions & 0 deletions Tests/SnapshotTestingTests/RecordTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ class RecordTests: XCTestCase {
#endif

func testRecordFailed_NoFailure() throws {
#if os(Android)
throw XCTSkip("cannot save next to file on Android")
#endif
try Data("42".utf8).write(to: snapshotURL)
let modifiedDate =
try FileManager.default
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
▿ User
- bio: "Blobbed around the world."
- id: 1
- name: "Blobby"

0 comments on commit c203f9d

Please sign in to comment.