github.com/databricks/cli@v0.203.0/bundle/root_test.go (about)

     1  package bundle
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/databricks/cli/bundle/config"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  // Changes into specified directory for the duration of the test.
    14  // Returns the current working directory.
    15  func chdir(t *testing.T, dir string) string {
    16  	wd, err := os.Getwd()
    17  	require.NoError(t, err)
    18  
    19  	abs, err := filepath.Abs(dir)
    20  	require.NoError(t, err)
    21  
    22  	err = os.Chdir(abs)
    23  	require.NoError(t, err)
    24  
    25  	t.Cleanup(func() {
    26  		err := os.Chdir(wd)
    27  		require.NoError(t, err)
    28  	})
    29  
    30  	return wd
    31  }
    32  
    33  func TestRootFromEnv(t *testing.T) {
    34  	dir := t.TempDir()
    35  	t.Setenv(envBundleRoot, dir)
    36  
    37  	// It should pull the root from the environment variable.
    38  	root, err := mustGetRoot()
    39  	require.NoError(t, err)
    40  	require.Equal(t, root, dir)
    41  }
    42  
    43  func TestRootFromEnvDoesntExist(t *testing.T) {
    44  	dir := t.TempDir()
    45  	t.Setenv(envBundleRoot, filepath.Join(dir, "doesntexist"))
    46  
    47  	// It should pull the root from the environment variable.
    48  	_, err := mustGetRoot()
    49  	require.Errorf(t, err, "invalid bundle root")
    50  }
    51  
    52  func TestRootFromEnvIsFile(t *testing.T) {
    53  	dir := t.TempDir()
    54  	f, err := os.Create(filepath.Join(dir, "invalid"))
    55  	require.NoError(t, err)
    56  	f.Close()
    57  	t.Setenv(envBundleRoot, f.Name())
    58  
    59  	// It should pull the root from the environment variable.
    60  	_, err = mustGetRoot()
    61  	require.Errorf(t, err, "invalid bundle root")
    62  }
    63  
    64  func TestRootIfEnvIsEmpty(t *testing.T) {
    65  	dir := ""
    66  	t.Setenv(envBundleRoot, dir)
    67  
    68  	// It should pull the root from the environment variable.
    69  	_, err := mustGetRoot()
    70  	require.Errorf(t, err, "invalid bundle root")
    71  }
    72  
    73  func TestRootLookup(t *testing.T) {
    74  	// Have to set then unset to allow the testing package to revert it to its original value.
    75  	t.Setenv(envBundleRoot, "")
    76  	os.Unsetenv(envBundleRoot)
    77  
    78  	chdir(t, t.TempDir())
    79  
    80  	// Create databricks.yml file.
    81  	f, err := os.Create(config.FileNames[0])
    82  	require.NoError(t, err)
    83  	defer f.Close()
    84  
    85  	// Create directory tree.
    86  	err = os.MkdirAll("./a/b/c", 0755)
    87  	require.NoError(t, err)
    88  
    89  	// It should find the project root from $PWD.
    90  	wd := chdir(t, "./a/b/c")
    91  	root, err := mustGetRoot()
    92  	require.NoError(t, err)
    93  	require.Equal(t, wd, root)
    94  }
    95  
    96  func TestRootLookupError(t *testing.T) {
    97  	// Have to set then unset to allow the testing package to revert it to its original value.
    98  	t.Setenv(envBundleRoot, "")
    99  	os.Unsetenv(envBundleRoot)
   100  
   101  	// It can't find a project root from a temporary directory.
   102  	_ = chdir(t, t.TempDir())
   103  	_, err := mustGetRoot()
   104  	require.ErrorContains(t, err, "unable to locate bundle root")
   105  }
   106  
   107  func TestLoadYamlWhenIncludesEnvPresent(t *testing.T) {
   108  	chdir(t, filepath.Join(".", "tests", "basic"))
   109  	t.Setenv(ExtraIncludePathsKey, "test")
   110  
   111  	bundle, err := MustLoad()
   112  	assert.NoError(t, err)
   113  	assert.Equal(t, "basic", bundle.Config.Bundle.Name)
   114  
   115  	cwd, err := os.Getwd()
   116  	assert.NoError(t, err)
   117  	assert.Equal(t, cwd, bundle.Config.Path)
   118  }
   119  
   120  func TestLoadDefautlBundleWhenNoYamlAndRootAndIncludesEnvPresent(t *testing.T) {
   121  	dir := t.TempDir()
   122  	chdir(t, dir)
   123  	t.Setenv(envBundleRoot, dir)
   124  	t.Setenv(ExtraIncludePathsKey, "test")
   125  
   126  	bundle, err := MustLoad()
   127  	assert.NoError(t, err)
   128  	assert.Equal(t, dir, bundle.Config.Path)
   129  }
   130  
   131  func TestErrorIfNoYamlNoRootEnvAndIncludesEnvPresent(t *testing.T) {
   132  	dir := t.TempDir()
   133  	chdir(t, dir)
   134  	t.Setenv(ExtraIncludePathsKey, "test")
   135  
   136  	_, err := MustLoad()
   137  	assert.Error(t, err)
   138  }
   139  
   140  func TestErrorIfNoYamlNoIncludesEnvAndRootEnvPresent(t *testing.T) {
   141  	dir := t.TempDir()
   142  	chdir(t, dir)
   143  	t.Setenv(envBundleRoot, dir)
   144  
   145  	_, err := MustLoad()
   146  	assert.Error(t, err)
   147  }