Skip to content

Commit

Permalink
Enable using tilde in path when creating folder from FileSystem
Browse files Browse the repository at this point in the history
You can now specify the user’s home directory using a tilde (~), when
creating a folder through `FileSystem.createFolder(at:)`.
  • Loading branch information
JohnSundell committed Mar 11, 2017
1 parent fe49c86 commit ca114ab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Sources/Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ public class FileSystem {
*/
@discardableResult public func createFolder(at path: String) throws -> Folder {
do {
try fileManager.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
let absolutePath = fileManager.absolutePath(for: path)
try fileManager.createDirectory(atPath: absolutePath, withIntermediateDirectories: true, attributes: nil)
return try Folder(path: path, using: fileManager)
} catch {
throw Folder.Error.creatingFolderFailed
Expand Down
20 changes: 17 additions & 3 deletions Tests/FilesTests/FilesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,13 @@ class FilesTests: XCTestCase {

func testReadingFileWithTildePath() {
performTest {
try FileSystem().homeFolder.createFile(named: "file")
let file = try File(path: "~/file")
try FileSystem().homeFolder.createFile(named: ".filestest")
let file = try File(path: "~/.filestest")
try XCTAssertEqual(file.read(), Data())
XCTAssertEqual(file.path, FileSystem().homeFolder.path + "file")
XCTAssertEqual(file.path, FileSystem().homeFolder.path + ".filestest")

// Cleanup since we're performing a test in the actual home folder
try file.delete()
}
}

Expand Down Expand Up @@ -408,6 +411,17 @@ class FilesTests: XCTestCase {
}
}

func testCreatingFolderWithTildePathFromFileSystem() {
performTest {
let fileSystem = FileSystem()
try fileSystem.createFolder(at: "~/.filestest")
let createdFolder = try fileSystem.homeFolder.subfolder(named: ".filestest")

// Cleanup since we're performing a test in the actual home folder
try createdFolder.delete()
}
}

func testCreateFileIfNeeded() {
performTest {
let fileA = try folder.createFileIfNeeded(withName: "file", contents: "Hello".data(using: .utf8)!)
Expand Down

0 comments on commit ca114ab

Please sign in to comment.