diff --git a/realize/projects.go b/realize/projects.go index d9deed2..3e37a70 100644 --- a/realize/projects.go +++ b/realize/projects.go @@ -158,9 +158,9 @@ func (p *Project) Change(event fsnotify.Event) { } // 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 { - 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 } var done bool @@ -286,7 +286,7 @@ func (p *Project) Watch(exit chan os.Signal) { // before start checks p.Before() // start watcher - go p.Reload(p.watcher, "", p.stop) + go p.Reload("", p.stop) L: for { select { @@ -304,7 +304,7 @@ L: close(p.stop) p.stop = make(chan bool) p.Change(event) - go p.Reload(p.watcher, "", p.stop) + go p.Reload( "", p.stop) } default: if p.Validate(event.Name, true) { @@ -320,7 +320,7 @@ L: close(p.stop) p.stop = make(chan bool) 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.lastFile = event.Name diff --git a/realize/projects_test.go b/realize/projects_test.go index bd01947..c571b2b 100644 --- a/realize/projects_test.go +++ b/realize/projects_test.go @@ -7,6 +7,10 @@ import ( "os" "strings" "testing" + "github.com/fsnotify/fsnotify" + "os/signal" + "syscall" + "time" ) func TestProject_After(t *testing.T) { @@ -73,5 +77,76 @@ func TestProject_Err(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) +} \ No newline at end of file