realize/realize/projects_test.go

155 lines
3.2 KiB
Go
Raw Normal View History

2017-11-20 15:36:48 +00:00
package realize
2017-11-22 16:23:41 +00:00
import (
2017-11-26 21:37:20 +00:00
"bytes"
"errors"
2017-12-04 14:53:59 +00:00
"github.com/fsnotify/fsnotify"
2017-11-26 23:01:08 +00:00
"log"
"os"
"strings"
2017-12-04 14:53:59 +00:00
"sync"
2017-11-26 23:01:08 +00:00
"testing"
2017-11-30 20:39:20 +00:00
"time"
2017-11-22 16:23:41 +00:00
)
2017-12-04 14:53:59 +00:00
func TestProject_After(t *testing.T) /**/ {
2017-11-26 21:37:20 +00:00
var buf bytes.Buffer
log.SetOutput(&buf)
r := Realize{}
input := "text"
2017-11-26 23:01:08 +00:00
r.After = func(context Context) {
2017-11-26 21:37:20 +00:00
log.Println(input)
}
2017-11-26 23:01:08 +00:00
r.Projects = append(r.Projects, Project{
2017-11-26 21:37:20 +00:00
parent: &r,
})
r.Projects[0].After()
if !strings.Contains(buf.String(), input) {
t.Error("Unexpected error")
}
}
2017-11-25 15:08:52 +00:00
2017-11-26 21:37:20 +00:00
func TestProject_Before(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
r := Realize{}
2017-11-26 23:01:08 +00:00
r.Projects = append(r.Projects, Project{
2017-11-26 21:37:20 +00:00
parent: &r,
})
input := "text"
2017-11-26 23:01:08 +00:00
r.Before = func(context Context) {
2017-11-26 21:37:20 +00:00
log.Println(input)
}
r.Projects[0].Before()
if !strings.Contains(buf.String(), input) {
t.Error("Unexpected error")
}
2017-11-25 15:08:52 +00:00
2017-11-26 21:37:20 +00:00
r = Realize{}
2017-11-26 23:01:08 +00:00
r.Projects = append(r.Projects, Project{
2017-11-26 21:37:20 +00:00
parent: &r,
Environment: map[string]string{
input: input,
},
})
r.Projects[0].Before()
2017-11-26 23:01:08 +00:00
if os.Getenv(input) != input {
t.Error("Unexpected error expected", input, "instead", os.Getenv(input))
2017-11-26 21:37:20 +00:00
}
}
2017-11-25 15:08:52 +00:00
2017-11-26 21:37:20 +00:00
func TestProject_Err(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
r := Realize{}
2017-11-26 23:01:08 +00:00
r.Projects = append(r.Projects, Project{
2017-11-26 21:37:20 +00:00
parent: &r,
})
input := "text"
2017-11-26 23:01:08 +00:00
r.Err = func(context Context) {
2017-11-26 21:37:20 +00:00
log.Println(input)
}
r.Projects[0].Err(errors.New(input))
if !strings.Contains(buf.String(), input) {
t.Error("Unexpected error")
}
}
2017-11-25 15:08:52 +00:00
2017-11-26 21:37:20 +00:00
func TestProject_Change(t *testing.T) {
2017-11-30 20:39:20 +00:00
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)
}
2017-12-04 14:53:59 +00:00
event := fsnotify.Event{Name: "test", Op: fsnotify.Write}
2017-11-30 20:39:20 +00:00
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"
2017-12-04 14:53:59 +00:00
r.Projects[0].watcher, _ = NewFileWatcher(false, 0)
2017-11-30 20:39:20 +00:00
r.Reload = func(context Context) {
log.Println(context.Path)
}
stop := make(chan bool)
2017-12-04 14:53:59 +00:00
r.Projects[0].Reload(input, stop)
2017-11-30 20:39:20 +00:00
if !strings.Contains(buf.String(), input) {
t.Error("Unexpected error")
}
}
2017-11-25 15:08:52 +00:00
2017-11-30 20:39:20 +00:00
func TestProject_Validate(t *testing.T) {
data := map[string]bool{
2017-12-04 14:53:59 +00:00
"": false,
"/test/.path/": false,
"./test/path/": false,
"/test/path/test.html": false,
"/test/path/test.go": false,
"/test/ignore/test.go": false,
2017-11-30 20:39:20 +00:00
"/test/check/notexist.go": false,
2017-12-04 14:53:59 +00:00
"/test/check/exist.go": false,
2017-11-30 20:39:20 +00:00
}
r := Realize{}
r.Projects = append(r.Projects, Project{
parent: &r,
Watcher: Watch{
Ignore: []string{"/test/ignore"},
},
})
for i, v := range data {
2017-12-04 14:53:59 +00:00
if r.Projects[0].Validate(i, true) != v {
t.Error("Unexpected error", i, "expected", v)
2017-11-30 20:39:20 +00:00
}
}
2017-11-26 23:01:08 +00:00
}
2017-11-30 20:39:20 +00:00
func TestProject_Watch(t *testing.T) {
2017-12-04 14:53:59 +00:00
var wg sync.WaitGroup
2017-11-30 20:39:20 +00:00
r := Realize{}
r.Projects = append(r.Projects, Project{
parent: &r,
2017-12-04 22:39:27 +00:00
exit: make(chan os.Signal, 1),
2017-11-30 20:39:20 +00:00
})
2017-12-04 14:53:59 +00:00
go func() {
2017-11-30 20:39:20 +00:00
time.Sleep(100)
2017-12-04 22:39:27 +00:00
close(r.Projects[0].exit)
2017-11-30 20:39:20 +00:00
}()
2017-12-04 14:53:59 +00:00
wg.Add(1)
// test before after and file change
r.Projects[0].Watch(&wg)
wg.Wait()
}