Repository navigation
Expand file tree
/
Copy pathFileManagerWrapper.swift
More file actions
106 lines (92 loc) · 4.33 KB
/
Copy pathFileManagerWrapper.swift
File metadata and controls
106 lines (92 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import Foundation
/// FileManagerWrapper benefits several points -
///
/// * FilePath as currency type
/// * Working directory concept per instance for parallaized-testing (FileManager.currentDirectoryPath is managed per "process")
/// * No thread-safe calls are leaked so that AI won't use them by default
/// * Sendable support (though it's unchecked)
/// * Hide use of "ObjCBool" in fileExists
///
/// Don't use FileManager.currentDirectoryPath or FileManager.changeCurrentDirectory in this file.
///
/// Quote on thread-safety in FileManager
/// > The methods of the shared FileManager object can be called from multiple threads safely.
/// > However, if you use a delegate to receive notifications about the status of move, copy, remove,
/// > and link operations, you should create a unique instance of the file manager object,
/// > assign your delegate to that object, and use that file manager to initiate your operations.
/// https://developer.apple.com/documentation/Foundation/FileManager
public struct FileManagerWrapper: @unchecked Sendable {
public let workingDirectory: FilePath
private let fileManager: FileManager
/// workingDirectory parameter needs to be app's very
///
/// - Parameters:
/// - workingDirectory: workingDirectory that can be used as FileManager.default.currentDirectoryPath in parallel testing
/// - fileManager: Different FileManager instance can be injected here in testing
public init(workingDirectory: FilePath, fileManager: FileManager = .default) {
self.workingDirectory = workingDirectory
self.fileManager = fileManager
}
public init(workingDirectory: String, fileManager: FileManager = .default) {
self.init(workingDirectory: FilePath(workingDirectory), fileManager: fileManager)
}
public func fileExists(atPath path: FilePath) -> Bool {
fileManager.fileExists(atPath: normalizePath(path))
}
public func fileExists(atPath path: FilePath, isDirectory: UnsafeMutablePointer<Bool>?) -> Bool {
var objcBool = ObjCBool(isDirectory?.pointee ?? false)
let result = fileManager.fileExists(atPath: normalizePath(path), isDirectory: &objcBool)
isDirectory?.pointee = objcBool.boolValue
return result
}
public func createFile(
atPath path: FilePath,
contents: Data?,
attributes: [FileAttributeKey : Any]? = nil,
) -> Bool {
fileManager.createFile(
atPath: normalizePath(path),
contents: contents,
attributes: attributes
)
}
public func createDirectory(
atPath path: FilePath,
withIntermediateDirectories createIntermediates: Bool,
attributes: [FileAttributeKey : Any]? = nil,
) throws {
try fileManager.createDirectory(
atPath: normalizePath(path),
withIntermediateDirectories: createIntermediates,
attributes: attributes
)
}
public func contents(atPath path: FilePath) -> Data? {
fileManager.contents(atPath: normalizePath(path))
}
public func copyItem(atPath srcPath: FilePath, toPath dstPath: FilePath) throws {
try fileManager.copyItem(atPath: normalizePath(srcPath), toPath: normalizePath(dstPath))
}
public func removeItem(atPath path: FilePath) throws {
try fileManager.removeItem(atPath: normalizePath(path))
}
public func enumerator(atPath path: FilePath) -> FileManager.DirectoryEnumerator? {
fileManager.enumerator(atPath: normalizePath(path))
}
public func contentsOfDirectory(atPath path: FilePath) throws -> [FilePath] {
try fileManager.contentsOfDirectory(atPath: normalizePath(path)).map { FilePath($0) }
}
public func attributesOfItem(atPath path: FilePath) throws -> [FileAttributeKey: Any] {
try fileManager.attributesOfItem(atPath: normalizePath(path))
}
private func normalizePath(_ path: FilePath) -> String {
// expected workingDirectory can be different from FileManager.currentDirectoryPath,
// when running unit tests using file resources in parallel. It has to isolate each other test case
// but currentDirectoryPath is managed per "process".
if fileManager.currentDirectoryPath != workingDirectory.string {
workingDirectory.appending(path.string).string
} else {
path.string
}
}
}