diff --git a/bundle/config/filename.go b/bundle/config/filename.go new file mode 100644 index 00000000..11af34d9 --- /dev/null +++ b/bundle/config/filename.go @@ -0,0 +1,43 @@ +package config + +import ( + "fmt" + "os" + "path/filepath" +) + +type ConfigFileNames []string + +// FileNames contains allowed names of root bundle configuration files. +var FileNames = ConfigFileNames{ + "databricks.yml", + "databricks.yaml", + "bundle.yml", + "bundle.yaml", +} + +func (c ConfigFileNames) FindInPath(path string) (string, error) { + result := "" + var firstErr error + + for _, file := range c { + filePath := filepath.Join(path, file) + _, err := os.Stat(filePath) + if err == nil { + if result != "" { + return "", fmt.Errorf("multiple bundle root configuration files found in %s", path) + } + result = filePath + } else { + if firstErr == nil { + firstErr = err + } + } + } + + if result == "" { + return "", firstErr + } + + return result, nil +} diff --git a/bundle/config/filename_test.go b/bundle/config/filename_test.go new file mode 100644 index 00000000..9d71fa7e --- /dev/null +++ b/bundle/config/filename_test.go @@ -0,0 +1,70 @@ +package config + +import ( + "os" + "path/filepath" + "runtime" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestConfigFileNames_FindInPath(t *testing.T) { + testCases := []struct { + name string + files []string + expected string + err string + }{ + { + name: "file found", + files: []string{"databricks.yml"}, + expected: "BASE/databricks.yml", + err: "", + }, + { + name: "file found", + files: []string{"bundle.yml"}, + expected: "BASE/bundle.yml", + err: "", + }, + { + name: "multiple files found", + files: []string{"databricks.yaml", "bundle.yml"}, + expected: "", + err: "multiple bundle root configuration files found", + }, + { + name: "file not found", + files: []string{}, + expected: "", + err: "no such file or directory", + }, + } + + if runtime.GOOS == "windows" { + testCases[3].err = "The system cannot find the file specified." + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + projectDir := t.TempDir() + for _, file := range tc.files { + f1, _ := os.Create(filepath.Join(projectDir, file)) + f1.Close() + } + + result, err := FileNames.FindInPath(projectDir) + + expected := strings.Replace(tc.expected, "BASE/", projectDir+string(os.PathSeparator), 1) + assert.Equal(t, expected, result) + + if tc.err != "" { + assert.ErrorContains(t, err, tc.err) + } else { + assert.NoError(t, err) + } + }) + } +} diff --git a/bundle/config/root.go b/bundle/config/root.go index bf203833..25d5ce5f 100644 --- a/bundle/config/root.go +++ b/bundle/config/root.go @@ -12,36 +12,6 @@ import ( "github.com/imdario/mergo" ) -type ConfigFileNames []string - -// FileNames contains allowed names of bundle configuration files. -var FileNames = ConfigFileNames{"databricks.yml", "databricks.yaml", "bundle.yml", "bundle.yaml"} - -func (c ConfigFileNames) FindInPath(path string) (string, error) { - result := "" - var firstErr error - - for _, file := range c { - filePath := filepath.Join(path, file) - _, err := os.Stat(filePath) - if err == nil { - if result != "" { - return "", fmt.Errorf("multiple bundle root configuration files found in %s", path) - } - result = filePath - } else { - if firstErr == nil { - firstErr = err - } - } - } - - if result == "" { - return "", firstErr - } - return result, nil -} - type Root struct { // Path contains the directory path to the root of the bundle. // It is set when loading `databricks.yml`. diff --git a/bundle/config/root_test.go b/bundle/config/root_test.go index 6e263667..b0dfc3ec 100644 --- a/bundle/config/root_test.go +++ b/bundle/config/root_test.go @@ -2,11 +2,7 @@ package config import ( "encoding/json" - "os" - "path/filepath" "reflect" - "runtime" - "strings" "testing" "github.com/databricks/cli/bundle/config/variable" @@ -167,62 +163,3 @@ func TestRootMergeTargetOverridesWithMode(t *testing.T) { require.NoError(t, root.MergeTargetOverrides(env)) assert.Equal(t, Development, root.Bundle.Mode) } - -func TestConfigFileNames_FindInPath(t *testing.T) { - testCases := []struct { - name string - files []string - expected string - err string - }{ - { - name: "file found", - files: []string{"databricks.yml"}, - expected: "BASE/databricks.yml", - err: "", - }, - { - name: "file found", - files: []string{"bundle.yml"}, - expected: "BASE/bundle.yml", - err: "", - }, - { - name: "multiple files found", - files: []string{"databricks.yaml", "bundle.yml"}, - expected: "", - err: "multiple bundle root configuration files found", - }, - { - name: "file not found", - files: []string{}, - expected: "", - err: "no such file or directory", - }, - } - - if runtime.GOOS == "windows" { - testCases[3].err = "The system cannot find the file specified." - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - projectDir := t.TempDir() - for _, file := range tc.files { - f1, _ := os.Create(filepath.Join(projectDir, file)) - f1.Close() - } - - result, err := FileNames.FindInPath(projectDir) - - expected := strings.Replace(tc.expected, "BASE/", projectDir+string(os.PathSeparator), 1) - assert.Equal(t, expected, result) - - if tc.err != "" { - assert.ErrorContains(t, err, tc.err) - } else { - assert.NoError(t, err) - } - }) - } -}