From bcf85c6e8fca241fa57fbdbbe3b76300329cb3b1 Mon Sep 17 00:00:00 2001 From: Mahmood Tahir Date: Fri, 11 Oct 2024 22:36:29 -0400 Subject: [PATCH] Make record option optional to support environment variable (#13) --- .../HaveValidSnapshot.swift | 22 +++++++++---------- .../Nimble-SnapshotTesting/PrettySyntax.swift | 12 +++++----- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Sources/Nimble-SnapshotTesting/HaveValidSnapshot.swift b/Sources/Nimble-SnapshotTesting/HaveValidSnapshot.swift index 462f707..f96eb67 100644 --- a/Sources/Nimble-SnapshotTesting/HaveValidSnapshot.swift +++ b/Sources/Nimble-SnapshotTesting/HaveValidSnapshot.swift @@ -3,7 +3,7 @@ import Nimble @_exported import SnapshotTesting /// The global recording mode for all snapshot tests -nonisolated(unsafe) public var isRecordingSnapshots: Bool = false +nonisolated(unsafe) public var isRecordingSnapshots: Bool? = nil // we are assuming the risk of modifying it from differen threads /// A counter is used internally for keeping track of unique test cases. Otherwise, we would end up with the library recording @@ -36,7 +36,7 @@ private enum Counter { public func haveValidSnapshot( as strategy: Snapshotting, named name: String? = nil, - record: Bool = false, + record: Bool? = nil, snapshotDirectory: String? = nil, timeout: TimeInterval = 5, file: StaticString = #file, @@ -46,7 +46,7 @@ public func haveValidSnapshot( ) -> Matcher { haveValidSnapshot(as: [strategy], named: name, - record: isRecordingSnapshots || record, + record: isRecordingSnapshots ?? record, snapshotDirectory: snapshotDirectory, timeout: timeout, file: file, @@ -71,7 +71,7 @@ public func haveValidSnapshot( public func haveValidSnapshot( as strategies: [Snapshotting], named name: String? = nil, - record: Bool = false, + record: Bool? = nil, snapshotDirectory: String? = nil, timeout: TimeInterval = 5, file: StaticString = #file, @@ -92,7 +92,7 @@ public func haveValidSnapshot( if let errorMessage = verifySnapshot(of: value, as: strategy, named: name ?? testCaseIdentifier(line: line), - record: isRecordingSnapshots || record, + record: isRecordingSnapshots ?? record, timeout: timeout, file: file, testName: testName, @@ -128,7 +128,7 @@ public extension SyncExpectation { timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.snapshotPollInterval, description: String? = nil) { - if isRecordingSnapshots { + if isRecordingSnapshots == true { to(matcher, description: description) } else { @@ -146,7 +146,7 @@ public extension SyncExpectation { timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.snapshotPollInterval, description: String? = nil) async { - if isRecordingSnapshots { + if isRecordingSnapshots == true { to(matcher, description: description) } else { @@ -181,7 +181,7 @@ public func haveValidSnapshot( ) -> Matcher { haveValidSnapshot(as: [strategy], named: name, - record: isRecordingSnapshots || record, + record: isRecordingSnapshots ?? record, recordDelay: recordDelay, snapshotDirectory: snapshotDirectory, timeout: timeout, @@ -244,10 +244,10 @@ public func haveValidSnapshot( function: String = #function ) -> Matcher { - if isRecordingSnapshots || record { + if (isRecordingSnapshots ?? record) == true { return haveValidSnapshot(as: strategies.map { .wait(for: recordDelay, on: $0) }, named: name, - record: isRecordingSnapshots || record, + record: isRecordingSnapshots ?? record, snapshotDirectory: snapshotDirectory, timeout: timeout, file: file, @@ -258,7 +258,7 @@ public func haveValidSnapshot( else { return haveValidSnapshot(as: strategies, named: name, - record: isRecordingSnapshots || record, + record: isRecordingSnapshots ?? record, snapshotDirectory: snapshotDirectory, timeout: timeout, file: file, diff --git a/Sources/Nimble-SnapshotTesting/PrettySyntax.swift b/Sources/Nimble-SnapshotTesting/PrettySyntax.swift index dd5d4d5..c664eee 100644 --- a/Sources/Nimble-SnapshotTesting/PrettySyntax.swift +++ b/Sources/Nimble-SnapshotTesting/PrettySyntax.swift @@ -7,7 +7,7 @@ import SnapshotTesting public struct Snapshot { let strategies: [Snapshotting] let name: String? - let record: Bool + let record: Bool? let timeout: TimeInterval let file: StaticString let line: UInt @@ -15,7 +15,7 @@ public struct Snapshot { public init(on strategies: [Snapshotting], name: String? = nil, - record: Bool = false, + record: Bool? = nil, timeout: TimeInterval = 5, file: StaticString = #file, line: UInt = #line, @@ -32,14 +32,14 @@ public struct Snapshot { public func snapshot(on strategy: Snapshotting, name: String? = nil, - record: Bool = false, + record: Bool? = nil, timeout: TimeInterval = 5, file: StaticString = #file, line: UInt = #line, function: String = #function) -> Snapshot { snapshot(on: [strategy], name: name, - record: isRecordingSnapshots || record, + record: isRecordingSnapshots ?? record, timeout: timeout, file: file, line: line, @@ -49,14 +49,14 @@ public func snapshot(on strategy: Snapshotting, public func snapshot(on strategies: [Snapshotting], name: String? = nil, - record: Bool = false, + record: Bool? = nil, timeout: TimeInterval = 5, file: StaticString = #file, line: UInt = #line, function: String = #function) -> Snapshot { Snapshot(on: strategies, name: name, - record: isRecordingSnapshots || record, + record: isRecordingSnapshots ?? record, timeout: timeout, file: file, line: line,