Skip to content

Commit 6916ee8

Browse files
committed
Add file assertion
1 parent bb33069 commit 6916ee8

File tree

9 files changed

+49
-6
lines changed

9 files changed

+49
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# v2.4.0
22

33
- Add ability to test suite from a url
4+
- Add `file` assertion to `stdout` and `stderr`
45

56
# v2.3.0
67

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ test-coverage-all:
4545

4646
integration-unix: build
4747
$(info INFO: Starting build $@)
48-
commander test commander_unix.yaml
48+
./commander test commander_unix.yaml
4949

5050
integration-linux: build
5151
$(info INFO: Starting build $@)
5252
./integration/setup_unix.sh
53-
commander test commander_unix.yaml
54-
commander test commander_linux.yaml
53+
./commander test commander_unix.yaml
54+
./commander test commander_linux.yaml
5555
./integration/teardown_unix.sh
5656

5757
integration-windows: build

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ tests:
167167
object.attr: hello # Make assertions on json objects
168168
xml:
169169
"//book//auhtor": Steven King # Make assertions on xml documents
170+
file: correct-output.txt
170171
exit-code: 127
171172
skip: false
172173

commander_unix.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ tests:
1616
contains:
1717
- ✓ [local] it should exit with error code
1818
- "- [local] it should skip, was skipped"
19-
line-count: 17
19+
line-count: 19
2020
exit-code: 0
2121

2222
it should assert that commander will fail:

integration/unix/commander_test.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ tests:
5959
/books/0/author: J. R. R. Tokien
6060
/books/1/author: Joanne K. Rowling
6161

62+
it should assert file contents on stdout:
63+
command: cat ./integration/unix/_fixtures/big_out.txt
64+
stdout:
65+
file: ./integration/unix/_fixtures/big_out.txt
66+
67+
it should assert file contents on stderr:
68+
command: cat ./integration/unix/_fixtures/big_out.txt >&2
69+
stderr:
70+
file: ./integration/unix/_fixtures/big_out.txt
71+
6272
it should inherit from parent env:
6373
config:
6474
inherit-env: true

pkg/matcher/matcher.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const (
2020
NotContains = "notcontains"
2121
JSON = "json"
2222
XML = "xml"
23+
File = "file"
2324
)
2425

2526
// NewMatcher creates a new matcher by type
@@ -37,6 +38,8 @@ func NewMatcher(matcher string) Matcher {
3738
return JSONMatcher{}
3839
case XML:
3940
return XMLMatcher{}
41+
case File:
42+
return TextMatcher{}
4043
default:
4144
panic(fmt.Sprintf("Validator '%s' does not exist!", matcher))
4245
}

pkg/runtime/runtime.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ type ExpectedOut struct {
9999
NotContains []string `yaml:"not-contains,omitempty"`
100100
JSON map[string]string `yaml:"json,omitempty"`
101101
XML map[string]string `yaml:"xml,omitempty"`
102+
File string `yaml:"file,omitempty"`
102103
}
103104

104105
// CommandUnderTest represents the command under test

pkg/runtime/validator.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ func validateExpectedOut(got string, expected ExpectedOut) matcher.MatcherResult
118118
}
119119
}
120120

121+
if expected.File != "" {
122+
m = matcher.NewMatcher(matcher.File)
123+
if result = m.Match(got, expected.Exactly); !result.Success {
124+
return result
125+
}
126+
}
127+
121128
return result
122129
}
123130

pkg/suite/yaml_suite.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package suite
22

33
import (
44
"fmt"
5+
"io/ioutil"
56
"reflect"
67
"strings"
78

@@ -101,6 +102,24 @@ func convertNodes(nodeConfs map[string]YAMLNodeConf) []runtime.Node {
101102
func convertYAMLSuiteConfToTestCases(conf YAMLSuiteConf, fileName string) []runtime.TestCase {
102103
var tests []runtime.TestCase
103104
for _, t := range conf.Tests {
105+
stdout := t.Stdout.(runtime.ExpectedOut)
106+
if stdout.File != "" {
107+
content, err := ioutil.ReadFile(stdout.File)
108+
if err != nil {
109+
panic(err.Error())
110+
}
111+
stdout.File = string(content)
112+
}
113+
114+
stderr := t.Stderr.(runtime.ExpectedOut)
115+
if stderr.File != "" {
116+
content, err := ioutil.ReadFile(stderr.File)
117+
if err != nil {
118+
panic(err.Error())
119+
}
120+
stderr.File = string(content)
121+
}
122+
104123
tests = append(tests, runtime.TestCase{
105124
Title: t.Title,
106125
Command: runtime.CommandUnderTest{
@@ -114,8 +133,8 @@ func convertYAMLSuiteConfToTestCases(conf YAMLSuiteConf, fileName string) []runt
114133
},
115134
Expected: runtime.Expected{
116135
ExitCode: t.ExitCode,
117-
Stdout: t.Stdout.(runtime.ExpectedOut),
118-
Stderr: t.Stderr.(runtime.ExpectedOut),
136+
Stdout: stdout,
137+
Stderr: stderr,
119138
},
120139
Nodes: t.Config.Nodes,
121140
FileName: fileName,
@@ -222,6 +241,7 @@ func (y *YAMLSuiteConf) convertToExpectedOut(value interface{}) runtime.Expected
222241
"lines",
223242
"json",
224243
"xml",
244+
"file",
225245
"not-contains":
226246
break
227247
default:

0 commit comments

Comments
 (0)