2017-09-01 16:21:46 +02:00
|
|
|
// Internal tests for march
|
2017-06-30 22:24:13 +02:00
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
package march
|
2017-06-30 22:24:13 +02:00
|
|
|
|
|
|
|
import (
|
2019-06-09 16:57:05 +02:00
|
|
|
"fmt"
|
2017-09-08 17:19:41 +02:00
|
|
|
"strings"
|
2017-06-30 22:24:13 +02:00
|
|
|
"testing"
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
"github.com/ncw/rclone/fs"
|
2019-06-09 16:57:05 +02:00
|
|
|
"github.com/ncw/rclone/fstest/mockdir"
|
2018-01-12 17:30:54 +01:00
|
|
|
"github.com/ncw/rclone/fstest/mockobject"
|
2017-06-30 22:24:13 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2017-09-08 17:19:41 +02:00
|
|
|
func TestNewMatchEntries(t *testing.T) {
|
|
|
|
var (
|
2018-01-12 17:30:54 +01:00
|
|
|
a = mockobject.Object("path/a")
|
|
|
|
A = mockobject.Object("path/A")
|
|
|
|
B = mockobject.Object("path/B")
|
|
|
|
c = mockobject.Object("path/c")
|
2017-09-08 17:19:41 +02:00
|
|
|
)
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
es := newMatchEntries(fs.DirEntries{a, A, B, c}, nil)
|
2017-09-08 17:19:41 +02:00
|
|
|
assert.Equal(t, es, matchEntries{
|
|
|
|
{name: "A", leaf: "A", entry: A},
|
|
|
|
{name: "B", leaf: "B", entry: B},
|
|
|
|
{name: "a", leaf: "a", entry: a},
|
|
|
|
{name: "c", leaf: "c", entry: c},
|
|
|
|
})
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
es = newMatchEntries(fs.DirEntries{a, A, B, c}, []matchTransformFn{strings.ToLower})
|
2017-09-08 17:19:41 +02:00
|
|
|
assert.Equal(t, es, matchEntries{
|
|
|
|
{name: "a", leaf: "A", entry: A},
|
|
|
|
{name: "a", leaf: "a", entry: a},
|
|
|
|
{name: "b", leaf: "B", entry: B},
|
|
|
|
{name: "c", leaf: "c", entry: c},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-30 22:24:13 +02:00
|
|
|
func TestMatchListings(t *testing.T) {
|
|
|
|
var (
|
2019-06-09 16:57:05 +02:00
|
|
|
a = mockobject.Object("a")
|
|
|
|
A = mockobject.Object("A")
|
|
|
|
b = mockobject.Object("b")
|
|
|
|
c = mockobject.Object("c")
|
|
|
|
d = mockobject.Object("d")
|
|
|
|
dirA = mockdir.New("A")
|
|
|
|
dirb = mockdir.New("b")
|
2017-06-30 22:24:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
for _, test := range []struct {
|
2017-09-08 17:19:41 +02:00
|
|
|
what string
|
2018-01-12 17:30:54 +01:00
|
|
|
input fs.DirEntries // pairs of input src, dst
|
|
|
|
srcOnly fs.DirEntries
|
|
|
|
dstOnly fs.DirEntries
|
2017-09-08 17:19:41 +02:00
|
|
|
matches []matchPair // pairs of output
|
|
|
|
transforms []matchTransformFn
|
2017-06-30 22:24:13 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
what: "only src or dst",
|
2018-01-12 17:30:54 +01:00
|
|
|
input: fs.DirEntries{
|
2017-06-30 22:24:13 +02:00
|
|
|
a, nil,
|
|
|
|
b, nil,
|
|
|
|
c, nil,
|
|
|
|
d, nil,
|
|
|
|
},
|
2018-01-12 17:30:54 +01:00
|
|
|
srcOnly: fs.DirEntries{
|
2017-06-30 22:24:13 +02:00
|
|
|
a, b, c, d,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
what: "typical sync #1",
|
2018-01-12 17:30:54 +01:00
|
|
|
input: fs.DirEntries{
|
2017-06-30 22:24:13 +02:00
|
|
|
a, nil,
|
|
|
|
b, b,
|
|
|
|
nil, c,
|
|
|
|
nil, d,
|
|
|
|
},
|
2018-01-12 17:30:54 +01:00
|
|
|
srcOnly: fs.DirEntries{
|
2017-06-30 22:24:13 +02:00
|
|
|
a,
|
|
|
|
},
|
2018-01-12 17:30:54 +01:00
|
|
|
dstOnly: fs.DirEntries{
|
2017-06-30 22:24:13 +02:00
|
|
|
c, d,
|
|
|
|
},
|
|
|
|
matches: []matchPair{
|
|
|
|
{b, b},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
what: "typical sync #2",
|
2018-01-12 17:30:54 +01:00
|
|
|
input: fs.DirEntries{
|
2017-06-30 22:24:13 +02:00
|
|
|
a, a,
|
|
|
|
b, b,
|
|
|
|
nil, c,
|
|
|
|
d, d,
|
|
|
|
},
|
2018-01-12 17:30:54 +01:00
|
|
|
dstOnly: fs.DirEntries{
|
2017-06-30 22:24:13 +02:00
|
|
|
c,
|
|
|
|
},
|
|
|
|
matches: []matchPair{
|
|
|
|
{a, a},
|
|
|
|
{b, b},
|
|
|
|
{d, d},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
what: "One duplicate",
|
2018-01-12 17:30:54 +01:00
|
|
|
input: fs.DirEntries{
|
2017-10-02 17:52:53 +02:00
|
|
|
A, A,
|
2017-06-30 22:24:13 +02:00
|
|
|
a, a,
|
|
|
|
a, nil,
|
2017-10-02 17:52:53 +02:00
|
|
|
b, b,
|
2017-06-30 22:24:13 +02:00
|
|
|
},
|
|
|
|
matches: []matchPair{
|
2017-10-02 17:52:53 +02:00
|
|
|
{A, A},
|
2017-06-30 22:24:13 +02:00
|
|
|
{a, a},
|
2017-10-02 17:52:53 +02:00
|
|
|
{b, b},
|
2017-06-30 22:24:13 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
what: "Two duplicates",
|
2018-01-12 17:30:54 +01:00
|
|
|
input: fs.DirEntries{
|
2017-06-30 22:24:13 +02:00
|
|
|
a, a,
|
|
|
|
a, a,
|
|
|
|
a, nil,
|
|
|
|
},
|
|
|
|
matches: []matchPair{
|
|
|
|
{a, a},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-09-08 17:19:41 +02:00
|
|
|
what: "Case insensitive duplicate - no transform",
|
2018-01-12 17:30:54 +01:00
|
|
|
input: fs.DirEntries{
|
2017-09-08 17:19:41 +02:00
|
|
|
a, a,
|
|
|
|
A, A,
|
|
|
|
},
|
|
|
|
matches: []matchPair{
|
|
|
|
{A, A},
|
|
|
|
{a, a},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
what: "Case insensitive duplicate - transform to lower case",
|
2018-01-12 17:30:54 +01:00
|
|
|
input: fs.DirEntries{
|
2017-09-08 17:19:41 +02:00
|
|
|
a, a,
|
|
|
|
A, A,
|
|
|
|
},
|
|
|
|
matches: []matchPair{
|
|
|
|
{A, A},
|
|
|
|
},
|
|
|
|
transforms: []matchTransformFn{strings.ToLower},
|
|
|
|
},
|
2019-06-09 16:57:05 +02:00
|
|
|
{
|
|
|
|
what: "File and directory are not duplicates - srcOnly",
|
|
|
|
input: fs.DirEntries{
|
|
|
|
dirA, nil,
|
|
|
|
A, nil,
|
|
|
|
},
|
|
|
|
srcOnly: fs.DirEntries{
|
|
|
|
dirA,
|
|
|
|
A,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
what: "File and directory are not duplicates - matches",
|
|
|
|
input: fs.DirEntries{
|
|
|
|
dirA, dirA,
|
|
|
|
A, A,
|
|
|
|
},
|
|
|
|
matches: []matchPair{
|
|
|
|
{dirA, dirA},
|
|
|
|
{A, A},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
what: "Sync with directory #1",
|
|
|
|
input: fs.DirEntries{
|
|
|
|
dirA, nil,
|
|
|
|
A, nil,
|
|
|
|
b, b,
|
|
|
|
nil, c,
|
|
|
|
nil, d,
|
|
|
|
},
|
|
|
|
srcOnly: fs.DirEntries{
|
|
|
|
dirA,
|
|
|
|
A,
|
|
|
|
},
|
|
|
|
dstOnly: fs.DirEntries{
|
|
|
|
c, d,
|
|
|
|
},
|
|
|
|
matches: []matchPair{
|
|
|
|
{b, b},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
what: "Sync with 2 directories",
|
|
|
|
input: fs.DirEntries{
|
|
|
|
dirA, dirA,
|
|
|
|
A, nil,
|
|
|
|
nil, dirb,
|
|
|
|
nil, b,
|
|
|
|
},
|
|
|
|
srcOnly: fs.DirEntries{
|
|
|
|
A,
|
|
|
|
},
|
|
|
|
dstOnly: fs.DirEntries{
|
|
|
|
dirb,
|
|
|
|
b,
|
|
|
|
},
|
|
|
|
matches: []matchPair{
|
|
|
|
{dirA, dirA},
|
|
|
|
},
|
|
|
|
},
|
2017-06-30 22:24:13 +02:00
|
|
|
} {
|
2019-06-09 16:57:05 +02:00
|
|
|
t.Run(fmt.Sprintf("TestMatchListings-%s", test.what), func(t *testing.T) {
|
|
|
|
var srcList, dstList fs.DirEntries
|
|
|
|
for i := 0; i < len(test.input); i += 2 {
|
|
|
|
src, dst := test.input[i], test.input[i+1]
|
|
|
|
if src != nil {
|
|
|
|
srcList = append(srcList, src)
|
|
|
|
}
|
|
|
|
if dst != nil {
|
|
|
|
dstList = append(dstList, dst)
|
|
|
|
}
|
2017-06-30 22:24:13 +02:00
|
|
|
}
|
2019-06-09 16:57:05 +02:00
|
|
|
srcOnly, dstOnly, matches := matchListings(srcList, dstList, test.transforms)
|
|
|
|
assert.Equal(t, test.srcOnly, srcOnly, test.what, "srcOnly differ")
|
|
|
|
assert.Equal(t, test.dstOnly, dstOnly, test.what, "dstOnly differ")
|
|
|
|
assert.Equal(t, test.matches, matches, test.what, "matches differ")
|
|
|
|
// now swap src and dst
|
|
|
|
dstOnly, srcOnly, matches = matchListings(dstList, srcList, test.transforms)
|
|
|
|
assert.Equal(t, test.srcOnly, srcOnly, test.what, "srcOnly differ")
|
|
|
|
assert.Equal(t, test.dstOnly, dstOnly, test.what, "dstOnly differ")
|
|
|
|
assert.Equal(t, test.matches, matches, test.what, "matches differ")
|
|
|
|
})
|
2017-06-30 22:24:13 +02:00
|
|
|
}
|
|
|
|
}
|