macOS: support reloading temporary config for testing

This commit is contained in:
Lukas
2026-03-30 13:47:21 +02:00
parent 5540f5f249
commit 5c5f645b61
3 changed files with 57 additions and 29 deletions

View File

@@ -45,6 +45,10 @@ extension Ghostty {
self.init(config: ghostty_config_clone(config))
}
func clone(config: ghostty_config_t) {
self.config = config
}
deinit {
self.config = nil
}

View File

@@ -1,6 +1,5 @@
import Testing
@testable import Ghostty
@testable import GhosttyKit
import SwiftUI
@Suite
@@ -182,6 +181,14 @@ struct ConfigTests {
#expect(config.loaded == true)
}
@Test func reloadConfig() throws {
let config = try TemporaryConfig("background-opacity = 0.5")
#expect(config.backgroundOpacity == 0.5)
try config.reload("background-opacity = 0.7")
#expect(config.backgroundOpacity == 0.7)
}
@Test func defaultConfigIsLoaded() throws {
let config = try TemporaryConfig("")
#expect(config.optionalAutoUpdateChannel != nil) // release or tip
@@ -214,31 +221,3 @@ struct ConfigTests {
#expect(config.focusFollowsMouse == true)
}
}
/// Create a temporary config file and delete it when this is deallocated
class TemporaryConfig: Ghostty.Config {
let temporaryFile: URL
init(_ configText: String, finalize: Bool = true) throws {
let temporaryFile = FileManager.default.temporaryDirectory
.appendingPathComponent(UUID().uuidString)
.appendingPathExtension("ghostty")
try configText.write(to: temporaryFile, atomically: true, encoding: .utf8)
self.temporaryFile = temporaryFile
super.init(config: Self.loadConfig(at: temporaryFile.path(), finalize: finalize))
}
var optionalAutoUpdateChannel: Ghostty.AutoUpdateChannel? {
guard let config = self.config else { return nil }
var v: UnsafePointer<Int8>?
let key = "auto-update-channel"
guard ghostty_config_get(config, &v, key, UInt(key.lengthOfBytes(using: .utf8))) else { return nil }
guard let ptr = v else { return nil }
let str = String(cString: ptr)
return Ghostty.AutoUpdateChannel(rawValue: str)
}
deinit {
try? FileManager.default.removeItem(at: temporaryFile)
}
}

View File

@@ -0,0 +1,45 @@
import Foundation
@testable import Ghostty
@testable import GhosttyKit
/// Create a temporary config file and delete it when this is deallocated
class TemporaryConfig: Ghostty.Config {
enum Error: Swift.Error {
case failedToLoad
}
let temporaryFile: URL
init(_ configText: String, finalize: Bool = true) throws {
let temporaryFile = FileManager.default.temporaryDirectory
.appendingPathComponent(UUID().uuidString)
.appendingPathExtension("ghostty")
try configText.write(to: temporaryFile, atomically: true, encoding: .utf8)
self.temporaryFile = temporaryFile
super.init(config: Self.loadConfig(at: temporaryFile.path(), finalize: finalize))
}
func reload(_ newConfigText: String?, finalize: Bool = true) throws {
if let newConfigText {
try newConfigText.write(to: temporaryFile, atomically: true, encoding: .utf8)
}
guard let cfg = Self.loadConfig(at: temporaryFile.path(), finalize: finalize) else {
throw Error.failedToLoad
}
clone(config: cfg)
}
var optionalAutoUpdateChannel: Ghostty.AutoUpdateChannel? {
guard let config = self.config else { return nil }
var v: UnsafePointer<Int8>?
let key = "auto-update-channel"
guard ghostty_config_get(config, &v, key, UInt(key.lengthOfBytes(using: .utf8))) else { return nil }
guard let ptr = v else { return nil }
let str = String(cString: ptr)
return Ghostty.AutoUpdateChannel(rawValue: str)
}
deinit {
try? FileManager.default.removeItem(at: temporaryFile)
}
}