Skip to content

Commit

Permalink
Add file assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
glesica committed Oct 15, 2020
1 parent bb33069 commit 6916ee8
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# v2.4.0

- Add ability to test suite from a url
- Add `file` assertion to `stdout` and `stderr`

# v2.3.0

Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ test-coverage-all:

integration-unix: build
$(info INFO: Starting build $@)
commander test commander_unix.yaml
./commander test commander_unix.yaml

integration-linux: build
$(info INFO: Starting build $@)
./integration/setup_unix.sh
commander test commander_unix.yaml
commander test commander_linux.yaml
./commander test commander_unix.yaml
./commander test commander_linux.yaml
./integration/teardown_unix.sh

integration-windows: build
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ tests:
object.attr: hello # Make assertions on json objects
xml:
"//book//auhtor": Steven King # Make assertions on xml documents
file: correct-output.txt
exit-code: 127
skip: false

Expand Down
2 changes: 1 addition & 1 deletion commander_unix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tests:
contains:
- ✓ [local] it should exit with error code
- "- [local] it should skip, was skipped"
line-count: 17
line-count: 19
exit-code: 0

it should assert that commander will fail:
Expand Down
10 changes: 10 additions & 0 deletions integration/unix/commander_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ tests:
/books/0/author: J. R. R. Tokien
/books/1/author: Joanne K. Rowling

it should assert file contents on stdout:
command: cat ./integration/unix/_fixtures/big_out.txt
stdout:
file: ./integration/unix/_fixtures/big_out.txt

it should assert file contents on stderr:
command: cat ./integration/unix/_fixtures/big_out.txt >&2
stderr:
file: ./integration/unix/_fixtures/big_out.txt

it should inherit from parent env:
config:
inherit-env: true
Expand Down
3 changes: 3 additions & 0 deletions pkg/matcher/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
NotContains = "notcontains"
JSON = "json"
XML = "xml"
File = "file"
)

// NewMatcher creates a new matcher by type
Expand All @@ -37,6 +38,8 @@ func NewMatcher(matcher string) Matcher {
return JSONMatcher{}
case XML:
return XMLMatcher{}
case File:
return TextMatcher{}
default:
panic(fmt.Sprintf("Validator '%s' does not exist!", matcher))
}
Expand Down
1 change: 1 addition & 0 deletions pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ type ExpectedOut struct {
NotContains []string `yaml:"not-contains,omitempty"`
JSON map[string]string `yaml:"json,omitempty"`
XML map[string]string `yaml:"xml,omitempty"`
File string `yaml:"file,omitempty"`
}

// CommandUnderTest represents the command under test
Expand Down
7 changes: 7 additions & 0 deletions pkg/runtime/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ func validateExpectedOut(got string, expected ExpectedOut) matcher.MatcherResult
}
}

if expected.File != "" {
m = matcher.NewMatcher(matcher.File)
if result = m.Match(got, expected.Exactly); !result.Success {
return result
}
}

return result
}

Expand Down
24 changes: 22 additions & 2 deletions pkg/suite/yaml_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package suite

import (
"fmt"
"io/ioutil"
"reflect"
"strings"

Expand Down Expand Up @@ -101,6 +102,24 @@ func convertNodes(nodeConfs map[string]YAMLNodeConf) []runtime.Node {
func convertYAMLSuiteConfToTestCases(conf YAMLSuiteConf, fileName string) []runtime.TestCase {
var tests []runtime.TestCase
for _, t := range conf.Tests {
stdout := t.Stdout.(runtime.ExpectedOut)
if stdout.File != "" {
content, err := ioutil.ReadFile(stdout.File)
if err != nil {
panic(err.Error())
}
stdout.File = string(content)
}

stderr := t.Stderr.(runtime.ExpectedOut)
if stderr.File != "" {
content, err := ioutil.ReadFile(stderr.File)
if err != nil {
panic(err.Error())
}
stderr.File = string(content)
}

tests = append(tests, runtime.TestCase{
Title: t.Title,
Command: runtime.CommandUnderTest{
Expand All @@ -114,8 +133,8 @@ func convertYAMLSuiteConfToTestCases(conf YAMLSuiteConf, fileName string) []runt
},
Expected: runtime.Expected{
ExitCode: t.ExitCode,
Stdout: t.Stdout.(runtime.ExpectedOut),
Stderr: t.Stderr.(runtime.ExpectedOut),
Stdout: stdout,
Stderr: stderr,
},
Nodes: t.Config.Nodes,
FileName: fileName,
Expand Down Expand Up @@ -222,6 +241,7 @@ func (y *YAMLSuiteConf) convertToExpectedOut(value interface{}) runtime.Expected
"lines",
"json",
"xml",
"file",
"not-contains":
break
default:
Expand Down

0 comments on commit 6916ee8

Please sign in to comment.