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 (
"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

View File

@ -88,11 +88,19 @@ type BufferOut struct {
// After stop watcher
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(){
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) {
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() {

View File

@ -2,20 +2,76 @@ package realize
import (
"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
// test error
// test exit
func TestProject_Change(t *testing.T) {
}