Skip to content

Commit 5ba30ae

Browse files
committed
feat: add brace expansion support
1 parent 08f2907 commit 5ba30ae

File tree

4 files changed

+77
-8
lines changed

4 files changed

+77
-8
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ Usage:
3939

4040
Available Commands:
4141
completion Generate the autocompletion script for the specified shell
42+
func Print all the supported functions
4243
help Help about any command
4344
json Print the JSON schema of the test suites struct
4445
run Run the test suite
4546
sample Generate a sample test case YAML file
4647
server Run as a server mode
48+
service Install atest as a Linux service
4749

4850
Flags:
4951
-h, --help help for atest

cmd/run.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/linuxsuren/api-testing/pkg/render"
1717
"github.com/linuxsuren/api-testing/pkg/runner"
1818
"github.com/linuxsuren/api-testing/pkg/testing"
19+
"github.com/linuxsuren/api-testing/pkg/util"
1920
"github.com/spf13/cobra"
2021
"golang.org/x/sync/semaphore"
2122
)
@@ -71,7 +72,7 @@ See also https://github.com/LinuxSuRen/api-testing/tree/master/sample`,
7172
// set flags
7273
flags := cmd.Flags()
7374
flags.StringVarP(&opt.pattern, "pattern", "p", "test-suite-*.yaml",
74-
"The file pattern which try to execute the test cases")
75+
"The file pattern which try to execute the test cases. Brace expansion is supported, such as: test-suite-{1,2}.yaml")
7576
flags.StringVarP(&opt.level, "level", "l", "info", "Set the output log level")
7677
flags.DurationVarP(&opt.duration, "duration", "", 0, "Running duration")
7778
flags.DurationVarP(&opt.requestTimeout, "request-timeout", "", time.Minute, "Timeout for per request")
@@ -125,7 +126,6 @@ func (o *runOption) preRunE(cmd *cobra.Command, args []string) (err error) {
125126
}
126127

127128
func (o *runOption) runE(cmd *cobra.Command, args []string) (err error) {
128-
var files []string
129129
o.startTime = time.Now()
130130
o.context = cmd.Context()
131131
o.limiter = limit.NewDefaultRateLimiter(o.qps, o.burst)
@@ -134,12 +134,20 @@ func (o *runOption) runE(cmd *cobra.Command, args []string) (err error) {
134134
o.limiter.Stop()
135135
}()
136136

137-
if files, err = filepath.Glob(o.pattern); err == nil {
138-
for i := range files {
139-
item := files[i]
140-
if err = o.runSuiteWithDuration(item); err != nil {
141-
break
142-
}
137+
var suites []string
138+
for _, pattern := range util.Expand(o.pattern) {
139+
var files []string
140+
if files, err = filepath.Glob(pattern); err == nil {
141+
suites = append(suites, files...)
142+
}
143+
}
144+
145+
cmd.Println("found suites:", len(suites))
146+
for i := range suites {
147+
item := suites[i]
148+
cmd.Println("run suite:", item)
149+
if err = o.runSuiteWithDuration(item); err != nil {
150+
break
143151
}
144152
}
145153

pkg/util/expand.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package util
2+
3+
import (
4+
"regexp"
5+
"strings"
6+
)
7+
8+
// Expand the text with brace syntax.
9+
// Such as: /home/{good,bad} -> [/home/good, /home/bad]
10+
func Expand(text string) (result []string) {
11+
reg := regexp.MustCompile(`\{.*\}`)
12+
if reg.MatchString(text) {
13+
brace := reg.FindString(text)
14+
braceItem := strings.TrimPrefix(brace, "{")
15+
braceItem = strings.TrimSuffix(braceItem, "}")
16+
items := strings.Split(braceItem, ",")
17+
18+
for _, item := range items {
19+
result = append(result, strings.ReplaceAll(text, brace, item))
20+
}
21+
} else {
22+
result = []string{text}
23+
}
24+
return
25+
}

pkg/util/expand_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package util_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/linuxsuren/api-testing/pkg/util"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestExpand(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
input string
14+
expect []string
15+
}{{
16+
name: "without brace",
17+
input: "/home",
18+
expect: []string{"/home"},
19+
}, {
20+
name: "with brace",
21+
input: "/home/{good,bad}",
22+
expect: []string{"/home/good", "/home/bad"},
23+
}, {
24+
name: "with brace, have suffix",
25+
input: "/home/{good,bad}.yaml",
26+
expect: []string{"/home/good.yaml", "/home/bad.yaml"},
27+
}}
28+
for _, tt := range tests {
29+
t.Run(tt.name, func(t *testing.T) {
30+
got := util.Expand(tt.input)
31+
assert.Equal(t, tt.expect, got, got)
32+
})
33+
}
34+
}

0 commit comments

Comments
 (0)