Skip to content

Commit bb33069

Browse files
authored
Merge pull request #145 from commander-cli/fetch-test-suites-over-http
add fetch test from http
2 parents bfa2319 + f32aa9f commit bb33069

34 files changed

+3208
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# v2.4.0
2+
3+
- Add ability to test suite from a url
4+
15
# v2.3.0
26

37
- Preserve test execution order alphabetical order

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ $ ./commander test /tmp/test.yaml
203203
# Execute a single test
204204
$ ./commander test /tmp/test.yaml "my test"
205205

206+
# Execute suite from url
207+
$ ./commander test https://raw.githubusercontent.com/commander-cli/commander/master/integration/unix/commander_test.yaml
208+
206209
# Execute suites within a test directory
207210
$ ./commander test --dir /tmp
208211
```

cmd/commander/commander.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ func createCliApp() *cli.App {
4545

4646
func createTestCommand() cli.Command {
4747
return cli.Command{
48-
Name: "test",
49-
Usage: `Execute cli app tests
48+
Name: "test",
49+
Usage: `Execute cli app tests
5050
5151
By default it will use the commander.yaml from your current directory.
5252
Tests are always executed in alphabetical order.
@@ -79,7 +79,7 @@ test commander.yaml --filter="^filter1$"
7979
Usage: "Execute all test files in a directory sorted by file name, this is not recursive - e.g. /path/to/test_files/",
8080
},
8181
cli.StringSliceFlag{
82-
Name: "filter",
82+
Name: "filter",
8383
Usage: `Filter tests by a given regex pattern. Tests are filtered by its title.`,
8484
},
8585
},

commander_unix.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,15 @@ tests:
123123
✓ [local] a
124124
✓ [local] b
125125
exit-code: 0
126+
127+
it should execute test from url:
128+
config:
129+
env:
130+
USER: from_parent
131+
command: ./commander test https://raw.githubusercontent.com/commander-cli/commander/master/integration/unix/commander_test.yaml
132+
stdout:
133+
contains:
134+
- ✓ [local] it should exit with error code
135+
- "- [local] it should skip, was skipped"
136+
line-count: 17
137+
exit-code: 0

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
2121
golang.org/x/net v0.0.0-20191014212845-da9a3fd4c582 // indirect
2222
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
23+
gopkg.in/h2non/gock.v1 v1.0.15
2324
gopkg.in/yaml.v2 v2.2.4
2425
)
2526

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKoh
1818
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
1919
github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw=
2020
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
21+
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
22+
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
2123
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
2224
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
2325
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
@@ -26,6 +28,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
2628
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
2729
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e h1:9MlwzLdW7QSDrhDjFlsEYmxpFyIoXmYRon3dt0io31k=
2830
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
31+
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
32+
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
2933
github.com/opencontainers/go-digest v1.0.0-rc1 h1:WzifXhOVOEOuFYOJAW6aQqW0TooG2iki3E3Ii+WN7gQ=
3034
github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
3135
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -61,6 +65,8 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
6165
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6266
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
6367
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
68+
gopkg.in/h2non/gock.v1 v1.0.15 h1:SzLqcIlb/fDfg7UvukMpNcWsu7sI5tWwL+KCATZqks0=
69+
gopkg.in/h2non/gock.v1 v1.0.15/go.mod h1:sX4zAkdYX1TRGJ2JY156cFspQn4yRWn6p9EMdODlynE=
6470
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
6571
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
6672
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=

integration/unix/remote_http.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
tests:
2+
hello world:
3+
command: echo hello world
4+
stdout: hello world
5+
exit-code: 0

pkg/app/test_command.go

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
2123
func 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+
5671
func 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+
80116
func 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-
97124
func 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
}

pkg/app/test_command_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
commanderRuntime "github.com/commander-cli/commander/pkg/runtime"
1414
"github.com/stretchr/testify/assert"
15+
"gopkg.in/h2non/gock.v1"
1516
)
1617

1718
func Test_TestCommand_Verbose(t *testing.T) {
@@ -68,6 +69,45 @@ func Test_TestCommand_Dir(t *testing.T) {
6869
}
6970
}
7071

72+
func Test_TestCommand_Dir_Err(t *testing.T) {
73+
err := TestCommand("http://foo.com/bar", TestCommandContext{Dir: true})
74+
75+
if runtime.GOOS == "windows" {
76+
assert.Contains(t, err.Error(), "Error: Input is not a directory")
77+
} else {
78+
assert.Equal(t, "Error: Input is not a directory", err.Error())
79+
}
80+
}
81+
82+
func Test_TestCommand_Http(t *testing.T) {
83+
defer gock.Off()
84+
85+
gock.New("http://foo.com").
86+
Get("/bar").
87+
Reply(200).
88+
BodyString(`
89+
tests:
90+
echo hello:
91+
exit-code: 0
92+
`)
93+
94+
out := captureOutput(func() {
95+
TestCommand("http://foo.com/bar", TestCommandContext{Verbose: true})
96+
})
97+
98+
assert.Contains(t, out, "✓ [local] echo hello")
99+
}
100+
101+
func Test_TestCommand_Http_Err(t *testing.T) {
102+
err := TestCommand("http://error/not/a/url", TestCommandContext{Dir: false})
103+
104+
if runtime.GOOS == "windows" {
105+
assert.NotNil(t, err)
106+
} else {
107+
assert.NotNil(t, err)
108+
}
109+
}
110+
71111
func Test_ConvergeResults(t *testing.T) {
72112
duration, _ := time.ParseDuration("5s")
73113

vendor/github.com/h2non/parth/LICENSE

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)