2022-09-14 15:50:29 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2023-03-17 14:17:31 +00:00
|
|
|
"context"
|
2022-12-12 13:31:06 +00:00
|
|
|
"fmt"
|
2022-09-14 15:50:29 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2022-09-15 13:40:47 +00:00
|
|
|
"time"
|
2022-09-14 15:50:29 +00:00
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/libs/git"
|
|
|
|
"github.com/databricks/cli/libs/testfile"
|
2024-05-30 07:41:50 +00:00
|
|
|
"github.com/databricks/cli/libs/vfs"
|
2022-09-14 15:50:29 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-01-24 07:30:10 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-09-14 15:50:29 +00:00
|
|
|
)
|
|
|
|
|
2023-10-03 13:47:46 +00:00
|
|
|
func assertKeysOfMap[T any](t *testing.T, m map[string]T, expectedKeys []string) {
|
2022-11-21 22:42:09 +00:00
|
|
|
keys := make([]string, len(m))
|
|
|
|
i := 0
|
|
|
|
for k := range m {
|
|
|
|
keys[i] = k
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
assert.ElementsMatch(t, expectedKeys, keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDiff(t *testing.T) {
|
2023-05-23 10:10:15 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-11-21 22:42:09 +00:00
|
|
|
// Create temp project dir
|
|
|
|
projectDir := t.TempDir()
|
2024-05-30 07:41:50 +00:00
|
|
|
fileSet, err := git.NewFileSet(vfs.MustNew(projectDir))
|
2023-01-27 15:04:58 +00:00
|
|
|
require.NoError(t, err)
|
2022-10-19 14:22:55 +00:00
|
|
|
state := Snapshot{
|
2023-10-03 13:47:46 +00:00
|
|
|
SnapshotState: &SnapshotState{
|
|
|
|
LastModifiedTimes: make(map[string]time.Time),
|
|
|
|
LocalToRemoteNames: make(map[string]string),
|
|
|
|
RemoteToLocalNames: make(map[string]string),
|
|
|
|
},
|
2022-10-19 14:22:55 +00:00
|
|
|
}
|
2022-09-14 15:50:29 +00:00
|
|
|
|
2023-01-10 12:16:30 +00:00
|
|
|
f1 := testfile.CreateFile(t, filepath.Join(projectDir, "hello.txt"))
|
|
|
|
defer f1.Close(t)
|
2022-11-21 22:42:09 +00:00
|
|
|
worldFilePath := filepath.Join(projectDir, "world.txt")
|
2023-01-10 12:16:30 +00:00
|
|
|
f2 := testfile.CreateFile(t, worldFilePath)
|
|
|
|
defer f2.Close(t)
|
2022-11-21 22:42:09 +00:00
|
|
|
|
|
|
|
// New files are put
|
2024-08-19 15:15:14 +00:00
|
|
|
files, err := fileSet.Files()
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.NoError(t, err)
|
2023-05-23 10:10:15 +00:00
|
|
|
change, err := state.diff(ctx, files)
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.NoError(t, err)
|
2022-09-14 15:50:29 +00:00
|
|
|
assert.Len(t, change.delete, 0)
|
|
|
|
assert.Len(t, change.put, 2)
|
|
|
|
assert.Contains(t, change.put, "hello.txt")
|
|
|
|
assert.Contains(t, change.put, "world.txt")
|
2023-10-03 13:47:46 +00:00
|
|
|
assertKeysOfMap(t, state.LastModifiedTimes, []string{"hello.txt", "world.txt"})
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.Equal(t, map[string]string{"hello.txt": "hello.txt", "world.txt": "world.txt"}, state.LocalToRemoteNames)
|
|
|
|
assert.Equal(t, map[string]string{"hello.txt": "hello.txt", "world.txt": "world.txt"}, state.RemoteToLocalNames)
|
2022-09-14 15:50:29 +00:00
|
|
|
|
2022-11-21 22:42:09 +00:00
|
|
|
// world.txt is editted
|
2023-01-10 12:16:30 +00:00
|
|
|
f2.Overwrite(t, "bunnies are cute.")
|
2022-09-14 15:50:29 +00:00
|
|
|
assert.NoError(t, err)
|
2024-08-19 15:15:14 +00:00
|
|
|
files, err = fileSet.Files()
|
2022-09-14 15:50:29 +00:00
|
|
|
assert.NoError(t, err)
|
2023-05-23 10:10:15 +00:00
|
|
|
change, err = state.diff(ctx, files)
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-09-14 15:50:29 +00:00
|
|
|
assert.Len(t, change.delete, 0)
|
|
|
|
assert.Len(t, change.put, 1)
|
|
|
|
assert.Contains(t, change.put, "world.txt")
|
2023-10-03 13:47:46 +00:00
|
|
|
assertKeysOfMap(t, state.LastModifiedTimes, []string{"hello.txt", "world.txt"})
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.Equal(t, map[string]string{"hello.txt": "hello.txt", "world.txt": "world.txt"}, state.LocalToRemoteNames)
|
|
|
|
assert.Equal(t, map[string]string{"hello.txt": "hello.txt", "world.txt": "world.txt"}, state.RemoteToLocalNames)
|
2022-09-14 15:50:29 +00:00
|
|
|
|
2022-11-21 22:42:09 +00:00
|
|
|
// hello.txt is deleted
|
2023-01-10 12:16:30 +00:00
|
|
|
f1.Remove(t)
|
2022-09-14 15:50:29 +00:00
|
|
|
assert.NoError(t, err)
|
2024-08-19 15:15:14 +00:00
|
|
|
files, err = fileSet.Files()
|
2022-09-14 15:50:29 +00:00
|
|
|
assert.NoError(t, err)
|
2023-05-23 10:10:15 +00:00
|
|
|
change, err = state.diff(ctx, files)
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.NoError(t, err)
|
2022-09-14 15:50:29 +00:00
|
|
|
assert.Len(t, change.delete, 1)
|
|
|
|
assert.Len(t, change.put, 0)
|
|
|
|
assert.Contains(t, change.delete, "hello.txt")
|
2023-10-03 13:47:46 +00:00
|
|
|
assertKeysOfMap(t, state.LastModifiedTimes, []string{"world.txt"})
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.Equal(t, map[string]string{"world.txt": "world.txt"}, state.LocalToRemoteNames)
|
|
|
|
assert.Equal(t, map[string]string{"world.txt": "world.txt"}, state.RemoteToLocalNames)
|
|
|
|
}
|
|
|
|
|
2023-02-15 16:02:54 +00:00
|
|
|
func TestSymlinkDiff(t *testing.T) {
|
2023-05-23 10:10:15 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-02-15 16:02:54 +00:00
|
|
|
// Create temp project dir
|
|
|
|
projectDir := t.TempDir()
|
2024-05-30 07:41:50 +00:00
|
|
|
fileSet, err := git.NewFileSet(vfs.MustNew(projectDir))
|
2023-02-15 16:02:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
state := Snapshot{
|
2023-10-03 13:47:46 +00:00
|
|
|
SnapshotState: &SnapshotState{
|
|
|
|
LastModifiedTimes: make(map[string]time.Time),
|
|
|
|
LocalToRemoteNames: make(map[string]string),
|
|
|
|
RemoteToLocalNames: make(map[string]string),
|
|
|
|
},
|
2023-02-15 16:02:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Mkdir(filepath.Join(projectDir, "foo"), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
f1 := testfile.CreateFile(t, filepath.Join(projectDir, "foo", "hello.txt"))
|
|
|
|
defer f1.Close(t)
|
|
|
|
|
|
|
|
err = os.Symlink(filepath.Join(projectDir, "foo"), filepath.Join(projectDir, "bar"))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-08-19 15:15:14 +00:00
|
|
|
files, err := fileSet.Files()
|
2023-02-15 16:02:54 +00:00
|
|
|
assert.NoError(t, err)
|
2023-05-23 10:10:15 +00:00
|
|
|
change, err := state.diff(ctx, files)
|
2023-02-15 16:02:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, change.put, 1)
|
|
|
|
}
|
|
|
|
|
2022-12-22 10:35:32 +00:00
|
|
|
func TestFolderDiff(t *testing.T) {
|
2023-05-23 10:10:15 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-12-22 10:35:32 +00:00
|
|
|
// Create temp project dir
|
|
|
|
projectDir := t.TempDir()
|
2024-05-30 07:41:50 +00:00
|
|
|
fileSet, err := git.NewFileSet(vfs.MustNew(projectDir))
|
2023-01-27 15:04:58 +00:00
|
|
|
require.NoError(t, err)
|
2022-12-22 10:35:32 +00:00
|
|
|
state := Snapshot{
|
2023-10-03 13:47:46 +00:00
|
|
|
SnapshotState: &SnapshotState{
|
|
|
|
LastModifiedTimes: make(map[string]time.Time),
|
|
|
|
LocalToRemoteNames: make(map[string]string),
|
|
|
|
RemoteToLocalNames: make(map[string]string),
|
|
|
|
},
|
2022-12-22 10:35:32 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 15:04:58 +00:00
|
|
|
err = os.Mkdir(filepath.Join(projectDir, "foo"), os.ModePerm)
|
2022-12-22 10:35:32 +00:00
|
|
|
assert.NoError(t, err)
|
2023-01-10 12:16:30 +00:00
|
|
|
f1 := testfile.CreateFile(t, filepath.Join(projectDir, "foo", "bar.py"))
|
|
|
|
defer f1.Close(t)
|
|
|
|
f1.Overwrite(t, "# Databricks notebook source\nprint(\"abc\")")
|
2022-12-22 10:35:32 +00:00
|
|
|
|
2024-08-19 15:15:14 +00:00
|
|
|
files, err := fileSet.Files()
|
2022-12-22 10:35:32 +00:00
|
|
|
assert.NoError(t, err)
|
2023-05-23 10:10:15 +00:00
|
|
|
change, err := state.diff(ctx, files)
|
2022-12-22 10:35:32 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, change.delete, 0)
|
2023-06-12 11:44:00 +00:00
|
|
|
assert.Len(t, change.rmdir, 0)
|
|
|
|
assert.Len(t, change.mkdir, 1)
|
2022-12-22 10:35:32 +00:00
|
|
|
assert.Len(t, change.put, 1)
|
2023-06-12 11:44:00 +00:00
|
|
|
assert.Contains(t, change.mkdir, "foo")
|
2022-12-22 10:35:32 +00:00
|
|
|
assert.Contains(t, change.put, "foo/bar.py")
|
|
|
|
|
2023-01-10 12:16:30 +00:00
|
|
|
f1.Remove(t)
|
2024-08-19 15:15:14 +00:00
|
|
|
files, err = fileSet.Files()
|
2022-12-22 10:35:32 +00:00
|
|
|
assert.NoError(t, err)
|
2023-05-23 10:10:15 +00:00
|
|
|
change, err = state.diff(ctx, files)
|
2022-12-22 10:35:32 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, change.delete, 1)
|
2023-06-12 11:44:00 +00:00
|
|
|
assert.Len(t, change.rmdir, 1)
|
|
|
|
assert.Len(t, change.mkdir, 0)
|
2022-12-22 10:35:32 +00:00
|
|
|
assert.Len(t, change.put, 0)
|
|
|
|
assert.Contains(t, change.delete, "foo/bar")
|
2023-06-12 11:44:00 +00:00
|
|
|
assert.Contains(t, change.rmdir, "foo")
|
2022-12-22 10:35:32 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 22:42:09 +00:00
|
|
|
func TestPythonNotebookDiff(t *testing.T) {
|
2023-05-23 10:10:15 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-11-21 22:42:09 +00:00
|
|
|
// Create temp project dir
|
|
|
|
projectDir := t.TempDir()
|
2024-05-30 07:41:50 +00:00
|
|
|
fileSet, err := git.NewFileSet(vfs.MustNew(projectDir))
|
2023-01-27 15:04:58 +00:00
|
|
|
require.NoError(t, err)
|
2022-11-21 22:42:09 +00:00
|
|
|
state := Snapshot{
|
2023-10-03 13:47:46 +00:00
|
|
|
SnapshotState: &SnapshotState{
|
|
|
|
LastModifiedTimes: make(map[string]time.Time),
|
|
|
|
LocalToRemoteNames: make(map[string]string),
|
|
|
|
RemoteToLocalNames: make(map[string]string),
|
|
|
|
},
|
2022-11-21 22:42:09 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 12:16:30 +00:00
|
|
|
foo := testfile.CreateFile(t, filepath.Join(projectDir, "foo.py"))
|
|
|
|
defer foo.Close(t)
|
2022-11-21 22:42:09 +00:00
|
|
|
|
|
|
|
// Case 1: notebook foo.py is uploaded
|
2024-08-19 15:15:14 +00:00
|
|
|
files, err := fileSet.Files()
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.NoError(t, err)
|
2023-01-10 12:16:30 +00:00
|
|
|
foo.Overwrite(t, "# Databricks notebook source\nprint(\"abc\")")
|
2023-05-23 10:10:15 +00:00
|
|
|
change, err := state.diff(ctx, files)
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, change.delete, 0)
|
|
|
|
assert.Len(t, change.put, 1)
|
|
|
|
assert.Contains(t, change.put, "foo.py")
|
2023-10-03 13:47:46 +00:00
|
|
|
assertKeysOfMap(t, state.LastModifiedTimes, []string{"foo.py"})
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.Equal(t, map[string]string{"foo.py": "foo"}, state.LocalToRemoteNames)
|
|
|
|
assert.Equal(t, map[string]string{"foo": "foo.py"}, state.RemoteToLocalNames)
|
|
|
|
|
|
|
|
// Case 2: notebook foo.py is converted to python script by removing
|
|
|
|
// magic keyword
|
2023-01-10 12:16:30 +00:00
|
|
|
foo.Overwrite(t, "print(\"abc\")")
|
2024-08-19 15:15:14 +00:00
|
|
|
files, err = fileSet.Files()
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.NoError(t, err)
|
2023-05-23 10:10:15 +00:00
|
|
|
change, err = state.diff(ctx, files)
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, change.delete, 1)
|
|
|
|
assert.Len(t, change.put, 1)
|
|
|
|
assert.Contains(t, change.put, "foo.py")
|
|
|
|
assert.Contains(t, change.delete, "foo")
|
2023-10-03 13:47:46 +00:00
|
|
|
assertKeysOfMap(t, state.LastModifiedTimes, []string{"foo.py"})
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.Equal(t, map[string]string{"foo.py": "foo.py"}, state.LocalToRemoteNames)
|
|
|
|
assert.Equal(t, map[string]string{"foo.py": "foo.py"}, state.RemoteToLocalNames)
|
|
|
|
|
|
|
|
// Case 3: Python script foo.py is converted to a databricks notebook
|
2023-01-10 12:16:30 +00:00
|
|
|
foo.Overwrite(t, "# Databricks notebook source\nprint(\"def\")")
|
2024-08-19 15:15:14 +00:00
|
|
|
files, err = fileSet.Files()
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.NoError(t, err)
|
2023-05-23 10:10:15 +00:00
|
|
|
change, err = state.diff(ctx, files)
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, change.delete, 1)
|
|
|
|
assert.Len(t, change.put, 1)
|
|
|
|
assert.Contains(t, change.put, "foo.py")
|
|
|
|
assert.Contains(t, change.delete, "foo.py")
|
2023-10-03 13:47:46 +00:00
|
|
|
assertKeysOfMap(t, state.LastModifiedTimes, []string{"foo.py"})
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.Equal(t, map[string]string{"foo.py": "foo"}, state.LocalToRemoteNames)
|
|
|
|
assert.Equal(t, map[string]string{"foo": "foo.py"}, state.RemoteToLocalNames)
|
|
|
|
|
|
|
|
// Case 4: Python notebook foo.py is deleted, and its remote name is used in change.delete
|
2023-01-10 12:16:30 +00:00
|
|
|
foo.Remove(t)
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.NoError(t, err)
|
2024-08-19 15:15:14 +00:00
|
|
|
files, err = fileSet.Files()
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.NoError(t, err)
|
2023-05-23 10:10:15 +00:00
|
|
|
change, err = state.diff(ctx, files)
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, change.delete, 1)
|
|
|
|
assert.Len(t, change.put, 0)
|
|
|
|
assert.Contains(t, change.delete, "foo")
|
2023-10-03 13:47:46 +00:00
|
|
|
assert.Len(t, state.LastModifiedTimes, 0)
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.Equal(t, map[string]string{}, state.LocalToRemoteNames)
|
|
|
|
assert.Equal(t, map[string]string{}, state.RemoteToLocalNames)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorWhenIdenticalRemoteName(t *testing.T) {
|
2023-05-23 10:10:15 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-11-21 22:42:09 +00:00
|
|
|
// Create temp project dir
|
|
|
|
projectDir := t.TempDir()
|
2024-05-30 07:41:50 +00:00
|
|
|
fileSet, err := git.NewFileSet(vfs.MustNew(projectDir))
|
2023-01-27 15:04:58 +00:00
|
|
|
require.NoError(t, err)
|
2022-11-21 22:42:09 +00:00
|
|
|
state := Snapshot{
|
2023-10-03 13:47:46 +00:00
|
|
|
SnapshotState: &SnapshotState{
|
|
|
|
LastModifiedTimes: make(map[string]time.Time),
|
|
|
|
LocalToRemoteNames: make(map[string]string),
|
|
|
|
RemoteToLocalNames: make(map[string]string),
|
|
|
|
},
|
2022-11-21 22:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// upload should work since they point to different destinations
|
2023-01-10 12:16:30 +00:00
|
|
|
pythonFoo := testfile.CreateFile(t, filepath.Join(projectDir, "foo.py"))
|
|
|
|
defer pythonFoo.Close(t)
|
|
|
|
vanillaFoo := testfile.CreateFile(t, filepath.Join(projectDir, "foo"))
|
|
|
|
defer vanillaFoo.Close(t)
|
2024-08-19 15:15:14 +00:00
|
|
|
files, err := fileSet.Files()
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.NoError(t, err)
|
2023-05-23 10:10:15 +00:00
|
|
|
change, err := state.diff(ctx, files)
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, change.delete, 0)
|
|
|
|
assert.Len(t, change.put, 2)
|
|
|
|
assert.Contains(t, change.put, "foo.py")
|
|
|
|
assert.Contains(t, change.put, "foo")
|
|
|
|
|
|
|
|
// errors out because they point to the same destination
|
2023-01-10 12:16:30 +00:00
|
|
|
pythonFoo.Overwrite(t, "# Databricks notebook source\nprint(\"def\")")
|
2024-08-19 15:15:14 +00:00
|
|
|
files, err = fileSet.Files()
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.NoError(t, err)
|
2023-05-23 10:10:15 +00:00
|
|
|
change, err = state.diff(ctx, files)
|
2022-11-21 22:42:09 +00:00
|
|
|
assert.ErrorContains(t, err, "both foo and foo.py point to the same remote file location foo. Please remove one of them from your local project")
|
2022-09-14 15:50:29 +00:00
|
|
|
}
|
2022-12-12 13:31:06 +00:00
|
|
|
|
2023-03-10 10:52:45 +00:00
|
|
|
func TestNoErrorRenameWithIdenticalRemoteName(t *testing.T) {
|
2023-05-23 10:10:15 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-03-10 10:52:45 +00:00
|
|
|
// Create temp project dir
|
|
|
|
projectDir := t.TempDir()
|
2024-05-30 07:41:50 +00:00
|
|
|
fileSet, err := git.NewFileSet(vfs.MustNew(projectDir))
|
2023-03-10 10:52:45 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
state := Snapshot{
|
2023-10-03 13:47:46 +00:00
|
|
|
SnapshotState: &SnapshotState{
|
|
|
|
LastModifiedTimes: make(map[string]time.Time),
|
|
|
|
LocalToRemoteNames: make(map[string]string),
|
|
|
|
RemoteToLocalNames: make(map[string]string),
|
|
|
|
},
|
2023-03-10 10:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// upload should work since they point to different destinations
|
|
|
|
pythonFoo := testfile.CreateFile(t, filepath.Join(projectDir, "foo.py"))
|
|
|
|
defer pythonFoo.Close(t)
|
|
|
|
pythonFoo.Overwrite(t, "# Databricks notebook source\n")
|
2024-08-19 15:15:14 +00:00
|
|
|
files, err := fileSet.Files()
|
2023-03-10 10:52:45 +00:00
|
|
|
assert.NoError(t, err)
|
2023-05-23 10:10:15 +00:00
|
|
|
change, err := state.diff(ctx, files)
|
2023-03-10 10:52:45 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, change.delete, 0)
|
|
|
|
assert.Len(t, change.put, 1)
|
|
|
|
assert.Contains(t, change.put, "foo.py")
|
|
|
|
|
|
|
|
pythonFoo.Remove(t)
|
|
|
|
sqlFoo := testfile.CreateFile(t, filepath.Join(projectDir, "foo.sql"))
|
|
|
|
defer sqlFoo.Close(t)
|
|
|
|
sqlFoo.Overwrite(t, "-- Databricks notebook source\n")
|
2024-08-19 15:15:14 +00:00
|
|
|
files, err = fileSet.Files()
|
2023-03-10 10:52:45 +00:00
|
|
|
assert.NoError(t, err)
|
2023-05-23 10:10:15 +00:00
|
|
|
change, err = state.diff(ctx, files)
|
2023-03-10 10:52:45 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, change.delete, 1)
|
|
|
|
assert.Len(t, change.put, 1)
|
|
|
|
assert.Contains(t, change.put, "foo.sql")
|
|
|
|
assert.Contains(t, change.delete, "foo")
|
|
|
|
}
|
|
|
|
|
2023-01-24 07:30:10 +00:00
|
|
|
func defaultOptions(t *testing.T) *SyncOptions {
|
|
|
|
return &SyncOptions{
|
|
|
|
Host: "www.foobar.com",
|
|
|
|
RemotePath: "/Repos/foo/bar",
|
|
|
|
SnapshotBasePath: t.TempDir(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-12 13:31:06 +00:00
|
|
|
func TestNewSnapshotDefaults(t *testing.T) {
|
2023-01-24 07:30:10 +00:00
|
|
|
opts := defaultOptions(t)
|
2023-03-17 14:17:31 +00:00
|
|
|
snapshot, err := newSnapshot(context.Background(), opts)
|
2023-01-24 07:30:10 +00:00
|
|
|
require.NoError(t, err)
|
2022-12-12 13:31:06 +00:00
|
|
|
|
|
|
|
assert.Equal(t, LatestSnapshotVersion, snapshot.Version)
|
2023-01-24 07:30:10 +00:00
|
|
|
assert.Equal(t, opts.RemotePath, snapshot.RemotePath)
|
|
|
|
assert.Equal(t, opts.Host, snapshot.Host)
|
2023-10-03 13:47:46 +00:00
|
|
|
assert.Empty(t, snapshot.LastModifiedTimes)
|
2022-12-12 13:31:06 +00:00
|
|
|
assert.Empty(t, snapshot.RemoteToLocalNames)
|
|
|
|
assert.Empty(t, snapshot.LocalToRemoteNames)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOldSnapshotInvalidation(t *testing.T) {
|
|
|
|
oldVersionSnapshot := `{
|
|
|
|
"version": "v0",
|
|
|
|
"host": "www.foobar.com",
|
|
|
|
"remote_path": "/Repos/foo/bar",
|
|
|
|
"last_modified_times": {},
|
|
|
|
"local_to_remote_names": {},
|
|
|
|
"remote_to_local_names": {}
|
|
|
|
}`
|
|
|
|
|
2023-01-24 07:30:10 +00:00
|
|
|
opts := defaultOptions(t)
|
|
|
|
snapshotPath, err := SnapshotPath(opts)
|
|
|
|
require.NoError(t, err)
|
2023-01-10 12:16:30 +00:00
|
|
|
snapshotFile := testfile.CreateFile(t, snapshotPath)
|
|
|
|
snapshotFile.Overwrite(t, oldVersionSnapshot)
|
|
|
|
snapshotFile.Close(t)
|
2022-12-12 13:31:06 +00:00
|
|
|
|
|
|
|
// assert snapshot did not get loaded
|
2023-03-17 14:17:31 +00:00
|
|
|
snapshot, err := loadOrNewSnapshot(context.Background(), opts)
|
2023-01-24 07:30:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, snapshot.New)
|
2022-12-12 13:31:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoVersionSnapshotInvalidation(t *testing.T) {
|
|
|
|
noVersionSnapshot := `{
|
|
|
|
"host": "www.foobar.com",
|
|
|
|
"remote_path": "/Repos/foo/bar",
|
|
|
|
"last_modified_times": {},
|
|
|
|
"local_to_remote_names": {},
|
|
|
|
"remote_to_local_names": {}
|
|
|
|
}`
|
|
|
|
|
2023-01-24 07:30:10 +00:00
|
|
|
opts := defaultOptions(t)
|
|
|
|
snapshotPath, err := SnapshotPath(opts)
|
|
|
|
require.NoError(t, err)
|
2023-01-10 12:16:30 +00:00
|
|
|
snapshotFile := testfile.CreateFile(t, snapshotPath)
|
|
|
|
snapshotFile.Overwrite(t, noVersionSnapshot)
|
|
|
|
snapshotFile.Close(t)
|
2022-12-12 13:31:06 +00:00
|
|
|
|
|
|
|
// assert snapshot did not get loaded
|
2023-03-17 14:17:31 +00:00
|
|
|
snapshot, err := loadOrNewSnapshot(context.Background(), opts)
|
2023-01-24 07:30:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, snapshot.New)
|
2022-12-12 13:31:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLatestVersionSnapshotGetsLoaded(t *testing.T) {
|
|
|
|
latestVersionSnapshot := fmt.Sprintf(`{
|
|
|
|
"version": "%s",
|
|
|
|
"host": "www.foobar.com",
|
|
|
|
"remote_path": "/Repos/foo/bar",
|
|
|
|
"last_modified_times": {},
|
|
|
|
"local_to_remote_names": {},
|
|
|
|
"remote_to_local_names": {}
|
|
|
|
}`, LatestSnapshotVersion)
|
|
|
|
|
2023-01-24 07:30:10 +00:00
|
|
|
opts := defaultOptions(t)
|
|
|
|
snapshotPath, err := SnapshotPath(opts)
|
|
|
|
require.NoError(t, err)
|
2023-01-10 12:16:30 +00:00
|
|
|
snapshotFile := testfile.CreateFile(t, snapshotPath)
|
|
|
|
snapshotFile.Overwrite(t, latestVersionSnapshot)
|
|
|
|
snapshotFile.Close(t)
|
2022-12-12 13:31:06 +00:00
|
|
|
|
|
|
|
// assert snapshot gets loaded
|
2023-03-17 14:17:31 +00:00
|
|
|
snapshot, err := loadOrNewSnapshot(context.Background(), opts)
|
2023-01-24 07:30:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.False(t, snapshot.New)
|
2022-12-12 13:31:06 +00:00
|
|
|
assert.Equal(t, LatestSnapshotVersion, snapshot.Version)
|
|
|
|
assert.Equal(t, "www.foobar.com", snapshot.Host)
|
|
|
|
assert.Equal(t, "/Repos/foo/bar", snapshot.RemotePath)
|
|
|
|
}
|