Dont use a global var for mocking

This commit is contained in:
Kovid Goyal
2025-01-05 20:58:33 +05:30
parent 86a6685446
commit 795bf7fb52
3 changed files with 8 additions and 8 deletions

View File

@@ -224,7 +224,7 @@ type KittyOpts struct {
Term, Shell_integration string Term, Shell_integration string
} }
func read_relevant_kitty_opts() KittyOpts { func read_relevant_kitty_opts(override_conf_path ...string) KittyOpts {
ans := KittyOpts{Term: kitty.KittyConfigDefaults.Term, Shell_integration: kitty.KittyConfigDefaults.Shell_integration} ans := KittyOpts{Term: kitty.KittyConfigDefaults.Term, Shell_integration: kitty.KittyConfigDefaults.Shell_integration}
handle_line := func(key, val string) error { handle_line := func(key, val string) error {
switch key { switch key {
@@ -235,7 +235,7 @@ func read_relevant_kitty_opts() KittyOpts {
} }
return nil return nil
} }
config.ReadKittyConfig(handle_line) config.ReadKittyConfig(handle_line, override_conf_path...)
return ans return ans
} }

View File

@@ -9,7 +9,6 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"kitty/tools/config"
"kitty/tools/utils/shlex" "kitty/tools/utils/shlex"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
@@ -67,10 +66,8 @@ func TestParseSSHArgs(t *testing.T) {
func TestRelevantKittyOpts(t *testing.T) { func TestRelevantKittyOpts(t *testing.T) {
tdir := t.TempDir() tdir := t.TempDir()
path := filepath.Join(tdir, "kitty.conf") path := filepath.Join(tdir, "kitty.conf")
config.OverrideEffectiveConfigPath = path
defer func() { config.OverrideEffectiveConfigPath = "" }()
os.WriteFile(path, []byte("term XXX\nshell_integration changed\nterm abcd"), 0o600) os.WriteFile(path, []byte("term XXX\nshell_integration changed\nterm abcd"), 0o600)
rko := read_relevant_kitty_opts() rko := read_relevant_kitty_opts(path)
if rko.Term != "abcd" { if rko.Term != "abcd" {
t.Fatalf("Unexpected TERM: %s", RelevantKittyOpts().Term) t.Fatalf("Unexpected TERM: %s", RelevantKittyOpts().Term)
} }

View File

@@ -403,9 +403,12 @@ func ReloadConfigInKitty(in_parent_only bool) error {
var OverrideEffectiveConfigPath string var OverrideEffectiveConfigPath string
func ReadKittyConfig(line_handler func(key, val string) error) error { func ReadKittyConfig(line_handler func(key, val string) error, override_effective_config_path ...string) error {
kp := os.Getenv("KITTY_PID") kp := os.Getenv("KITTY_PID")
kitty_conf_path := OverrideEffectiveConfigPath kitty_conf_path := ""
if len(override_effective_config_path) > 0 {
kitty_conf_path = override_effective_config_path[0]
}
if _, err := strconv.Atoi(kp); err == nil && kitty_conf_path == "" { if _, err := strconv.Atoi(kp); err == nil && kitty_conf_path == "" {
effective_config_path := filepath.Join(utils.CacheDir(), "effective-config", kp) effective_config_path := filepath.Join(utils.CacheDir(), "effective-config", kp)
if unix.Access(effective_config_path, unix.R_OK) == nil { if unix.Access(effective_config_path, unix.R_OK) == nil {