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"
"gopkg.in/urfave/cli.v2"
"log"
"os"
"reflect"
"testing"
"time"
)
type logT struct{}
type loggerT struct{}
func (logT) Write(bytes []byte) (int, error) {
func (loggerT) Write(bytes []byte) (int, error) {
return 0, nil
}
func TestMain(m *testing.M) {
log.SetFlags(0)
log.SetOutput(logT{})
log.SetOutput(loggerT{})
os.Exit(m.Run())
}
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) {
r := realize{}
// 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) {
set := flag.NewFlagSet("test", 0)
params := cli.NewContext(nil, set, nil)
@ -141,3 +125,44 @@ func TestRealize_Run(t *testing.T) {
go r.run(params)
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
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
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
//import (
// "flag"
// "fmt"
// "github.com/tockins/realize/settings"
// "github.com/tockins/realize/style"
// "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)
// result := prefix(input)
// if result == "" {
// t.Fatal("Expected a string")
// }
// if result != value {
// t.Fatal("Expected", value, "Instead", result)
// }
//}
//
//func TestBefore(t *testing.T) {
// context := cli.Context{}
// if err := before(&context); err != nil {
// t.Fatal(err)
// }
//}
//
//func TestInsert(t *testing.T) {
// b := Blueprint{}
// 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")
// }
//}
import (
"fmt"
"gopkg.in/urfave/cli.v2"
"reflect"
"testing"
)
func TestPrefix(t *testing.T) {
input := random(10)
value := fmt.Sprint(yellow.bold("[")+"REALIZE"+yellow.bold("]"), input)
result := prefix(input)
if result == "" {
t.Fatal("Expected a string")
}
if result != value {
t.Fatal("Expected", value, "Instead", result)
}
}
func TestBefore(t *testing.T) {
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())
if reflect.TypeOf(r).String() != "main.realize" {
t.Error("Expected a realize struct")
}
}

View File

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

View File

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

View File

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

View File

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