From 3140a8feef26ccccb7197f481e0fb9a7ef1fc3d3 Mon Sep 17 00:00:00 2001 From: Kartik Gupta <88345179+kartikgupta-db@users.noreply.github.com> Date: Wed, 2 Aug 2023 19:22:47 +0200 Subject: [PATCH] Initialise a empty default bundle if BUNDLE_ROOT and DATABRICKS_BUNDLE_INCLUDES env vars are present (#604) ## Changes ## Tests --- bundle/bundle.go | 17 ++++++++ .../config/mutator/process_root_includes.go | 4 +- .../mutator/process_root_includes_test.go | 6 +-- bundle/root_test.go | 43 +++++++++++++++++++ 4 files changed, 64 insertions(+), 6 deletions(-) diff --git a/bundle/bundle.go b/bundle/bundle.go index 81fdfd4a8..0147883ca 100644 --- a/bundle/bundle.go +++ b/bundle/bundle.go @@ -43,10 +43,27 @@ type Bundle struct { AutoApprove bool } +const ExtraIncludePathsKey string = "DATABRICKS_BUNDLE_INCLUDES" + func Load(path string) (*Bundle, error) { bundle := &Bundle{} + stat, err := os.Stat(path) + if err != nil { + return nil, err + } configFile, err := config.FileNames.FindInPath(path) 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 } err = bundle.Config.Load(configFile) diff --git a/bundle/config/mutator/process_root_includes.go b/bundle/config/mutator/process_root_includes.go index 1b0faa77e..c2dffc6ee 100644 --- a/bundle/config/mutator/process_root_includes.go +++ b/bundle/config/mutator/process_root_includes.go @@ -12,11 +12,9 @@ import ( "golang.org/x/exp/slices" ) -const ExtraIncludePathsKey string = "DATABRICKS_BUNDLE_INCLUDES" - // Get extra include paths from environment variable func GetExtraIncludePaths() []string { - value, exists := os.LookupEnv(ExtraIncludePathsKey) + value, exists := os.LookupEnv(bundle.ExtraIncludePathsKey) if !exists { return nil } diff --git a/bundle/config/mutator/process_root_includes_test.go b/bundle/config/mutator/process_root_includes_test.go index 449e3a02c..1ce094bc3 100644 --- a/bundle/config/mutator/process_root_includes_test.go +++ b/bundle/config/mutator/process_root_includes_test.go @@ -129,9 +129,9 @@ func TestProcessRootIncludesExtrasFromEnvVar(t *testing.T) { rootPath := t.TempDir() testYamlName := "extra_include_path.yml" touch(t, rootPath, testYamlName) - os.Setenv(mutator.ExtraIncludePathsKey, path.Join(rootPath, testYamlName)) + os.Setenv(bundle.ExtraIncludePathsKey, path.Join(rootPath, testYamlName)) t.Cleanup(func() { - os.Unsetenv(mutator.ExtraIncludePathsKey) + os.Unsetenv(bundle.ExtraIncludePathsKey) }) bundle := &bundle.Bundle{ @@ -149,7 +149,7 @@ func TestProcessRootIncludesDedupExtrasFromEnvVar(t *testing.T) { rootPath := t.TempDir() testYamlName := "extra_include_path.yml" 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{ Config: config.Root{ diff --git a/bundle/root_test.go b/bundle/root_test.go index 2f8304921..e85c4fdcb 100644 --- a/bundle/root_test.go +++ b/bundle/root_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/databricks/cli/bundle/config" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -102,3 +103,45 @@ func TestRootLookupError(t *testing.T) { _, err := mustGetRoot() 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) +}