realize/cmd_test.go

132 lines
2.7 KiB
Go
Raw Normal View History

package main
import (
"flag"
"gopkg.in/urfave/cli.v2"
"reflect"
"testing"
"time"
)
func TestBlueprint_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 {
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 {
t.Error("Expected only one project")
}
}
func TestBlueprint_Add(t *testing.T) {
2017-10-08 21:24:15 +00:00
r := realize{}
// 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, "")
set.String("path", "", "")
c := cli.NewContext(nil, set, nil)
set.Parse([]string{"--path=test_path", "--fmt", "--install", "--run", "--build", "--generate", "--test", "--vet"})
2017-10-08 21:24:15 +00:00
r.add(c)
expected := Project{
Name: "test_path",
Path: "test_path",
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{"/"},
Ignore: []string{"vendor"},
Exts: []string{"go"},
},
}
2017-10-08 21:24:15 +00:00
if !reflect.DeepEqual(r.Schema[0], expected) {
t.Error("Expected equal struct")
}
}
func TestBlueprint_Check(t *testing.T) {
2017-10-08 21:24:15 +00:00
r := realize{}
err := r.check()
if err == nil {
t.Error("There is no project, error expected")
}
2017-10-08 21:24:15 +00:00
r.Schema = append(r.Schema, Project{Name: "test0"})
err = r.check()
if err != nil {
t.Error("There is a project, error unexpected", err)
}
}
func TestBlueprint_Remove(t *testing.T) {
2017-10-08 21:24:15 +00:00
r := realize{}
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)
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)
if err != nil {
t.Error("Error unexpected, the project should be remove", err)
}
}
func TestBlueprint_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{}
r.Schema = []Project{
{
2017-10-08 21:24:15 +00:00
Name: "test0",
Path: ".",
},
{
Name: "test1",
2017-10-08 21:24:15 +00:00
Path: "/test",
},
{
2017-10-08 21:24:15 +00:00
Name: "test2",
Path: "/test",
},
}
2017-10-08 21:24:15 +00:00
go r.run(params)
time.Sleep(1 * time.Millisecond)
}