Compare commits

...

7 Commits
v0.1.0 ... main

Author SHA1 Message Date
f8a14e46ed Improved the Build subcommand to support the building of Docker images (#11)
This PR contains the work done to support the building of Docker images in the `Build` subcommand in the executable target. So, for this purpose, the following task have been done:

- added a basic boilerplate of the `docker-compose.yml` file;
- fixed some issues found in the boilerplate of the `Dockerfile` file;
- defined the `Randomable` protocol;
- defined the `Artifact` enumeration;
- updated the `BuildProjectTask` task to support building Docker images if required;
- renamed the `BuildProjectTask` task as `BuildArtifactTask`.

Reviewed-on: #11
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
2025-02-22 00:33:52 +00:00
888d00c1e8 Set executable, package, and IDE tasks in the Makefile (#10)
This PR contains the work done to implement the necessary tasks in the `Makefile` file to manage the executable file as well as the SPM package dependencies. In addition, some tasks related to IDEs have been implemented.

Reviewed-on: #10
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
2025-02-20 22:51:47 +00:00
fce5a23734 Implemented the Open subcommand. (#9)
This PR contains the work done to implement the `Open` subcommand to the `colibri` executable, which open a *Hummingbird* project with either Visual Studio Code or Xcode.

Reviewed-on: #9
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
2025-02-19 23:27:21 +00:00
ced12d509e Implemented the Clean subcommand (#8)
This PR contains the work done to implement the `Update` subcommand that clean the  *Hummingbird* project.

Reviewed-on: #8
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
2025-02-19 01:02:32 +00:00
ab5f589547 Implemented the Update subcommand (#7)
This PR contains the work done to implement the `Update` subcommand that update the package dependencies in a *Hummingbird* project.

Reviewed-on: #7
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
2025-02-19 00:14:00 +00:00
7ee071010d Implemented the Outdated subcommand (#6)
This PR contains the work done to implement the `Outdated` subcommand that check for outdated package dependencies in a *Hummingbird* project.

Reviewed-on: #6
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
2025-02-18 23:50:54 +00:00
6a3b9b5141 Implemented the Create subcommand (#5)
This PR contains the work done to add the *Build* subcommand that build a *Hummingbird* project from the command line to the `Colibri` command.

Reviewed-on: #5
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
2025-02-18 23:03:51 +00:00
35 changed files with 890 additions and 15 deletions

View File

@ -7,7 +7,15 @@ struct Colibri: AsyncParsableCommand {
static let configuration = CommandConfiguration(
abstract: "The utility to manage your Hummingbird apps",
subcommands: [Create.self]
subcommands: [
Build.self,
Clean.self,
Create.self,
Open.self,
Outdated.self,
Update.self
],
defaultSubcommand: Create.self
)
}

View File

@ -0,0 +1,29 @@
import ArgumentParser
import ColibriLibrary
extension Colibri {
struct Build: AsyncParsableCommand {
// MARK: Properties
static let configuration = CommandConfiguration(
commandName: "build-project",
abstract: "Build a Hummingbird app",
helpNames: .shortAndLong,
aliases: ["build"]
)
@OptionGroup var options: Options
// MARK: Functions
mutating func run() async throws {
let terminalService = TerminalService()
let buildArtifact = BuildArtifactTask(terminalService: terminalService)
try await buildArtifact(options.artifact, at: options.locationURL)
}
}
}

View File

@ -0,0 +1,31 @@
import ArgumentParser
import ColibriLibrary
extension Colibri {
struct Clean: AsyncParsableCommand {
// MARK: Properties
static let configuration = CommandConfiguration(
commandName: "clean-project",
abstract: "Clean a Hummingbird app",
helpNames: .shortAndLong,
aliases: ["clean"]
)
@OptionGroup var options: Options
// MARK: Functions
mutating func run() async throws {
let terminalService = TerminalService()
let cleanProject = CleanProjectTask(terminalService: terminalService)
try await cleanProject(at: options.locationURL,
shouldReset: options.reset,
purgeCache: options.purgeCache)
}
}
}

View File

@ -0,0 +1,29 @@
import ArgumentParser
import ColibriLibrary
extension Colibri {
struct Open: AsyncParsableCommand {
// MARK: Properties
static let configuration = CommandConfiguration(
commandName: "open-project",
abstract: "Open a Hummingbird app",
helpNames: .shortAndLong,
aliases: ["open"]
)
@OptionGroup var options: Options
// MARK: Functions
mutating func run() async throws {
let terminalService = TerminalService()
let openProject = OpenProjectTask(terminalService: terminalService)
try await openProject(with: options.ide, at: options.locationURL)
}
}
}

View File

@ -0,0 +1,29 @@
import ArgumentParser
import ColibriLibrary
extension Colibri {
struct Outdated: AsyncParsableCommand {
// MARK: Properties
static let configuration = CommandConfiguration(
commandName: "outdated-dependencies",
abstract: "Check for outdated package dependencies in a Hummingbird app",
helpNames: .shortAndLong,
aliases: ["outdated"]
)
@OptionGroup var options: Options
// MARK: Functions
mutating func run() async throws {
let terminalService = TerminalService()
let updateDependencies = UpdateDependenciesTask(terminalService: terminalService)
try await updateDependencies(at: options.locationURL, checkOutdated: true)
}
}
}

View File

@ -0,0 +1,29 @@
import ArgumentParser
import ColibriLibrary
extension Colibri {
struct Update: AsyncParsableCommand {
// MARK: Properties
static let configuration = CommandConfiguration(
commandName: "update-dependencies",
abstract: "Update package dependencies in a Hummingbird app",
helpNames: .shortAndLong,
aliases: ["update"]
)
@OptionGroup var options: Options
// MARK: Functions
mutating func run() async throws {
let terminalService = TerminalService()
let updateDependencies = UpdateDependenciesTask(terminalService: terminalService)
try await updateDependencies(at: options.locationURL)
}
}
}

View File

@ -0,0 +1,7 @@
import ArgumentParser
import ColibriLibrary
// MARK: - ExpressibleByArgument
extension Artifact: ExpressibleByArgument {}
extension IDE: ExpressibleByArgument {}

View File

@ -0,0 +1,16 @@
import ArgumentParser
import ColibriLibrary
extension Colibri.Build {
struct Options: ParsableArguments, Locationable {
// MARK: Properties
@Option(name: .shortAndLong)
var artifact: Artifact = .executable
@Option(name: .shortAndLong)
var location: String?
}
}

View File

@ -0,0 +1,19 @@
import ArgumentParser
import ColibriLibrary
extension Colibri.Clean {
struct Options: ParsableArguments, Locationable {
// MARK: Properties
@Flag(name: .shortAndLong)
var reset: Bool = false
@Flag(name: .shortAndLong)
var purgeCache: Bool = false
@Option(name: .shortAndLong)
var location: String?
}
}

View File

@ -1,8 +1,8 @@
import ArgumentParser
import Foundation
import ColibriLibrary
extension Colibri.Create {
struct Options: ParsableArguments {
struct Options: ParsableArguments, Locationable {
// MARK: Properties
@ -12,11 +12,5 @@ extension Colibri.Create {
@Option(name: .shortAndLong)
var location: String?
// MARK: Computed
var locationURL: URL? {
location.flatMap { URL(fileURLWithPath: $0) }
}
}
}

View File

@ -0,0 +1,16 @@
import ArgumentParser
import ColibriLibrary
extension Colibri.Open {
struct Options: ParsableArguments, Locationable {
// MARK: Properties
@Option(name: .shortAndLong)
var ide: IDE = .xcode
@Option(name: .shortAndLong)
var location: String?
}
}

View File

@ -0,0 +1,13 @@
import ArgumentParser
import ColibriLibrary
extension Colibri.Outdated {
struct Options: ParsableArguments, Locationable {
// MARK: Properties
@Option(name: .shortAndLong)
var location: String?
}
}

View File

@ -0,0 +1,13 @@
import ArgumentParser
import ColibriLibrary
extension Colibri.Update {
struct Options: ParsableArguments, Locationable {
// MARK: Properties
@Option(name: .shortAndLong)
var location: String?
}
}

View File

@ -0,0 +1,7 @@
services:
app:
build:
context: .
port:
- 3000:8080
command: ["--hostname", "0.0.0.0", "--port", "8080"]

View File

@ -1,7 +1,7 @@
# ================================
# Build image
# ================================
FROM swift:6.0-noble as build
FROM swift:6.0.3-noble AS build
# Install OS updates
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
@ -83,5 +83,5 @@ USER hummingbird:hummingbird
EXPOSE 8080
# Start the Hummingbird service when the image is run, default to listening on 8080 in production environment
ENTRYPOINT ["./App]
ENTRYPOINT ["./App"]
CMD ["--hostname", "0.0.0.0", "--port", "8080"]

View File

@ -2,6 +2,7 @@ enum File: String {
case appArguments = "AppArguments"
case appBuilder = "AppBuilder"
case appOptions = "AppOptions"
case dockerCompose = "DockerCompose"
case dockerFile = "DockerFile"
case dockerIgnore = "DockerIgnore"
case environment = "Environment"
@ -24,6 +25,7 @@ extension File {
case .appArguments: "AppArguments.swift"
case .appBuilder: "AppBuilder.swift"
case .appOptions: "AppOptions.swift"
case .dockerCompose: "docker-compose.yml"
case .dockerFile: "Dockerfile"
case .dockerIgnore: ".dockerignore"
case .environment: "Environment+Properties.swift"

View File

@ -16,7 +16,7 @@ extension URL {
var pathString: String {
if #available(macOS 13.0, *) {
path(percentEncoded: true)
path(percentEncoded: false)
} else {
path
}

View File

@ -0,0 +1,7 @@
protocol Randomable: CaseIterable {
// MARK: Functions
static func random() -> Self
}

View File

@ -0,0 +1,16 @@
public enum Artifact: String {
case executable
case image
}
// MARK: - Randomable
extension Artifact: Randomable {
// MARK: Functions
static func random() -> Artifact {
.allCases.randomElement() ?? .executable
}
}

View File

@ -0,0 +1,16 @@
public enum IDE: String {
case vscode
case xcode
}
// MARK: - Randomable
extension IDE: Randomable {
// MARK: Functions
static func random() -> IDE {
.allCases.randomElement() ?? .xcode
}
}

View File

@ -0,0 +1,21 @@
import Foundation
public protocol Locationable {
// MARK: Properties
var location: String? { get set }
}
// MARK: - Locationable+Properties
public extension Locationable {
// MARK: Properties
var locationURL: URL? {
location.flatMap { URL(fileURLWithPath: $0) }
}
}

View File

@ -0,0 +1,40 @@
import Foundation
public struct BuildArtifactTask {
// MARK: Properties
private let terminalService: TerminalServicing
// MARK: Initialisers
public init(terminalService: TerminalServicing) {
self.terminalService = terminalService
}
// MARK: Functions
public func callAsFunction(_ artifact: Artifact, at location: URL? = nil) async throws (TerminalServiceError) {
let executableURL: URL = switch artifact {
case .executable: .init(at: "/usr/bin/swift")
case .image: .init(at: "/usr/local/bin/docker")
}
var arguments: [String] = switch artifact {
case .executable: ["build"]
case .image: ["compose", "build"]
}
if let location {
if case .executable = artifact {
arguments.append(contentsOf: ["--package-path", location.pathString])
} else if case .image = artifact {
arguments.insert(contentsOf: ["--project-directory", location.pathString], at: 1)
}
}
try await terminalService.run(executableURL, arguments: arguments)
}
}

View 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)
}
}
}

View File

@ -0,0 +1,44 @@
import Foundation
public struct OpenProjectTask {
// MARK: Properties
private let terminalService: TerminalServicing
// MARK: Initialisers
public init(terminalService: TerminalServicing) {
self.terminalService = terminalService
}
// MARK: Functions
public func callAsFunction(with ide: IDE, at location: URL? = nil) async throws (TerminalServiceError) {
let executableURL: URL = switch ide {
case .vscode: .init(at: "/usr/local/bin/code")
case .xcode: .init(at: "/usr/bin/open")
}
let locationPath = switch ide {
case .vscode: location?.pathString ?? "."
case .xcode: location?.appendingPath(.Path.package).pathString ?? .Path.package
}
let arguments: [String] = switch ide {
case .vscode: [locationPath]
case .xcode: ["-a", "Xcode", locationPath]
}
try await terminalService.run(executableURL, arguments: arguments)
}
}
// MARK: - String+Constants
private extension String {
enum Path {
static let package = "Package.swift"
}
}

View File

@ -0,0 +1,33 @@
import Foundation
public struct UpdateDependenciesTask {
// MARK: Properties
private let terminalService: TerminalServicing
// MARK: Initialisers
public init(terminalService: TerminalServicing) {
self.terminalService = terminalService
}
// MARK: Functions
public func callAsFunction(at location: URL? = nil, checkOutdated: Bool = false) async throws (TerminalServiceError) {
let executableURL = URL(at: "/usr/bin/swift")
var arguments: [String] = ["package", "update"]
if let location {
arguments.append(contentsOf: ["--package-path", location.pathString])
}
if checkOutdated {
arguments.append("--dry-run")
}
try await terminalService.run(executableURL, arguments: arguments)
}
}

59
Makefile Normal file
View File

@ -0,0 +1,59 @@
# VARIABLES
BINARY_FOLDER = $(prefix)/bin
BUILD_FOLDER = .build/release
EXECUTABLE_FILE = colibri
# INPUT ARGUMENTS
prefix ?= /usr/local
# EXECUTABLE MANAGEMENT
build: ## Build the executable from source code
@swift build -c release --disable-sandbox
install: build ## Install the built executable into the system
@install -d "$(BINARY_FOLDER)"
@install "$(BUILD_FOLDER)/$(EXECUTABLE_FILE)" "$(BINARY_FOLDER)"
uninstall: ## Uninstall the built executable from the system
@rm -rf "$(BINARY_FOLDER)/$(EXECUTABLE_FILE)"
# PACKAGE MANAGEMENT
clean: ## Delete built SPM artifacts from the package
@swift package clean
outdated: ## List the SPM package dependencies that can be updated
@swift package update --dry-run
purge: ## Purge the global SPM package repository
@swift package purge-cache
reset: ## Reset the complete SPM cache/build folder from the package
@swift package reset
update: ## Update the SPM package dependencies
@swift package update
wipe: clean reset purge ## Wipe all SPM package dependencies and purge the global SPM repository
# OPEN IDEs
vscode: ## Opens this project with Visual Studio Code
@code .
xcode: ## Opens this project with Xcode
@open -a Xcode Package.swift
# HELP
# Output the documentation for each of the defined tasks when `help` is called.
# Reference: https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
.PHONY: help
help: ## Prints the written documentation for all the defined tasks
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
.DEFAULT_GOAL := help

View File

@ -56,6 +56,7 @@ private extension FileTests {
"AppArguments.swift",
"AppBuilder.swift",
"AppOptions.swift",
"docker-compose.yml",
"Dockerfile",
".dockerignore",
"Environment+Properties.swift",
@ -70,6 +71,7 @@ private extension FileTests {
"Library/Sources/Public/AppArguments.swift",
"Library/Sources/Public/AppBuilder.swift",
"App/Sources/AppOptions.swift",
"docker-compose.yml",
"Dockerfile",
".dockerignore",
"Library/Sources/Internal/Environment+Properties.swift",
@ -86,6 +88,7 @@ private extension FileTests {
.app,
.root,
.root,
.root,
.libraryInternal,
.root,
.root,
@ -100,6 +103,7 @@ private extension FileTests {
"Resources/Files/Sources/App",
"Resources/Files/Sources",
"Resources/Files/Sources",
"Resources/Files/Sources",
"Resources/Files/Sources/Library",
"Resources/Files/Sources",
"Resources/Files/Sources",

View File

@ -24,8 +24,8 @@ struct URL_ExtensionsTests {
// MARK: Computed tests
@Test(arguments: zip([URL.someFile, .dotFile, .tildeFile, .someURL],
[String.someFilePath, .dotPath, .tildePath, .empty]))
@Test(arguments: zip([URL.someFile, .dotFile, .tildeFile, .someEncodedFile, .someURL],
[String.someFilePath, .dotPath, .tildePath, .someEncodedPath, .empty]))
func pathString(
with url: URL,
expects path: String
@ -63,6 +63,7 @@ private extension String {
static let dotPath = "."
static let empty = ""
static let tildePath = "~"
static let someEncodedPath = "/sömê/páth/fîlê"
static let someFilePath = "/some/file/path"
}
@ -70,6 +71,7 @@ private extension String {
private extension URL {
static let dotFile = URL(at: .dotPath)
static let someEncodedFile = URL(at: "/sömê/páth/fîlê")
static let someFile = URL(at: .someFilePath)
static let someURL = URL(string: "https://some.url.path")!
static let tildeFile = URL(at: .tildePath)

View File

@ -0,0 +1,32 @@
import Testing
@testable import ColibriLibrary
struct RandomableTest {
@Test func random() {
// GIVEN
let allCases = TestRandomable.allCases
// WHEN
let random = TestRandomable.random()
// THEN
#expect(allCases.contains(random))
}
}
// MARK: - Enumerations
enum TestRandomable: Randomable {
case someCase
case someOtherCase
// MARK: Functions
static func random() -> TestRandomable {
.allCases.randomElement() ?? .someCase
}
}

View File

@ -0,0 +1,63 @@
import Foundation
import Testing
@testable import ColibriLibrary
struct BuildArtifactTaskTests {
@Test(arguments: [nil, URL.someCurrentFolder])
func taskForExecutable(at location: URL?) async throws {
// GIVEN
let terminalService = TerminalServiceSpy()
let task = BuildArtifactTask(terminalService: terminalService)
// WHEN
try await task(.executable, at: location)
// THEN
let executableURL = URL(at: "/usr/bin/swift")
let arguments = if let location {
["build", "--package-path", location.pathString]
} else {
["build"]
}
#expect(terminalService.actions.count == 1)
#expect(terminalService.actions[0] == .ran(executableURL, arguments))
}
@Test(arguments: [nil, URL.someCurrentFolder])
func taskForImage(at location: URL?) async throws {
// GIVEN
let terminalService = TerminalServiceSpy()
let task = BuildArtifactTask(terminalService: terminalService)
// WHEN
try await task(.image, at: location)
// THEN
let executableURL = URL(at: "/usr/local/bin/docker")
let arguments = if let location {
["compose", "--project-directory", location.pathString, "build"]
} else {
["compose", "build"]
}
#expect(terminalService.actions.count == 1)
#expect(terminalService.actions[0] == .ran(executableURL, arguments))
}
@Test(arguments: [nil, URL.someCurrentFolder], [TerminalServiceError.unexpected, .output(""), .captured("")])
func taskForArtifact(at location: URL?, throws error: TerminalServiceError) async throws {
// GIVEN
let terminalService = TerminalServiceMock(action: .error(error))
let task = BuildArtifactTask(terminalService: terminalService)
// WHEN
// THEN
await #expect(throws: error) {
try await task(.random(), at: location)
}
}
}

View 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())
}
}
}

View File

@ -0,0 +1,56 @@
import Foundation
import Testing
@testable import ColibriLibrary
struct OpenProjectTaskTests {
@Test(arguments: [nil, URL.someCurrentFolder])
func taskWithVSCodeIDE(at location: URL?) async throws {
// GIVEN
let terminalService = TerminalServiceSpy()
let task = OpenProjectTask(terminalService: terminalService)
// WHEN
try await task(with: .vscode, at: location)
// THEN
let executableURL = URL(at: "/usr/local/bin/code")
let arguments = [location?.pathString ?? "."]
#expect(terminalService.actions.count == 1)
#expect(terminalService.actions[0] == .ran(executableURL, arguments))
}
@Test(arguments: [nil, URL.someCurrentFolder])
func taskWithXcodeIDE(at location: URL?) async throws {
// GIVEN
let terminalService = TerminalServiceSpy()
let task = OpenProjectTask(terminalService: terminalService)
// WHEN
try await task(with: .xcode, at: location)
// THEN
let locationPath = location?.appendingPath("Package.swift").pathString ?? "Package.swift"
let executableURL = URL(at: "/usr/bin/open")
let arguments = ["-a", "Xcode", locationPath]
#expect(terminalService.actions.count == 1)
#expect(terminalService.actions[0] == .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 = OpenProjectTask(terminalService: terminalService)
// WHEN
// THEN
await #expect(throws: error) {
try await task(with: .random(), at: location)
}
}
}

View File

@ -0,0 +1,47 @@
import Foundation
import Testing
@testable import ColibriLibrary
struct UpdateDependenciesTaskTests {
@Test(arguments: [nil, URL.someCurrentFolder], [false, true])
func task(at location: URL?, checkOutdated: Bool) async throws {
// GIVEN
let terminalService = TerminalServiceSpy()
let task = UpdateDependenciesTask(terminalService: terminalService)
// WHEN
try await task(at: location, checkOutdated: checkOutdated)
// THEN
let executableURL = URL(at: "/usr/bin/swift")
var arguments = if let location {
["package", "update", "--package-path", location.pathString]
} else {
["package", "update"]
}
if checkOutdated {
arguments.append("--dry-run")
}
#expect(terminalService.actions.count == 1)
#expect(terminalService.actions[0] == .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 = UpdateDependenciesTask(terminalService: terminalService)
// WHEN
// THEN
await #expect(throws: error) {
try await task(at: location, checkOutdated: .random())
}
}
}

View File

@ -19,6 +19,14 @@ final class TemplateServiceMock {
self.spy = spy
}
init(
actions: [Action],
spy: TemplateServiceSpy? = nil
) {
self.actions = actions
self.spy = spy
}
}
// MARK: - TemplateServicing

View File

@ -19,6 +19,14 @@ final class TerminalServiceMock {
self.spy = spy
}
init(
actions: [Action],
spy: TerminalServiceSpy? = nil
) {
self.actions = actions
self.spy = spy
}
}
// MARK: - TerminalServicing