From 19139c287b708ce4dcb723ae56ee942b12843cf0 Mon Sep 17 00:00:00 2001 From: asoseil Date: Sun, 26 Nov 2017 22:37:20 +0100 Subject: [PATCH] bug fix, tests --- realize/cli.go | 4 +- realize/projects.go | 32 ++++++++++++--- realize/projects_test.go | 84 +++++++++++++++++++++++++++++++++------- 3 files changed, 99 insertions(+), 21 deletions(-) diff --git a/realize/cli.go b/realize/cli.go index 0942e3f..fdae684 100644 --- a/realize/cli.go +++ b/realize/cli.go @@ -3,7 +3,7 @@ package realize import ( "fmt" "go/build" - "gopkg.in/fsnotify.v1" + "github.com/fsnotify/fsnotify" "log" "os" "os/signal" @@ -40,7 +40,7 @@ type ( Context struct { Path string - Project Project + Project *Project Stop <-chan bool Watcher FileWatcher Event fsnotify.Event diff --git a/realize/projects.go b/realize/projects.go index feed8e7..8d3648b 100644 --- a/realize/projects.go +++ b/realize/projects.go @@ -87,12 +87,20 @@ type BufferOut struct { } // After stop watcher -func (p *Project) After() { +func (p *Project) After(){ + if p.parent.After != nil{ + p.parent.After(Context{Project:p}) + return + } p.cmd(nil, "after", true) } // Before start watcher -func (p *Project) Before() { +func (p *Project) Before(){ + if p.parent.Before != nil{ + p.parent.Before(Context{Project:p}) + return + } // setup go tools p.Tools.Setup() // set env const @@ -121,13 +129,23 @@ func (p *Project) Before() { // Error occurred func (p *Project) Err(err error) { - msg = fmt.Sprintln(p.pname(p.Name, 2), ":", Red.Regular(err.Error())) - out = BufferOut{Time: time.Now(), Text: err.Error()} - p.stamp("error", out, msg, "") + if p.parent.Err != nil{ + p.parent.Err(Context{Project:p}) + return + } + if err != nil { + msg = fmt.Sprintln(p.pname(p.Name, 2), ":", Red.Regular(err.Error())) + out = BufferOut{Time: time.Now(), Text: err.Error()} + p.stamp("error", out, msg, "") + } } // Change event message func (p *Project) Change(event fsnotify.Event) { + if p.parent.Change != nil{ + p.parent.Change(Context{Project:p,Event:event}) + return + } // file extension ext := ext(event.Name) if ext == "" { @@ -141,6 +159,10 @@ 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) { + if p.parent.Reload != nil{ + p.parent.Reload(Context{Project:p,Watcher:watcher,Path:path,Stop:stop}) + return + } var done bool var install, build Response go func() { diff --git a/realize/projects_test.go b/realize/projects_test.go index 1e23122..64e0a16 100644 --- a/realize/projects_test.go +++ b/realize/projects_test.go @@ -2,20 +2,76 @@ package realize import ( "testing" + "log" + "bytes" + "strings" + "os" + "errors" ) -func TestProject_Watch(t *testing.T) { - - // test force polling watcher - - // test indexing - - // test event - - // test event types - - // test error - - // test exit - +func TestProject_After(t *testing.T) { + var buf bytes.Buffer + log.SetOutput(&buf) + r := Realize{} + input := "text" + r.After = func(context Context){ + log.Println(input) + } + r.Projects = append(r.Projects,Project{ + parent: &r, + }) + r.Projects[0].After() + if !strings.Contains(buf.String(), input) { + t.Error("Unexpected error") + } } + +func TestProject_Before(t *testing.T) { + var buf bytes.Buffer + log.SetOutput(&buf) + r := Realize{} + r.Projects = append(r.Projects,Project{ + parent: &r, + }) + input := "text" + r.Before = func(context Context){ + log.Println(input) + } + r.Projects[0].Before() + if !strings.Contains(buf.String(), input) { + t.Error("Unexpected error") + } + + r = Realize{} + r.Projects = append(r.Projects,Project{ + parent: &r, + Environment: map[string]string{ + input: input, + }, + }) + r.Projects[0].Before() + if os.Getenv(input) != input{ + t.Error("Unexpected error expected", input, "instead",os.Getenv(input)) + } +} + +func TestProject_Err(t *testing.T) { + var buf bytes.Buffer + log.SetOutput(&buf) + r := Realize{} + r.Projects = append(r.Projects,Project{ + parent: &r, + }) + input := "text" + r.Err = func(context Context){ + log.Println(input) + } + r.Projects[0].Err(errors.New(input)) + if !strings.Contains(buf.String(), input) { + t.Error("Unexpected error") + } +} + +func TestProject_Change(t *testing.T) { + +} \ No newline at end of file