2017-10-08 21:09:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"gopkg.in/urfave/cli.v2"
|
2017-10-14 21:40:39 +00:00
|
|
|
"log"
|
2017-10-15 19:35:50 +00:00
|
|
|
"os"
|
2017-10-08 21:09:45 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2017-10-22 21:54:02 +00:00
|
|
|
"path/filepath"
|
2017-10-08 21:09:45 +00:00
|
|
|
)
|
|
|
|
|
2017-10-15 19:35:50 +00:00
|
|
|
type loggerT struct{}
|
2017-10-14 21:40:39 +00:00
|
|
|
|
2017-10-15 19:35:50 +00:00
|
|
|
func (loggerT) Write(bytes []byte) (int, error) {
|
2017-10-14 21:40:39 +00:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
log.SetFlags(0)
|
2017-10-15 19:35:50 +00:00
|
|
|
log.SetOutput(loggerT{})
|
|
|
|
os.Exit(m.Run())
|
2017-10-14 21:40:39 +00:00
|
|
|
}
|
|
|
|
|
2017-10-14 21:52:26 +00:00
|
|
|
func TestRealize_Clean(t *testing.T) {
|
2017-10-08 21:24:15 +00:00
|
|
|
r := realize{}
|
|
|
|
r.Schema = append(r.Schema, Project{Name: "test0"})
|
|
|
|
r.Schema = append(r.Schema, Project{Name: "test0"})
|
|
|
|
r.clean()
|
|
|
|
if len(r.Schema) > 1 {
|
2017-10-08 21:09:45 +00:00
|
|
|
t.Error("Expected only one project")
|
|
|
|
}
|
2017-10-08 21:24:15 +00:00
|
|
|
r.Schema = append(r.Schema, Project{Path: "test1"})
|
|
|
|
r.Schema = append(r.Schema, Project{Path: "test1"})
|
|
|
|
r.clean()
|
|
|
|
if len(r.Schema) > 2 {
|
2017-10-08 21:09:45 +00:00
|
|
|
t.Error("Expected only one project")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-14 21:52:26 +00:00
|
|
|
func TestRealize_Add(t *testing.T) {
|
2017-10-08 21:24:15 +00:00
|
|
|
r := realize{}
|
2017-10-08 21:09:45 +00:00
|
|
|
// add all flags, test with expected
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("fmt", false, "")
|
|
|
|
set.Bool("vet", false, "")
|
|
|
|
set.Bool("test", false, "")
|
|
|
|
set.Bool("install", false, "")
|
|
|
|
set.Bool("run", false, "")
|
|
|
|
set.Bool("build", false, "")
|
|
|
|
set.Bool("generate", false, "")
|
2017-10-22 21:54:02 +00:00
|
|
|
set.String("path", wdir(), "")
|
2017-10-08 21:09:45 +00:00
|
|
|
c := cli.NewContext(nil, set, nil)
|
2017-10-22 21:54:02 +00:00
|
|
|
set.Parse([]string{"--fmt", "--install", "--run", "--build", "--generate", "--test", "--vet"})
|
2017-10-08 21:24:15 +00:00
|
|
|
r.add(c)
|
2017-10-08 21:09:45 +00:00
|
|
|
expected := Project{
|
2017-10-22 21:54:02 +00:00
|
|
|
Name: filepath.Base(wdir()),
|
|
|
|
Path: wdir(),
|
2017-10-08 21:09:45 +00:00
|
|
|
Cmds: Cmds{
|
|
|
|
Fmt: Cmd{
|
|
|
|
Status: true,
|
|
|
|
},
|
|
|
|
Install: Cmd{
|
|
|
|
Status: true,
|
|
|
|
},
|
|
|
|
Generate: Cmd{
|
|
|
|
Status: true,
|
|
|
|
},
|
|
|
|
Test: Cmd{
|
|
|
|
Status: true,
|
|
|
|
},
|
|
|
|
Build: Cmd{
|
|
|
|
Status: true,
|
|
|
|
},
|
|
|
|
Vet: Cmd{
|
|
|
|
Status: true,
|
|
|
|
},
|
|
|
|
Run: true,
|
|
|
|
},
|
|
|
|
Watcher: Watch{
|
|
|
|
Paths: []string{"/"},
|
2017-10-22 21:54:02 +00:00
|
|
|
Ignore: []string{".git",".realize","vendor"},
|
2017-10-08 21:09:45 +00:00
|
|
|
Exts: []string{"go"},
|
|
|
|
},
|
|
|
|
}
|
2017-10-08 21:24:15 +00:00
|
|
|
if !reflect.DeepEqual(r.Schema[0], expected) {
|
2017-10-08 21:09:45 +00:00
|
|
|
t.Error("Expected equal struct")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-15 19:35:50 +00:00
|
|
|
func TestRealize_Run(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
params := cli.NewContext(nil, set, nil)
|
|
|
|
m := make(map[string]string)
|
|
|
|
m["test"] = "test"
|
2017-10-08 21:24:15 +00:00
|
|
|
r := realize{}
|
2017-10-15 19:35:50 +00:00
|
|
|
r.Schema = []Project{
|
|
|
|
{
|
|
|
|
Name: "test0",
|
|
|
|
Path: ".",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "test1",
|
|
|
|
Path: "/test",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "test2",
|
|
|
|
Path: "/test",
|
|
|
|
},
|
2017-10-08 21:09:45 +00:00
|
|
|
}
|
2017-10-15 19:35:50 +00:00
|
|
|
go r.run(params)
|
|
|
|
time.Sleep(1 * time.Second)
|
2017-10-08 21:09:45 +00:00
|
|
|
}
|
|
|
|
|
2017-10-14 21:52:26 +00:00
|
|
|
func TestRealize_Remove(t *testing.T) {
|
2017-10-08 21:24:15 +00:00
|
|
|
r := realize{}
|
2017-10-08 21:09:45 +00:00
|
|
|
set := flag.NewFlagSet("name", 0)
|
|
|
|
set.String("name", "", "")
|
|
|
|
c := cli.NewContext(nil, set, nil)
|
|
|
|
set.Parse([]string{"--name=test0"})
|
2017-10-08 21:24:15 +00:00
|
|
|
err := r.remove(c)
|
2017-10-08 21:09:45 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected an error, there are no projects")
|
|
|
|
}
|
|
|
|
// Append a new project
|
2017-10-08 21:24:15 +00:00
|
|
|
r.Schema = append(r.Schema, Project{Name: "test0"})
|
|
|
|
err = r.remove(c)
|
2017-10-08 21:09:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Error unexpected, the project should be remove", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-15 19:35:50 +00:00
|
|
|
func TestRealize_Insert(t *testing.T) {
|
2017-10-08 21:24:15 +00:00
|
|
|
r := realize{}
|
2017-10-15 19:35:50 +00:00
|
|
|
// 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))
|
2017-10-08 21:09:45 +00:00
|
|
|
}
|
|
|
|
}
|