Function to return bundle's cache directory (#109)

Parallel of `project.CacheDir()` introduced in
https://github.com/databricks/bricks/pull/82.
This commit is contained in:
Pieter Noordhuis 2022-11-30 14:40:41 +01:00 committed by GitHub
parent e1669b0352
commit 4064a21797
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package bundle
import (
"os"
"path/filepath"
"sync"
@ -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
}

View File

@ -2,6 +2,8 @@ package bundle
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
@ -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))
}