mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-04-24 08:15:21 +00:00
macOS: support reloading temporary config for testing
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
45
macos/Tests/Helpers/TemporaryConfig.swift
Normal file
45
macos/Tests/Helpers/TemporaryConfig.swift
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user