bug fix, tests

This commit is contained in:
asoseil 2017-11-26 22:37:20 +01:00
parent 1c1bf5b9ff
commit 19139c287b
3 changed files with 99 additions and 21 deletions

View File

@ -3,7 +3,7 @@ package realize
import ( import (
"fmt" "fmt"
"go/build" "go/build"
"gopkg.in/fsnotify.v1" "github.com/fsnotify/fsnotify"
"log" "log"
"os" "os"
"os/signal" "os/signal"
@ -40,7 +40,7 @@ type (
Context struct { Context struct {
Path string Path string
Project Project Project *Project
Stop <-chan bool Stop <-chan bool
Watcher FileWatcher Watcher FileWatcher
Event fsnotify.Event Event fsnotify.Event

View File

@ -87,12 +87,20 @@ type BufferOut struct {
} }
// After stop watcher // 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) p.cmd(nil, "after", true)
} }
// Before start watcher // 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 // setup go tools
p.Tools.Setup() p.Tools.Setup()
// set env const // set env const
@ -121,13 +129,23 @@ func (p *Project) Before() {
// Error occurred // Error occurred
func (p *Project) Err(err error) { func (p *Project) Err(err error) {
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())) msg = fmt.Sprintln(p.pname(p.Name, 2), ":", Red.Regular(err.Error()))
out = BufferOut{Time: time.Now(), Text: err.Error()} out = BufferOut{Time: time.Now(), Text: err.Error()}
p.stamp("error", out, msg, "") p.stamp("error", out, msg, "")
}
} }
// Change event message // Change event message
func (p *Project) Change(event fsnotify.Event) { func (p *Project) Change(event fsnotify.Event) {
if p.parent.Change != nil{
p.parent.Change(Context{Project:p,Event:event})
return
}
// file extension // file extension
ext := ext(event.Name) ext := ext(event.Name)
if ext == "" { if ext == "" {
@ -141,6 +159,10 @@ 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(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 done bool
var install, build Response var install, build Response
go func() { go func() {

View File

@ -2,20 +2,76 @@ package realize
import ( import (
"testing" "testing"
"log"
"bytes"
"strings"
"os"
"errors"
) )
func TestProject_Watch(t *testing.T) { 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")
}
}
// test force polling watcher 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")
}
// test indexing 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))
}
}
// test event 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")
}
}
// test event types func TestProject_Change(t *testing.T) {
// test error
// test exit
} }