Skip to content
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

Swift Testing: Reset file counter for each test #966

Merged
merged 2 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 40 additions & 10 deletions Sources/SnapshotTesting/AssertSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,9 @@ public func verifySnapshot<Value, Format>(
if let name = name {
identifier = sanitizePathComponent(name)
} else {
let counter = counterQueue.sync { () -> Int in
let key = snapshotDirectoryUrl.appendingPathComponent(testName)
counterMap[key, default: 0] += 1
return counterMap[key]!
}
identifier = String(counter)
identifier = String(
counter.next(for: snapshotDirectoryUrl.appendingPathComponent(testName).absoluteString)
)
}

let testName = sanitizePathComponent(testName)
Expand Down Expand Up @@ -504,8 +501,19 @@ public func verifySnapshot<Value, Format>(

// MARK: - Private

private let counterQueue = DispatchQueue(label: "co.pointfree.SnapshotTesting.counter")
private var counterMap: [URL: Int] = [:]
private var counter: File.Counter {
#if canImport(Testing)
if Test.current != nil {
return File.counter
} else {
return _counter
}
#else
return _counter
#endif
}

private let _counter = File.Counter()

func sanitizePathComponent(_ string: String) -> String {
return
Expand Down Expand Up @@ -546,8 +554,30 @@ private class CleanCounterBetweenTestCases: NSObject, XCTestObservation {
}

func testCaseDidFinish(_ testCase: XCTestCase) {
counterQueue.sync {
counterMap = [:]
_counter.reset()
}
}

enum File {
@TaskLocal static var counter = Counter()

final class Counter: @unchecked Sendable {
private var counts: [String: Int] = [:]
private let lock = NSLock()

init() {}

func next(for key: String) -> Int {
lock.lock()
defer { lock.unlock() }
counts[key, default: 0] += 1
return counts[key]!
}

func reset() {
lock.lock()
defer { lock.unlock() }
counts.removeAll()
}
}
}
9 changes: 8 additions & 1 deletion Sources/SnapshotTesting/SnapshotsTestTrait.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
}

extension Trait where Self == _SnapshotsTestTrait {
/// Configure snapshot testing in a suite or test.
public static var snapshots: Self {
snapshots()
}

/// Configure snapshot testing in a suite or test.
///
/// - Parameters:
Expand Down Expand Up @@ -46,7 +51,9 @@
record: configuration.record,
diffTool: configuration.diffTool
) {
try await function()
try await File.$counter.withValue(File.Counter()) {
try await function()
}
}
}
}
Expand Down