Skip to content

Commit

Permalink
Make record option optional to support environment variable (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
tahirmt authored Oct 12, 2024
1 parent 29b2e8d commit bcf85c6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
22 changes: 11 additions & 11 deletions Sources/Nimble-SnapshotTesting/HaveValidSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -36,7 +36,7 @@ private enum Counter {
public func haveValidSnapshot<Value, Format>(
as strategy: Snapshotting<Value, Format>,
named name: String? = nil,
record: Bool = false,
record: Bool? = nil,
snapshotDirectory: String? = nil,
timeout: TimeInterval = 5,
file: StaticString = #file,
Expand All @@ -46,7 +46,7 @@ public func haveValidSnapshot<Value, Format>(
) -> Matcher<Value> {
haveValidSnapshot(as: [strategy],
named: name,
record: isRecordingSnapshots || record,
record: isRecordingSnapshots ?? record,
snapshotDirectory: snapshotDirectory,
timeout: timeout,
file: file,
Expand All @@ -71,7 +71,7 @@ public func haveValidSnapshot<Value, Format>(
public func haveValidSnapshot<Value, Format>(
as strategies: [Snapshotting<Value, Format>],
named name: String? = nil,
record: Bool = false,
record: Bool? = nil,
snapshotDirectory: String? = nil,
timeout: TimeInterval = 5,
file: StaticString = #file,
Expand All @@ -92,7 +92,7 @@ public func haveValidSnapshot<Value, Format>(
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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -181,7 +181,7 @@ public func haveValidSnapshot<Value, Format>(
) -> Matcher<Value> {
haveValidSnapshot(as: [strategy],
named: name,
record: isRecordingSnapshots || record,
record: isRecordingSnapshots ?? record,
recordDelay: recordDelay,
snapshotDirectory: snapshotDirectory,
timeout: timeout,
Expand Down Expand Up @@ -244,10 +244,10 @@ public func haveValidSnapshot<Value, Format>(
function: String = #function
) -> Matcher<Value> {

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,
Expand All @@ -258,7 +258,7 @@ public func haveValidSnapshot<Value, Format>(
else {
return haveValidSnapshot(as: strategies,
named: name,
record: isRecordingSnapshots || record,
record: isRecordingSnapshots ?? record,
snapshotDirectory: snapshotDirectory,
timeout: timeout,
file: file,
Expand Down
12 changes: 6 additions & 6 deletions Sources/Nimble-SnapshotTesting/PrettySyntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import SnapshotTesting
public struct Snapshot<Value, Format> {
let strategies: [Snapshotting<Value, Format>]
let name: String?
let record: Bool
let record: Bool?
let timeout: TimeInterval
let file: StaticString
let line: UInt
let function: String

public init(on strategies: [Snapshotting<Value, Format>],
name: String? = nil,
record: Bool = false,
record: Bool? = nil,
timeout: TimeInterval = 5,
file: StaticString = #file,
line: UInt = #line,
Expand All @@ -32,14 +32,14 @@ public struct Snapshot<Value, Format> {

public func snapshot<Value, Format>(on strategy: Snapshotting<Value, Format>,
name: String? = nil,
record: Bool = false,
record: Bool? = nil,
timeout: TimeInterval = 5,
file: StaticString = #file,
line: UInt = #line,
function: String = #function) -> Snapshot<Value, Format> {
snapshot(on: [strategy],
name: name,
record: isRecordingSnapshots || record,
record: isRecordingSnapshots ?? record,
timeout: timeout,
file: file,
line: line,
Expand All @@ -49,14 +49,14 @@ public func snapshot<Value, Format>(on strategy: Snapshotting<Value, Format>,

public func snapshot<Value, Format>(on strategies: [Snapshotting<Value, Format>],
name: String? = nil,
record: Bool = false,
record: Bool? = nil,
timeout: TimeInterval = 5,
file: StaticString = #file,
line: UInt = #line,
function: String = #function) -> Snapshot<Value, Format> {
Snapshot(on: strategies,
name: name,
record: isRecordingSnapshots || record,
record: isRecordingSnapshots ?? record,
timeout: timeout,
file: file,
line: line,
Expand Down

0 comments on commit bcf85c6

Please sign in to comment.