From 776bb35b8dea6a92044a9d67cd0a9c438f97f4b2 Mon Sep 17 00:00:00 2001 From: John Sundell Date: Wed, 11 Sep 2019 23:14:37 +0200 Subject: [PATCH] =?UTF-8?q?Enable=20referring=20to=20locations=20using=20t?= =?UTF-8?q?he=20current=20folder=E2=80=99s=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change enables files and folders to be referred to using a path that starts with `./`. While this is redundant when using Files, since paths are assumed to start at the current folder, it’s useful when accepting file paths as input, and to better conform to system conventions. --- Sources/Files.swift | 8 +++++++- Tests/FilesTests/FilesTests.swift | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Sources/Files.swift b/Sources/Files.swift index d8e0368..7ecb5a3 100644 --- a/Sources/Files.swift +++ b/Sources/Files.swift @@ -204,6 +204,8 @@ public final class Storage { } private func validatePath() throws { + path = path.removingPrefix("./") + switch LocationType.kind { case .file: guard !path.isEmpty else { @@ -230,6 +232,10 @@ public final class Storage { path.replaceSubrange(.. Folder { - let folderPath = path + folderPath.removingPrefix("/") + let folderPath = path + folderPath.removingPrefix("/").removingPrefix("./") let storage = try Storage(path: folderPath, fileManager: fileManager) return Folder(storage: storage) } diff --git a/Tests/FilesTests/FilesTests.swift b/Tests/FilesTests/FilesTests.swift index 7076461..578204c 100644 --- a/Tests/FilesTests/FilesTests.swift +++ b/Tests/FilesTests/FilesTests.swift @@ -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 { @@ -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),