Implement the core logic for comparing two archives (diff) and performing incremental updates (sync). - Introduces a new `borg diff` command to show differences between two collection archives. - Adds new `pkg/diff` and `pkg/sync` packages with corresponding business logic and unit tests. - The `diff` command supports reading compressed archives and prints a formatted summary of added, removed, and modified files. - The `sync` package includes `append`, `mirror`, and `update` strategies. Next steps involve integrating the sync logic into the `collect` commands. Co-authored-by: Snider <631881+Snider@users.noreply.github.com>
79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
package sync
|
|
|
|
import (
|
|
"io/fs"
|
|
"reflect"
|
|
"sort"
|
|
"testing"
|
|
|
|
"github.com/Snider/Borg/pkg/datanode"
|
|
)
|
|
|
|
func TestSync_Append(t *testing.T) {
|
|
a := datanode.New()
|
|
a.AddData("file1.txt", []byte("hello"))
|
|
a.AddData("file2.txt", []byte("world"))
|
|
|
|
b := datanode.New()
|
|
b.AddData("file1.txt", []byte("different"))
|
|
b.AddData("file3.txt", []byte("new"))
|
|
|
|
result, err := Sync(a, b, AppendStrategy)
|
|
if err != nil {
|
|
t.Fatalf("Sync() error = %v", err)
|
|
}
|
|
|
|
expectedFiles := []string{"file1.txt", "file2.txt", "file3.txt"}
|
|
assertDataNodeFiles(t, result, expectedFiles)
|
|
}
|
|
|
|
func TestSync_Mirror(t *testing.T) {
|
|
a := datanode.New()
|
|
a.AddData("file1.txt", []byte("hello"))
|
|
a.AddData("file2.txt", []byte("world"))
|
|
|
|
b := datanode.New()
|
|
b.AddData("file3.txt", []byte("new"))
|
|
|
|
result, err := Sync(a, b, MirrorStrategy)
|
|
if err != nil {
|
|
t.Fatalf("Sync() error = %v", err)
|
|
}
|
|
|
|
expectedFiles := []string{"file3.txt"}
|
|
assertDataNodeFiles(t, result, expectedFiles)
|
|
}
|
|
|
|
func TestSync_Update(t *testing.T) {
|
|
a := datanode.New()
|
|
a.AddData("file1.txt", []byte("hello"))
|
|
a.AddData("file2.txt", []byte("world"))
|
|
|
|
b := datanode.New()
|
|
b.AddData("file1.txt", []byte("updated"))
|
|
b.AddData("file3.txt", []byte("new"))
|
|
|
|
result, err := Sync(a, b, UpdateStrategy)
|
|
if err != nil {
|
|
t.Fatalf("Sync() error = %v", err)
|
|
}
|
|
|
|
expectedFiles := []string{"file1.txt", "file2.txt", "file3.txt"}
|
|
assertDataNodeFiles(t, result, expectedFiles)
|
|
}
|
|
|
|
func assertDataNodeFiles(t *testing.T, dn *datanode.DataNode, expected []string) {
|
|
t.Helper()
|
|
var actual []string
|
|
dn.Walk(".", func(path string, d fs.DirEntry, err error) error {
|
|
if !d.IsDir() {
|
|
actual = append(actual, path)
|
|
}
|
|
return nil
|
|
})
|
|
sort.Strings(actual)
|
|
sort.Strings(expected)
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
t.Errorf("Expected files %v, got %v", expected, actual)
|
|
}
|
|
}
|