From 81b18593f2432704145266e065dc5ddffda022a9 Mon Sep 17 00:00:00 2001 From: ak1ra24 Date: Wed, 5 Feb 2020 11:26:49 +0900 Subject: [PATCH] add testcmd option: specify all or testname --- command_func.go | 21 ++++++++++++- internal/pkg/shell/shell.go | 9 ++---- internal/pkg/shell/shell_test.go | 51 +++++++++++++++----------------- 3 files changed, 47 insertions(+), 34 deletions(-) diff --git a/command_func.go b/command_func.go index 705ea0e..2330d64 100644 --- a/command_func.go +++ b/command_func.go @@ -307,7 +307,26 @@ func CmdTest(c *cli.Context) error { return err } - tnTestCmds := shell.TnTestCmdExec(tnconfig.Test) + testName := c.Args().Get(0) + + var tnTestCmds []string + + if testName == "all" || testName == "" { + for _, test := range tnconfig.Test { + tnTestCmds = test.TnTestCmdExec() + } + } else { + for _, test := range tnconfig.Test { + if testName == test.Name { + tnTestCmds = test.TnTestCmdExec() + } + } + } + + if len(tnTestCmds) == 0 { + return fmt.Errorf("not found test name\n") + } + fmt.Fprintln(os.Stdout, strings.Join(tnTestCmds, "\n")) return nil diff --git a/internal/pkg/shell/shell.go b/internal/pkg/shell/shell.go index fb3dd77..21458a7 100644 --- a/internal/pkg/shell/shell.go +++ b/internal/pkg/shell/shell.go @@ -365,14 +365,11 @@ func Pull(nodes []Node) []string { } // TnTestCmdExec Execute test cmds -func TnTestCmdExec(tests []Test) []string { +func (t *Test) TnTestCmdExec() []string { var tnTestCmds []string - for _, test := range tests { - - for _, testCmd := range test.Cmds { - tnTestCmds = append(tnTestCmds, testCmd.Cmd) - } + for _, testCmd := range t.Cmds { + tnTestCmds = append(tnTestCmds, testCmd.Cmd) } return tnTestCmds diff --git a/internal/pkg/shell/shell_test.go b/internal/pkg/shell/shell_test.go index 7a54946..8af6b4e 100644 --- a/internal/pkg/shell/shell_test.go +++ b/internal/pkg/shell/shell_test.go @@ -387,24 +387,21 @@ func TestPull(t *testing.T) { } func TestTnTestCmdExec(t *testing.T) { - type args struct { - tests []Test + type fields struct { + Name string + Cmds []Cmd } tests := []struct { - name string - args args - want []string + name string + fields fields + want []string }{ { name: "test name not set", - args: args{ - tests: []Test{ - Test{ - Cmds: []Cmd{ - Cmd{ - Cmd: "echo slankdev", - }, - }, + fields: fields{ + Cmds: []Cmd{ + Cmd{ + Cmd: "echo slankdev", }, }, }, @@ -412,18 +409,14 @@ func TestTnTestCmdExec(t *testing.T) { }, { name: "test name set", - args: args{ - tests: []Test{ - Test{ - Name: "p2p", - Cmds: []Cmd{ - Cmd{ - Cmd: "docker exec R1 echo hello", - }, - Cmd{ - Cmd: "docker exec R2 echo world", - }, - }, + fields: fields{ + Name: "p2p", + Cmds: []Cmd{ + Cmd{ + Cmd: "docker exec R1 echo hello", + }, + Cmd{ + Cmd: "docker exec R2 echo world", }, }, }, @@ -432,8 +425,12 @@ func TestTnTestCmdExec(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := TnTestCmdExec(tt.args.tests); !reflect.DeepEqual(got, tt.want) { - t.Errorf("TnTestCmdExec() = %v, want %v", got, tt.want) + tr := &Test{ + Name: tt.fields.Name, + Cmds: tt.fields.Cmds, + } + if got := tr.TnTestCmdExec(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Test.TnTestCmdExec() = %v, want %v", got, tt.want) } }) }