2022-09-14 15:50:29 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2022-09-15 13:40:47 +00:00
|
|
|
"time"
|
2022-09-14 15:50:29 +00:00
|
|
|
|
|
|
|
"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)
|
2022-09-14 15:50:29 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer f2.Close()
|
|
|
|
|
|
|
|
fileSet := git.NewFileSet(projectDir)
|
|
|
|
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))
|
|
|
|
|
2022-09-14 15:50:29 +00:00
|
|
|
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")
|
|
|
|
}
|