Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable referring to locations using the current folder’s path #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Sources/Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ public final class Storage<LocationType: Location> {
}

private func validatePath() throws {
path = path.removingPrefix("./")

switch LocationType.kind {
case .file:
guard !path.isEmpty else {
Expand All @@ -230,6 +232,10 @@ public final class Storage<LocationType: Location> {
path.replaceSubrange(..<parentReferenceRange.upperBound, with: parentPath)
}

if !path.hasPrefix("/") {
path = fileManager.currentDirectoryPath.appendingSuffixIfNeeded("/") + path
}

guard fileManager.locationExists(at: path, kind: LocationType.kind) else {
throw LocationError(path: path, reason: .missing)
}
Expand Down Expand Up @@ -292,7 +298,7 @@ private extension Storage where LocationType == Folder {
}

func subfolder(at folderPath: String) throws -> Folder {
let folderPath = path + folderPath.removingPrefix("/")
let folderPath = path + folderPath.removingPrefix("/").removingPrefix("./")
let storage = try Storage(path: folderPath, fileManager: fileManager)
return Folder(storage: storage)
}
Expand Down
29 changes: 29 additions & 0 deletions Tests/FilesTests/FilesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,32 @@ class FilesTests: XCTestCase {
XCTAssertEqual(Folder.current, folder)
}
}

func testAccessingCurrentFolderWithRelativePath() {
performTest {
let folderA = try Folder(path: "./")
let folderB = try Folder.current.subfolder(at: "./")
XCTAssertEqual(folderA, folderB)
XCTAssertEqual(folderA, .current)
}
}

func testAccessingFileInCurrentFolderWithRelativePath() {
performTest {
let fileA = try Folder.current.createFile(named: "Test")
let fileB = try File(path: "./Test")
XCTAssertEqual(fileA, fileB)
}
}

func testAccessingParentFolderWithRelativePath() {
performTest {
let folderA = try Folder(path: "../")
let folderB = try Folder.current.subfolder(at: "../")
XCTAssertEqual(folderA, folderB)
XCTAssertEqual(folderA, Folder.current.parent)
}
}

func testNameExcludingExtensionWithLongFileName() {
performTest {
Expand Down Expand Up @@ -865,6 +891,9 @@ class FilesTests: XCTestCase {
("testMovingFolderHiddenContents", testMovingFolderHiddenContents),
("testAccessingHomeFolder", testAccessingHomeFolder),
("testAccessingCurrentWorkingDirectory", testAccessingCurrentWorkingDirectory),
("testAccessingCurrentFolderWithRelativePath", testAccessingCurrentFolderWithRelativePath),
("testAccessingFileInCurrentFolderWithRelativePath", testAccessingFileInCurrentFolderWithRelativePath),
("testAccessingParentFolderWithRelativePath", testAccessingParentFolderWithRelativePath),
("testNameExcludingExtensionWithLongFileName", testNameExcludingExtensionWithLongFileName),
("testRelativePaths", testRelativePaths),
("testRelativePathIsAbsolutePathForNonParent", testRelativePathIsAbsolutePathForNonParent),
Expand Down