Implemented the CreatedRootFolderTask task in the Library target.
This commit is contained in:
parent
3f98d08a00
commit
af1703e6c1
38
Sources/Library/Tasks/CreateRootFolderTask.swift
Normal file
38
Sources/Library/Tasks/CreateRootFolderTask.swift
Normal file
@ -0,0 +1,38 @@
|
||||
import Foundation
|
||||
|
||||
public struct CreateRootFolderTask {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private let fileService: FileServicing
|
||||
|
||||
// MARK: Initialisers
|
||||
|
||||
public init(fileService: FileServicing) {
|
||||
self.fileService = fileService
|
||||
}
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
public func callAsFunction(
|
||||
name: String,
|
||||
at location: URL? = nil
|
||||
) async throws -> URL {
|
||||
let rootFolder = if let location {
|
||||
location
|
||||
} else {
|
||||
await fileService.currentFolder
|
||||
}
|
||||
|
||||
let newFolder = if #available(macOS 13.0, *) {
|
||||
rootFolder.appending(path: name)
|
||||
} else {
|
||||
rootFolder.appendingPathComponent(name)
|
||||
}
|
||||
|
||||
try await fileService.createFolder(at: newFolder)
|
||||
|
||||
return newFolder
|
||||
}
|
||||
|
||||
}
|
@ -1,8 +1,7 @@
|
||||
import ColibriLibrary
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
@testable import ColibriLibrary
|
||||
|
||||
struct FileServiceTests {
|
||||
|
||||
// MARK: Properties
|
||||
@ -153,14 +152,3 @@ struct FileServiceTests {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - URL+Constants
|
||||
|
||||
private extension URL {
|
||||
static let someCurrentFolder = URL(at: "/some/current/folder")
|
||||
static let someExistingFolder = URL(at: "/some/existing/folder")
|
||||
static let someExistingFile = URL(at: "/some/existing/file")
|
||||
static let someNewFolder = URL(at: "/some/new/folder")
|
||||
static let someNewFile = URL(at: "/some/new/file")
|
||||
static let someRandomURL = URL(string: "some.random.url")!
|
||||
}
|
||||
|
64
Tests/Library/Cases/Tasks/CreateRootFolderTaskTests.swift
Normal file
64
Tests/Library/Cases/Tasks/CreateRootFolderTaskTests.swift
Normal file
@ -0,0 +1,64 @@
|
||||
import ColibriLibrary
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
struct CreateRootFolderTaskTests {
|
||||
|
||||
// MARK: Functions tests
|
||||
|
||||
@Test(arguments: [String.someProjectName], [URL.someCurrentProjectFolder, .someNewProjectFolder])
|
||||
func task(
|
||||
name: String,
|
||||
expects folder: URL
|
||||
) async throws {
|
||||
// GIVEN
|
||||
let fileService = FileServiceMock(
|
||||
currentFolder: .someCurrentFolder,
|
||||
action: .createFolder(folder)
|
||||
)
|
||||
|
||||
let task = CreateRootFolderTask(fileService: fileService)
|
||||
|
||||
// WHEN
|
||||
let result = try await task(name: name,
|
||||
at: folder == .someNewProjectFolder ? .someNewFolder : nil)
|
||||
|
||||
// THEN
|
||||
#expect(result == folder)
|
||||
#expect(result.isFileURL == true)
|
||||
}
|
||||
|
||||
@Test(arguments: [String.someProjectName], [FileServiceError.urlAlreadyExists])
|
||||
func task(
|
||||
name: String,
|
||||
throws error: FileServiceError
|
||||
) async throws {
|
||||
// GIVEN
|
||||
let fileService = FileServiceMock(
|
||||
currentFolder: .someCurrentFolder,
|
||||
action: .error(error)
|
||||
)
|
||||
|
||||
let task = CreateRootFolderTask(fileService: fileService)
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: error) {
|
||||
try await task(name: name)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - String+Constants
|
||||
|
||||
private extension String {
|
||||
static let someProjectName = "SomeProjectName"
|
||||
}
|
||||
|
||||
// MARK: - URL+Constants
|
||||
|
||||
private extension URL {
|
||||
static let someCurrentProjectFolder = URL.someCurrentFolder.appending(component: String.someProjectName)
|
||||
static let someNewProjectFolder = URL.someNewFolder.appending(component: String.someProjectName)
|
||||
}
|
16
Tests/Library/Helpers/Extensions/URL+Samples.swift
Normal file
16
Tests/Library/Helpers/Extensions/URL+Samples.swift
Normal file
@ -0,0 +1,16 @@
|
||||
import Foundation
|
||||
|
||||
@testable import ColibriLibrary
|
||||
|
||||
extension URL {
|
||||
|
||||
// MARK: Constants
|
||||
|
||||
static let someCurrentFolder = URL(at: "/some/current/folder")
|
||||
static let someExistingFolder = URL(at: "/some/existing/folder")
|
||||
static let someExistingFile = URL(at: "/some/existing/file")
|
||||
static let someNewFolder = URL(at: "/some/new/folder")
|
||||
static let someNewFile = URL(at: "/some/new/file")
|
||||
static let someRandomURL = URL(string: "some.random.url")!
|
||||
|
||||
}
|
@ -39,9 +39,3 @@ extension FileServiceSpy: FileServicing {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - URL+Constants
|
||||
|
||||
private extension URL {
|
||||
static let someCurrentFolder = URL(at: "some/current/folder")
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user