From 484a0c2c8cfec70a79ecc718412c74f46de3938c Mon Sep 17 00:00:00 2001 From: Mladen Jablanovic Date: Tue, 16 Dec 2025 12:35:52 +0100 Subject: [PATCH 1/2] feat(go): Bubble up directories looking for config #SCD-148 --- clients/go/config.go | 14 +++++-- clients/go/config_test.go | 39 +++++++++++++++---- clients/go/testdata/.phraseapp.yml | 0 .../{ => config_files}/empty/.gitkeep | 0 4 files changed, 43 insertions(+), 10 deletions(-) delete mode 100644 clients/go/testdata/.phraseapp.yml rename clients/go/testdata/{ => config_files}/empty/.gitkeep (100%) diff --git a/clients/go/config.go b/clients/go/config.go index 5c2ffbb08..24ee04ff1 100644 --- a/clients/go/config.go +++ b/clients/go/config.go @@ -107,9 +107,17 @@ func configPath() (string, error) { } for _, configName := range configNames { - possiblePath := filepath.Join(workingDir, configName) - if _, err := os.Stat(possiblePath); err == nil { - return possiblePath, nil + dir := workingDir + // loop up the directory tree + for { + possiblePath := filepath.Join(dir, configName) + if _, err := os.Stat(possiblePath); err == nil { + return possiblePath, nil + } + dir = filepath.Dir(dir) + if dir == "/" { + break + } } } diff --git a/clients/go/config_test.go b/clients/go/config_test.go index a271edbc6..ee6febc6a 100644 --- a/clients/go/config_test.go +++ b/clients/go/config_test.go @@ -219,7 +219,7 @@ func TestConfigPath_ConfigInCWD(t *testing.T) { t.Fatalf("Cannot Getwd, got: %s", err) } - cwd := pwd + "/testdata" + cwd := pwd + "/testdata/config_files" oldDir, _ := os.Getwd() err = os.Chdir(cwd) @@ -232,7 +232,7 @@ func TestConfigPath_ConfigInCWD(t *testing.T) { if err != nil { t.Fatalf("didn't expect an error, got: %s", err) } - expPath := cwd + "/.phraseapp.yml" + expPath := cwd + "/.phrase.yml" if path != expPath { t.Errorf("expected path to be %q, got %q", expPath, path) } @@ -269,7 +269,7 @@ func TestConfigPath_ConfigInHomeDir(t *testing.T) { t.Fatalf("Cannot Getwd, got: %s", err) } - cwd := pwd + "/testdata/empty" + cwd := pwd + "/testdata/empty2" oldDir, _ := os.Getwd() err = os.Chdir(cwd) @@ -278,7 +278,7 @@ func TestConfigPath_ConfigInHomeDir(t *testing.T) { } defer os.Chdir(oldDir) - newHome := pwd + "/testdata" + newHome := pwd + "/testdata/config_files" oldHome := os.Getenv("HOME") os.Setenv("HOME", newHome) defer os.Setenv("HOME", oldHome) @@ -287,7 +287,32 @@ func TestConfigPath_ConfigInHomeDir(t *testing.T) { if err != nil { t.Fatalf("didn't expect an error, got: %s", err) } - expPath := newHome + "/.phraseapp.yml" + expPath := newHome + "/.phrase.yml" + if path != expPath { + t.Errorf("expected path to be %q, got %q", expPath, path) + } +} + +func TestConfigPath_ConfigInParentDir(t *testing.T) { + pwd, err := os.Getwd() + if err != nil { + t.Fatalf("Cannot Getwd, got: %s", err) + } + + cwd := pwd + "/testdata/config_files/empty" + + oldDir, _ := os.Getwd() + err = os.Chdir(cwd) + if err != nil { + t.Fatalf("didn't expect an error changing the working directory, got: %s", err) + } + defer os.Chdir(oldDir) + + path, err := configPath() + if err != nil { + t.Fatalf("didn't expect an error, got: %s", err) + } + expPath := pwd + "/testdata/config_files/.phrase.yml" if path != expPath { t.Errorf("expected path to be %q, got %q", expPath, path) } @@ -302,7 +327,7 @@ func TestConfigPath_NoConfigAvailable(t *testing.T) { t.Fatalf("Cannot Getwd, got: %s", err) } - cwd := pwd + "/testdata/empty" + cwd := pwd + "/testdata/empty2" oldDir, _ := os.Getwd() err = os.Chdir(cwd) @@ -318,7 +343,7 @@ func TestConfigPath_NoConfigAvailable(t *testing.T) { t.Fatalf("Cannot Getwd, got: %s", err) } - os.Setenv("HOME", pwd+"/testdata/empty2") + os.Setenv("HOME", pwd+"/testdata") defer os.Setenv("HOME", oldHome) path, err := configPath() diff --git a/clients/go/testdata/.phraseapp.yml b/clients/go/testdata/.phraseapp.yml deleted file mode 100644 index e69de29bb..000000000 diff --git a/clients/go/testdata/empty/.gitkeep b/clients/go/testdata/config_files/empty/.gitkeep similarity index 100% rename from clients/go/testdata/empty/.gitkeep rename to clients/go/testdata/config_files/empty/.gitkeep From 46f37dd298f686058a244bbec88562069f2b881f Mon Sep 17 00:00:00 2001 From: Mladen Jablanovic Date: Tue, 16 Dec 2025 13:59:10 +0100 Subject: [PATCH 2/2] copilot suggestions --- clients/go/config.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clients/go/config.go b/clients/go/config.go index 24ee04ff1..d11244678 100644 --- a/clients/go/config.go +++ b/clients/go/config.go @@ -114,10 +114,10 @@ func configPath() (string, error) { if _, err := os.Stat(possiblePath); err == nil { return possiblePath, nil } - dir = filepath.Dir(dir) - if dir == "/" { + if filepath.Dir(dir) == dir { // reached the root directory break } + dir = filepath.Dir(dir) } }