11package  cmd
22
33import  (
4+ 	"fmt" 
45	"path" 
56	"path/filepath" 
7+ 	"strings" 
68
79	"github.com/linuxsuren/api-testing/pkg/runner" 
810	"github.com/linuxsuren/api-testing/pkg/testing" 
@@ -17,8 +19,12 @@ type runOption struct {
1719func  CreateRunCommand () (cmd  * cobra.Command ) {
1820	opt  :=  & runOption {}
1921	cmd  =  & cobra.Command {
20- 		Use :  "run" ,
21- 		RunE : opt .runE ,
22+ 		Use :     "run" ,
23+ 		Aliases : []string {"r" },
24+ 		Example : `atest run -p sample.yaml 
25+ See also https://github.com/LinuxSuRen/api-testing/tree/master/sample` ,
26+ 		Short : "Run the test suite" ,
27+ 		RunE :  opt .runE ,
2228	}
2329
2430	// set flags 
@@ -30,31 +36,47 @@ func CreateRunCommand() (cmd *cobra.Command) {
3036
3137func  (o  * runOption ) runE (cmd  * cobra.Command , args  []string ) (err  error ) {
3238	var  files  []string 
33- 
34- 	ctx  :=  map [string ]interface {}{}
39+ 	ctx  :=  getDefaultContext ()
3540
3641	if  files , err  =  filepath .Glob (o .pattern ); err  ==  nil  {
3742		for  i  :=  range  files  {
3843			item  :=  files [i ]
39- 
40- 			var  testSuite  * testing.TestSuite 
41- 			if  testSuite , err  =  testing .Parse (item ); err  !=  nil  {
44+ 			fmt .Println (item , "===" , o .pattern , args )
45+ 			if  err  =  runSuite (item , ctx ); err  !=  nil  {
4246				return 
4347			}
48+ 		}
49+ 	}
50+ 	return 
51+ }
4452
45- 			for  _ , testCase  :=  range  testSuite .Items  {
46- 				setRelativeDir (item , & testCase )
47- 				var  output  interface {}
48- 				if  output , err  =  runner .RunTestCase (& testCase , ctx ); err  !=  nil  {
49- 					return 
50- 				}
51- 				ctx [testCase .Name ] =  output 
52- 			}
53+ func  runSuite (suite  string , ctx  map [string ]interface {}) (err  error ) {
54+ 	var  testSuite  * testing.TestSuite 
55+ 	if  testSuite , err  =  testing .Parse (suite ); err  !=  nil  {
56+ 		return 
57+ 	}
58+ 
59+ 	testSuite .API  =  strings .TrimSuffix (testSuite .API , "/" )
60+ 	for  _ , testCase  :=  range  testSuite .Items  {
61+ 		// reuse the API prefix 
62+ 		if  strings .HasPrefix (testCase .Request .API , "/" ) {
63+ 			testCase .Request .API  =  fmt .Sprintf ("%s/%s" , testSuite .API , testCase .Request .API )
64+ 		}
65+ 
66+ 		setRelativeDir (suite , & testCase )
67+ 		var  output  interface {}
68+ 		if  output , err  =  runner .RunTestCase (& testCase , ctx ); err  !=  nil  {
69+ 			return 
5370		}
71+ 		ctx [testCase .Name ] =  output 
5472	}
5573	return 
5674}
5775
76+ func  getDefaultContext () map [string ]interface {} {
77+ 	return  map [string ]interface {}{}
78+ }
79+ 
5880func  setRelativeDir (configFile  string , testcase  * testing.TestCase ) {
5981	dir  :=  filepath .Dir (configFile )
6082
0 commit comments