From 9c213fc5cf337e0e04af0be5a086149fc7a32589 Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Tue, 5 Feb 2019 07:09:08 -0500 Subject: [PATCH] Child config should have priority over parent config --- config.go | 2 +- config_test.go | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/config.go b/config.go index 351984a2..e48e9ce9 100644 --- a/config.go +++ b/config.go @@ -431,7 +431,7 @@ func findConfigFiles(folder string) (result []string) { } if parent := filepath.Dir(folder); parent != folder { - result = append(result, findConfigFiles(parent)...) + result = append(findConfigFiles(parent), result...) } return diff --git a/config_test.go b/config_test.go index 7f3b6340..7b155c3a 100644 --- a/config_test.go +++ b/config_test.go @@ -58,8 +58,7 @@ func TestCheckVersionRange(t *testing.T) { } func TestSetConfigDefaultValues(t *testing.T) { - tempDir, _ := ioutil.TempDir("", "TestGetConfig") - tempDir, _ = filepath.EvalSymlinks(tempDir) + tempDir, _ := filepath.EvalSymlinks(must(ioutil.TempDir("", "TestGetConfig")).(string)) currentDir, _ := os.Getwd() os.Chdir(tempDir) fmt.Println(tempDir) @@ -116,6 +115,40 @@ func TestSetConfigDefaultValues(t *testing.T) { assert.Nil(t, config.ImageVersion) } +func TestTwoLevelsOfTgfConfig(t *testing.T) { + tempDir, _ := filepath.EvalSymlinks(must(ioutil.TempDir("", "TestGetConfig")).(string)) + subFolder := path.Join(tempDir, "sub-folder") + must(os.Mkdir(subFolder, os.ModePerm)) + tempDir = subFolder + currentDir, _ := os.Getwd() + os.Chdir(tempDir) + fmt.Println(tempDir) + defer func() { + os.Chdir(currentDir) + os.RemoveAll(tempDir) + }() + + testParentTgfConfigFile := fmt.Sprintf("%s/../.tgf.config", tempDir) + testTgfConfigFile := fmt.Sprintf("%s/.tgf.config", tempDir) + testSSMParameterFolder := fmt.Sprintf("/test/tgf-%v", randInt()) + + parentTgfConfig := []byte(String(` + docker-image: coveo/stuff + docker-image-version: 2.0.1 + `).UnIndent().TrimSpace()) + ioutil.WriteFile(testParentTgfConfigFile, parentTgfConfig, 0644) + + // Current directory config overwrites parent directory config + tgfConfig := []byte(String(`docker-image-version: 2.0.2`)) + ioutil.WriteFile(testTgfConfigFile, tgfConfig, 0644) + + config := InitConfig() + config.setDefaultValues(testSSMParameterFolder) + + assert.Equal(t, "coveo/stuff", config.Image) + assert.Equal(t, "2.0.2", *config.ImageVersion) +} + func TestWeirdDirName(t *testing.T) { tempDir, _ := ioutil.TempDir("", "bad@(){}-good-_.1234567890ABC") currentDir, _ := os.Getwd()