lib/ignore, lib/scanner: Catch included items below ignored ones (#4811)

This commit is contained in:
Simon Frei
2018-05-14 09:47:23 +02:00
committed by GitHub
parent 22b0bd8e13
commit d59aecba31
12 changed files with 244 additions and 104 deletions

View File

@@ -41,15 +41,17 @@ func TestMain(m *testing.M) {
testFs = NewFilesystem(FilesystemTypeBasic, testDirAbs)
backendBuffer = 10
defer func() {
backendBuffer = 500
os.RemoveAll(testDir)
}()
os.Exit(m.Run())
exitCode := m.Run()
backendBuffer = 500
os.RemoveAll(testDir)
os.Exit(exitCode)
}
const (
testDir = "temporary_test_root"
testDir = "testdata"
)
var (
@@ -75,7 +77,31 @@ func TestWatchIgnore(t *testing.T) {
{name, NonRemove},
}
testScenario(t, name, testCase, expectedEvents, allowedEvents, ignored)
testScenario(t, name, testCase, expectedEvents, allowedEvents, fakeMatcher{ignore: filepath.Join(name, ignored), skipIgnoredDirs: true})
}
func TestWatchInclude(t *testing.T) {
name := "include"
file := "file"
ignored := "ignored"
testFs.MkdirAll(filepath.Join(name, ignored), 0777)
included := filepath.Join(ignored, "included")
testCase := func() {
createTestFile(name, file)
createTestFile(name, included)
}
expectedEvents := []Event{
{file, NonRemove},
{included, NonRemove},
}
allowedEvents := []Event{
{name, NonRemove},
}
testScenario(t, name, testCase, expectedEvents, allowedEvents, fakeMatcher{ignore: filepath.Join(name, ignored), include: filepath.Join(name, included)})
}
func TestWatchRename(t *testing.T) {
@@ -104,7 +130,7 @@ func TestWatchRename(t *testing.T) {
// set the "allow others" flag because we might get the create of
// "oldfile" initially
testScenario(t, name, testCase, expectedEvents, allowedEvents, "")
testScenario(t, name, testCase, expectedEvents, allowedEvents, fakeMatcher{})
}
// TestWatchOutside checks that no changes from outside the folder make it in
@@ -120,7 +146,11 @@ func TestWatchOutside(t *testing.T) {
go func() {
defer func() {
if recover() == nil {
t.Fatalf("Watch did not panic on receiving event outside of folder")
select {
case <-ctx.Done(): // timed out
default:
t.Fatalf("Watch did not panic on receiving event outside of folder")
}
}
cancel()
}()
@@ -128,6 +158,13 @@ func TestWatchOutside(t *testing.T) {
}()
backendChan <- fakeEventInfo(filepath.Join(filepath.Dir(testDirAbs), "outside"))
select {
case <-time.After(10 * time.Second):
cancel()
t.Errorf("Timed out before panicing")
case <-ctx.Done():
}
}
func TestWatchSubpath(t *testing.T) {
@@ -178,7 +215,7 @@ func TestWatchOverflow(t *testing.T) {
}
}
testScenario(t, name, testCase, expectedEvents, allowedEvents, "")
testScenario(t, name, testCase, expectedEvents, allowedEvents, fakeMatcher{})
}
func TestWatchErrorLinuxInterpretation(t *testing.T) {
@@ -283,7 +320,7 @@ func TestWatchIssue4877(t *testing.T) {
testFs = origTestFs
}()
testScenario(t, name, testCase, expectedEvents, allowedEvents, "")
testScenario(t, name, testCase, expectedEvents, allowedEvents, fakeMatcher{})
}
// path relative to folder root, also creates parent dirs if necessary
@@ -312,7 +349,7 @@ func sleepMs(ms int) {
time.Sleep(time.Duration(ms) * time.Millisecond)
}
func testScenario(t *testing.T, name string, testCase func(), expectedEvents, allowedEvents []Event, ignored string) {
func testScenario(t *testing.T, name string, testCase func(), expectedEvents, allowedEvents []Event, fm fakeMatcher) {
if err := testFs.MkdirAll(name, 0755); err != nil {
panic(fmt.Sprintf("Failed to create directory %s: %s", name, err))
}
@@ -321,11 +358,7 @@ func testScenario(t *testing.T, name string, testCase func(), expectedEvents, al
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if ignored != "" {
ignored = filepath.Join(name, ignored)
}
eventChan, err := testFs.Watch(name, fakeMatcher{ignored}, ctx, false)
eventChan, err := testFs.Watch(name, fm, ctx, false)
if err != nil {
panic(err)
}
@@ -335,7 +368,7 @@ func testScenario(t *testing.T, name string, testCase func(), expectedEvents, al
testCase()
select {
case <-time.NewTimer(time.Minute).C:
case <-time.After(time.Minute):
t.Errorf("Timed out before receiving all expected events")
case <-ctx.Done():
@@ -382,10 +415,19 @@ func testWatchOutput(t *testing.T, name string, in <-chan Event, expectedEvents,
}
}
type fakeMatcher struct{ match string }
// Matches are done via direct comparison against both ignore and include
type fakeMatcher struct {
ignore string
include string
skipIgnoredDirs bool
}
func (fm fakeMatcher) ShouldIgnore(name string) bool {
return name == fm.match
return name != fm.include && name == fm.ignore
}
func (fm fakeMatcher) SkipIgnoredDirs() bool {
return fm.skipIgnoredDirs
}
type fakeEventInfo string