55 "fmt"
66 "io"
77 "os"
8- "path"
9- "path/filepath"
108 "strings"
119 "sync"
1210 "time"
@@ -16,7 +14,6 @@ import (
1614 "github.com/linuxsuren/api-testing/pkg/render"
1715 "github.com/linuxsuren/api-testing/pkg/runner"
1816 "github.com/linuxsuren/api-testing/pkg/testing"
19- "github.com/linuxsuren/api-testing/pkg/util"
2017 "github.com/spf13/cobra"
2118 "golang.org/x/sync/semaphore"
2219)
@@ -40,12 +37,16 @@ type runOption struct {
4037 swaggerURL string
4138 level string
4239 caseItems []string
40+
41+ // for internal use
42+ loader testing.Loader
4343}
4444
4545func newDefaultRunOption () * runOption {
4646 return & runOption {
4747 reporter : runner .NewMemoryTestReporter (),
4848 reportWriter : runner .NewResultWriter (os .Stdout ),
49+ loader : testing .NewFileLoader (),
4950 }
5051}
5152
@@ -134,19 +135,13 @@ func (o *runOption) runE(cmd *cobra.Command, args []string) (err error) {
134135 o .limiter .Stop ()
135136 }()
136137
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- }
138+ if err = o .loader .Put (o .pattern ); err != nil {
139+ return
143140 }
144141
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 {
142+ cmd .Println ("found suites:" , o .loader .GetCount ())
143+ for o .loader .HasMore () {
144+ if err = o .runSuiteWithDuration (o .loader ); err != nil {
150145 break
151146 }
152147 }
@@ -166,7 +161,7 @@ func (o *runOption) runE(cmd *cobra.Command, args []string) (err error) {
166161 return
167162}
168163
169- func (o * runOption ) runSuiteWithDuration (suite string ) (err error ) {
164+ func (o * runOption ) runSuiteWithDuration (loader testing. Loader ) (err error ) {
170165 sem := semaphore .NewWeighted (o .thread )
171166 stop := false
172167 var timeout * time.Ticker
@@ -204,7 +199,7 @@ func (o *runOption) runSuiteWithDuration(suite string) (err error) {
204199 }()
205200
206201 dataContext := getDefaultContext ()
207- ch <- o .runSuite (suite , dataContext , o .context , stopSingal )
202+ ch <- o .runSuite (loader , dataContext , o .context , stopSingal )
208203 }(errChannel , sem )
209204 if o .duration <= 0 {
210205 stop = true
@@ -221,9 +216,14 @@ func (o *runOption) runSuiteWithDuration(suite string) (err error) {
221216 return
222217}
223218
224- func (o * runOption ) runSuite (suite string , dataContext map [string ]interface {}, ctx context.Context , stopSingal chan struct {}) (err error ) {
219+ func (o * runOption ) runSuite (loader testing.Loader , dataContext map [string ]interface {}, ctx context.Context , stopSingal chan struct {}) (err error ) {
220+ var data []byte
221+ if data , err = loader .Load (); err != nil {
222+ return
223+ }
224+
225225 var testSuite * testing.TestSuite
226- if testSuite , err = testing .Parse (suite ); err != nil {
226+ if testSuite , err = testing .Parse (data ); err != nil {
227227 return
228228 }
229229
@@ -253,7 +253,7 @@ func (o *runOption) runSuite(suite string, dataContext map[string]interface{}, c
253253 o .limiter .Accept ()
254254
255255 ctxWithTimeout , _ := context .WithTimeout (ctx , o .requestTimeout )
256- ctxWithTimeout = context .WithValue (ctxWithTimeout , runner .ContextKey ("" ).ParentDir (), path . Dir ( suite ))
256+ ctxWithTimeout = context .WithValue (ctxWithTimeout , runner .ContextKey ("" ).ParentDir (), loader . GetContext ( ))
257257
258258 simpleRunner := runner .NewSimpleTestCaseRunner ()
259259 simpleRunner .WithTestReporter (o .reporter )
0 commit comments