Skip to content

Commit

Permalink
refactor: dependency inject FileSystemInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanPodymov committed Feb 15, 2020
1 parent 73f8e9d commit c8f1235
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
21 changes: 16 additions & 5 deletions Sources/PathKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,35 @@ public struct Path {
public static let separator = "/"

/// The underlying string representation
internal var path: String
internal let path: String

internal static var fileManager = FileManager.default
internal static let fileManager = FileManager.default

internal var fileSystemInfo: FileSystemInfo = DefaultFileSystemInfo()
internal let fileSystemInfo: FileSystemInfo

// MARK: Init

public init() {
self.path = ""
self.init("")
}

/// Create a Path from a given String
public init(_ path: String) {
self.init(path, fileSystemInfo: DefaultFileSystemInfo())
}

internal init(_ path: String, fileSystemInfo: FileSystemInfo) {
self.path = path
self.fileSystemInfo = fileSystemInfo
}

internal init(fileSystemInfo: FileSystemInfo) {
self.init("", fileSystemInfo: fileSystemInfo)
}

/// Create a Path by joining multiple path components together
public init<S : Collection>(components: S) where S.Iterator.Element == String {
let path: String
if components.isEmpty {
path = "."
} else if components.first == Path.separator && components.count > 1 {
Expand All @@ -46,6 +56,7 @@ public struct Path {
} else {
path = components.joined(separator: Path.separator)
}
self.init(path)
}
}

Expand All @@ -65,7 +76,7 @@ extension Path : ExpressibleByStringLiteral {
}

public init(stringLiteral value: StringLiteralType) {
self.path = value
self.init(value)
}
}

Expand Down
6 changes: 2 additions & 4 deletions Tests/PathKitTests/PathKitSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,15 @@ describe("PathKit") {
$0.it("can abbreviate paths on a case sensitive fs") {
let home = Path.home.string
let fakeFSInfo = FakeFSInfo(caseSensitive: true)
var path = Path("\(home.uppercased())")
path.fileSystemInfo = fakeFSInfo
let path = Path("\(home.uppercased())", fileSystemInfo: fakeFSInfo)

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

$0.it("can abbreviate paths on a case insensitive fs") {
let home = Path.home.string
let fakeFSInfo = FakeFSInfo(caseSensitive: false)
var path = Path("\(home.uppercased())")
path.fileSystemInfo = fakeFSInfo
let path = Path("\(home.uppercased())", fileSystemInfo: fakeFSInfo)

try expect(path.abbreviate()) == Path("~")
}
Expand Down

0 comments on commit c8f1235

Please sign in to comment.