include hidden files in watch

This commit is contained in:
Rodrigo Brito 2017-10-21 20:47:46 -02:00
parent 01f52c34cb
commit 5dffb8b933
2 changed files with 74 additions and 2 deletions

View File

@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"github.com/fsnotify/fsnotify"
"log" "log"
"math/big" "math/big"
"os" "os"
@ -13,6 +12,8 @@ import (
"sync" "sync"
"syscall" "syscall"
"time" "time"
"github.com/fsnotify/fsnotify"
) )
var ( var (
@ -315,7 +316,7 @@ func (p *Project) walk(path string, info os.FileInfo, err error) error {
return nil return nil
} }
} }
if !strings.Contains(path, "/.") && !strings.HasPrefix(path, ".") && (info.IsDir() || array(ext(path), p.Watcher.Exts)) { if !strings.HasPrefix(path, ".") && (info.IsDir() || array(ext(path), p.Watcher.Exts)) {
result := p.watcher.Walk(path, p.init) result := p.watcher.Walk(path, p.init)
if result != "" { if result != "" {
if info.IsDir() { if info.IsDir() {

View File

@ -1 +1,72 @@
package main package main
import (
"testing"
"os"
)
type fileWatcherMock struct {
FileWatcher
}
func (f *fileWatcherMock) Walk(path string, _ bool) string {
return path
}
type fileInfoMock struct {
os.FileInfo
FileIsDir bool
}
func (m *fileInfoMock) IsDir() bool { return m.FileIsDir }
func TestWalk(t *testing.T) {
p := Project{
Name: "Test Project",
Watcher: Watch{
Paths: []string{"/"},
Ignore: []string{"vendor"},
Exts: []string{"go"},
},
base: "/go/project",
watcher: &fileWatcherMock{},
init: true,
}
files := []struct {
Path string
IsDir bool
}{
// valid files
{"/go/project", true},
{"/go/project/main.go", false},
{"/go/project/main_test.go", false},
// invalid relative path
{"./relative/path", true},
{"./relative/path/file.go", false},
// invalid extension
{"/go/project/settings.yaml", false},
// invalid vendor files
{"/go/project/vendor/foo", true},
{"/go/project/vendor/foo/main.go", false},
}
for _, file := range files {
fileInfo := fileInfoMock{
FileIsDir: file.IsDir,
}
err := p.walk(file.Path, &fileInfo, nil)
if err != nil {
t.Errorf("Error not expected: %s", err)
}
}
if p.files != 2 {
t.Errorf("Exepeted %d files, but was %d", 2, p.files)
}
if p.folders != 1 {
t.Errorf("Exepeted %d folders, but was %d", 2, p.folders)
}
}