databricks-cli/cmd/sync/snapshot_test.go

65 lines
1.7 KiB
Go
Raw Permalink Normal View History

package sync
import (
"os"
"path/filepath"
"testing"
2022-09-15 13:40:47 +00:00
"time"
"github.com/databricks/bricks/git"
"github.com/stretchr/testify/assert"
)
func TestDiff(t *testing.T) {
// Create temp project dir
projectDir := t.TempDir()
f1, err := os.Create(filepath.Join(projectDir, "hello.txt"))
assert.NoError(t, err)
defer f1.Close()
2022-09-15 13:40:47 +00:00
worldFilePath := filepath.Join(projectDir, "world.txt")
f2, err := os.Create(worldFilePath)
assert.NoError(t, err)
defer f2.Close()
2022-09-23 10:10:46 +00:00
fileSet := git.NewFileSet(projectDir, nil)
files, err := fileSet.All()
assert.NoError(t, err)
state := snapshot{}
change := state.diff(files)
// New files are added to put
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")
2022-09-15 13:40:47 +00:00
// Edited files are added to put.
// File system in the github actions env does not update
// mtime on writes to a file. So we are manually editting it
// instead of writing to world.txt
worldFileInfo, err := os.Stat(worldFilePath)
assert.NoError(t, err)
os.Chtimes(worldFilePath,
worldFileInfo.ModTime().Add(time.Nanosecond),
worldFileInfo.ModTime().Add(time.Nanosecond))
assert.NoError(t, err)
files, err = fileSet.All()
assert.NoError(t, err)
change = state.diff(files)
assert.Len(t, change.delete, 0)
assert.Len(t, change.put, 1)
assert.Contains(t, change.put, "world.txt")
// Removed files are added to delete
err = os.Remove(filepath.Join(projectDir, "hello.txt"))
assert.NoError(t, err)
files, err = fileSet.All()
assert.NoError(t, err)
change = state.diff(files)
assert.Len(t, change.delete, 1)
assert.Len(t, change.put, 0)
assert.Contains(t, change.delete, "hello.txt")
}