feat: Uses curl to download toml config to allow for pre-built binaries.
Some checks failed
CI / Run Tests (push) Failing after 2m44s

This commit is contained in:
2024-12-17 19:03:16 -05:00
parent fb246df01a
commit 9b99b35436
4 changed files with 20 additions and 12 deletions

View File

@@ -70,6 +70,7 @@ let package = Package(
name: "ConfigurationClient", name: "ConfigurationClient",
dependencies: [ dependencies: [
"CodersClient", "CodersClient",
"CommandClient",
"FileClient", "FileClient",
.product(name: "Dependencies", package: "swift-dependencies"), .product(name: "Dependencies", package: "swift-dependencies"),
.product(name: "DependenciesMacros", package: "swift-dependencies"), .product(name: "DependenciesMacros", package: "swift-dependencies"),

View File

@@ -1,4 +1,5 @@
import CodersClient import CodersClient
import CommandClient
import Dependencies import Dependencies
import DependenciesMacros import DependenciesMacros
import FileClient import FileClient
@@ -155,6 +156,7 @@ struct LiveConfigurationClient {
private let environment: [String: String] private let environment: [String: String]
@Dependency(\.coders) var coders @Dependency(\.coders) var coders
@Dependency(\.commandClient) var commandClient
@Dependency(\.fileClient) var fileManager @Dependency(\.fileClient) var fileManager
@Dependency(\.logger) var logger @Dependency(\.logger) var logger
@@ -245,19 +247,13 @@ struct LiveConfigurationClient {
try await fileManager.createDirectory(fileDirectory) try await fileManager.createDirectory(fileDirectory)
} }
// TODO: The hpa file needs to be copied somewhere on the system during install and
// not use bundle, as it only works if the tool was built on the users system.
if case .toml = file { if case .toml = file {
// In the case of toml, we copy the internal resource that includes // Copy the file using curl, because when installed as a pre-built binary we
// usage comments in the file. // don't have access to bundled resources.
guard let resourceFile = Bundle.module.url( try await commandClient.run(
forResource: HPAKey.resourceFileName, quiet: true,
withExtension: HPAKey.resourceFileExtension ["curl", HPAKey.tomlConfigUrl, "--output", fileUrl.path]
) else { )
throw ConfigurationError.resourceNotFound
}
try await fileManager.copy(resourceFile, fileUrl)
} else { } else {
// Json does not allow comments, so we write the mock configuration // Json does not allow comments, so we write the mock configuration
// to the file path. // to the file path.

View File

@@ -15,6 +15,7 @@ public enum HPAKey {
public static let resourceFileExtension = "toml" public static let resourceFileExtension = "toml"
public static let defaultFileName = "config.toml" public static let defaultFileName = "config.toml"
public static let defaultFileNameWithoutExtension = "config" public static let defaultFileNameWithoutExtension = "config"
public static let tomlConfigUrl = "https://git.housh.dev/michael/swift-hpa/raw/branch/main/Sources/ConfigurationClient/Resources/hpa.toml"
} }
extension [String: String] { extension [String: String] {

View File

@@ -18,6 +18,8 @@ struct ConfigurationClientTests: TestCase {
@Test(arguments: ["config.toml", "config.json"]) @Test(arguments: ["config.toml", "config.json"])
func generateConfigFile(fileName: String) async throws { func generateConfigFile(fileName: String) async throws {
try await withTestLogger(key: "generateConfigFile") { try await withTestLogger(key: "generateConfigFile") {
$0.asyncShellClient = .liveValue
$0.commandClient = .liveValue
$0.coders = .liveValue $0.coders = .liveValue
$0.fileClient = .liveValue $0.fileClient = .liveValue
} operation: { } operation: {
@@ -42,6 +44,8 @@ struct ConfigurationClientTests: TestCase {
@Test(arguments: ["config.toml", "config.json", nil]) @Test(arguments: ["config.toml", "config.json", nil])
func loadConfigFile(fileName: String?) async throws { func loadConfigFile(fileName: String?) async throws {
try await withTestLogger(key: "generateConfigFile") { try await withTestLogger(key: "generateConfigFile") {
$0.asyncShellClient = .liveValue
$0.commandClient = .liveValue
$0.coders = .liveValue $0.coders = .liveValue
$0.fileClient = .liveValue $0.fileClient = .liveValue
} operation: { } operation: {
@@ -65,6 +69,8 @@ struct ConfigurationClientTests: TestCase {
@Test(arguments: ["config.toml", "config.json", ".hparc.json", ".hparc.toml"]) @Test(arguments: ["config.toml", "config.json", ".hparc.json", ".hparc.toml"])
func findConfiguration(fileName: String) async throws { func findConfiguration(fileName: String) async throws {
try await withTestLogger(key: "findConfiguration") { try await withTestLogger(key: "findConfiguration") {
$0.asyncShellClient = .liveValue
$0.commandClient = .liveValue
$0.fileClient = .liveValue $0.fileClient = .liveValue
} operation: { } operation: {
@Dependency(\.logger) var logger @Dependency(\.logger) var logger
@@ -94,6 +100,8 @@ struct ConfigurationClientTests: TestCase {
@Test(arguments: ["config.toml", "config.json", ".hparc.json", ".hparc.toml"]) @Test(arguments: ["config.toml", "config.json", ".hparc.json", ".hparc.toml"])
func findXdgConfiguration(fileName: String) async throws { func findXdgConfiguration(fileName: String) async throws {
try await withTestLogger(key: "findXdgConfiguration") { try await withTestLogger(key: "findXdgConfiguration") {
$0.asyncShellClient = .liveValue
$0.commandClient = .liveValue
$0.fileClient = .liveValue $0.fileClient = .liveValue
} operation: { } operation: {
@Dependency(\.logger) var logger @Dependency(\.logger) var logger
@@ -133,6 +141,8 @@ struct ConfigurationClientTests: TestCase {
@Test @Test
func writeCreatesBackupFile() async throws { func writeCreatesBackupFile() async throws {
try await withDependencies { try await withDependencies {
$0.asyncShellClient = .liveValue
$0.commandClient = .liveValue
$0.fileClient = .liveValue $0.fileClient = .liveValue
} operation: { } operation: {
let client = ConfigurationClient.liveValue let client = ConfigurationClient.liveValue