Skip to content

Commit

Permalink
Improve test coverage for watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcnunes committed Jul 25, 2018
1 parent be41602 commit a81558c
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 2 deletions.
Empty file added testdata/ignore-test-name.txt
Empty file.
1 change: 1 addition & 0 deletions testdata/ignore-test-name/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package ignoretestname
Empty file.
Empty file.
10 changes: 9 additions & 1 deletion watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -158,6 +159,7 @@ func (w *watcher) ignoreFile(path string, info os.FileInfo) bool {
if _, ignored := w.ignoreItems[path]; ignored {
return true
}

return false
}

Expand All @@ -180,7 +182,7 @@ func resolvePaths(paths []string, extensions map[string]bool) (map[string]bool,
// don't care for extension filter right now for non glob paths
// since they could be a directory
if isGlob {
if _, ok := extensions[filepath.Ext(path)]; !ok {
if _, ok := extensions[filepath.Ext(match)]; !ok {
continue
}
}
Expand All @@ -198,13 +200,19 @@ func resolvePaths(paths []string, extensions map[string]bool) (map[string]bool,

// remove overlapped paths so it makes the scan for changes later faster and simpler
func removeOverlappedPaths(mapPaths map[string]bool) {
startDot := regexp.MustCompile(`^\./`)

for p1 := range mapPaths {
p1 = startDot.ReplaceAllString(p1, "")

// skip to next item if this path has already been checked
if v, ok := mapPaths[p1]; ok && !v {
continue
}

for p2 := range mapPaths {
p2 = startDot.ReplaceAllString(p2, "")

if p1 == p2 {
continue
}
Expand Down
132 changes: 131 additions & 1 deletion watcher_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gaper

import (
"log"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -60,7 +61,7 @@ func TestWatcherGlobPath(t *testing.T) {
func TestWatcherRemoveOverlapdPaths(t *testing.T) {
pollInterval := 0
watchItems := []string{filepath.Join("testdata", "server")}
ignoreItems := []string{"./testdata/**/*", "./testdata/server"}
ignoreItems := []string{"./testdata/server/**/*", "./testdata/server"}
var extensions []string

wCfg := WatcherConfig{
Expand Down Expand Up @@ -120,3 +121,132 @@ func TestWatcherWatchChange(t *testing.T) {
assert.Nil(t, err, "wacher event error")
}
}

func TestWatcherIgnoreFile(t *testing.T) {
testCases := []struct {
name, file, ignoreFile string
defaultIgnore, expectIgnore bool
}{
{
name: "with default ignore enabled it ignores vendor folder",
file: "vendor",
defaultIgnore: true,
expectIgnore: true,
},
{
name: "without default ignore enabled it does not ignore vendor folder",
file: "vendor",
defaultIgnore: false,
expectIgnore: false,
},
{
name: "with default ignore enabled it ignores test file",
file: filepath.Join("testdata", "server", "main_test.go"),
defaultIgnore: true,
expectIgnore: true,
},
{
name: "with default ignore enabled it does no ignore non test files which have test in the name",
file: filepath.Join("testdata", "ignore-test-name.txt"),
defaultIgnore: true,
expectIgnore: false,
},
{
name: "without default ignore enabled it does not ignore test file",
file: filepath.Join("testdata", "server", "main_test.go"),
defaultIgnore: false,
expectIgnore: false,
},
{
name: "with default ignore enabled it ignores ignored items",
file: filepath.Join("testdata", "server", "main.go"),
ignoreFile: filepath.Join("testdata", "server", "main.go"),
defaultIgnore: true,
expectIgnore: true,
},
{
name: "without default ignore enabled it ignores ignored items",
file: filepath.Join("testdata", "server", "main.go"),
ignoreFile: filepath.Join("testdata", "server", "main.go"),
defaultIgnore: false,
expectIgnore: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
srvdir := "."

watchItems := []string{srvdir}
ignoreItems := []string{}
if len(tc.ignoreFile) > 0 {
ignoreItems = append(ignoreItems, tc.ignoreFile)
}
extensions := []string{"go"}

wCfg := WatcherConfig{
DefaultIgnore: tc.defaultIgnore,
WatchItems: watchItems,
IgnoreItems: ignoreItems,
Extensions: extensions,
}
w, err := NewWatcher(wCfg)
assert.Nil(t, err, "wacher error")

wt := w.(*watcher)

filePath := tc.file
file, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
}

fileInfo, err := file.Stat()
if err != nil {
log.Fatal(err)
}

assert.Equal(t, tc.expectIgnore, wt.ignoreFile(filePath, fileInfo))
})
}
}

func TestWatcherResolvePaths(t *testing.T) {
testCases := []struct {
name string
paths []string
extensions, expectPaths map[string]bool
err error
}{
{
name: "remove duplicated paths",
paths: []string{"testdata/test-duplicated-paths", "testdata/test-duplicated-paths"},
extensions: map[string]bool{".txt": true},
expectPaths: map[string]bool{"testdata/test-duplicated-paths": true},
},
{
name: "remove duplicated paths from glob",
paths: []string{"testdata/test-duplicated-paths", "testdata/test-duplicated-paths/**/*"},
extensions: map[string]bool{".txt": true},
expectPaths: map[string]bool{"testdata/test-duplicated-paths": true},
},
{
name: "remove duplicated paths from glob with inverse order",
paths: []string{"testdata/test-duplicated-paths/**/*", "testdata/test-duplicated-paths"},
extensions: map[string]bool{".txt": true},
expectPaths: map[string]bool{"testdata/test-duplicated-paths": true},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
paths, err := resolvePaths(tc.paths, tc.extensions)
if tc.err == nil {
assert.Nil(t, err, "resolve path error")
assert.Equal(t, tc.expectPaths, paths)
} else {
assert.Equal(t, tc.err, err)
}
})
}
}

0 comments on commit a81558c

Please sign in to comment.