test refactoring

This commit is contained in:
asoseil 2017-10-08 23:24:15 +02:00
parent ae86421ed5
commit f38b2891ab

View File

@ -3,33 +3,30 @@ package main
import ( import (
"flag" "flag"
"gopkg.in/urfave/cli.v2" "gopkg.in/urfave/cli.v2"
"os"
"reflect" "reflect"
"testing" "testing"
"time" "time"
) )
func TestBlueprint_Clean(t *testing.T) { func TestBlueprint_Clean(t *testing.T) {
blp := Blueprint{} r := realize{}
blp.Settings = &Settings{} r.Schema = append(r.Schema, Project{Name: "test0"})
blp.Projects = append(blp.Projects, Project{Name: "test0"}) r.Schema = append(r.Schema, Project{Name: "test0"})
blp.Projects = append(blp.Projects, Project{Name: "test0"}) r.clean()
blp.clean() if len(r.Schema) > 1 {
if len(blp.Projects) > 1 {
t.Error("Expected only one project") t.Error("Expected only one project")
} }
blp.Projects = append(blp.Projects, Project{Path: "test1"}) r.Schema = append(r.Schema, Project{Path: "test1"})
blp.Projects = append(blp.Projects, Project{Path: "test1"}) r.Schema = append(r.Schema, Project{Path: "test1"})
blp.clean() r.clean()
if len(blp.Projects) > 2 { if len(r.Schema) > 2 {
t.Error("Expected only one project") t.Error("Expected only one project")
} }
} }
func TestBlueprint_Add(t *testing.T) { func TestBlueprint_Add(t *testing.T) {
blp := Blueprint{} r := realize{}
blp.Settings = &Settings{}
// add all flags, test with expected // add all flags, test with expected
set := flag.NewFlagSet("test", 0) set := flag.NewFlagSet("test", 0)
set.Bool("fmt", false, "") set.Bool("fmt", false, "")
@ -42,7 +39,7 @@ func TestBlueprint_Add(t *testing.T) {
set.String("path", "", "") set.String("path", "", "")
c := cli.NewContext(nil, set, nil) c := cli.NewContext(nil, set, nil)
set.Parse([]string{"--path=test_path", "--fmt", "--install", "--run", "--build", "--generate", "--test", "--vet"}) set.Parse([]string{"--path=test_path", "--fmt", "--install", "--run", "--build", "--generate", "--test", "--vet"})
blp.add(c) r.add(c)
expected := Project{ expected := Project{
Name: "test_path", Name: "test_path",
Path: "test_path", Path: "test_path",
@ -73,39 +70,37 @@ func TestBlueprint_Add(t *testing.T) {
Exts: []string{"go"}, Exts: []string{"go"},
}, },
} }
if !reflect.DeepEqual(blp.Projects[0], expected) { if !reflect.DeepEqual(r.Schema[0], expected) {
t.Error("Expected equal struct") t.Error("Expected equal struct")
} }
} }
func TestBlueprint_Check(t *testing.T) { func TestBlueprint_Check(t *testing.T) {
blp := Blueprint{} r := realize{}
blp.Settings = &Settings{} err := r.check()
err := blp.check()
if err == nil { if err == nil {
t.Error("There is no project, error expected") t.Error("There is no project, error expected")
} }
blp.Projects = append(blp.Projects, Project{Name: "test0"}) r.Schema = append(r.Schema, Project{Name: "test0"})
err = blp.check() err = r.check()
if err != nil { if err != nil {
t.Error("There is a project, error unexpected", err) t.Error("There is a project, error unexpected", err)
} }
} }
func TestBlueprint_Remove(t *testing.T) { func TestBlueprint_Remove(t *testing.T) {
blp := Blueprint{} r := realize{}
blp.Settings = &Settings{}
set := flag.NewFlagSet("name", 0) set := flag.NewFlagSet("name", 0)
set.String("name", "", "") set.String("name", "", "")
c := cli.NewContext(nil, set, nil) c := cli.NewContext(nil, set, nil)
set.Parse([]string{"--name=test0"}) set.Parse([]string{"--name=test0"})
err := blp.remove(c) err := r.remove(c)
if err == nil { if err == nil {
t.Error("Expected an error, there are no projects") t.Error("Expected an error, there are no projects")
} }
// Append a new project // Append a new project
blp.Projects = append(blp.Projects, Project{Name: "test0"}) r.Schema = append(r.Schema, Project{Name: "test0"})
err = blp.remove(c) err = r.remove(c)
if err != nil { if err != nil {
t.Error("Error unexpected, the project should be remove", err) t.Error("Error unexpected, the project should be remove", err)
} }
@ -116,26 +111,21 @@ func TestBlueprint_Run(t *testing.T) {
params := cli.NewContext(nil, set, nil) params := cli.NewContext(nil, set, nil)
m := make(map[string]string) m := make(map[string]string)
m["test"] = "test" m["test"] = "test"
projects := Blueprint{} r := realize{}
projects.Settings = &Settings{} r.Schema = []Project{
projects.Projects = []Project{
{ {
Name: "test0", Name: "test0",
Path: ".", Path: ".",
Environment: m,
}, },
{ {
Name: "test1", Name: "test1",
Path: ".", Path: "/test",
}, },
{ {
Name: "test1", Name: "test2",
Path: ".", Path: "/test",
}, },
} }
go projects.run(params) go r.run(params)
if os.Getenv("test") != "test" { time.Sleep(1 * time.Millisecond)
t.Error("Env variable seems different from that given", os.Getenv("test"), "expected", m["test"])
}
time.Sleep(5 * time.Second)
} }