Skip to content

Commit 5b712e9

Browse files
authored
feat(go): Bubble up directories looking for config #SCD-148 (#985)
1 parent 944c07d commit 5b712e9

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

clients/go/config.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,17 @@ func configPath() (string, error) {
107107
}
108108

109109
for _, configName := range configNames {
110-
possiblePath := filepath.Join(workingDir, configName)
111-
if _, err := os.Stat(possiblePath); err == nil {
112-
return possiblePath, nil
110+
dir := workingDir
111+
// loop up the directory tree
112+
for {
113+
possiblePath := filepath.Join(dir, configName)
114+
if _, err := os.Stat(possiblePath); err == nil {
115+
return possiblePath, nil
116+
}
117+
if filepath.Dir(dir) == dir { // reached the root directory
118+
break
119+
}
120+
dir = filepath.Dir(dir)
113121
}
114122
}
115123

clients/go/config_test.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func TestConfigPath_ConfigInCWD(t *testing.T) {
219219
t.Fatalf("Cannot Getwd, got: %s", err)
220220
}
221221

222-
cwd := pwd + "/testdata"
222+
cwd := pwd + "/testdata/config_files"
223223

224224
oldDir, _ := os.Getwd()
225225
err = os.Chdir(cwd)
@@ -232,7 +232,7 @@ func TestConfigPath_ConfigInCWD(t *testing.T) {
232232
if err != nil {
233233
t.Fatalf("didn't expect an error, got: %s", err)
234234
}
235-
expPath := cwd + "/.phraseapp.yml"
235+
expPath := cwd + "/.phrase.yml"
236236
if path != expPath {
237237
t.Errorf("expected path to be %q, got %q", expPath, path)
238238
}
@@ -269,7 +269,7 @@ func TestConfigPath_ConfigInHomeDir(t *testing.T) {
269269
t.Fatalf("Cannot Getwd, got: %s", err)
270270
}
271271

272-
cwd := pwd + "/testdata/empty"
272+
cwd := pwd + "/testdata/empty2"
273273

274274
oldDir, _ := os.Getwd()
275275
err = os.Chdir(cwd)
@@ -278,7 +278,7 @@ func TestConfigPath_ConfigInHomeDir(t *testing.T) {
278278
}
279279
defer os.Chdir(oldDir)
280280

281-
newHome := pwd + "/testdata"
281+
newHome := pwd + "/testdata/config_files"
282282
oldHome := os.Getenv("HOME")
283283
os.Setenv("HOME", newHome)
284284
defer os.Setenv("HOME", oldHome)
@@ -287,7 +287,32 @@ func TestConfigPath_ConfigInHomeDir(t *testing.T) {
287287
if err != nil {
288288
t.Fatalf("didn't expect an error, got: %s", err)
289289
}
290-
expPath := newHome + "/.phraseapp.yml"
290+
expPath := newHome + "/.phrase.yml"
291+
if path != expPath {
292+
t.Errorf("expected path to be %q, got %q", expPath, path)
293+
}
294+
}
295+
296+
func TestConfigPath_ConfigInParentDir(t *testing.T) {
297+
pwd, err := os.Getwd()
298+
if err != nil {
299+
t.Fatalf("Cannot Getwd, got: %s", err)
300+
}
301+
302+
cwd := pwd + "/testdata/config_files/empty"
303+
304+
oldDir, _ := os.Getwd()
305+
err = os.Chdir(cwd)
306+
if err != nil {
307+
t.Fatalf("didn't expect an error changing the working directory, got: %s", err)
308+
}
309+
defer os.Chdir(oldDir)
310+
311+
path, err := configPath()
312+
if err != nil {
313+
t.Fatalf("didn't expect an error, got: %s", err)
314+
}
315+
expPath := pwd + "/testdata/config_files/.phrase.yml"
291316
if path != expPath {
292317
t.Errorf("expected path to be %q, got %q", expPath, path)
293318
}
@@ -302,7 +327,7 @@ func TestConfigPath_NoConfigAvailable(t *testing.T) {
302327
t.Fatalf("Cannot Getwd, got: %s", err)
303328
}
304329

305-
cwd := pwd + "/testdata/empty"
330+
cwd := pwd + "/testdata/empty2"
306331

307332
oldDir, _ := os.Getwd()
308333
err = os.Chdir(cwd)
@@ -318,7 +343,7 @@ func TestConfigPath_NoConfigAvailable(t *testing.T) {
318343
t.Fatalf("Cannot Getwd, got: %s", err)
319344
}
320345

321-
os.Setenv("HOME", pwd+"/testdata/empty2")
346+
os.Setenv("HOME", pwd+"/testdata")
322347
defer os.Setenv("HOME", oldHome)
323348

324349
path, err := configPath()

clients/go/testdata/.phraseapp.yml

Whitespace-only changes.
File renamed without changes.

0 commit comments

Comments
 (0)