2017-10-08 21:09:45 +00:00
|
|
|
package main
|
2017-10-21 22:47:46 +00:00
|
|
|
|
|
|
|
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"},
|
|
|
|
},
|
2017-10-22 21:54:02 +00:00
|
|
|
Path: "/go/project",
|
2017-10-21 22:47:46 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|