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

Create exists file/folder should throw error #133

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
14 changes: 14 additions & 0 deletions Sources/Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,9 @@ public extension Folder {
/// - throws: `WriteError` if the operation couldn't be completed.
@discardableResult
func createSubfolder(at path: String) throws -> Folder {
guard !containsSubfolder(at: path) else {
throw FilesError(path: path, reason: LocationErrorReason.alreadyExists)
}
return try storage.createSubfolder(at: path)
}

Expand All @@ -743,6 +746,9 @@ public extension Folder {
/// - throws: `WriteError` if the operation couldn't be completed.
@discardableResult
func createSubfolder(named name: String) throws -> Folder {
guard !containsSubfolder(named: name) else {
throw FilesError(path: path + name.removingPrefix("/"), reason: LocationErrorReason.alreadyExists)
}
return try storage.createSubfolder(at: name)
}

Expand Down Expand Up @@ -801,6 +807,9 @@ public extension Folder {
/// - throws: `WriteError` if the operation couldn't be completed.
@discardableResult
func createFile(at path: String, contents: Data? = nil) throws -> File {
guard !containsFile(at: path) else {
throw FilesError(path: path, reason: LocationErrorReason.alreadyExists)
}
return try storage.createFile(at: path, contents: contents)
}

Expand All @@ -811,6 +820,9 @@ public extension Folder {
/// - throws: `WriteError` if the operation couldn't be completed.
@discardableResult
func createFile(named fileName: String, contents: Data? = nil) throws -> File {
guard !containsFile(named: fileName) else {
throw FilesError(path: path + fileName.removingPrefix("/"), reason: LocationErrorReason.alreadyExists)
}
return try storage.createFile(at: fileName, contents: contents)
}

Expand Down Expand Up @@ -960,6 +972,8 @@ extension FilesError: CustomStringConvertible {
public enum LocationErrorReason {
/// The location couldn't be found.
case missing
/// The location already has file/folder exists.
case alreadyExists
/// An empty path was given when refering to a file.
case emptyFilePath
/// The user attempted to rename the file system's root folder.
Expand Down
14 changes: 10 additions & 4 deletions Tests/FilesTests/FilesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class FilesTests: XCTestCase {
XCTAssertFalse(folder.containsFile(at: path))
try folder.createFile(at: path, contents: Data("Hello".utf8))

// create existing file should throw error
XCTAssertThrowsError(try folder.createFile(at: path))

XCTAssertTrue(folder.containsFile(at: path))
XCTAssertTrue(folder.containsSubfolder(named: "a"))
XCTAssertTrue(folder.containsSubfolder(at: "a/b"))
Expand Down Expand Up @@ -154,6 +157,9 @@ class FilesTests: XCTestCase {
XCTAssertFalse(folder.containsSubfolder(at: path))
try folder.createSubfolder(at: path).createFile(named: "d.txt")

// create existing folder should throw
XCTAssertThrowsError(try folder.createSubfolder(at: path))

XCTAssertTrue(folder.containsSubfolder(at: path))
XCTAssertTrue(folder.containsSubfolder(named: "a"))
XCTAssertTrue(folder.containsSubfolder(at: "a/b"))
Expand Down Expand Up @@ -332,17 +338,17 @@ class FilesTests: XCTestCase {
performTest {
let emptySubfolder = try folder.createSubfolder(named: "1")
XCTAssertTrue(emptySubfolder.isEmpty())

let subfolderWithFile = try folder.createSubfolder(named: "2")
try subfolderWithFile.createFile(named: "A")
XCTAssertFalse(subfolderWithFile.isEmpty())

let subfolderWithHiddenFile = try folder.createSubfolder(named: "3")
try subfolderWithHiddenFile.createFile(named: ".B")
XCTAssertTrue(subfolderWithHiddenFile.isEmpty())
XCTAssertFalse(subfolderWithHiddenFile.isEmpty(includingHidden: true))
let subfolderWithFolder = try folder.createSubfolder(named: "3")

let subfolderWithFolder = try folder.createSubfolder(named: "4")
try subfolderWithFolder.createSubfolder(named: "4")
XCTAssertFalse(subfolderWithFile.isEmpty())
}
Expand Down