2022-09-16 09:06:58 +00:00
|
|
|
package project
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-19 14:22:55 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-11-08 12:51:08 +00:00
|
|
|
"strings"
|
2022-09-16 09:06:58 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestProjectInitialize(t *testing.T) {
|
2022-09-22 11:40:11 +00:00
|
|
|
ctx, err := Initialize(context.Background(), "./testdata", DefaultEnvironment)
|
2022-09-16 09:06:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, Get(ctx).config.Name, "dev")
|
|
|
|
}
|
2022-10-19 14:22:55 +00:00
|
|
|
|
2022-11-08 12:51:08 +00:00
|
|
|
func TestProjectInitializationCreatesGitIgnoreIfAbsent(t *testing.T) {
|
2022-10-19 14:22:55 +00:00
|
|
|
// create project root with databricks.yml
|
|
|
|
projectDir := t.TempDir()
|
|
|
|
f1, err := os.Create(filepath.Join(projectDir, "databricks.yml"))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer f1.Close()
|
|
|
|
|
|
|
|
ctx, err := Initialize(context.Background(), projectDir, DefaultEnvironment)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-11-08 12:51:08 +00:00
|
|
|
gitIgnorePath := filepath.Join(projectDir, ".gitignore")
|
|
|
|
assert.FileExists(t, gitIgnorePath)
|
|
|
|
fileBytes, err := os.ReadFile(gitIgnorePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, string(fileBytes), ".databricks")
|
|
|
|
|
2022-10-19 14:22:55 +00:00
|
|
|
prj := Get(ctx)
|
|
|
|
_, err = prj.CacheDir()
|
2022-11-08 12:51:08 +00:00
|
|
|
assert.NoError(t, err)
|
2022-10-19 14:22:55 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 12:51:08 +00:00
|
|
|
func TestProjectInitializationAddsCacheDirToGitIgnore(t *testing.T) {
|
2022-10-19 14:22:55 +00:00
|
|
|
// create project root with databricks.yml
|
|
|
|
projectDir := t.TempDir()
|
|
|
|
f1, err := os.Create(filepath.Join(projectDir, "databricks.yml"))
|
|
|
|
assert.NoError(t, err)
|
2022-11-28 10:34:25 +00:00
|
|
|
f1.Close()
|
2022-10-19 14:22:55 +00:00
|
|
|
|
2022-11-08 12:51:08 +00:00
|
|
|
gitIgnorePath := filepath.Join(projectDir, ".gitignore")
|
|
|
|
f2, err := os.Create(gitIgnorePath)
|
2022-10-19 14:22:55 +00:00
|
|
|
assert.NoError(t, err)
|
2022-11-28 10:34:25 +00:00
|
|
|
f2.Close()
|
2022-10-19 14:22:55 +00:00
|
|
|
|
|
|
|
ctx, err := Initialize(context.Background(), projectDir, DefaultEnvironment)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-11-08 12:51:08 +00:00
|
|
|
fileBytes, err := os.ReadFile(gitIgnorePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, string(fileBytes), ".databricks")
|
|
|
|
|
2022-10-19 14:22:55 +00:00
|
|
|
prj := Get(ctx)
|
|
|
|
_, err = prj.CacheDir()
|
2022-11-08 12:51:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProjectInitializationDoesNotAddCacheDirToGitIgnoreIfAlreadyPresent(t *testing.T) {
|
|
|
|
// create project root with databricks.yml
|
|
|
|
projectDir := t.TempDir()
|
|
|
|
f1, err := os.Create(filepath.Join(projectDir, "databricks.yml"))
|
|
|
|
assert.NoError(t, err)
|
2022-11-28 10:34:25 +00:00
|
|
|
f1.Close()
|
2022-11-08 12:51:08 +00:00
|
|
|
|
|
|
|
gitIgnorePath := filepath.Join(projectDir, ".gitignore")
|
|
|
|
|
|
|
|
err = os.WriteFile(gitIgnorePath, []byte("/.databricks/"), 0o644)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = Initialize(context.Background(), projectDir, DefaultEnvironment)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
fileBytes, err := os.ReadFile(gitIgnorePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 1, strings.Count(string(fileBytes), ".databricks"))
|
2022-10-19 14:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProjectCacheDir(t *testing.T) {
|
|
|
|
// create project root with databricks.yml
|
|
|
|
projectDir := t.TempDir()
|
|
|
|
f1, err := os.Create(filepath.Join(projectDir, "databricks.yml"))
|
|
|
|
assert.NoError(t, err)
|
2022-11-28 10:34:25 +00:00
|
|
|
f1.Close()
|
2022-10-19 14:22:55 +00:00
|
|
|
|
|
|
|
// create .gitignore with the .databricks dir in it
|
|
|
|
f2, err := os.Create(filepath.Join(projectDir, ".gitignore"))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
content := []byte("/.databricks/")
|
|
|
|
_, err = f2.Write(content)
|
|
|
|
assert.NoError(t, err)
|
2022-11-28 10:34:25 +00:00
|
|
|
f2.Close()
|
2022-10-19 14:22:55 +00:00
|
|
|
|
|
|
|
ctx, err := Initialize(context.Background(), projectDir, DefaultEnvironment)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
prj := Get(ctx)
|
|
|
|
cacheDir, err := prj.CacheDir()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, filepath.Join(projectDir, ".databricks"), cacheDir)
|
|
|
|
}
|