Initialise a empty default bundle if BUNDLE_ROOT and DATABRICKS_BUNDLE_INCLUDES env vars are present (#604)

## Changes
<!-- Summary of your changes that are easy to understand -->

## Tests
<!-- How is this tested? -->
This commit is contained in:
Kartik Gupta 2023-08-02 19:22:47 +02:00 committed by GitHub
parent 31b178ad6c
commit 3140a8feef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 6 deletions

View File

@ -43,10 +43,27 @@ type Bundle struct {
AutoApprove bool AutoApprove bool
} }
const ExtraIncludePathsKey string = "DATABRICKS_BUNDLE_INCLUDES"
func Load(path string) (*Bundle, error) { func Load(path string) (*Bundle, error) {
bundle := &Bundle{} bundle := &Bundle{}
stat, err := os.Stat(path)
if err != nil {
return nil, err
}
configFile, err := config.FileNames.FindInPath(path) configFile, err := config.FileNames.FindInPath(path)
if err != nil { if err != nil {
_, hasIncludePathEnv := os.LookupEnv(ExtraIncludePathsKey)
_, hasBundleRootEnv := os.LookupEnv(envBundleRoot)
if hasIncludePathEnv && hasBundleRootEnv && stat.IsDir() {
bundle.Config = config.Root{
Path: path,
Bundle: config.Bundle{
Name: filepath.Base(path),
},
}
return bundle, nil
}
return nil, err return nil, err
} }
err = bundle.Config.Load(configFile) err = bundle.Config.Load(configFile)

View File

@ -12,11 +12,9 @@ import (
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
) )
const ExtraIncludePathsKey string = "DATABRICKS_BUNDLE_INCLUDES"
// Get extra include paths from environment variable // Get extra include paths from environment variable
func GetExtraIncludePaths() []string { func GetExtraIncludePaths() []string {
value, exists := os.LookupEnv(ExtraIncludePathsKey) value, exists := os.LookupEnv(bundle.ExtraIncludePathsKey)
if !exists { if !exists {
return nil return nil
} }

View File

@ -129,9 +129,9 @@ func TestProcessRootIncludesExtrasFromEnvVar(t *testing.T) {
rootPath := t.TempDir() rootPath := t.TempDir()
testYamlName := "extra_include_path.yml" testYamlName := "extra_include_path.yml"
touch(t, rootPath, testYamlName) touch(t, rootPath, testYamlName)
os.Setenv(mutator.ExtraIncludePathsKey, path.Join(rootPath, testYamlName)) os.Setenv(bundle.ExtraIncludePathsKey, path.Join(rootPath, testYamlName))
t.Cleanup(func() { t.Cleanup(func() {
os.Unsetenv(mutator.ExtraIncludePathsKey) os.Unsetenv(bundle.ExtraIncludePathsKey)
}) })
bundle := &bundle.Bundle{ bundle := &bundle.Bundle{
@ -149,7 +149,7 @@ func TestProcessRootIncludesDedupExtrasFromEnvVar(t *testing.T) {
rootPath := t.TempDir() rootPath := t.TempDir()
testYamlName := "extra_include_path.yml" testYamlName := "extra_include_path.yml"
touch(t, rootPath, testYamlName) touch(t, rootPath, testYamlName)
t.Setenv(mutator.ExtraIncludePathsKey, fmt.Sprintf("%s%s%s", path.Join(rootPath, testYamlName), string(os.PathListSeparator), path.Join(rootPath, testYamlName))) t.Setenv(bundle.ExtraIncludePathsKey, fmt.Sprintf("%s%s%s", path.Join(rootPath, testYamlName), string(os.PathListSeparator), path.Join(rootPath, testYamlName)))
bundle := &bundle.Bundle{ bundle := &bundle.Bundle{
Config: config.Root{ Config: config.Root{

View File

@ -6,6 +6,7 @@ import (
"testing" "testing"
"github.com/databricks/cli/bundle/config" "github.com/databricks/cli/bundle/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -102,3 +103,45 @@ func TestRootLookupError(t *testing.T) {
_, err := mustGetRoot() _, err := mustGetRoot()
require.ErrorContains(t, err, "unable to locate bundle root") require.ErrorContains(t, err, "unable to locate bundle root")
} }
func TestLoadYamlWhenIncludesEnvPresent(t *testing.T) {
chdir(t, filepath.Join(".", "tests", "basic"))
t.Setenv(ExtraIncludePathsKey, "test")
bundle, err := MustLoad()
assert.NoError(t, err)
assert.Equal(t, "basic", bundle.Config.Bundle.Name)
cwd, err := os.Getwd()
assert.NoError(t, err)
assert.Equal(t, cwd, bundle.Config.Path)
}
func TestLoadDefautlBundleWhenNoYamlAndRootAndIncludesEnvPresent(t *testing.T) {
dir := t.TempDir()
chdir(t, dir)
t.Setenv(envBundleRoot, dir)
t.Setenv(ExtraIncludePathsKey, "test")
bundle, err := MustLoad()
assert.NoError(t, err)
assert.Equal(t, dir, bundle.Config.Path)
}
func TestErrorIfNoYamlNoRootEnvAndIncludesEnvPresent(t *testing.T) {
dir := t.TempDir()
chdir(t, dir)
t.Setenv(ExtraIncludePathsKey, "test")
_, err := MustLoad()
assert.Error(t, err)
}
func TestErrorIfNoYamlNoIncludesEnvAndRootEnvPresent(t *testing.T) {
dir := t.TempDir()
chdir(t, dir)
t.Setenv(envBundleRoot, dir)
_, err := MustLoad()
assert.Error(t, err)
}