Skip to content

Commit

Permalink
Merge pull request #246 from klaviyo/ab/update_tca_suggestions
Browse files Browse the repository at this point in the history
remove `unchecked` from `FileClient`
  • Loading branch information
ab1470 authored Dec 19, 2024
2 parents 1a6b25f + 1f1dcf6 commit 4170349
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
30 changes: 18 additions & 12 deletions Sources/KlaviyoCore/Utils/FileUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,34 @@ func write(data: Data, url: URL) throws {
try data.write(to: url, options: .atomic)
}

public struct FileClient: @unchecked Sendable {
public struct FileClient: Sendable {
public init(
write: @escaping (Data, URL) throws -> Void,
fileExists: @escaping (String) -> Bool,
removeItem: @escaping (String) throws -> Void,
libraryDirectory: @escaping () -> URL) {
write: @Sendable @escaping (Data, URL) throws -> Void,
fileExists: @Sendable @escaping (String) -> Bool,
removeItem: @Sendable @escaping (String) throws -> Void,
libraryDirectory: @Sendable @escaping () -> URL) {
self.write = write
self.fileExists = fileExists
self.removeItem = removeItem
self.libraryDirectory = libraryDirectory
}

public var write: (Data, URL) throws -> Void
public var fileExists: (String) -> Bool
public var removeItem: (String) throws -> Void
public var libraryDirectory: () -> URL
public var write: @Sendable (Data, URL) throws -> Void
public var fileExists: @Sendable (String) -> Bool
public var removeItem: @Sendable (String) throws -> Void
public var libraryDirectory: @Sendable () -> URL

public static let production = FileClient(
write: write(data:url:),
fileExists: FileManager.default.fileExists(atPath:),
removeItem: FileManager.default.removeItem(atPath:),
libraryDirectory: { FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first! })
fileExists: { path in
FileManager.default.fileExists(atPath: path)
},
removeItem: { path in
try FileManager.default.removeItem(atPath: path)
},
libraryDirectory: {
FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first!
})
}

/**
Expand Down
12 changes: 3 additions & 9 deletions Tests/KlaviyoCoreTests/ArchivalUtilsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Combine
import XCTest

@MainActor
class ArchivalUtilsTests: XCTestCase {
class ArchivalUtilsTests: XCTestCase, Sendable {
#if swift(>=6)
nonisolated(unsafe) var dataToWrite: Data?
nonisolated(unsafe) var wroteToFile = false
Expand Down Expand Up @@ -86,14 +86,8 @@ class ArchivalUtilsTests: XCTestCase {
}

func testUnarchiveUnableToRemoveFile() throws {
var firstCall = true
environment.fileClient.fileExists = { _ in
if firstCall {
firstCall = false
return true
}
return false
}
environment.fileClient.fileExists = { _ in true }
environment.fileClient.removeItem = { _ in }
let archiveResult = unarchiveFromFile(fileClient: environment.fileClient, fileURL: TEST_URL)

XCTAssertEqual(SAMPLE_DATA, archiveResult)
Expand Down
2 changes: 1 addition & 1 deletion Tests/KlaviyoCoreTests/FileUtilsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import XCTest

@MainActor
class FileUtilsTests: XCTestCase {
class FileUtilsTests: XCTestCase, Sendable {
#if swift(>=6)
nonisolated(unsafe) var dataToWrite: Data?
nonisolated(unsafe) var wroteToFile = false
Expand Down

0 comments on commit 4170349

Please sign in to comment.