Repository: diggerhq/digger
Pull request #481 (2023-08-07 13:23:49)

fix default project name in AutoDetectDiggerConfig

motatoes: please write some tests for this edge case

Patch:
diff --git a/pkg/configuration/digger_config.go b/pkg/configuration/digger_config.go
index b473a967..81abe99e 100644
--- a/pkg/configuration/digger_config.go
+++ b/pkg/configuration/digger_config.go
@@ -147,7 +147,7 @@ func LoadDiggerConfig(workingDir string) (*DiggerConfig, graph.Graph[string, str
 		if err != nil {
 			log.Printf("failed to marshal auto detected digger config: %v", err)
 		} else {
-			log.Printf("Auto detected digger config: \n%v", marshalledConfig)
+			log.Printf("Auto detected digger config: \n%v", string(marshalledConfig))
 		}
 	} else {
 		data, err := os.ReadFile(fileName)
@@ -210,14 +210,13 @@ func AutoDetectDiggerConfig(workingDir string) (*DiggerConfigYaml, error) {
 	}
 
 	terraformDirs, err := terraformDirWalker.GetDirs(workingDir)
-
 	if err != nil {
 		return nil, err
 	}
 
 	moduleDirs, err := moduleDirWalker.GetDirs(workingDir)
 
-	modulePatterns := []string{}
+	var modulePatterns []string
 	for _, dir := range moduleDirs {
 		modulePatterns = append(modulePatterns, dir+"/**")
 	}
@@ -228,13 +227,21 @@ func AutoDetectDiggerConfig(workingDir string) (*DiggerConfigYaml, error) {
 	if len(terragruntDirs) > 0 {
 		// TODO: add support for dependency graph when parsing terragrunt config
 		for _, dir := range terragruntDirs {
-			project := ProjectYaml{Name: dir, Dir: dir, Workflow: defaultWorkflowName, Workspace: "default", Terragrunt: true, IncludePatterns: modulePatterns}
+			projectName := dir
+			if dir == "./" {
+				projectName = "default"
+			}
+			project := ProjectYaml{Name: projectName, Dir: dir, Workflow: defaultWorkflowName, Workspace: "default", Terragrunt: true, IncludePatterns: modulePatterns}
 			configYaml.Projects = append(configYaml.Projects, &project)
 		}
 		return configYaml, nil
 	} else if len(terraformDirs) > 0 {
 		for _, dir := range terraformDirs {
-			project := ProjectYaml{Name: dir, Dir: dir, Workflow: defaultWorkflowName, Workspace: "default", Terragrunt: false, IncludePatterns: modulePatterns}
+			projectName := dir
+			if dir == "./" {
+				projectName = "default"
+			}
+			project := ProjectYaml{Name: projectName, Dir: dir, Workflow: defaultWorkflowName, Workspace: "default", Terragrunt: false, IncludePatterns: modulePatterns}
 			configYaml.Projects = append(configYaml.Projects, &project)
 		}
 		return configYaml, nil
diff --git a/pkg/digger/digger.go b/pkg/digger/digger.go
index ebf88ebd..a25b782c 100644
--- a/pkg/digger/digger.go
+++ b/pkg/digger/digger.go
@@ -482,6 +482,7 @@ func SortedCommandsByDependency(project []models.ProjectCommand, dependencyGraph
 		return s < s2
 	})
 	if err != nil {
+		fmt.Printf("dependencyGraph: %v", dependencyGraph)
 		log.Fatalf("failed to sort commands by dependency, %v", err)
 	}
 	for _, node := range sortedGraph {


Test patch:
diff --git a/pkg/configuration/digger_config_test.go b/pkg/configuration/digger_config_test.go
index 133470e4..bcc78230 100644
--- a/pkg/configuration/digger_config_test.go
+++ b/pkg/configuration/digger_config_test.go
@@ -58,6 +58,37 @@ projects:
 	assert.Equal(t, "path/to/module/test", dg.GetDirectory("prod"))
 }
 
+func TestNoDiggerYaml(t *testing.T) {
+	tempDir, teardown := setUp()
+	defer teardown()
+
+	terraformFile := ""
+	deleteFile := createFile(path.Join(tempDir, "main.tf"), terraformFile)
+	defer deleteFile()
+
+	os.Chdir(tempDir)
+	dg, _, err := LoadDiggerConfig("./")
+
+	assert.NoError(t, err, "expected error to be nil")
+	assert.NotNil(t, dg, "expected digger config to be not nil")
+	assert.Equal(t, 1, len(dg.Projects))
+	assert.Equal(t, false, dg.AutoMerge)
+	assert.Equal(t, true, dg.CollectUsageData)
+	assert.Equal(t, 1, len(dg.Workflows))
+	assert.Equal(t, "default", dg.Projects[0].Name)
+	assert.Equal(t, "./", dg.Projects[0].Dir)
+
+	workflow := dg.Workflows["default"]
+	assert.NotNil(t, workflow, "expected workflow to be not nil")
+	assert.NotNil(t, workflow.Plan)
+	assert.NotNil(t, workflow.Plan.Steps)
+
+	assert.NotNil(t, workflow.Apply)
+	assert.NotNil(t, workflow.Apply.Steps)
+	assert.NotNil(t, workflow.EnvVars)
+	assert.NotNil(t, workflow.Configuration)
+}
+
 func TestDefaultDiggerConfig(t *testing.T) {
 	tempDir, teardown := setUp()
 	defer teardown()