partial tests

This commit is contained in:
asoseil 2017-10-15 21:35:50 +02:00
parent 45a965639a
commit 9dd5cc1a74
8 changed files with 487 additions and 337 deletions

View File

@ -4,20 +4,22 @@ import (
"flag" "flag"
"gopkg.in/urfave/cli.v2" "gopkg.in/urfave/cli.v2"
"log" "log"
"os"
"reflect" "reflect"
"testing" "testing"
"time" "time"
) )
type logT struct{} type loggerT struct{}
func (logT) Write(bytes []byte) (int, error) { func (loggerT) Write(bytes []byte) (int, error) {
return 0, nil return 0, nil
} }
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
log.SetFlags(0) log.SetFlags(0)
log.SetOutput(logT{}) log.SetOutput(loggerT{})
os.Exit(m.Run())
} }
func TestRealize_Clean(t *testing.T) { func TestRealize_Clean(t *testing.T) {
@ -37,6 +39,19 @@ func TestRealize_Clean(t *testing.T) {
} }
func TestRealize_Check(t *testing.T) {
r := realize{}
err := r.check()
if err == nil {
t.Error("There is no project, error expected")
}
r.Schema = append(r.Schema, Project{Name: "test0"})
err = r.check()
if err != nil {
t.Error("There is a project, error unexpected", err)
}
}
func TestRealize_Add(t *testing.T) { func TestRealize_Add(t *testing.T) {
r := realize{} r := realize{}
// add all flags, test with expected // add all flags, test with expected
@ -87,37 +102,6 @@ func TestRealize_Add(t *testing.T) {
} }
} }
func TestRealize_Check(t *testing.T) {
r := realize{}
err := r.check()
if err == nil {
t.Error("There is no project, error expected")
}
r.Schema = append(r.Schema, Project{Name: "test0"})
err = r.check()
if err != nil {
t.Error("There is a project, error unexpected", err)
}
}
func TestRealize_Remove(t *testing.T) {
r := realize{}
set := flag.NewFlagSet("name", 0)
set.String("name", "", "")
c := cli.NewContext(nil, set, nil)
set.Parse([]string{"--name=test0"})
err := r.remove(c)
if err == nil {
t.Error("Expected an error, there are no projects")
}
// Append a new project
r.Schema = append(r.Schema, Project{Name: "test0"})
err = r.remove(c)
if err != nil {
t.Error("Error unexpected, the project should be remove", err)
}
}
func TestRealize_Run(t *testing.T) { func TestRealize_Run(t *testing.T) {
set := flag.NewFlagSet("test", 0) set := flag.NewFlagSet("test", 0)
params := cli.NewContext(nil, set, nil) params := cli.NewContext(nil, set, nil)
@ -141,3 +125,44 @@ func TestRealize_Run(t *testing.T) {
go r.run(params) go r.run(params)
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
} }
func TestRealize_Remove(t *testing.T) {
r := realize{}
set := flag.NewFlagSet("name", 0)
set.String("name", "", "")
c := cli.NewContext(nil, set, nil)
set.Parse([]string{"--name=test0"})
err := r.remove(c)
if err == nil {
t.Error("Expected an error, there are no projects")
}
// Append a new project
r.Schema = append(r.Schema, Project{Name: "test0"})
err = r.remove(c)
if err != nil {
t.Error("Error unexpected, the project should be remove", err)
}
}
func TestRealize_Insert(t *testing.T) {
r := realize{}
// add all flags, test with expected
set := flag.NewFlagSet("test", 0)
set.Bool("no-config", false, "")
c := cli.NewContext(nil, set, nil)
set.Parse([]string{"--no-config"})
r.insert(c)
if len(r.Schema) != 1 {
t.Error("Expected one project instead", len(r.Schema))
}
r.Schema = []Project{}
r.Schema = append(r.Schema, Project{})
r.Schema = append(r.Schema, Project{})
c = cli.NewContext(nil, set, nil)
r.insert(c)
if len(r.Schema) != 1 {
t.Error("Expected one project instead", len(r.Schema))
}
}

View File

@ -1 +1,29 @@
package main package main
import (
"testing"
"time"
)
func TestProject_GoCompile(t *testing.T) {
p := Project{}
stop := make(chan bool)
response := make(chan string)
result, err := p.goCompile(stop, []string{"echo", "test"})
if err != nil {
t.Error("Unexpected", err)
}
go func() {
result, err = p.goCompile(stop, []string{"sleep", "20s"})
response <- result
}()
close(stop)
select {
case v := <-response:
if v != "killed" {
t.Error("Unexpected result", response)
}
case <-time.After(2 * time.Second):
t.Error("Channel doesn't works")
}
}

View File

@ -1 +1,118 @@
package main package main
import (
"fmt"
"github.com/fsnotify/fsnotify"
"io/ioutil"
"os"
"runtime"
"testing"
"time"
)
func TestPoller_AddRemove(t *testing.T) {
w := PollingWatcher()
if err := w.Add("no-such-file"); err == nil {
t.Fatal("should have gotten error when adding a non-existent file")
}
if err := w.Remove("no-such-file"); err == nil {
t.Fatal("should have gotten error when removing non-existent watch")
}
f, err := ioutil.TempFile("", "asdf")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(f.Name())
if err := w.Add(f.Name()); err != nil {
t.Fatal(err)
}
if err := w.Remove(f.Name()); err != nil {
t.Fatal(err)
}
}
func TestPoller_Event(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("No chmod on Windows")
}
w := PollingWatcher()
f, err := ioutil.TempFile("", "test-poller")
if err != nil {
t.Fatal("error creating temp file")
}
defer os.RemoveAll(f.Name())
f.Close()
if err := w.Add(f.Name()); err != nil {
t.Fatal(err)
}
select {
case <-w.Events():
t.Fatal("got event before anything happened")
case <-w.Errors():
t.Fatal("got error before anything happened")
default:
}
if err := ioutil.WriteFile(f.Name(), []byte("hello"), 0644); err != nil {
t.Fatal(err)
}
if err := assertEvent(w, fsnotify.Write); err != nil {
t.Fatal(err)
}
if err := os.Chmod(f.Name(), 600); err != nil {
t.Fatal(err)
}
if err := assertEvent(w, fsnotify.Chmod); err != nil {
t.Fatal(err)
}
if err := os.Remove(f.Name()); err != nil {
t.Fatal(err)
}
if err := assertEvent(w, fsnotify.Remove); err != nil {
t.Fatal(err)
}
}
func TestPoller_Close(t *testing.T) {
w := PollingWatcher()
if err := w.Close(); err != nil {
t.Fatal(err)
}
// test double-close
if err := w.Close(); err != nil {
t.Fatal(err)
}
f, err := ioutil.TempFile("", "asdf")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(f.Name())
if err := w.Add(f.Name()); err == nil {
t.Fatal("should have gotten error adding watch for closed watcher")
}
}
func assertEvent(w FileWatcher, eType fsnotify.Op) error {
var err error
select {
case e := <-w.Events():
if e.Op != eType {
err = fmt.Errorf("got wrong event type, expected %q: %v", eType, e.Op)
}
case e := <-w.Errors():
err = fmt.Errorf("got unexpected error waiting for events %v: %v", eType, e)
case <-time.After(time.Duration(1) * time.Second):
err = fmt.Errorf("timeout waiting for event %v", eType)
}
return err
}

View File

@ -1,45 +1,35 @@
package main package main
//import ( import (
// "flag" "fmt"
// "fmt" "gopkg.in/urfave/cli.v2"
// "github.com/tockins/realize/settings" "reflect"
// "github.com/tockins/realize/style" "testing"
// "github.com/tockins/realize/watcher" )
// "gopkg.in/urfave/cli.v2"
// "testing" func TestPrefix(t *testing.T) {
//) input := random(10)
// value := fmt.Sprint(yellow.bold("[")+"REALIZE"+yellow.bold("]"), input)
//func TestPrefix(t *testing.T) { result := prefix(input)
// input := random(10) if result == "" {
// value := fmt.Sprint(yellow.bold("[")+"REALIZE"+yellow.bold("]"), input) t.Fatal("Expected a string")
// result := prefix(input) }
// if result == "" { if result != value {
// t.Fatal("Expected a string") t.Fatal("Expected", value, "Instead", result)
// } }
// if result != value { }
// t.Fatal("Expected", value, "Instead", result)
// } func TestBefore(t *testing.T) {
//} context := cli.Context{}
// if err := before(&context); err != nil {
//func TestBefore(t *testing.T) { t.Fatal(err)
// context := cli.Context{} }
// if err := before(&context); err != nil { }
// t.Fatal(err)
// } func TestNew(t *testing.T) {
//} r := new()
// t.Log(reflect.TypeOf(r).String())
//func TestInsert(t *testing.T) { if reflect.TypeOf(r).String() != "main.realize" {
// b := Blueprint{} t.Error("Expected a realize struct")
// b.Settings = &Settings{} }
// set := flag.NewFlagSet("test", 0) }
// set.String("name", random(5), "")
// set.String("path", random(5), "")
// params := cli.NewContext(nil, set, nil)
// if err := insert(params, &b); err != nil {
// t.Fatal(err)
// }
// if len(b.Projects) == 0 {
// t.Error("Expected one project")
// }
//}

View File

@ -1,26 +1,19 @@
package main package main
//
//import ( //import (
// "fmt" // "fmt"
// "github.com/tockins/realize/settings"
// "net/http" // "net/http"
// "testing" // "testing"
//) //)
// //
//func TestServer_Start(t *testing.T) { //func TestServer_Start(t *testing.T) {
// s := settings.Settings{ // s := Server{
// Server: settings.Server{ // Status: true,
// Status: true, // Open: false,
// Open: false, // Host: "localhost",
// Host: "localhost", // Port: 5000,
// Port: 5000,
// },
// } // }
// server := Server{ // err := s.start(nil)
// Settings: &s,
// }
// err := server.Start(nil)
// if err != nil { // if err != nil {
// t.Fatal(err) // t.Fatal(err)
// } // }
@ -51,17 +44,12 @@ package main
// } // }
//} //}
// //
//func TestOpen(t *testing.T) { //func TestServer_Open(t *testing.T) {
// config := settings.Settings{ // c := Server{
// Server: settings.Server{ // Open: true,
// Open: true,
// },
// }
// s := Server{
// Settings: &config,
// } // }
// url := "open_test" // url := "open_test"
// out, err := s.openURL(url) // out, err := c.openURL(url)
// if err == nil { // if err == nil {
// t.Fatal("Unexpected, invalid url", url, err) // t.Fatal("Unexpected, invalid url", url, err)
// } // }

View File

@ -1,149 +1,148 @@
package main package main
// import (
//import ( "errors"
// "errors" "io/ioutil"
// "io/ioutil" "os"
// "os" "path/filepath"
// "path/filepath" "strings"
// "strings" "testing"
// "testing" )
//)
// func TestSettings_Flimit(t *testing.T) {
//func TestSettings_Flimit(t *testing.T) { s := Settings{}
// s := Settings{} s.FileLimit = 100
// s.FileLimit = 100 if err := s.flimit(); err != nil {
// if err := s.flimit(); err != nil { t.Fatal("Unable to increase limit", err)
// t.Fatal("Unable to increase limit", err) }
// } }
//}
// func TestSettings_Stream(t *testing.T) {
//func TestSettings_Stream(t *testing.T) { s := Settings{}
// s := Settings{} filename := random(4)
// filename := random(4) if _, err := s.stream(filename); err == nil {
// if _, err := s.stream(filename); err == nil { t.Fatal("Error expected, none found", filename, err)
// t.Fatal("Error expected, none found", filename, err) }
// }
// filename = "settings.go"
// filename = "io.go" if _, err := s.stream(filename); err != nil {
// if _, err := s.stream(filename); err != nil { t.Fatal("Error unexpected", filename, err)
// t.Fatal("Error unexpected", filename, err) }
// } }
//}
// func TestSettings_Write(t *testing.T) {
//func TestSettings_Write(t *testing.T) { s := Settings{}
// s := Settings{} data := "abcdefgh"
// data := "abcdefgh" d, err := ioutil.TempFile("", "io_test")
// d, err := ioutil.TempFile("", "io_test") if err != nil {
// if err != nil { t.Fatal(err)
// t.Fatal(err) }
// } if err := s.write(d.Name(), []byte(data)); err != nil {
// if err := s.write(d.Name(), []byte(data)); err != nil { t.Fatal(err)
// t.Fatal(err) }
// } }
//}
// func TestSettings_Create(t *testing.T) {
//func TestSettings_Create(t *testing.T) { s := Settings{}
// s := Settings{} p, err := os.Getwd()
// p, err := os.Getwd() if err != nil {
// if err != nil { t.Fatal(err)
// t.Fatal(err) }
// } f := s.create(p, "io_test")
// f := s.create(p, "io_test") os.Remove(f.Name())
// os.Remove(f.Name()) }
//}
// func TestSettings_Read(t *testing.T) {
//func TestSettings_Read(t *testing.T) { s := Settings{}
// s := Settings{} var a interface{}
// var a interface{} s.file = "settings_b"
// s.File = "settings_b" if err := s.read(a); err == nil {
// if err := s.read(a); err == nil { t.Fatal("Error unexpected", err)
// t.Fatal("Error unexpected", err) }
// }
// s.file = "settings_test.yaml"
// s.File = "settings_test.yaml" dir, err := ioutil.TempDir("", directory)
// dir, err := ioutil.TempDir("", Directory) if err != nil {
// if err != nil { t.Fatal(err)
// t.Fatal(err) }
// } d, err := ioutil.TempFile(dir, "settings_test.yaml")
// d, err := ioutil.TempFile(dir, "settings_test.yaml") if err != nil {
// if err != nil { t.Fatal(err)
// t.Fatal(err) }
// } s.file = d.Name()
// s.File = d.Name() if err := s.read(a); err != nil {
// if err := s.read(a); err != nil { t.Fatal("Error unexpected", err)
// t.Fatal("Error unexpected", err) }
// } }
//}
// func TestSettings_Del(t *testing.T) {
//func TestSettings_Remove(t *testing.T) { s := Settings{}
// s := Settings{} if err := s.del("abcd"); err == nil {
// if err := s.delete("abcd"); err == nil { t.Fatal("Error unexpected, dir dosn't exist", err)
// t.Fatal("Error unexpected, dir dosn't exist", err) }
// }
// d, err := ioutil.TempDir("", "settings_test")
// d, err := ioutil.TempDir("", "settings_test") if err != nil {
// if err != nil { t.Fatal(err)
// t.Fatal(err) }
// } if err := s.del(d); err != nil {
// if err := s.delete(d); err != nil { t.Fatal("Error unexpected, dir exist", err)
// t.Fatal("Error unexpected, dir exist", err) }
// } }
//}
// func TestSettings_Record(t *testing.T) {
//func TestSettings_Record(t *testing.T) { s := Settings{}
// s := Settings{} s.file = "settings_test.yaml"
// s.File = "settings_test.yaml" var a interface{}
// var a interface{} if err := s.record(a); err != nil {
// if err := s.record(a); err != nil { t.Fatal(err)
// t.Fatal(err) }
// } s.del(filepath.Join(directory, s.file))
// s.delete(filepath.Join(Directory, s.File)) }
//}
// func TestSettings_Wdir(t *testing.T) {
//func TestSettings_Wdir(t *testing.T) { s := Settings{}
// s := Settings{} expected, err := os.Getwd()
// expected, err := os.Getwd() if err != nil {
// if err != nil { t.Error(err)
// t.Error(err) }
// } result := s.wdir()
// result := s.wdir() if result != filepath.Base(expected) {
// if result != filepath.Base(expected) { t.Error("Expected", filepath.Base(expected), "instead", result)
// t.Error("Expected", filepath.Base(expected), "instead", result) }
// } }
//}
// func TestSettings_Validate(t *testing.T) {
//func TestSettings_Validate(t *testing.T) { s := Settings{}
// s := Settings{} input := errors.New("")
// input := errors.New("") input = nil
// input = nil if err := s.validate(input); err != nil {
// if err := s.validate(input); err != nil { t.Error("Expected", input, "instead", err)
// t.Error("Expected", input, "instead", err) }
// } }
//}
// func TestSettings_Name(t *testing.T) {
//func TestSettings_Name(t *testing.T) { s := Settings{}
// s := Settings{} name := random(8)
// name := random(8) path := random(5)
// path := random(5) dir, err := os.Getwd()
// dir, err := os.Getwd() if err != nil {
// if err != nil { t.Fatal(err)
// t.Fatal(err) }
// } result := s.name(name, path)
// result := s.name(name, path) if result != dir && result != filepath.Base(path) {
// if result != dir && result != filepath.Base(path) { t.Fatal("Expected", dir, "or", filepath.Base(path), "instead", result)
// t.Fatal("Expected", dir, "or", filepath.Base(path), "instead", result) }
// }
// }
//}
// func TestSettings_Path(t *testing.T) {
//func TestSettings_Path(t *testing.T) { s := Settings{}
// s := Settings{} path := random(5)
// path := random(5) expected := strings.Replace(filepath.Clean(path), "\\", "/", -1)
// expected := strings.Replace(filepath.Clean(path), "\\", "/", -1) result := s.path(path)
// result := s.path(path) if result != expected {
// if result != expected { t.Fatal("Expected", expected, "instead", result)
// t.Fatal("Expected", expected, "instead", result) }
// }
// }
//}

View File

@ -1,35 +1,36 @@
package main package main
// import (
//import ( "bytes"
// "bytes" "fmt"
// "fmt" "github.com/fatih/color"
// "github.com/fatih/color" "testing"
// "testing" )
//)
// func TestStyle_Regular(t *testing.T) {
//func TestStyle_Regular(t *testing.T) { strs := []string{"a", "b", "c"}
// strs := []string{"a", "b", "c"} input := make([]interface{}, len(strs))
// input := make([]interface{}, len(strs)) for i, s := range strs {
// for i, s := range strs { input[i] = s
// input[i] = s }
// } result := red.regular(input)
// result := style.Regular(input) c := color.New(color.FgRed).SprintFunc()
// expected := fmt.Sprint(input) expected := fmt.Sprint(c(input))
// if !bytes.Equal([]byte(result), []byte(expected)) { if !bytes.Equal([]byte(result), []byte(expected)) {
// t.Error("Expected:", expected, "instead", result) t.Error("Expected:", expected, "instead", result)
// } }
//} }
//
//func TestStyle_Bold(t *testing.T) { func TestStyle_Bold(t *testing.T) {
// strs := []string{"a", "b", "c"} strs := []string{"a", "b", "c"}
// input := make([]interface{}, len(strs)) input := make([]interface{}, len(strs))
// for i, s := range strs { for i, s := range strs {
// input[i] = s input[i] = s
// } }
// result := style.Bold(input) result := red.bold(input)
// expected := fmt.Sprint(input) c := color.New(color.FgRed, color.Bold).SprintFunc()
// if !bytes.Equal([]byte(result), []byte(expected)) { expected := fmt.Sprint(c(input))
// t.Error("Expected:", expected, "instead", result) if !bytes.Equal([]byte(result), []byte(expected)) {
// } t.Error("Expected:", expected, "instead", result)
//} }
}

View File

@ -1,60 +1,62 @@
package main package main
// import (
//import ( "flag"
// "flag" "gopkg.in/urfave/cli.v2"
// "gopkg.in/urfave/cli.v2" "os"
// "os" "path/filepath"
// "path/filepath" "testing"
// "testing" )
//)
// func TestArgsParam(t *testing.T) {
//func TestArgsParam(t *testing.T) { set := flag.NewFlagSet("test", 0)
// set := flag.NewFlagSet("test", 0) set.Bool("myflag", false, "doc")
// set.Bool("myflag", false, "doc") p := cli.NewContext(nil, set, nil)
// params := cli.NewContext(nil, set, nil) set.Parse([]string{"--myflag", "bat", "baz"})
// set.Parse([]string{"--myflag", "bat", "baz"}) result := params(p)
// result := argsParam(params) if len(result) != 2 {
// if len(result) != 2 { t.Fatal("Expected 2 instead", len(result))
// t.Fatal("Expected 2 instead", len(result)) }
// } }
//}
// func TestDuplicates(t *testing.T) {
//func TestDuplicates(t *testing.T) { projects := []Project{
// projects := []Project{ {
// { Name: "a",
// Name: "a", Path: "a",
// }, { }, {
// Name: "b", Name: "a",
// }, { Path: "a",
// Name: "c", }, {
// }, Name: "c",
// } Path: "c",
// _, err := duplicates(projects[0], projects) },
// if err == nil { }
// t.Fatal("Error unexpected", err) _, err := duplicates(projects[0], projects)
// } if err == nil {
// _, err = duplicates(Project{}, projects) t.Fatal("Error unexpected", err)
// if err != nil { }
// t.Fatal("Error unexpected", err) _, err = duplicates(Project{}, projects)
// } if err != nil {
// t.Fatal("Error unexpected", err)
//} }
//
//func TestInArray(t *testing.T) { }
// arr := []string{"a", "b", "c"}
// if !inArray(arr[0], arr) { func TestInArray(t *testing.T) {
// t.Fatal("Unexpected", arr[0], "should be in", arr) arr := []string{"a", "b", "c"}
// } if !array(arr[0], arr) {
// if inArray("d", arr) { t.Fatal("Unexpected", arr[0], "should be in", arr)
// t.Fatal("Unexpected", "d", "shouldn't be in", arr) }
// } if array("d", arr) {
//} t.Fatal("Unexpected", "d", "shouldn't be in", arr)
// }
//func TestGetEnvPath(t *testing.T) { }
// expected := filepath.SplitList(os.Getenv("GOPATH"))[0]
// result := getEnvPath("GOPATH") func TestGetEnvPath(t *testing.T) {
// if expected != result { expected := filepath.SplitList(os.Getenv("GOPATH"))[0]
// t.Fatal("Expected", expected, "instead", result) result := getEnvPath("GOPATH")
// } if expected != result {
//} t.Fatal("Expected", expected, "instead", result)
}
}