Skip to content

Commit 781486a

Browse files
committed
Implement file matcher / assertion
1 parent bb33069 commit 781486a

11 files changed

Lines changed: 82 additions & 5 deletions

File tree

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

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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/antchfx/xmlquery"
66
"github.com/pmezard/go-difflib/difflib"
77
"github.com/tidwall/gjson"
8+
"io/ioutil"
89
"log"
910
"strings"
1011
)
@@ -20,6 +21,7 @@ const (
2021
NotContains = "notcontains"
2122
JSON = "json"
2223
XML = "xml"
24+
File = "file"
2325
)
2426

2527
// NewMatcher creates a new matcher by type
@@ -37,6 +39,8 @@ func NewMatcher(matcher string) Matcher {
3739
return JSONMatcher{}
3840
case XML:
3941
return XMLMatcher{}
42+
case File:
43+
return FileMatcher{}
4044
default:
4145
panic(fmt.Sprintf("Validator '%s' does not exist!", matcher))
4246
}
@@ -240,3 +244,32 @@ to be equal to
240244

241245
return result
242246
}
247+
248+
// FileMatcher matches output captured from stdout or stderr
249+
// against the contents of a file
250+
type FileMatcher struct {
251+
}
252+
253+
func (m FileMatcher) Match(got interface{}, expected interface{}) MatcherResult {
254+
expectedText, err := ioutil.ReadFile(expected.(string))
255+
if err != nil {
256+
panic(err.Error())
257+
}
258+
expectedString := string(expectedText)
259+
260+
result := got == expectedString
261+
262+
diff := difflib.UnifiedDiff{
263+
A: difflib.SplitLines(got.(string)),
264+
B: difflib.SplitLines(expectedString),
265+
FromFile: "Got",
266+
ToFile: "Expected",
267+
Context: 3,
268+
}
269+
diffText, _ := difflib.GetUnifiedDiffString(diff)
270+
271+
return MatcherResult{
272+
Diff: diffText,
273+
Success: result,
274+
}
275+
}

pkg/matcher/matcher_test.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ func TestTextMatcher_ValidateFails(t *testing.T) {
4848

4949
func TestEqualMatcher_Validate(t *testing.T) {
5050
m := EqualMatcher{}
51-
got := m.Match(1, 1)
51+
got := m.Match(2, 2)
5252
assert.True(t, got.Success)
5353
}
5454

5555
func TestEqualMatcher_ValidateFails(t *testing.T) {
5656
m := EqualMatcher{}
57-
got := m.Match(1, 0)
57+
got := m.Match(2, 3)
5858
assert.False(t, got.Success)
59-
assert.Contains(t, got.Diff, "+0")
60-
assert.Contains(t, got.Diff, "-1")
59+
assert.Contains(t, got.Diff, "+3")
60+
assert.Contains(t, got.Diff, "-2")
6161
}
6262

6363
func TestContainsMatcher_Match(t *testing.T) {
@@ -183,3 +183,19 @@ another`
183183
assert.False(t, r.Success)
184184
assert.Equal(t, diff, r.Diff)
185185
}
186+
187+
func TestFileMatcher_Validate(t *testing.T) {
188+
m := FileMatcher{}
189+
got := m.Match("zoom", "zoom.txt")
190+
assert.True(t, got.Success)
191+
assert.Equal(t, "", got.Diff)
192+
}
193+
194+
func TestFileMatcher_ValidateFails(t *testing.T) {
195+
m := FileMatcher{}
196+
got := m.Match("zoom", "zoologist.txt")
197+
assert.False(t, got.Success)
198+
println(got.Diff)
199+
assert.Contains(t, got.Diff, "+zoologist")
200+
assert.Contains(t, got.Diff, "-zoom")
201+
}

pkg/matcher/zoologist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
zoologist

pkg/matcher/zoom.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
zoom

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.File); !result.Success {
124+
return result
125+
}
126+
}
127+
121128
return result
122129
}
123130

0 commit comments

Comments
 (0)