Implemented the CleanProjectTask task in the library target.
This commit is contained in:
parent
ab5f589547
commit
76af32225d
49
Library/Sources/Public/Tasks/CleanProjectTask.swift
Normal file
49
Library/Sources/Public/Tasks/CleanProjectTask.swift
Normal file
@ -0,0 +1,49 @@
|
||||
import Foundation
|
||||
|
||||
public struct CleanProjectTask {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private let terminalService: TerminalServicing
|
||||
|
||||
// MARK: Initialisers
|
||||
|
||||
public init(terminalService: TerminalServicing) {
|
||||
self.terminalService = terminalService
|
||||
}
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
public func callAsFunction(
|
||||
at location: URL? = nil,
|
||||
shouldReset: Bool = false,
|
||||
purgeCache: Bool = false
|
||||
) async throws (TerminalServiceError) {
|
||||
let executableURL = URL(at: "/usr/bin/swift")
|
||||
|
||||
var arguments: [String] = ["package"]
|
||||
|
||||
if let location {
|
||||
arguments.append(contentsOf: ["--package-path", location.pathString])
|
||||
}
|
||||
|
||||
arguments.insert("clean", at: 1)
|
||||
|
||||
try await terminalService.run(executableURL, arguments: arguments)
|
||||
|
||||
if shouldReset {
|
||||
arguments.remove(at: 1)
|
||||
arguments.insert("reset", at: 1)
|
||||
|
||||
try await terminalService.run(executableURL, arguments: arguments)
|
||||
}
|
||||
|
||||
if purgeCache {
|
||||
arguments.remove(at: 1)
|
||||
arguments.insert("purge-cache", at: 1)
|
||||
|
||||
try await terminalService.run(executableURL, arguments: arguments)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
128
Test/Sources/Cases/Public/Tasks/CleanProjectTaskTests.swift
Normal file
128
Test/Sources/Cases/Public/Tasks/CleanProjectTaskTests.swift
Normal file
@ -0,0 +1,128 @@
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
@testable import ColibriLibrary
|
||||
|
||||
struct CleanProjectTaskTests {
|
||||
|
||||
@Test(arguments: [nil, URL.someCurrentFolder])
|
||||
func task(at location: URL?) async throws {
|
||||
// GIVEN
|
||||
let terminalService = TerminalServiceSpy()
|
||||
let task = CleanProjectTask(terminalService: terminalService)
|
||||
|
||||
// WHEN
|
||||
try await task(at: location)
|
||||
|
||||
// THEN
|
||||
let executableURL = URL(at: "/usr/bin/swift")
|
||||
let arguments = if let location {
|
||||
["package", "clean", "--package-path", location.pathString]
|
||||
} else {
|
||||
["package", "clean"]
|
||||
}
|
||||
|
||||
#expect(terminalService.actions.count == 1)
|
||||
#expect(terminalService.actions[0] == .ran(executableURL, arguments))
|
||||
}
|
||||
|
||||
@Test(arguments: [nil, URL.someCurrentFolder])
|
||||
func taskWithReset(at location: URL?) async throws {
|
||||
// GIVEN
|
||||
let terminalService = TerminalServiceSpy()
|
||||
let task = CleanProjectTask(terminalService: terminalService)
|
||||
|
||||
// WHEN
|
||||
try await task(at: location, shouldReset: true)
|
||||
|
||||
// THEN
|
||||
let executableURL = URL(at: "/usr/bin/swift")
|
||||
|
||||
var arguments = if let location {
|
||||
["package", "clean", "--package-path", location.pathString]
|
||||
} else {
|
||||
["package", "clean"]
|
||||
}
|
||||
|
||||
#expect(terminalService.actions.count == 2)
|
||||
#expect(terminalService.actions[0] == .ran(executableURL, arguments))
|
||||
|
||||
arguments.remove(at: 1)
|
||||
arguments.insert("reset", at: 1)
|
||||
|
||||
#expect(terminalService.actions[1] == .ran(executableURL, arguments))
|
||||
}
|
||||
|
||||
@Test(arguments: [nil, URL.someCurrentFolder])
|
||||
func taskWithPurgeCache(at location: URL?) async throws {
|
||||
// GIVEN
|
||||
let terminalService = TerminalServiceSpy()
|
||||
let task = CleanProjectTask(terminalService: terminalService)
|
||||
|
||||
// WHEN
|
||||
try await task(at: location, purgeCache: true)
|
||||
|
||||
// THEN
|
||||
let executableURL = URL(at: "/usr/bin/swift")
|
||||
|
||||
var arguments = if let location {
|
||||
["package", "clean", "--package-path", location.pathString]
|
||||
} else {
|
||||
["package", "clean"]
|
||||
}
|
||||
|
||||
#expect(terminalService.actions.count == 2)
|
||||
#expect(terminalService.actions[0] == .ran(executableURL, arguments))
|
||||
|
||||
arguments.remove(at: 1)
|
||||
arguments.insert("purge-cache", at: 1)
|
||||
|
||||
#expect(terminalService.actions[1] == .ran(executableURL, arguments))
|
||||
}
|
||||
|
||||
@Test(arguments: [nil, URL.someCurrentFolder])
|
||||
func taskWithResetAndPurgeCache(at location: URL?) async throws {
|
||||
// GIVEN
|
||||
let terminalService = TerminalServiceSpy()
|
||||
let task = CleanProjectTask(terminalService: terminalService)
|
||||
|
||||
// WHEN
|
||||
try await task(at: location, shouldReset: true, purgeCache: true)
|
||||
|
||||
// THEN
|
||||
let executableURL = URL(at: "/usr/bin/swift")
|
||||
|
||||
var arguments = if let location {
|
||||
["package", "clean", "--package-path", location.pathString]
|
||||
} else {
|
||||
["package", "clean"]
|
||||
}
|
||||
|
||||
#expect(terminalService.actions.count == 3)
|
||||
#expect(terminalService.actions[0] == .ran(executableURL, arguments))
|
||||
|
||||
arguments.remove(at: 1)
|
||||
arguments.insert("reset", at: 1)
|
||||
|
||||
#expect(terminalService.actions[1] == .ran(executableURL, arguments))
|
||||
|
||||
arguments.remove(at: 1)
|
||||
arguments.insert("purge-cache", at: 1)
|
||||
|
||||
#expect(terminalService.actions[2] == .ran(executableURL, arguments))
|
||||
}
|
||||
|
||||
@Test(arguments: [nil, URL.someCurrentFolder], [TerminalServiceError.unexpected, .output(""), .captured("")])
|
||||
func task(at location: URL?, throws error: TerminalServiceError) async throws {
|
||||
// GIVEN
|
||||
let terminalService = TerminalServiceMock(action: .error(error))
|
||||
let task = CleanProjectTask(terminalService: terminalService)
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: error) {
|
||||
try await task(at: location, shouldReset: .random(), purgeCache: .random())
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user