This commit is contained in:
asoseil 2017-11-30 21:39:20 +01:00
parent d389e5b387
commit 0d2f8856f6
2 changed files with 81 additions and 6 deletions

View File

@ -158,9 +158,9 @@ func (p *Project) Change(event fsnotify.Event) {
} }
// Reload launches the toolchain run, build, install // Reload launches the toolchain run, build, install
func (p *Project) Reload(watcher FileWatcher, path string, stop <-chan bool) { func (p *Project) Reload(path string, stop <-chan bool) {
if p.parent.Reload != nil { if p.parent.Reload != nil {
p.parent.Reload(Context{Project: p, Watcher: watcher, Path: path, Stop: stop}) p.parent.Reload(Context{Project: p, Watcher: p.watcher, Path: path, Stop: stop})
return return
} }
var done bool var done bool
@ -286,7 +286,7 @@ func (p *Project) Watch(exit chan os.Signal) {
// before start checks // before start checks
p.Before() p.Before()
// start watcher // start watcher
go p.Reload(p.watcher, "", p.stop) go p.Reload("", p.stop)
L: L:
for { for {
select { select {
@ -304,7 +304,7 @@ L:
close(p.stop) close(p.stop)
p.stop = make(chan bool) p.stop = make(chan bool)
p.Change(event) p.Change(event)
go p.Reload(p.watcher, "", p.stop) go p.Reload( "", p.stop)
} }
default: default:
if p.Validate(event.Name, true) { if p.Validate(event.Name, true) {
@ -320,7 +320,7 @@ L:
close(p.stop) close(p.stop)
p.stop = make(chan bool) p.stop = make(chan bool)
p.Change(event) p.Change(event)
go p.Reload(p.watcher, event.Name, p.stop) go p.Reload(event.Name, p.stop)
} }
p.lastTime = time.Now().Truncate(time.Second) p.lastTime = time.Now().Truncate(time.Second)
p.lastFile = event.Name p.lastFile = event.Name

View File

@ -7,6 +7,10 @@ import (
"os" "os"
"strings" "strings"
"testing" "testing"
"github.com/fsnotify/fsnotify"
"os/signal"
"syscall"
"time"
) )
func TestProject_After(t *testing.T) { func TestProject_After(t *testing.T) {
@ -73,5 +77,76 @@ func TestProject_Err(t *testing.T) {
} }
func TestProject_Change(t *testing.T) { func TestProject_Change(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
r := Realize{}
r.Projects = append(r.Projects, Project{
parent: &r,
})
r.Change = func(context Context) {
log.Println(context.Event.Name)
}
event := fsnotify.Event{Name:"test",Op:fsnotify.Write}
r.Projects[0].Change(event)
if !strings.Contains(buf.String(), event.Name) {
t.Error("Unexpected error")
}
} }
func TestProject_Reload(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
r := Realize{}
r.Projects = append(r.Projects, Project{
parent: &r,
})
input := "test/path"
r.Projects[0].watcher, _ = NewFileWatcher(false,0)
r.Reload = func(context Context) {
log.Println(context.Path)
}
stop := make(chan bool)
r.Projects[0].Reload(input,stop)
if !strings.Contains(buf.String(), input) {
t.Error("Unexpected error")
}
}
func TestProject_Validate(t *testing.T) {
data := map[string]bool{
"": false,
"/test/.path/": false,
"./test/path/": false,
"/test/path/test.html": false,
"/test/path/test.go": false,
"/test/ignore/test.go": false,
"/test/check/notexist.go": false,
"/test/check/exist.go": false,
}
r := Realize{}
r.Projects = append(r.Projects, Project{
parent: &r,
Watcher: Watch{
Ignore: []string{"/test/ignore"},
},
})
for i, v := range data {
if r.Projects[0].Validate(i,true) != v{
t.Error("Unexpected error",i,"expected",v)
}
}
}
func TestProject_Watch(t *testing.T) {
r := Realize{}
r.Projects = append(r.Projects, Project{
parent: &r,
})
r.exit = make(chan os.Signal, 2)
signal.Notify(r.exit, os.Interrupt, syscall.SIGTERM)
go func(){
time.Sleep(100)
close(r.exit)
}()
r.Projects[0].Watch(r.exit)
}