mirror of https://github.com/mickael-menu/zk
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
310 lines
4.6 KiB
Go
310 lines
4.6 KiB
Go
4 years ago
|
package paths
|
||
4 years ago
|
|
||
|
import (
|
||
4 years ago
|
"errors"
|
||
4 years ago
|
"testing"
|
||
|
"time"
|
||
|
|
||
4 years ago
|
"github.com/mickael-menu/zk/internal/util/test/assert"
|
||
4 years ago
|
)
|
||
|
|
||
|
var date1 = time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
|
||
|
var date2 = time.Date(2012, 10, 20, 12, 34, 58, 651387237, time.UTC)
|
||
|
var date3 = time.Date(2014, 12, 10, 3, 34, 58, 651387237, time.UTC)
|
||
|
var date4 = time.Date(2016, 13, 11, 4, 34, 58, 651387237, time.UTC)
|
||
|
|
||
|
func TestDiffEmpty(t *testing.T) {
|
||
4 years ago
|
source := []Metadata{}
|
||
|
target := []Metadata{}
|
||
4 years ago
|
test(t, source, target, false, []DiffChange{})
|
||
4 years ago
|
}
|
||
|
|
||
|
func TestNoDiff(t *testing.T) {
|
||
4 years ago
|
files := []Metadata{
|
||
4 years ago
|
{
|
||
4 years ago
|
Path: "a/1",
|
||
4 years ago
|
Modified: date1,
|
||
|
},
|
||
|
{
|
||
4 years ago
|
Path: "a/2",
|
||
4 years ago
|
Modified: date2,
|
||
|
},
|
||
|
{
|
||
4 years ago
|
Path: "b/1",
|
||
4 years ago
|
Modified: date3,
|
||
|
},
|
||
|
}
|
||
|
|
||
4 years ago
|
test(t, files, files, false, []DiffChange{})
|
||
4 years ago
|
}
|
||
|
|
||
|
func TestDiff(t *testing.T) {
|
||
4 years ago
|
source := []Metadata{
|
||
4 years ago
|
{
|
||
4 years ago
|
Path: "a/1",
|
||
4 years ago
|
Modified: date1,
|
||
|
},
|
||
|
{
|
||
4 years ago
|
Path: "a/2",
|
||
4 years ago
|
Modified: date2,
|
||
|
},
|
||
|
{
|
||
4 years ago
|
Path: "b/1",
|
||
4 years ago
|
Modified: date3,
|
||
|
},
|
||
|
}
|
||
|
|
||
4 years ago
|
target := []Metadata{
|
||
4 years ago
|
{
|
||
|
// Date changed
|
||
4 years ago
|
Path: "a/1",
|
||
4 years ago
|
Modified: date1.Add(time.Hour),
|
||
|
},
|
||
|
// 2 is added
|
||
|
{
|
||
|
// 3 is removed
|
||
4 years ago
|
Path: "a/3",
|
||
4 years ago
|
Modified: date3,
|
||
|
},
|
||
|
{
|
||
|
// No change
|
||
4 years ago
|
Path: "b/1",
|
||
4 years ago
|
Modified: date3,
|
||
|
},
|
||
|
}
|
||
|
|
||
4 years ago
|
test(t, source, target, false, []DiffChange{
|
||
4 years ago
|
{
|
||
4 years ago
|
Path: "a/1",
|
||
4 years ago
|
Kind: DiffModified,
|
||
|
},
|
||
|
{
|
||
4 years ago
|
Path: "a/2",
|
||
4 years ago
|
Kind: DiffAdded,
|
||
|
},
|
||
|
{
|
||
4 years ago
|
Path: "a/3",
|
||
4 years ago
|
Kind: DiffRemoved,
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
|
||
4 years ago
|
func TestDiffForceModified(t *testing.T) {
|
||
|
source := []Metadata{
|
||
|
{
|
||
|
Path: "a/1",
|
||
|
Modified: date1,
|
||
|
},
|
||
|
{
|
||
|
Path: "a/2",
|
||
|
Modified: date2,
|
||
|
},
|
||
|
{
|
||
|
Path: "b/1",
|
||
|
Modified: date3,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
target := []Metadata{
|
||
|
{
|
||
|
// Date changed
|
||
|
Path: "a/1",
|
||
|
Modified: date1.Add(time.Hour),
|
||
|
},
|
||
|
// 2 is added
|
||
|
{
|
||
|
// 3 is removed
|
||
|
Path: "a/3",
|
||
|
Modified: date3,
|
||
|
},
|
||
|
{
|
||
|
// No change
|
||
|
Path: "b/1",
|
||
|
Modified: date3,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
test(t, source, target, true, []DiffChange{
|
||
|
{
|
||
|
Path: "a/1",
|
||
|
Kind: DiffModified,
|
||
|
},
|
||
|
{
|
||
|
Path: "a/2",
|
||
|
Kind: DiffAdded,
|
||
|
},
|
||
|
{
|
||
|
Path: "a/3",
|
||
|
Kind: DiffRemoved,
|
||
|
},
|
||
|
{
|
||
|
// Forced modified
|
||
|
Path: "b/1",
|
||
|
Kind: DiffModified,
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
|
||
4 years ago
|
func TestDiffWithMoreInSource(t *testing.T) {
|
||
4 years ago
|
source := []Metadata{
|
||
4 years ago
|
{
|
||
4 years ago
|
Path: "a/1",
|
||
4 years ago
|
Modified: date1,
|
||
|
},
|
||
|
{
|
||
4 years ago
|
Path: "a/2",
|
||
4 years ago
|
Modified: date2,
|
||
|
},
|
||
|
}
|
||
|
|
||
4 years ago
|
target := []Metadata{
|
||
4 years ago
|
{
|
||
4 years ago
|
Path: "a/1",
|
||
4 years ago
|
Modified: date1,
|
||
|
},
|
||
|
}
|
||
|
|
||
4 years ago
|
test(t, source, target, false, []DiffChange{
|
||
4 years ago
|
{
|
||
4 years ago
|
Path: "a/2",
|
||
4 years ago
|
Kind: DiffAdded,
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func TestDiffWithMoreInTarget(t *testing.T) {
|
||
4 years ago
|
source := []Metadata{
|
||
4 years ago
|
{
|
||
4 years ago
|
Path: "a/1",
|
||
4 years ago
|
Modified: date1,
|
||
|
},
|
||
|
}
|
||
|
|
||
4 years ago
|
target := []Metadata{
|
||
4 years ago
|
{
|
||
4 years ago
|
Path: "a/1",
|
||
4 years ago
|
Modified: date1,
|
||
|
},
|
||
|
{
|
||
4 years ago
|
Path: "a/2",
|
||
4 years ago
|
Modified: date2,
|
||
|
},
|
||
|
}
|
||
|
|
||
4 years ago
|
test(t, source, target, false, []DiffChange{
|
||
4 years ago
|
{
|
||
4 years ago
|
Path: "a/2",
|
||
4 years ago
|
Kind: DiffRemoved,
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func TestDiffEmptySource(t *testing.T) {
|
||
4 years ago
|
source := []Metadata{}
|
||
4 years ago
|
|
||
4 years ago
|
target := []Metadata{
|
||
4 years ago
|
{
|
||
4 years ago
|
Path: "a/1",
|
||
4 years ago
|
Modified: date1,
|
||
|
},
|
||
|
{
|
||
4 years ago
|
Path: "a/2",
|
||
4 years ago
|
Modified: date2,
|
||
|
},
|
||
|
}
|
||
|
|
||
4 years ago
|
test(t, source, target, false, []DiffChange{
|
||
4 years ago
|
{
|
||
4 years ago
|
Path: "a/1",
|
||
4 years ago
|
Kind: DiffRemoved,
|
||
|
},
|
||
|
{
|
||
4 years ago
|
Path: "a/2",
|
||
4 years ago
|
Kind: DiffRemoved,
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func TestDiffEmptyTarget(t *testing.T) {
|
||
4 years ago
|
source := []Metadata{
|
||
4 years ago
|
{
|
||
4 years ago
|
Path: "a/1",
|
||
4 years ago
|
Modified: date1,
|
||
|
},
|
||
|
{
|
||
4 years ago
|
Path: "a/2",
|
||
4 years ago
|
Modified: date2,
|
||
|
},
|
||
|
}
|
||
|
|
||
4 years ago
|
target := []Metadata{}
|
||
4 years ago
|
|
||
4 years ago
|
test(t, source, target, false, []DiffChange{
|
||
4 years ago
|
{
|
||
4 years ago
|
Path: "a/1",
|
||
4 years ago
|
Kind: DiffAdded,
|
||
|
},
|
||
|
{
|
||
4 years ago
|
Path: "a/2",
|
||
4 years ago
|
Kind: DiffAdded,
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
|
||
4 years ago
|
func TestDiffCancellation(t *testing.T) {
|
||
4 years ago
|
source := []Metadata{
|
||
4 years ago
|
{
|
||
4 years ago
|
Path: "a/1",
|
||
4 years ago
|
Modified: date1,
|
||
|
},
|
||
|
{
|
||
4 years ago
|
Path: "a/2",
|
||
4 years ago
|
Modified: date2,
|
||
|
},
|
||
|
}
|
||
|
|
||
4 years ago
|
target := []Metadata{}
|
||
4 years ago
|
|
||
|
received := make([]DiffChange, 0)
|
||
4 years ago
|
_, err := Diff(toChannel(source), toChannel(target), false, func(change DiffChange) error {
|
||
4 years ago
|
received = append(received, change)
|
||
|
|
||
|
if len(received) == 1 {
|
||
|
return errors.New("cancelled")
|
||
|
} else {
|
||
|
return nil
|
||
|
}
|
||
|
})
|
||
|
|
||
|
assert.Equal(t, received, []DiffChange{
|
||
|
{
|
||
4 years ago
|
Path: "a/1",
|
||
4 years ago
|
Kind: DiffAdded,
|
||
|
},
|
||
|
})
|
||
|
assert.Err(t, err, "cancelled")
|
||
|
}
|
||
|
|
||
4 years ago
|
func test(t *testing.T, source, target []Metadata, forceModified bool, expected []DiffChange) {
|
||
4 years ago
|
expectedCount := len(source)
|
||
4 years ago
|
received := make([]DiffChange, 0)
|
||
4 years ago
|
actualCount, err := Diff(toChannel(source), toChannel(target), forceModified, func(change DiffChange) error {
|
||
4 years ago
|
received = append(received, change)
|
||
|
return nil
|
||
|
})
|
||
|
assert.Nil(t, err)
|
||
4 years ago
|
assert.Equal(t, actualCount, expectedCount)
|
||
4 years ago
|
assert.Equal(t, received, expected)
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
func toChannel(fm []Metadata) <-chan Metadata {
|
||
|
c := make(chan Metadata)
|
||
4 years ago
|
go func() {
|
||
|
for _, m := range fm {
|
||
|
c <- m
|
||
|
}
|
||
|
close(c)
|
||
|
}()
|
||
|
return c
|
||
|
}
|