Skip to content

Commit

Permalink
Function to return bundle's cache directory (#109)
Browse files Browse the repository at this point in the history
Parallel of `project.CacheDir()` introduced in
#82.
  • Loading branch information
pietern authored Nov 30, 2022
1 parent e1669b0 commit 4064a21
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
20 changes: 20 additions & 0 deletions bundle/bundle.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bundle

import (
"os"
"path/filepath"
"sync"

Expand Down Expand Up @@ -49,3 +50,22 @@ 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() (string, error) {
if b.Config.Bundle.Environment == "" {
panic("environment not set")
}

// Make directory if it doesn't exist yet.
dir := filepath.Join(b.Config.Path, cacheDirName, b.Config.Bundle.Environment)
err := os.MkdirAll(dir, 0700)
if err != nil {
return "", err
}

return dir, nil
}
20 changes: 20 additions & 0 deletions bundle/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package bundle

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -19,3 +21,21 @@ func TestLoadExists(t *testing.T) {
require.Nil(t, err)
assert.Equal(t, "basic", b.Config.Bundle.Name)
}

func TestBundleCacheDir(t *testing.T) {
projectDir := 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"

cacheDir, err := bundle.CacheDir()
assert.NoError(t, err)
assert.True(t, strings.HasPrefix(cacheDir, projectDir))
}

0 comments on commit 4064a21

Please sign in to comment.