mirror of
https://github.com/octoleo/syncthing.git
synced 2025-01-22 14:48:30 +00:00
Test cases for ignore #include
This commit is contained in:
parent
8c42aea827
commit
7c604beb73
0
scanner/testdata/.foo/bar
vendored
0
scanner/testdata/.foo/bar
vendored
8
scanner/testdata/.stignore
vendored
8
scanner/testdata/.stignore
vendored
@ -1,2 +1,6 @@
|
||||
.*
|
||||
quux
|
||||
#include excludes
|
||||
#include nonexistent-file
|
||||
|
||||
bfile
|
||||
dir1/cfile
|
||||
|
||||
|
1
scanner/testdata/afile
vendored
Normal file
1
scanner/testdata/afile
vendored
Normal file
@ -0,0 +1 @@
|
||||
foo
|
1
scanner/testdata/bar
vendored
1
scanner/testdata/bar
vendored
@ -1 +0,0 @@
|
||||
foobarbaz
|
1
scanner/testdata/baz/quux
vendored
1
scanner/testdata/baz/quux
vendored
@ -1 +0,0 @@
|
||||
baazquux
|
1
scanner/testdata/bfile
vendored
Normal file
1
scanner/testdata/bfile
vendored
Normal file
@ -0,0 +1 @@
|
||||
bar
|
1
scanner/testdata/dir1/cfile
vendored
Normal file
1
scanner/testdata/dir1/cfile
vendored
Normal file
@ -0,0 +1 @@
|
||||
baz
|
1
scanner/testdata/dir1/dfile
vendored
Normal file
1
scanner/testdata/dir1/dfile
vendored
Normal file
@ -0,0 +1 @@
|
||||
quux
|
1
scanner/testdata/dir2/cfile
vendored
Normal file
1
scanner/testdata/dir2/cfile
vendored
Normal file
@ -0,0 +1 @@
|
||||
baz
|
1
scanner/testdata/dir2/dfile
vendored
Normal file
1
scanner/testdata/dir2/dfile
vendored
Normal file
@ -0,0 +1 @@
|
||||
quux
|
1
scanner/testdata/dir3/cfile
vendored
Normal file
1
scanner/testdata/dir3/cfile
vendored
Normal file
@ -0,0 +1 @@
|
||||
baz
|
1
scanner/testdata/dir3/dfile
vendored
Normal file
1
scanner/testdata/dir3/dfile
vendored
Normal file
@ -0,0 +1 @@
|
||||
quux
|
0
scanner/testdata/empty
vendored
0
scanner/testdata/empty
vendored
4
scanner/testdata/excludes
vendored
Normal file
4
scanner/testdata/excludes
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
dir2/dfile
|
||||
#include excludes
|
||||
#include further-excludes
|
||||
#include loop-excludes
|
1
scanner/testdata/foo
vendored
1
scanner/testdata/foo
vendored
@ -1 +0,0 @@
|
||||
foobar
|
1
scanner/testdata/further-excludes
vendored
Normal file
1
scanner/testdata/further-excludes
vendored
Normal file
@ -0,0 +1 @@
|
||||
dir3
|
1
scanner/testdata/loop-excludes
vendored
Normal file
1
scanner/testdata/loop-excludes
vendored
Normal file
@ -0,0 +1 @@
|
||||
#include excludes
|
@ -8,22 +8,30 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"sort"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/syncthing/syncthing/protocol"
|
||||
)
|
||||
|
||||
var testdata = []struct {
|
||||
type testfile struct {
|
||||
name string
|
||||
size int
|
||||
hash string
|
||||
}{
|
||||
{"bar", 10, "2f72cc11a6fcd0271ecef8c61056ee1eb1243be3805bf9a9df98f92f7636b05c"},
|
||||
{"baz", 0, ""},
|
||||
{"empty", 0, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
|
||||
{"foo", 7, "aec070645fe53ee3b3763059376134f058cc337247c978add178b6ccdfb0019f"},
|
||||
}
|
||||
|
||||
type testfileList []testfile
|
||||
|
||||
var testdata = testfileList{
|
||||
{"afile", 4, "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c"},
|
||||
{"dir1", 128, ""},
|
||||
{"dir1/dfile", 5, "49ae93732fcf8d63fe1cce759664982dbd5b23161f007dba8561862adc96d063"},
|
||||
{"dir2", 128, ""},
|
||||
{"dir2/cfile", 4, "bf07a7fbb825fc0aae7bf4a1177b2b31fcf8a3feeaf7092761e18c859ee52a9c"},
|
||||
{"excludes", 78, "1f5ac95d9e6fb2516629a029d788d27953c7bb2f4dc09184b660fdda0c8f2f04"},
|
||||
{"further-excludes", 5, "7eb0a548094fa6295f7fd9200d69973e5f5ec5c04f2a86d998080ac43ecf89f1"},
|
||||
{"loop-excludes", 18, "2db057aa82a8b8fe4b1367ccc875259ed4b8020255820d4e3d4bfe78f0dd3f2a"},
|
||||
}
|
||||
|
||||
var correctIgnores = map[string][]string{
|
||||
@ -33,7 +41,7 @@ var correctIgnores = map[string][]string{
|
||||
func TestWalkSub(t *testing.T) {
|
||||
w := Walker{
|
||||
Dir: "testdata",
|
||||
Sub: "foo",
|
||||
Sub: "dir2",
|
||||
BlockSize: 128 * 1024,
|
||||
IgnoreFile: ".stignore",
|
||||
}
|
||||
@ -46,11 +54,17 @@ func TestWalkSub(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(files) != 1 {
|
||||
t.Fatalf("Incorrect length %d != 1", len(files))
|
||||
// The directory contains two files, where one is ignored from a higher
|
||||
// level. We should see only the directory and one of the files.
|
||||
|
||||
if len(files) != 2 {
|
||||
t.Fatalf("Incorrect length %d != 2", len(files))
|
||||
}
|
||||
if files[0].Name != "foo" {
|
||||
t.Errorf("Incorrect file %v != foo", files[0])
|
||||
if files[0].Name != "dir2" {
|
||||
t.Errorf("Incorrect file %v != dir2", files[0])
|
||||
}
|
||||
if files[1].Name != "dir2/cfile" {
|
||||
t.Errorf("Incorrect file %v != dir2/cfile", files[1])
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,39 +74,21 @@ func TestWalk(t *testing.T) {
|
||||
BlockSize: 128 * 1024,
|
||||
IgnoreFile: ".stignore",
|
||||
}
|
||||
fchan, err := w.Walk()
|
||||
var files []protocol.FileInfo
|
||||
for f := range fchan {
|
||||
files = append(files, f)
|
||||
}
|
||||
sort.Sort(fileList(files))
|
||||
|
||||
fchan, err := w.Walk()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if l1, l2 := len(files), len(testdata); l1 != l2 {
|
||||
t.Log(files)
|
||||
t.Log(testdata)
|
||||
t.Fatalf("Incorrect number of walked files %d != %d", l1, l2)
|
||||
var tmp []protocol.FileInfo
|
||||
for f := range fchan {
|
||||
tmp = append(tmp, f)
|
||||
}
|
||||
sort.Sort(fileList(tmp))
|
||||
files := fileList(tmp).testfiles()
|
||||
|
||||
for i := range testdata {
|
||||
if n1, n2 := testdata[i].name, files[i].Name; n1 != n2 {
|
||||
t.Errorf("Incorrect file name %q != %q for case #%d", n1, n2, i)
|
||||
}
|
||||
|
||||
if testdata[i].hash != "" {
|
||||
if h1, h2 := fmt.Sprintf("%x", files[i].Blocks[0].Hash), testdata[i].hash; h1 != h2 {
|
||||
t.Errorf("Incorrect hash %q != %q for case #%d", h1, h2, i)
|
||||
}
|
||||
}
|
||||
|
||||
t0 := time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
|
||||
t1 := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
|
||||
if mt := files[i].Modified; mt < t0 || mt > t1 {
|
||||
t.Errorf("Unrealistic modtime %d for test %d", mt, i)
|
||||
}
|
||||
if !reflect.DeepEqual(files, testdata) {
|
||||
t.Errorf("Walk returned unexpected data\nExpected: %v\nActual: %v", testdata, files)
|
||||
}
|
||||
}
|
||||
|
||||
@ -194,3 +190,27 @@ func (f fileList) Less(a, b int) bool {
|
||||
func (f fileList) Swap(a, b int) {
|
||||
f[a], f[b] = f[b], f[a]
|
||||
}
|
||||
|
||||
func (l fileList) testfiles() testfileList {
|
||||
testfiles := make(testfileList, len(l))
|
||||
for i, f := range l {
|
||||
if len(f.Blocks) > 1 {
|
||||
panic("simple test case stuff only supports a single block per file")
|
||||
}
|
||||
testfiles[i] = testfile{name: f.Name, size: int(f.Size())}
|
||||
if len(f.Blocks) == 1 {
|
||||
testfiles[i].hash = fmt.Sprintf("%x", f.Blocks[0].Hash)
|
||||
}
|
||||
}
|
||||
return testfiles
|
||||
}
|
||||
|
||||
func (l testfileList) String() string {
|
||||
var b bytes.Buffer
|
||||
b.WriteString("{\n")
|
||||
for _, f := range l {
|
||||
fmt.Fprintf(&b, " %s (%d bytes): %s\n", f.name, f.size, f.hash)
|
||||
}
|
||||
b.WriteString("}")
|
||||
return b.String()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user