Rework .stignore functionality (fixes #561) (...)
- Only one .stignore is supported, at the repo root - Negative patterns (!) are supported - Ignore patterns affect sent and received indexes, not only scanning
This commit is contained in:
104
ignore/ignore_test.go
Normal file
104
ignore/ignore_test.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package ignore_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/syncthing/syncthing/ignore"
|
||||
)
|
||||
|
||||
func TestIgnore(t *testing.T) {
|
||||
pats, err := ignore.Load("testdata/.stignore")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var tests = []struct {
|
||||
f string
|
||||
r bool
|
||||
}{
|
||||
{"afile", false},
|
||||
{"bfile", true},
|
||||
{"cfile", false},
|
||||
{"dfile", false},
|
||||
{"efile", true},
|
||||
{"ffile", true},
|
||||
|
||||
{"dir1", false},
|
||||
{filepath.Join("dir1", "cfile"), true},
|
||||
{filepath.Join("dir1", "dfile"), false},
|
||||
{filepath.Join("dir1", "efile"), true},
|
||||
{filepath.Join("dir1", "ffile"), false},
|
||||
|
||||
{"dir2", false},
|
||||
{filepath.Join("dir2", "cfile"), false},
|
||||
{filepath.Join("dir2", "dfile"), true},
|
||||
{filepath.Join("dir2", "efile"), true},
|
||||
{filepath.Join("dir2", "ffile"), false},
|
||||
|
||||
{filepath.Join("dir3"), true},
|
||||
{filepath.Join("dir3", "afile"), true},
|
||||
}
|
||||
|
||||
for i, tc := range tests {
|
||||
if r := pats.Match(tc.f); r != tc.r {
|
||||
t.Errorf("Incorrect ignoreFile() #%d (%s); E: %v, A: %v", i, tc.f, tc.r, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExcludes(t *testing.T) {
|
||||
stignore := `
|
||||
!iex2
|
||||
!ign1/ex
|
||||
ign1
|
||||
i*2
|
||||
!ign2
|
||||
`
|
||||
pats, err := ignore.Parse(bytes.NewBufferString(stignore), ".stignore")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var tests = []struct {
|
||||
f string
|
||||
r bool
|
||||
}{
|
||||
{"ign1", true},
|
||||
{"ign2", true},
|
||||
{"ibla2", true},
|
||||
{"iex2", false},
|
||||
{"ign1/ign", true},
|
||||
{"ign1/ex", false},
|
||||
{"ign1/iex2", false},
|
||||
{"iex2/ign", false},
|
||||
{"foo/bar/ign1", true},
|
||||
{"foo/bar/ign2", true},
|
||||
{"foo/bar/iex2", false},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
if r := pats.Match(tc.f); r != tc.r {
|
||||
t.Errorf("Incorrect match for %s: %v != %v", tc.f, r, tc.r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadPatterns(t *testing.T) {
|
||||
var badPatterns = []string{
|
||||
"[",
|
||||
"/[",
|
||||
"**/[",
|
||||
"#include nonexistent",
|
||||
"#include .stignore",
|
||||
"!#include makesnosense",
|
||||
}
|
||||
|
||||
for _, pat := range badPatterns {
|
||||
parsed, err := ignore.Parse(bytes.NewBufferString(pat), ".stignore")
|
||||
if err == nil {
|
||||
t.Errorf("No error for pattern %q: %v", pat, parsed)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user