Skip to content
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
17 changes: 12 additions & 5 deletions bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,27 @@ func (b *Bundle) WorkspaceClient() *databricks.WorkspaceClient {
return b.client
}

var cacheDirName = filepath.Join(".databricks", "bundle")

// CacheDir returns directory to use for temporary files for this bundle.
// Scoped to the bundle's environment.
func (b *Bundle) CacheDir(paths ...string) (string, error) {
if b.Config.Bundle.Environment == "" {
panic("environment not set")
}

cacheDirName, exists := os.LookupEnv("DATABRICKS_BUNDLE_TMP")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we use the root dir instead? e.g. there's already the env variable for root

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was @pietern suggestion. I think the rationale is that we do want to keep some files in ".databricks"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The BUNDLE_ROOT env var is to specify the root of the bundle sources if it cannot be derived from $PWD.

(we should rename that to DATABRICKS_BUNDLE_ROOT btw...).


if !exists || cacheDirName == "" {
cacheDirName = filepath.Join(
// Anchor at bundle root directory.
b.Config.Path,
// Static cache directory.
".databricks",
"bundle",
)
}

// Fixed components of the result path.
parts := []string{
// Anchor at bundle root directory.
b.Config.Path,
// Static cache directory.
cacheDirName,
// Scope with environment name.
b.Config.Bundle.Environment,
Expand Down
32 changes: 30 additions & 2 deletions bundle/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package bundle
import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -35,9 +34,38 @@ func TestBundleCacheDir(t *testing.T) {
// This is otherwise done by [mutators.SelectEnvironment].
bundle.Config.Bundle.Environment = "default"

// unset env variable in case it's set
t.Setenv("DATABRICKS_BUNDLE_TMP", "")

cacheDir, err := bundle.CacheDir()

// format is <CWD>/.databricks/bundle/<environment>
assert.NoError(t, err)
assert.Equal(t, filepath.Join(projectDir, ".databricks", "bundle", "default"), cacheDir)
}

func TestBundleCacheDirOverride(t *testing.T) {
projectDir := t.TempDir()
bundleTmpDir := t.TempDir()
f1, err := os.Create(filepath.Join(projectDir, "bundle.yml"))
require.NoError(t, err)
f1.Close()

bundle, err := Load(projectDir)
require.NoError(t, err)

// Artificially set environment.
// This is otherwise done by [mutators.SelectEnvironment].
bundle.Config.Bundle.Environment = "default"

// now we expect to use 'bundleTmpDir' instead of CWD/.databricks/bundle
t.Setenv("DATABRICKS_BUNDLE_TMP", bundleTmpDir)

cacheDir, err := bundle.CacheDir()

// format is <DATABRICKS_BUNDLE_TMP>/<environment>
assert.NoError(t, err)
assert.True(t, strings.HasPrefix(cacheDir, projectDir))
assert.Equal(t, filepath.Join(bundleTmpDir, "default"), cacheDir)
}

func TestBundleMustLoadSuccess(t *testing.T) {
Expand Down