Skip to content

Commit 64a9a5d

Browse files
Smart test label normalization (#174)
* Normalize label hanlding in smart-test commands * Improve texts * PR feedback
1 parent 24fac27 commit 64a9a5d

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed

internal/command/smarttest/execution_cancel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func newCancel(tConfig *config.SmartTestExec) *cobra.Command {
1616
SmartTestExec: tConfig,
1717
}
1818
cmd := &cobra.Command{
19-
Use: "cancel [<execution-ID> | --run-id <run-ID>]",
19+
Use: "cancel [ID | --run-id RUN_ID]",
2020
Short: "Cancel a test execution",
2121
RunE: func(cmd *cobra.Command, args []string) error {
2222
return cancel(cmd.Context(), cfg, cmd.OutOrStdout(), cmd.ErrOrStderr(), args)

internal/command/smarttest/execution_get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func newGet(tConfig *config.SmartTestExec) *cobra.Command {
1414
SmartTestExec: tConfig,
1515
}
1616
cmd := &cobra.Command{
17-
Use: "get <execution-ID>",
17+
Use: "get ID",
1818
Short: "Get a test execution",
1919
Args: cobra.ExactArgs(1),
2020
RunE: func(cmd *cobra.Command, args []string) error {

internal/command/smarttest/execution_list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func newList(tConfig *config.SmartTestExec) *cobra.Command {
1616
SmartTestExec: tConfig,
1717
}
1818
cmd := &cobra.Command{
19-
Use: "list [--test-name <test-name> | --run-id <run-ID>]",
19+
Use: "list [filter-opts]",
2020
Short: "List test executions",
2121
RunE: func(cmd *cobra.Command, args []string) error {
2222
return list(cfg, cmd.OutOrStdout(), cmd.ErrOrStderr(), args)
@@ -59,7 +59,7 @@ func list(cfg *config.SmartTestExecList, wOut, wErr io.Writer, args []string) er
5959
params.WithExecutionPhase(&cfg.ExecutionPhase)
6060
}
6161
if len(cfg.Labels) > 0 {
62-
params.WithLabel(cfg.Labels)
62+
params.WithLabel(cfg.Labels.ToQueryFilter())
6363
}
6464
result, err := cfg.Client.TestExecutions.QueryTestExecutions(params, nil)
6565
if err != nil {

internal/config/smarttest.go

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type SmartTestRun struct {
2020
*SmartTest
2121
Directory string
2222
File string
23-
Labels RunLabels
23+
Labels TestExecLabels
2424
Cluster string
2525
Sandbox string
2626
RouteGroup string
@@ -29,9 +29,9 @@ type SmartTestRun struct {
2929
NoWait bool
3030
}
3131

32-
type RunLabels map[string]string
32+
type TestExecLabels map[string]string
3333

34-
func (rl RunLabels) String() string {
34+
func (rl TestExecLabels) String() string {
3535
keys := make([]string, 0, len(rl))
3636
for k := range rl {
3737
keys = append(keys, k)
@@ -47,32 +47,43 @@ func (rl RunLabels) String() string {
4747
return res.String()
4848
}
4949

50-
func (rl RunLabels) Set(v string) error {
50+
func (rl TestExecLabels) Set(v string) error {
5151
key, val, ok := strings.Cut(v, "=")
5252
if !ok {
53-
return fmt.Errorf("%q should be in form <key>:<value>", v)
53+
return fmt.Errorf("%q should be in form <key>=<value>", v)
5454
}
5555
rl[key] = val
5656
return nil
5757
}
5858

59-
func (tl RunLabels) Type() string {
59+
func (tl TestExecLabels) Type() string {
6060
return "labels"
6161
}
6262

63+
func (tl TestExecLabels) ToQueryFilter() []string {
64+
if len(tl) == 0 {
65+
return nil
66+
}
67+
result := make([]string, 0, len(tl))
68+
for k, v := range tl {
69+
result = append(result, k+":"+v)
70+
}
71+
return result
72+
}
73+
6374
// AddFlags adds the flags for the test run command
6475
func (c *SmartTestRun) AddFlags(cmd *cobra.Command) {
65-
cmd.Flags().StringVarP(&c.Directory, "directory", "d", "", "Base directory for finding tests")
66-
cmd.Flags().StringVarP(&c.File, "file", "f", "", "Smart Test file to run")
67-
cmd.Flags().StringVar(&c.Cluster, "cluster", "", "Cluster where to run tests")
68-
cmd.Flags().StringVar(&c.Sandbox, "sandbox", "", "Sandbox where to run tests")
69-
cmd.Flags().StringVar(&c.RouteGroup, "route-group", "", "Route group where to run tests")
70-
cmd.Flags().BoolVar(&c.Publish, "publish", false, "Publish test results")
76+
cmd.Flags().StringVarP(&c.Directory, "directory", "d", "", "base directory for finding tests")
77+
cmd.Flags().StringVarP(&c.File, "file", "f", "", "smart test file to run")
78+
cmd.Flags().StringVar(&c.Cluster, "cluster", "", "cluster where to run tests")
79+
cmd.Flags().StringVar(&c.Sandbox, "sandbox", "", "sandbox where to run tests")
80+
cmd.Flags().StringVar(&c.RouteGroup, "route-group", "", "route group where to run tests")
81+
cmd.Flags().BoolVar(&c.Publish, "publish", false, "publish test results")
7182
cmd.Flags().DurationVar(&c.Timeout, "timeout", 0, "timeout when waiting for the tests to complete, if 0 is specified, no timeout will be applied (default 0)")
7283
cmd.Flags().BoolVar(&c.NoWait, "no-wait", false, "do not wait until the tests are completed")
7384

7485
c.Labels = make(map[string]string)
75-
cmd.Flags().Var(c.Labels, "set-label", "set a label in form key:value for all test executions in the run")
86+
cmd.Flags().Var(&c.Labels, "set-label", "set a label in form key=value for all test executions in the run (can be specified multiple times)")
7687
}
7788

7889
type SmartTestExec struct {
@@ -92,18 +103,20 @@ type SmartTestExecList struct {
92103
RepoPath string
93104
RepoCommitSHA string
94105
ExecutionPhase string
95-
Labels []string
106+
Labels TestExecLabels
96107
}
97108

98109
func (c *SmartTestExecList) AddFlags(cmd *cobra.Command) {
99-
cmd.Flags().StringVar(&c.TestName, "test-name", "", "Filter test executions by test name")
100-
cmd.Flags().StringVar(&c.RunID, "run-id", "", "Filter test executions by run ID")
101-
cmd.Flags().StringVar(&c.Sandbox, "sandbox", "", "Filter test executions by sandbox name")
102-
cmd.Flags().StringVar(&c.Repo, "repo", "", "Filter test executions by repository name")
103-
cmd.Flags().StringVar(&c.RepoPath, "repo-path", "", "Filter test executions by repository path")
104-
cmd.Flags().StringVar(&c.RepoCommitSHA, "repo-commit-sha", "", "Filter test executions by repository commit SHA")
105-
cmd.Flags().StringVar(&c.ExecutionPhase, "phase", "", "Filter test executions by phase (one of 'pending', 'in_progress', 'succeeded', 'canceled' or 'failed')")
106-
cmd.Flags().StringArrayVar(&c.Labels, "label", []string{}, "Filter test executions by label in the format key:value (can be specified multiple times)")
110+
cmd.Flags().StringVar(&c.TestName, "test-name", "", "filter test executions by test name")
111+
cmd.Flags().StringVar(&c.RunID, "run-id", "", "filter test executions by run ID")
112+
cmd.Flags().StringVar(&c.Sandbox, "sandbox", "", "filter test executions by sandbox name")
113+
cmd.Flags().StringVar(&c.Repo, "repo", "", "filter test executions by repository name")
114+
cmd.Flags().StringVar(&c.RepoPath, "repo-path", "", "filter test executions by repository path")
115+
cmd.Flags().StringVar(&c.RepoCommitSHA, "repo-commit-sha", "", "filter test executions by repository commit SHA")
116+
cmd.Flags().StringVar(&c.ExecutionPhase, "phase", "", "filter test executions by phase (one of 'pending', 'in_progress', 'succeeded', 'canceled' or 'failed')")
117+
118+
c.Labels = make(map[string]string)
119+
cmd.Flags().Var(&c.Labels, "label", "filter test executions by label in the format key=value (can be specified multiple times)")
107120
}
108121

109122
type SmartTestExecCancel struct {
@@ -112,5 +125,5 @@ type SmartTestExecCancel struct {
112125
}
113126

114127
func (c *SmartTestExecCancel) AddFlags(cmd *cobra.Command) {
115-
cmd.Flags().StringVar(&c.RunID, "run-id", "", "Cancel all test executions of this run ID")
128+
cmd.Flags().StringVar(&c.RunID, "run-id", "", "cancel all test executions in the run")
116129
}

0 commit comments

Comments
 (0)