Improved the CreatedRootFolderTask task in the library target to throw error in case the function receives an empty name.
This commit is contained in:
parent
db1df0ec62
commit
6bf9c30ad1
@ -18,6 +18,10 @@ public struct CreateRootFolderTask {
|
|||||||
name: String,
|
name: String,
|
||||||
at location: URL? = nil
|
at location: URL? = nil
|
||||||
) async throws -> URL {
|
) async throws -> URL {
|
||||||
|
guard !name.isEmpty else {
|
||||||
|
throw CreateRootFolderError.nameIsEmpty
|
||||||
|
}
|
||||||
|
|
||||||
let rootFolder = if let location {
|
let rootFolder = if let location {
|
||||||
location
|
location
|
||||||
} else {
|
} else {
|
||||||
@ -36,3 +40,9 @@ public struct CreateRootFolderTask {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - Errors
|
||||||
|
|
||||||
|
public enum CreateRootFolderError: Error {
|
||||||
|
case nameIsEmpty
|
||||||
|
}
|
||||||
|
@ -6,12 +6,19 @@ struct CreateRootFolderTaskTests {
|
|||||||
|
|
||||||
// MARK: Functions tests
|
// MARK: Functions tests
|
||||||
|
|
||||||
@Test(arguments: [String.someProjectName], [URL.someCurrentProjectFolder, .someNewProjectFolder])
|
@Test(arguments: [String.someProjectName], [URL.someCurrentProjectFolder, .someNewProjectFolder, .someDotProjectFolder, .someTildeProjectFolder])
|
||||||
func task(
|
func task(
|
||||||
name: String,
|
name: String,
|
||||||
expects folder: URL
|
expects folder: URL
|
||||||
) async throws {
|
) async throws {
|
||||||
// GIVEN
|
// GIVEN
|
||||||
|
let location: URL? = switch folder {
|
||||||
|
case .someNewProjectFolder: .someNewFolder
|
||||||
|
case .someDotProjectFolder: .someDotFolder
|
||||||
|
case .someTildeProjectFolder: .someTildeFolder
|
||||||
|
default: nil
|
||||||
|
}
|
||||||
|
|
||||||
let fileService = FileServiceMock(
|
let fileService = FileServiceMock(
|
||||||
currentFolder: .someCurrentFolder,
|
currentFolder: .someCurrentFolder,
|
||||||
action: .createFolder(folder)
|
action: .createFolder(folder)
|
||||||
@ -21,7 +28,7 @@ struct CreateRootFolderTaskTests {
|
|||||||
|
|
||||||
// WHEN
|
// WHEN
|
||||||
let result = try await task(name: name,
|
let result = try await task(name: name,
|
||||||
at: folder == .someNewProjectFolder ? .someNewFolder : nil)
|
at: location)
|
||||||
|
|
||||||
// THEN
|
// THEN
|
||||||
#expect(result == folder)
|
#expect(result == folder)
|
||||||
@ -47,12 +54,30 @@ struct CreateRootFolderTaskTests {
|
|||||||
try await task(name: name)
|
try await task(name: name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(arguments: [String.someEmptyName], [CreateRootFolderError.nameIsEmpty])
|
||||||
|
func task(
|
||||||
|
name: String,
|
||||||
|
throws error: CreateRootFolderError
|
||||||
|
) async throws {
|
||||||
|
// GIVEN
|
||||||
|
let fileService = FileServiceMock(currentFolder: .someCurrentFolder)
|
||||||
|
|
||||||
|
let task = CreateRootFolderTask(fileService: fileService)
|
||||||
|
|
||||||
|
// WHEN
|
||||||
|
// THEN
|
||||||
|
await #expect(throws: error) {
|
||||||
|
try await task(name: name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - String+Constants
|
// MARK: - String+Constants
|
||||||
|
|
||||||
private extension String {
|
private extension String {
|
||||||
|
static let someEmptyName = ""
|
||||||
static let someProjectName = "SomeProjectName"
|
static let someProjectName = "SomeProjectName"
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,5 +85,7 @@ private extension String {
|
|||||||
|
|
||||||
private extension URL {
|
private extension URL {
|
||||||
static let someCurrentProjectFolder = URL.someCurrentFolder.appending(component: String.someProjectName)
|
static let someCurrentProjectFolder = URL.someCurrentFolder.appending(component: String.someProjectName)
|
||||||
|
static let someDotProjectFolder = URL.someDotFolder.appending(component: String.someProjectName)
|
||||||
static let someNewProjectFolder = URL.someNewFolder.appending(component: String.someProjectName)
|
static let someNewProjectFolder = URL.someNewFolder.appending(component: String.someProjectName)
|
||||||
|
static let someTildeProjectFolder = URL.someTildeFolder.appending(component: String.someProjectName)
|
||||||
}
|
}
|
||||||
|
@ -7,10 +7,12 @@ extension URL {
|
|||||||
// MARK: Constants
|
// MARK: Constants
|
||||||
|
|
||||||
static let someCurrentFolder = URL(at: "/some/current/folder")
|
static let someCurrentFolder = URL(at: "/some/current/folder")
|
||||||
|
static let someDotFolder = URL(at: ".")
|
||||||
static let someExistingFolder = URL(at: "/some/existing/folder")
|
static let someExistingFolder = URL(at: "/some/existing/folder")
|
||||||
static let someExistingFile = URL(at: "/some/existing/file")
|
static let someExistingFile = URL(at: "/some/existing/file")
|
||||||
static let someNewFolder = URL(at: "/some/new/folder")
|
static let someNewFolder = URL(at: "/some/new/folder")
|
||||||
static let someNewFile = URL(at: "/some/new/file")
|
static let someNewFile = URL(at: "/some/new/file")
|
||||||
static let someRandomURL = URL(string: "http://some.random.url")!
|
static let someRandomURL = URL(string: "http://some.random.url")!
|
||||||
|
static let someTildeFolder = URL(at: "~")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user