Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move bundle configuration filename code #917

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions bundle/config/filename.go
Original file line number Diff line number Diff line change
@@ -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
}
70 changes: 70 additions & 0 deletions bundle/config/filename_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
30 changes: 0 additions & 30 deletions bundle/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
63 changes: 0 additions & 63 deletions bundle/config/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ package config

import (
"encoding/json"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"

"github.com/databricks/cli/bundle/config/variable"
Expand Down Expand Up @@ -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)
}
})
}
}