Repository: diggerhq/digger
Pull request #70 (2023-03-28 01:07:04)

Return full path & refactor file creation in tests
### Changelog

#### Fix
* Return full path to config instead of just the name

#### Refactor
* Added `createFile` function to create & delete easily while testing


Patch:
diff --git a/pkg/digger/digger_config.go b/pkg/digger/digger_config.go
index 452bdb25..37bb9975 100644
--- a/pkg/digger/digger_config.go
+++ b/pkg/digger/digger_config.go
@@ -153,10 +153,10 @@ func retrieveConfigFile(workingDir string) (string, error) {
 	// At this point we know there are no duplicates
 	// Return the first one that exists
 	if ymlCfg {
-		return "digger.yml", nil
+		return path.Join(workingDir, "digger.yml"), nil
 	}
 	if yamlCfg {
-		return "digger.yaml", nil
+		return path.Join(workingDir, "digger.yaml"), nil
 	}
 
 	// Passing this point means digger config file is


Test patch:
diff --git a/pkg/digger/digger_config_test.go b/pkg/digger/digger_config_test.go
index 44581b8d..b5e36ccf 100644
--- a/pkg/digger/digger_config_test.go
+++ b/pkg/digger/digger_config_test.go
@@ -47,57 +47,55 @@ func TestDiggerConfigWhenOnlyYamlExists(t *testing.T) {
 	tempDir, teardown := setUp()
 	defer teardown()
 
-	_, err := os.Create(path.Join(tempDir, "digger.yaml"))
-	if err != nil {
-		t.Fatal(err)
-	}
+	diggerCfg := `
+projects:
+- name: prod
+  branch: /main/
+  dir: path/to/module/test
+  workspace: default
+`
+	deleteFile := createFile(path.Join(tempDir, "digger.yaml"), diggerCfg)
+	defer deleteFile()
 
 	dg, err := NewDiggerConfig(tempDir)
 	assert.NoError(t, err, "expected error to be nil")
 	assert.NotNil(t, dg, "expected digger config to be not nil")
+	assert.Equal(t, "path/to/module/test", dg.GetDirectory("prod"))
 }
 
 func TestDiggerConfigWhenOnlyYmlExists(t *testing.T) {
 	tempDir, teardown := setUp()
 	defer teardown()
 
-	_, err := os.Create(path.Join(tempDir, "digger.yml"))
-	if err != nil {
-		t.Fatal(err)
-	}
+	diggerCfg := `
+projects:
+- name: dev
+  branch: /main/
+  dir: path/to/module
+  workspace: default
+`
+	deleteFile := createFile(path.Join(tempDir, "digger.yml"), diggerCfg)
+	defer deleteFile()
 
 	dg, err := NewDiggerConfig(tempDir)
 	assert.NoError(t, err, "expected error to be nil")
 	assert.NotNil(t, dg, "expected digger config to be not nil")
+	assert.Equal(t, "path/to/module", dg.GetDirectory("dev"))
 }
 
 func TestDefaultValuesForWorkflowConfiguration(t *testing.T) {
 	tempDir, teardown := setUp()
 	defer teardown()
 
-	f, err := os.Create(path.Join(tempDir, "digger.yaml"))
-	if err != nil {
-		log.Fatal(err)
-	}
-
-	defer func(f *os.File) {
-		err := f.Close()
-		if err != nil {
-
-		}
-	}(f)
-
-	digger_yml := `
+	diggerCfg := `
 projects:
 - name: dev
   branch: /main/
   dir: .
   workspace: default
 `
-	_, err2 := f.WriteString(digger_yml)
-	if err2 != nil {
-		log.Fatal(err2)
-	}
+	deleteFile := createFile(path.Join(tempDir, "digger.yaml"), diggerCfg)
+	defer deleteFile()
 
 	dg, err := NewDiggerConfig(tempDir)
 	assert.NoError(t, err, "expected error to be nil")
@@ -120,3 +118,22 @@ func deleteTempDir(name string) {
 		log.Fatal(err)
 	}
 }
+
+func createFile(filepath string, content string) func() {
+	f, err := os.Create(filepath)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	_, err = f.WriteString(content)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	return func() {
+		err := f.Close()
+		if err != nil {
+			log.Fatal(err)
+		}
+	}
+}