mirror of https://github.com/databricks/cli.git
add unit tests
This commit is contained in:
parent
62b2451c10
commit
08b6b10ff0
|
@ -23,7 +23,7 @@ type file interface {
|
||||||
DstPath() *destinationPath
|
DstPath() *destinationPath
|
||||||
|
|
||||||
// Write file to disk at the destination path.
|
// Write file to disk at the destination path.
|
||||||
PersistToDisk(ctx context.Context) error
|
PersistToDisk() error
|
||||||
}
|
}
|
||||||
|
|
||||||
type destinationPath struct {
|
type destinationPath struct {
|
||||||
|
@ -62,7 +62,7 @@ func (f *copyFile) DstPath() *destinationPath {
|
||||||
return f.dstPath
|
return f.dstPath
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *copyFile) PersistToDisk(ctx context.Context) error {
|
func (f *copyFile) PersistToDisk() error {
|
||||||
path := f.DstPath().absPath()
|
path := f.DstPath().absPath()
|
||||||
err := os.MkdirAll(filepath.Dir(path), 0755)
|
err := os.MkdirAll(filepath.Dir(path), 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -78,10 +78,12 @@ func (f *copyFile) PersistToDisk(ctx context.Context) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return writeFile(ctx, path, content)
|
return writeFile(f.ctx, path, content, f.perm)
|
||||||
}
|
}
|
||||||
|
|
||||||
type inMemoryFile struct {
|
type inMemoryFile struct {
|
||||||
|
ctx context.Context
|
||||||
|
|
||||||
dstPath *destinationPath
|
dstPath *destinationPath
|
||||||
|
|
||||||
content []byte
|
content []byte
|
||||||
|
@ -94,7 +96,7 @@ func (f *inMemoryFile) DstPath() *destinationPath {
|
||||||
return f.dstPath
|
return f.dstPath
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *inMemoryFile) PersistToDisk(ctx context.Context) error {
|
func (f *inMemoryFile) PersistToDisk() error {
|
||||||
path := f.DstPath().absPath()
|
path := f.DstPath().absPath()
|
||||||
|
|
||||||
err := os.MkdirAll(filepath.Dir(path), 0755)
|
err := os.MkdirAll(filepath.Dir(path), 0755)
|
||||||
|
@ -102,7 +104,7 @@ func (f *inMemoryFile) PersistToDisk(ctx context.Context) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return writeFile(ctx, path, f.content)
|
return writeFile(f.ctx, path, f.content, f.perm)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runsOnDatabricks(ctx context.Context) bool {
|
func runsOnDatabricks(ctx context.Context) bool {
|
||||||
|
@ -110,11 +112,15 @@ func runsOnDatabricks(ctx context.Context) bool {
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeFile(ctx context.Context, path string, content []byte) error {
|
func shouldUseImportNotebook(ctx context.Context, path string) bool {
|
||||||
if strings.HasPrefix(path, "/Workspace/") && runsOnDatabricks(ctx) && strings.HasSuffix(path, ".ipynb") {
|
return strings.HasPrefix(path, "/Workspace/") && runsOnDatabricks(ctx) && strings.HasSuffix(path, ".ipynb")
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeFile(ctx context.Context, path string, content []byte, perm fs.FileMode) error {
|
||||||
|
if shouldUseImportNotebook(ctx, path) {
|
||||||
return importNotebook(ctx, path, content)
|
return importNotebook(ctx, path, content)
|
||||||
} else {
|
} else {
|
||||||
return os.WriteFile(path, content, 0644)
|
return os.WriteFile(path, content, perm)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,11 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go/experimental/mocks"
|
||||||
|
"github.com/databricks/databricks-sdk-go/service/workspace"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
|
||||||
|
"github.com/databricks/cli/cmd/root"
|
||||||
"github.com/databricks/cli/libs/filer"
|
"github.com/databricks/cli/libs/filer"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
@ -17,6 +22,7 @@ func testInMemoryFile(t *testing.T, perm fs.FileMode) {
|
||||||
tmpDir := t.TempDir()
|
tmpDir := t.TempDir()
|
||||||
|
|
||||||
f := &inMemoryFile{
|
f := &inMemoryFile{
|
||||||
|
ctx: context.Background(),
|
||||||
dstPath: &destinationPath{
|
dstPath: &destinationPath{
|
||||||
root: tmpDir,
|
root: tmpDir,
|
||||||
relPath: "a/b/c",
|
relPath: "a/b/c",
|
||||||
|
@ -24,7 +30,7 @@ func testInMemoryFile(t *testing.T, perm fs.FileMode) {
|
||||||
perm: perm,
|
perm: perm,
|
||||||
content: []byte("123"),
|
content: []byte("123"),
|
||||||
}
|
}
|
||||||
err := f.PersistToDisk(context.Background())
|
err := f.PersistToDisk()
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
assertFileContent(t, filepath.Join(tmpDir, "a/b/c"), "123")
|
assertFileContent(t, filepath.Join(tmpDir, "a/b/c"), "123")
|
||||||
|
@ -38,10 +44,9 @@ func testCopyFile(t *testing.T, perm fs.FileMode) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
err = os.WriteFile(filepath.Join(tmpDir, "source"), []byte("qwerty"), perm)
|
err = os.WriteFile(filepath.Join(tmpDir, "source"), []byte("qwerty"), perm)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
f := ©File{
|
f := ©File{
|
||||||
ctx: ctx,
|
ctx: context.Background(),
|
||||||
dstPath: &destinationPath{
|
dstPath: &destinationPath{
|
||||||
root: tmpDir,
|
root: tmpDir,
|
||||||
relPath: "a/b/c",
|
relPath: "a/b/c",
|
||||||
|
@ -50,7 +55,7 @@ func testCopyFile(t *testing.T, perm fs.FileMode) {
|
||||||
srcPath: "source",
|
srcPath: "source",
|
||||||
srcFiler: templateFiler,
|
srcFiler: templateFiler,
|
||||||
}
|
}
|
||||||
err = f.PersistToDisk(ctx)
|
err = f.PersistToDisk()
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
assertFileContent(t, filepath.Join(tmpDir, "a/b/c"), "qwerty")
|
assertFileContent(t, filepath.Join(tmpDir, "a/b/c"), "qwerty")
|
||||||
|
@ -110,3 +115,35 @@ func TestTemplateCopyFilePersistToDiskForWindows(t *testing.T) {
|
||||||
// fs.FileMode values we can use for different operating systems.
|
// fs.FileMode values we can use for different operating systems.
|
||||||
testCopyFile(t, 0666)
|
testCopyFile(t, 0666)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestShouldUseImportNotebook(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
assert.False(t, shouldUseImportNotebook(ctx, "./foo/bar"))
|
||||||
|
assert.False(t, shouldUseImportNotebook(ctx, "./foo/bar.ipynb"))
|
||||||
|
assert.False(t, shouldUseImportNotebook(ctx, "/Workspace/foo/bar"))
|
||||||
|
assert.False(t, shouldUseImportNotebook(ctx, "/Workspace/foo/bar.ipynb"))
|
||||||
|
|
||||||
|
t.Setenv("DATABRICKS_RUNTIME_VERSION", "14.3")
|
||||||
|
assert.False(t, shouldUseImportNotebook(ctx, "./foo/bar"))
|
||||||
|
assert.False(t, shouldUseImportNotebook(ctx, "./foo/bar.ipynb"))
|
||||||
|
assert.False(t, shouldUseImportNotebook(ctx, "/Workspace/foo/bar"))
|
||||||
|
assert.True(t, shouldUseImportNotebook(ctx, "/Workspace/foo/bar.ipynb"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestImportNotebook(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
ctx = root.SetWorkspaceClient(ctx, m.WorkspaceClient)
|
||||||
|
|
||||||
|
workspaceApi := m.GetMockWorkspaceAPI()
|
||||||
|
workspaceApi.EXPECT().Import(mock.Anything, workspace.Import{
|
||||||
|
Content: "cXdlcnR5", // base64 of "qwerty"
|
||||||
|
Format: "AUTO",
|
||||||
|
Overwrite: false,
|
||||||
|
Path: "/Workspace/foo/bar.ipynb",
|
||||||
|
}).Return(nil)
|
||||||
|
|
||||||
|
err := importNotebook(ctx, "/Workspace/foo/bar.ipynb", []byte("qwerty"))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
|
@ -194,6 +194,7 @@ func (r *renderer) computeFile(relPathTemplate string) (file, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return &inMemoryFile{
|
return &inMemoryFile{
|
||||||
|
ctx: r.ctx,
|
||||||
dstPath: &destinationPath{
|
dstPath: &destinationPath{
|
||||||
root: r.instanceRoot,
|
root: r.instanceRoot,
|
||||||
relPath: relPath,
|
relPath: relPath,
|
||||||
|
@ -320,7 +321,7 @@ func (r *renderer) persistToDisk() error {
|
||||||
|
|
||||||
// Persist files to disk
|
// Persist files to disk
|
||||||
for _, file := range filesToPersist {
|
for _, file := range filesToPersist {
|
||||||
err := file.PersistToDisk(r.ctx)
|
err := file.PersistToDisk()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -329,6 +329,7 @@ func TestRendererPersistToDisk(t *testing.T) {
|
||||||
skipPatterns: []string{"a/b/c", "mn*"},
|
skipPatterns: []string{"a/b/c", "mn*"},
|
||||||
files: []file{
|
files: []file{
|
||||||
&inMemoryFile{
|
&inMemoryFile{
|
||||||
|
ctx: ctx,
|
||||||
dstPath: &destinationPath{
|
dstPath: &destinationPath{
|
||||||
root: tmpDir,
|
root: tmpDir,
|
||||||
relPath: "a/b/c",
|
relPath: "a/b/c",
|
||||||
|
@ -337,6 +338,7 @@ func TestRendererPersistToDisk(t *testing.T) {
|
||||||
content: nil,
|
content: nil,
|
||||||
},
|
},
|
||||||
&inMemoryFile{
|
&inMemoryFile{
|
||||||
|
ctx: ctx,
|
||||||
dstPath: &destinationPath{
|
dstPath: &destinationPath{
|
||||||
root: tmpDir,
|
root: tmpDir,
|
||||||
relPath: "mno",
|
relPath: "mno",
|
||||||
|
@ -345,6 +347,7 @@ func TestRendererPersistToDisk(t *testing.T) {
|
||||||
content: nil,
|
content: nil,
|
||||||
},
|
},
|
||||||
&inMemoryFile{
|
&inMemoryFile{
|
||||||
|
ctx: ctx,
|
||||||
dstPath: &destinationPath{
|
dstPath: &destinationPath{
|
||||||
root: tmpDir,
|
root: tmpDir,
|
||||||
relPath: "a/b/d",
|
relPath: "a/b/d",
|
||||||
|
@ -353,6 +356,7 @@ func TestRendererPersistToDisk(t *testing.T) {
|
||||||
content: []byte("123"),
|
content: []byte("123"),
|
||||||
},
|
},
|
||||||
&inMemoryFile{
|
&inMemoryFile{
|
||||||
|
ctx: ctx,
|
||||||
dstPath: &destinationPath{
|
dstPath: &destinationPath{
|
||||||
root: tmpDir,
|
root: tmpDir,
|
||||||
relPath: "mmnn",
|
relPath: "mmnn",
|
||||||
|
|
Loading…
Reference in New Issue