Skip to content

Commit c8f1235

Browse files
authored
refactor: dependency inject FileSystemInfo
1 parent 73f8e9d commit c8f1235

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

Sources/PathKit.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,35 @@ public struct Path {
1919
public static let separator = "/"
2020

2121
/// The underlying string representation
22-
internal var path: String
22+
internal let path: String
2323

24-
internal static var fileManager = FileManager.default
24+
internal static let fileManager = FileManager.default
2525

26-
internal var fileSystemInfo: FileSystemInfo = DefaultFileSystemInfo()
26+
internal let fileSystemInfo: FileSystemInfo
2727

2828
// MARK: Init
2929

3030
public init() {
31-
self.path = ""
31+
self.init("")
3232
}
3333

3434
/// Create a Path from a given String
3535
public init(_ path: String) {
36+
self.init(path, fileSystemInfo: DefaultFileSystemInfo())
37+
}
38+
39+
internal init(_ path: String, fileSystemInfo: FileSystemInfo) {
3640
self.path = path
41+
self.fileSystemInfo = fileSystemInfo
3742
}
3843

44+
internal init(fileSystemInfo: FileSystemInfo) {
45+
self.init("", fileSystemInfo: fileSystemInfo)
46+
}
47+
3948
/// Create a Path by joining multiple path components together
4049
public init<S : Collection>(components: S) where S.Iterator.Element == String {
50+
let path: String
4151
if components.isEmpty {
4252
path = "."
4353
} else if components.first == Path.separator && components.count > 1 {
@@ -46,6 +56,7 @@ public struct Path {
4656
} else {
4757
path = components.joined(separator: Path.separator)
4858
}
59+
self.init(path)
4960
}
5061
}
5162

@@ -65,7 +76,7 @@ extension Path : ExpressibleByStringLiteral {
6576
}
6677

6778
public init(stringLiteral value: StringLiteralType) {
68-
self.path = value
79+
self.init(value)
6980
}
7081
}
7182

Tests/PathKitTests/PathKitSpec.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,15 @@ describe("PathKit") {
167167
$0.it("can abbreviate paths on a case sensitive fs") {
168168
let home = Path.home.string
169169
let fakeFSInfo = FakeFSInfo(caseSensitive: true)
170-
var path = Path("\(home.uppercased())")
171-
path.fileSystemInfo = fakeFSInfo
170+
let path = Path("\(home.uppercased())", fileSystemInfo: fakeFSInfo)
172171

173172
try expect(path.abbreviate().string) == home.uppercased()
174173
}
175174

176175
$0.it("can abbreviate paths on a case insensitive fs") {
177176
let home = Path.home.string
178177
let fakeFSInfo = FakeFSInfo(caseSensitive: false)
179-
var path = Path("\(home.uppercased())")
180-
path.fileSystemInfo = fakeFSInfo
178+
let path = Path("\(home.uppercased())", fileSystemInfo: fakeFSInfo)
181179

182180
try expect(path.abbreviate()) == Path("~")
183181
}

0 commit comments

Comments
 (0)