Skip to content

Commit

Permalink
Merge pull request #145 from commander-cli/fetch-test-suites-over-http
Browse files Browse the repository at this point in the history
add fetch test from http
  • Loading branch information
SimonBaeumer authored Oct 2, 2020
2 parents bfa2319 + f32aa9f commit bb33069
Show file tree
Hide file tree
Showing 34 changed files with 3,208 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v2.4.0

- Add ability to test suite from a url

# v2.3.0

- Preserve test execution order alphabetical order
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ $ ./commander test /tmp/test.yaml
# Execute a single test
$ ./commander test /tmp/test.yaml "my test"

# Execute suite from url
$ ./commander test https://raw.githubusercontent.com/commander-cli/commander/master/integration/unix/commander_test.yaml

# Execute suites within a test directory
$ ./commander test --dir /tmp
```
Expand Down
6 changes: 3 additions & 3 deletions cmd/commander/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func createCliApp() *cli.App {

func createTestCommand() cli.Command {
return cli.Command{
Name: "test",
Usage: `Execute cli app tests
Name: "test",
Usage: `Execute cli app tests
By default it will use the commander.yaml from your current directory.
Tests are always executed in alphabetical order.
Expand Down Expand Up @@ -79,7 +79,7 @@ test commander.yaml --filter="^filter1$"
Usage: "Execute all test files in a directory sorted by file name, this is not recursive - e.g. /path/to/test_files/",
},
cli.StringSliceFlag{
Name: "filter",
Name: "filter",
Usage: `Filter tests by a given regex pattern. Tests are filtered by its title.`,
},
},
Expand Down
12 changes: 12 additions & 0 deletions commander_unix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,15 @@ tests:
✓ [local] a
✓ [local] b
exit-code: 0

it should execute test from url:
config:
env:
USER: from_parent
command: ./commander test https://raw.githubusercontent.com/commander-cli/commander/master/integration/unix/commander_test.yaml
stdout:
contains:
- ✓ [local] it should exit with error code
- "- [local] it should skip, was skipped"
line-count: 17
exit-code: 0
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
golang.org/x/net v0.0.0-20191014212845-da9a3fd4c582 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/h2non/gock.v1 v1.0.15
gopkg.in/yaml.v2 v2.2.4
)

Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKoh
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
Expand All @@ -26,6 +28,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e h1:9MlwzLdW7QSDrhDjFlsEYmxpFyIoXmYRon3dt0io31k=
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/opencontainers/go-digest v1.0.0-rc1 h1:WzifXhOVOEOuFYOJAW6aQqW0TooG2iki3E3Ii+WN7gQ=
github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -61,6 +65,8 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/h2non/gock.v1 v1.0.15 h1:SzLqcIlb/fDfg7UvukMpNcWsu7sI5tWwL+KCATZqks0=
gopkg.in/h2non/gock.v1 v1.0.15/go.mod h1:sX4zAkdYX1TRGJ2JY156cFspQn4yRWn6p9EMdODlynE=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
Expand Down
5 changes: 5 additions & 0 deletions integration/unix/remote_http.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tests:
hello world:
command: echo hello world
stdout: hello world
exit-code: 0
55 changes: 41 additions & 14 deletions pkg/app/test_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strings"

"github.com/commander-cli/commander/pkg/output"
"github.com/commander-cli/commander/pkg/runtime"
Expand All @@ -16,8 +18,8 @@ var out output.OutputWriter

// TestCommand executes the test argument
// testPath is the path to the test suite config, it can be a dir or file
// ctx holds the command flags. If directory scanning is enabled with --dir it is
// not supported to filter tests, therefore testFilterTitle is an empty string
// ctx holds the command flags. If directory scanning is enabled with --dir,
// test filtering is not supported
func TestCommand(testPath string, ctx TestCommandContext) error {
if ctx.Verbose {
log.SetOutput(os.Stdout)
Expand All @@ -36,6 +38,10 @@ func TestCommand(testPath string, ctx TestCommandContext) error {
fmt.Println("Starting test against directory: " + testPath + "...")
fmt.Println("")
result, err = testDir(testPath, ctx.Filters)
case isURL(testPath):
fmt.Println("Starting test from " + testPath + "...")
fmt.Println("")
result, err = testURL(testPath, ctx.Filters)
default:
fmt.Println("Starting test file " + testPath + "...")
fmt.Println("")
Expand All @@ -53,11 +59,20 @@ func TestCommand(testPath string, ctx TestCommandContext) error {
return nil
}

func testFile(filePath string, fileName string, filters runtime.Filters) (runtime.Result, error) {
s, err := readFile(filePath, fileName)
if err != nil {
return runtime.Result{}, fmt.Errorf("Error " + err.Error())
}

return execute(s, filters)
}

func testDir(directory string, filters runtime.Filters) (runtime.Result, error) {
result := runtime.Result{}
files, err := ioutil.ReadDir(directory)
if err != nil {
return result, fmt.Errorf(err.Error())
return result, fmt.Errorf("Error: Input is not a directory")
}

for _, f := range files {
Expand All @@ -77,6 +92,27 @@ func testDir(directory string, filters runtime.Filters) (runtime.Result, error)
return result, nil
}

func testURL(url string, filters runtime.Filters) (runtime.Result, error) {
resp, err := http.Get(url)
if err != nil {
return runtime.Result{}, err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return runtime.Result{}, err
}

s := suite.ParseYAML(body, "")

return execute(s, filters)
}

func isURL(s string) bool {
return strings.HasPrefix(s, "http://") || strings.HasPrefix(s, "https://")
}

func convergeResults(result runtime.Result, new runtime.Result) runtime.Result {
result.TestResults = append(result.TestResults, new.TestResults...)
result.Failed += new.Failed
Expand All @@ -85,15 +121,6 @@ func convergeResults(result runtime.Result, new runtime.Result) runtime.Result {
return result
}

func testFile(filePath string, fileName string, filters runtime.Filters) (runtime.Result, error) {
s, err := readFile(filePath, fileName)
if err != nil {
return runtime.Result{}, fmt.Errorf("Error " + err.Error())
}

return execute(s, filters)
}

func execute(s suite.Suite, filters runtime.Filters) (runtime.Result, error) {
tests := s.GetTests()
if len(filters) != 0 {
Expand All @@ -115,7 +142,7 @@ func execute(s suite.Suite, filters runtime.Filters) (runtime.Result, error) {
return result, nil
}

func readFile(filePath string, filName string) (suite.Suite, error) {
func readFile(filePath string, fileName string) (suite.Suite, error) {
s := suite.Suite{}

f, err := os.Stat(filePath)
Expand All @@ -132,7 +159,7 @@ func readFile(filePath string, filName string) (suite.Suite, error) {
return s, err
}

s = suite.ParseYAML(content, filName)
s = suite.ParseYAML(content, fileName)

return s, nil
}
40 changes: 40 additions & 0 deletions pkg/app/test_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

commanderRuntime "github.com/commander-cli/commander/pkg/runtime"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)

func Test_TestCommand_Verbose(t *testing.T) {
Expand Down Expand Up @@ -68,6 +69,45 @@ func Test_TestCommand_Dir(t *testing.T) {
}
}

func Test_TestCommand_Dir_Err(t *testing.T) {
err := TestCommand("http://foo.com/bar", TestCommandContext{Dir: true})

if runtime.GOOS == "windows" {
assert.Contains(t, err.Error(), "Error: Input is not a directory")
} else {
assert.Equal(t, "Error: Input is not a directory", err.Error())
}
}

func Test_TestCommand_Http(t *testing.T) {
defer gock.Off()

gock.New("http://foo.com").
Get("/bar").
Reply(200).
BodyString(`
tests:
echo hello:
exit-code: 0
`)

out := captureOutput(func() {
TestCommand("http://foo.com/bar", TestCommandContext{Verbose: true})
})

assert.Contains(t, out, "✓ [local] echo hello")
}

func Test_TestCommand_Http_Err(t *testing.T) {
err := TestCommand("http://error/not/a/url", TestCommandContext{Dir: false})

if runtime.GOOS == "windows" {
assert.NotNil(t, err)
} else {
assert.NotNil(t, err)
}
}

func Test_ConvergeResults(t *testing.T) {
duration, _ := time.ParseDuration("5s")

Expand Down
22 changes: 22 additions & 0 deletions vendor/github.com/h2non/parth/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bb33069

Please sign in to comment.