From 8b203f9bb7466bdac6563deb19f8ec1eb0481f26 Mon Sep 17 00:00:00 2001 From: asoseil Date: Sun, 3 Dec 2017 15:44:38 +0100 Subject: [PATCH] tests nad bug fix --- realize.go | 5 +- realize/cli.go | 37 +++++++++------ realize/cli_test.go | 10 +++- realize/schema.go | 17 ++++++- realize/schema_test.go | 103 +++++++++++++++++++++++++++++++++++++++++ realize/settings.go | 1 + 6 files changed, 154 insertions(+), 19 deletions(-) diff --git a/realize.go b/realize.go index 7519e14..35bec6a 100644 --- a/realize.go +++ b/realize.go @@ -1168,7 +1168,7 @@ func start(c *cli.Context) (err error) { } if c.String("name") != "" { // filter by name flag if exist - r.Schema.Filter("name", c.String("name")) + r.Schema.Projects = r.Schema.Filter("Name", c.String("name")) } // increase file limit if r.Settings.FileLimit != 0 { @@ -1205,8 +1205,7 @@ func start(c *cli.Context) (err error) { } } // start workflow - r.Start() - return + return r.Start() } // Remove a project from an existing config diff --git a/realize/cli.go b/realize/cli.go index 0f2d50f..55e0c37 100644 --- a/realize/cli.go +++ b/realize/cli.go @@ -10,6 +10,7 @@ import ( "path/filepath" "strings" "time" + "github.com/go-siris/siris/core/errors" ) const ( @@ -70,23 +71,33 @@ func init() { } // Stop realize workflow -func (r *Realize) Stop() { - close(r.exit) +func (r *Realize) Stop() error{ + if r.exit != nil{ + close(r.exit) + return nil + }else{ + return errors.New("exit chan undefined") + } } // Start realize workflow -func (r *Realize) Start() { - r.exit = make(chan os.Signal, 1) - signal.Notify(r.exit, os.Interrupt) - for k := range r.Schema.Projects { - r.Schema.Projects[k].parent = r - go r.Schema.Projects[k].Watch(r.exit) - } - for { - select { - case <-r.exit: - return +func (r *Realize) Start() error { + if len(r.Schema.Projects) > 0 { + r.exit = make(chan os.Signal, 1) + signal.Notify(r.exit, os.Interrupt) + for k := range r.Schema.Projects { + r.Schema.Projects[k].parent = r + go r.Schema.Projects[k].Watch(r.exit) } + for { + select { + case <-r.exit: + return nil + } + } + return nil + }else{ + return errors.New("there are no projects") } } diff --git a/realize/cli_test.go b/realize/cli_test.go index 7f4b498..b03bd1a 100644 --- a/realize/cli_test.go +++ b/realize/cli_test.go @@ -21,6 +21,11 @@ func TestRealize_Stop(t *testing.T) { func TestRealize_Start(t *testing.T) { r := Realize{} + err := r.Start() + if err == nil{ + t.Error("Error expected") + } + r.Projects = append(r.Projects,Project{Name:"test"}) go func() { time.Sleep(100) close(r.exit) @@ -29,7 +34,10 @@ func TestRealize_Start(t *testing.T) { t.Error("Unexpected error", "channel should be closed") } }() - r.Start() + err = r.Start() + if err != nil{ + t.Error("Unexpected error", err) + } } func TestRealize_Prefix(t *testing.T) { diff --git a/realize/schema.go b/realize/schema.go index 3df05a4..4dcfbac 100644 --- a/realize/schema.go +++ b/realize/schema.go @@ -36,7 +36,7 @@ func (s *Schema) Remove(name string) error { // New create a project using cli fields func (s *Schema) New(c *cli.Context) Project { name := filepath.Base(c.String("path")) - if name == "." { + if len(name) == 0 || name == "." { name = filepath.Base(Wdir()) } project := Project{ @@ -76,4 +76,17 @@ func (s *Schema) New(c *cli.Context) Project { } // Filter project list by field -func (s *Schema) Filter(field string, value interface{}) {} +func (s *Schema) Filter(field string, value interface{}) []Project{ + result := []Project{} + for _, item := range s.Projects{ + v := reflect.ValueOf(item) + for i := 0; i < v.NumField(); i++ { + if v.Type().Field(i).Name == field { + if reflect.DeepEqual(v.Field(i).Interface(), value) { + result = append(result,item) + } + } + } + } + return result +} diff --git a/realize/schema_test.go b/realize/schema_test.go index 4b7b005..5e6c16f 100644 --- a/realize/schema_test.go +++ b/realize/schema_test.go @@ -1 +1,104 @@ package realize + +import ( + "testing" + "flag" + "gopkg.in/urfave/cli.v2" + "path/filepath" +) + +func TestSchema_Add(t *testing.T) { + r := Realize{} + p := Project{Name:"test"} + r.Add(p) + if len(r.Schema.Projects) != 1{ + t.Error("Unexpected error there are",len(r.Schema.Projects),"instead one") + } + r.Add(p) + if len(r.Schema.Projects) != 1{ + t.Error("Unexpected error there are",len(r.Schema.Projects),"instead one") + } + r.Add(Project{Name:"testing"}) + if len(r.Schema.Projects) != 2{ + t.Error("Unexpected error there are",len(r.Schema.Projects),"instead two") + } +} + +func TestSchema_Remove(t *testing.T) { + r := Realize{} + r.Schema.Projects = []Project{ + { + Name: "test", + },{ + Name: "testing", + },{ + Name: "testing", + }, + } + r.Remove("testing") + if len(r.Schema.Projects) != 2{ + t.Error("Unexpected errore there are",len(r.Schema.Projects),"instead one") + } +} + +func TestSchema_New(t *testing.T) { + r := Realize{} + 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{"--fmt", "--install", "--run", "--build", "--generate", "--test", "--vet"}) + p := r.New(c) + if p.Name != filepath.Base(Wdir()){ + t.Error("Unexpected error",p.Name,"instead",filepath.Base(Wdir())) + } + if !p.Tools.Install.Status{ + t.Error("Install should be enabled") + } + if !p.Tools.Fmt.Status{ + t.Error("Fmt should be enabled") + } + if !p.Tools.Run.Status{ + t.Error("Run should be enabled") + } + if !p.Tools.Build.Status{ + t.Error("Build should be enabled") + } + if !p.Tools.Generate.Status{ + t.Error("Generate should be enabled") + } + if !p.Tools.Test.Status{ + t.Error("Test should be enabled") + } + if !p.Tools.Vet.Status{ + t.Error("Vet should be enabled") + } +} + +func TestSchema_Filter(t *testing.T) { + r := Realize{} + r.Schema.Projects = []Project{ + { + Name: "test", + },{ + Name: "test", + }, + { + Name: "example", + }, + } + result := r.Filter("Name","test") + if len(result) != 2{ + t.Error("Expected two project") + } + result = r.Filter("Name","example") + if len(result) != 1{ + t.Error("Expected one project") + } +} \ No newline at end of file diff --git a/realize/settings.go b/realize/settings.go index b95ccac..acf061c 100644 --- a/realize/settings.go +++ b/realize/settings.go @@ -52,6 +52,7 @@ type Files struct { // Resource status and file name type Resource struct { Status bool + Path string Name string }