@@ -4,8 +4,10 @@ import (
44 "fmt"
55 "io/ioutil"
66 "log"
7+ "net/http"
78 "os"
89 "path"
10+ "strings"
911
1012 "github.com/commander-cli/commander/pkg/output"
1113 "github.com/commander-cli/commander/pkg/runtime"
@@ -16,8 +18,8 @@ var out output.OutputWriter
1618
1719// TestCommand executes the test argument
1820// testPath is the path to the test suite config, it can be a dir or file
19- // ctx holds the command flags. If directory scanning is enabled with --dir it is
20- // not supported to filter tests, therefore testFilterTitle is an empty string
21+ // ctx holds the command flags. If directory scanning is enabled with --dir,
22+ // test filtering is not supported
2123func TestCommand (testPath string , ctx TestCommandContext ) error {
2224 if ctx .Verbose {
2325 log .SetOutput (os .Stdout )
@@ -36,6 +38,10 @@ func TestCommand(testPath string, ctx TestCommandContext) error {
3638 fmt .Println ("Starting test against directory: " + testPath + "..." )
3739 fmt .Println ("" )
3840 result , err = testDir (testPath , ctx .Filters )
41+ case isURL (testPath ):
42+ fmt .Println ("Starting test from " + testPath + "..." )
43+ fmt .Println ("" )
44+ result , err = testURL (testPath , ctx .Filters )
3945 default :
4046 fmt .Println ("Starting test file " + testPath + "..." )
4147 fmt .Println ("" )
@@ -53,11 +59,20 @@ func TestCommand(testPath string, ctx TestCommandContext) error {
5359 return nil
5460}
5561
62+ func testFile (filePath string , fileName string , filters runtime.Filters ) (runtime.Result , error ) {
63+ s , err := readFile (filePath , fileName )
64+ if err != nil {
65+ return runtime.Result {}, fmt .Errorf ("Error " + err .Error ())
66+ }
67+
68+ return execute (s , filters )
69+ }
70+
5671func testDir (directory string , filters runtime.Filters ) (runtime.Result , error ) {
5772 result := runtime.Result {}
5873 files , err := ioutil .ReadDir (directory )
5974 if err != nil {
60- return result , fmt .Errorf (err . Error () )
75+ return result , fmt .Errorf (" Error: Input is not a directory" )
6176 }
6277
6378 for _ , f := range files {
@@ -77,6 +92,27 @@ func testDir(directory string, filters runtime.Filters) (runtime.Result, error)
7792 return result , nil
7893}
7994
95+ func testURL (url string , filters runtime.Filters ) (runtime.Result , error ) {
96+ resp , err := http .Get (url )
97+ if err != nil {
98+ return runtime.Result {}, err
99+ }
100+ defer resp .Body .Close ()
101+
102+ body , err := ioutil .ReadAll (resp .Body )
103+ if err != nil {
104+ return runtime.Result {}, err
105+ }
106+
107+ s := suite .ParseYAML (body , "" )
108+
109+ return execute (s , filters )
110+ }
111+
112+ func isURL (s string ) bool {
113+ return strings .HasPrefix (s , "http://" ) || strings .HasPrefix (s , "https://" )
114+ }
115+
80116func convergeResults (result runtime.Result , new runtime.Result ) runtime.Result {
81117 result .TestResults = append (result .TestResults , new .TestResults ... )
82118 result .Failed += new .Failed
@@ -85,15 +121,6 @@ func convergeResults(result runtime.Result, new runtime.Result) runtime.Result {
85121 return result
86122}
87123
88- func testFile (filePath string , fileName string , filters runtime.Filters ) (runtime.Result , error ) {
89- s , err := readFile (filePath , fileName )
90- if err != nil {
91- return runtime.Result {}, fmt .Errorf ("Error " + err .Error ())
92- }
93-
94- return execute (s , filters )
95- }
96-
97124func execute (s suite.Suite , filters runtime.Filters ) (runtime.Result , error ) {
98125 tests := s .GetTests ()
99126 if len (filters ) != 0 {
@@ -115,7 +142,7 @@ func execute(s suite.Suite, filters runtime.Filters) (runtime.Result, error) {
115142 return result , nil
116143}
117144
118- func readFile (filePath string , filName string ) (suite.Suite , error ) {
145+ func readFile (filePath string , fileName string ) (suite.Suite , error ) {
119146 s := suite.Suite {}
120147
121148 f , err := os .Stat (filePath )
@@ -132,7 +159,7 @@ func readFile(filePath string, filName string) (suite.Suite, error) {
132159 return s , err
133160 }
134161
135- s = suite .ParseYAML (content , filName )
162+ s = suite .ParseYAML (content , fileName )
136163
137164 return s , nil
138165}
0 commit comments