2022-11-18 09:57:31 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
2023-08-11 12:28:05 +00:00
|
|
|
"context"
|
2022-11-18 09:57:31 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle/config"
|
2023-09-11 08:18:43 +00:00
|
|
|
"github.com/databricks/cli/bundle/env"
|
2023-08-02 17:22:47 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-11-18 09:57:31 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Changes into specified directory for the duration of the test.
|
|
|
|
// Returns the current working directory.
|
|
|
|
func chdir(t *testing.T, dir string) string {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
abs, err := filepath.Abs(dir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = os.Chdir(abs)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
err := os.Chdir(wd)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
return wd
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRootFromEnv(t *testing.T) {
|
2023-09-11 08:18:43 +00:00
|
|
|
ctx := context.Background()
|
2022-11-18 09:57:31 +00:00
|
|
|
dir := t.TempDir()
|
2023-09-11 08:18:43 +00:00
|
|
|
t.Setenv(env.RootVariable, dir)
|
2022-11-18 09:57:31 +00:00
|
|
|
|
|
|
|
// It should pull the root from the environment variable.
|
2023-09-11 08:18:43 +00:00
|
|
|
root, err := mustGetRoot(ctx)
|
2022-11-18 09:57:31 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, root, dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRootFromEnvDoesntExist(t *testing.T) {
|
2023-09-11 08:18:43 +00:00
|
|
|
ctx := context.Background()
|
2022-11-18 09:57:31 +00:00
|
|
|
dir := t.TempDir()
|
2023-09-11 08:18:43 +00:00
|
|
|
t.Setenv(env.RootVariable, filepath.Join(dir, "doesntexist"))
|
2022-11-18 09:57:31 +00:00
|
|
|
|
|
|
|
// It should pull the root from the environment variable.
|
2023-09-11 08:18:43 +00:00
|
|
|
_, err := mustGetRoot(ctx)
|
2022-11-18 09:57:31 +00:00
|
|
|
require.Errorf(t, err, "invalid bundle root")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRootFromEnvIsFile(t *testing.T) {
|
2023-09-11 08:18:43 +00:00
|
|
|
ctx := context.Background()
|
2022-11-18 09:57:31 +00:00
|
|
|
dir := t.TempDir()
|
|
|
|
f, err := os.Create(filepath.Join(dir, "invalid"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
f.Close()
|
2023-09-11 08:18:43 +00:00
|
|
|
t.Setenv(env.RootVariable, f.Name())
|
2022-11-18 09:57:31 +00:00
|
|
|
|
|
|
|
// It should pull the root from the environment variable.
|
2023-09-11 08:18:43 +00:00
|
|
|
_, err = mustGetRoot(ctx)
|
2022-11-18 09:57:31 +00:00
|
|
|
require.Errorf(t, err, "invalid bundle root")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRootIfEnvIsEmpty(t *testing.T) {
|
2023-09-11 08:18:43 +00:00
|
|
|
ctx := context.Background()
|
2022-11-18 09:57:31 +00:00
|
|
|
dir := ""
|
2023-09-11 08:18:43 +00:00
|
|
|
t.Setenv(env.RootVariable, dir)
|
2022-11-18 09:57:31 +00:00
|
|
|
|
|
|
|
// It should pull the root from the environment variable.
|
2023-09-11 08:18:43 +00:00
|
|
|
_, err := mustGetRoot(ctx)
|
2022-11-18 09:57:31 +00:00
|
|
|
require.Errorf(t, err, "invalid bundle root")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRootLookup(t *testing.T) {
|
2023-09-11 08:18:43 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-11-18 09:57:31 +00:00
|
|
|
// Have to set then unset to allow the testing package to revert it to its original value.
|
2023-09-11 08:18:43 +00:00
|
|
|
t.Setenv(env.RootVariable, "")
|
|
|
|
os.Unsetenv(env.RootVariable)
|
2022-11-18 09:57:31 +00:00
|
|
|
|
|
|
|
chdir(t, t.TempDir())
|
|
|
|
|
2023-07-18 10:16:34 +00:00
|
|
|
// Create databricks.yml file.
|
|
|
|
f, err := os.Create(config.FileNames[0])
|
2022-11-18 09:57:31 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
// Create directory tree.
|
|
|
|
err = os.MkdirAll("./a/b/c", 0755)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// It should find the project root from $PWD.
|
|
|
|
wd := chdir(t, "./a/b/c")
|
2023-09-11 08:18:43 +00:00
|
|
|
root, err := mustGetRoot(ctx)
|
2022-11-18 09:57:31 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, wd, root)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRootLookupError(t *testing.T) {
|
2023-09-11 08:18:43 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-11-18 09:57:31 +00:00
|
|
|
// Have to set then unset to allow the testing package to revert it to its original value.
|
2023-09-11 08:18:43 +00:00
|
|
|
t.Setenv(env.RootVariable, "")
|
|
|
|
os.Unsetenv(env.RootVariable)
|
2022-11-18 09:57:31 +00:00
|
|
|
|
|
|
|
// It can't find a project root from a temporary directory.
|
|
|
|
_ = chdir(t, t.TempDir())
|
2023-09-11 08:18:43 +00:00
|
|
|
_, err := mustGetRoot(ctx)
|
2022-11-18 09:57:31 +00:00
|
|
|
require.ErrorContains(t, err, "unable to locate bundle root")
|
|
|
|
}
|
2023-08-02 17:22:47 +00:00
|
|
|
|
|
|
|
func TestLoadYamlWhenIncludesEnvPresent(t *testing.T) {
|
2023-09-11 08:18:43 +00:00
|
|
|
ctx := context.Background()
|
2023-08-02 17:22:47 +00:00
|
|
|
chdir(t, filepath.Join(".", "tests", "basic"))
|
2023-09-11 08:18:43 +00:00
|
|
|
t.Setenv(env.IncludesVariable, "test")
|
2023-08-02 17:22:47 +00:00
|
|
|
|
2023-09-11 08:18:43 +00:00
|
|
|
bundle, err := MustLoad(ctx)
|
2023-08-02 17:22:47 +00:00
|
|
|
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) {
|
2023-09-11 08:18:43 +00:00
|
|
|
ctx := context.Background()
|
2023-08-02 17:22:47 +00:00
|
|
|
dir := t.TempDir()
|
|
|
|
chdir(t, dir)
|
2023-09-11 08:18:43 +00:00
|
|
|
t.Setenv(env.RootVariable, dir)
|
|
|
|
t.Setenv(env.IncludesVariable, "test")
|
2023-08-02 17:22:47 +00:00
|
|
|
|
2023-09-11 08:18:43 +00:00
|
|
|
bundle, err := MustLoad(ctx)
|
2023-08-02 17:22:47 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, dir, bundle.Config.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorIfNoYamlNoRootEnvAndIncludesEnvPresent(t *testing.T) {
|
2023-09-11 08:18:43 +00:00
|
|
|
ctx := context.Background()
|
2023-08-02 17:22:47 +00:00
|
|
|
dir := t.TempDir()
|
|
|
|
chdir(t, dir)
|
2023-09-11 08:18:43 +00:00
|
|
|
t.Setenv(env.IncludesVariable, "test")
|
2023-08-02 17:22:47 +00:00
|
|
|
|
2023-09-11 08:18:43 +00:00
|
|
|
_, err := MustLoad(ctx)
|
2023-08-02 17:22:47 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorIfNoYamlNoIncludesEnvAndRootEnvPresent(t *testing.T) {
|
2023-09-11 08:18:43 +00:00
|
|
|
ctx := context.Background()
|
2023-08-02 17:22:47 +00:00
|
|
|
dir := t.TempDir()
|
|
|
|
chdir(t, dir)
|
2023-09-11 08:18:43 +00:00
|
|
|
t.Setenv(env.RootVariable, dir)
|
2023-08-02 17:22:47 +00:00
|
|
|
|
2023-09-11 08:18:43 +00:00
|
|
|
_, err := MustLoad(ctx)
|
2023-08-02 17:22:47 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|