Skip to content

Commit 2a616dc

Browse files
committed
Add validator test and allow fake ReadFile
1 parent fe89129 commit 2a616dc

5 files changed

Lines changed: 40 additions & 9 deletions

File tree

pkg/matcher/matcher.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ const (
2424
File = "file"
2525
)
2626

27+
// The function used to open files when necessary for matching
28+
// Allows the file IO to be overridden during tests
29+
var ReadFile = ioutil.ReadFile
30+
2731
// NewMatcher creates a new matcher by type
2832
func NewMatcher(matcher string) Matcher {
2933
switch matcher {
@@ -251,7 +255,7 @@ type FileMatcher struct {
251255
}
252256

253257
func (m FileMatcher) Match(got interface{}, expected interface{}) MatcherResult {
254-
expectedText, err := ioutil.ReadFile(expected.(string))
258+
expectedText, err := ReadFile(expected.(string))
255259
if err != nil {
256260
panic(err.Error())
257261
}

pkg/matcher/matcher_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,23 @@ another`
184184
assert.Equal(t, diff, r.Diff)
185185
}
186186

187-
func TestFileMatcher_Validate(t *testing.T) {
187+
func TestFileMatcher_Match(t *testing.T) {
188+
ReadFile = func(filename string) ([]byte, error) {
189+
return []byte("line one\nline two"), nil
190+
}
188191
m := FileMatcher{}
189-
got := m.Match("zoom", "zoom.txt")
192+
got := m.Match("line one\nline two", "fake.txt")
190193
assert.True(t, got.Success)
191194
assert.Equal(t, "", got.Diff)
192195
}
193196

194197
func TestFileMatcher_ValidateFails(t *testing.T) {
198+
ReadFile = func(filename string) ([]byte, error) {
199+
return []byte("line one\nline two"), nil
200+
}
195201
m := FileMatcher{}
196-
got := m.Match("zoom", "zoologist.txt")
202+
got := m.Match("line one\nline three", "fake.txt")
197203
assert.False(t, got.Success)
198-
println(got.Diff)
199-
assert.Contains(t, got.Diff, "+zoologist")
200-
assert.Contains(t, got.Diff, "-zoom")
204+
assert.Contains(t, got.Diff, "+line two")
205+
assert.Contains(t, got.Diff, "-line three")
201206
}

pkg/matcher/zoologist.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

pkg/matcher/zoom.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

pkg/runtime/validator_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package runtime
33
import (
44
"github.com/commander-cli/commander/pkg/matcher"
55
"github.com/stretchr/testify/assert"
6+
"io/ioutil"
67
"testing"
78
)
89

@@ -192,6 +193,29 @@ test`
192193
assert.Equal(t, diff, r.Diff)
193194
}
194195

196+
func Test_ValidateExpectedOut_ValidateFile(t *testing.T) {
197+
content := "line one"
198+
matcher.ReadFile = func(filename string) ([]byte, error) {
199+
return []byte(content), nil
200+
}
201+
r := validateExpectedOut(content, ExpectedOut{File: "fake.txt"})
202+
assert.True(t, r.Success)
203+
assert.Equal(t, "", r.Diff)
204+
205+
diff := `--- Got
206+
+++ Expected
207+
@@ -1,2 +1 @@
208+
line one
209+
-line two
210+
`
211+
212+
r = validateExpectedOut(content+"\nline two", ExpectedOut{File: "fake.txt"})
213+
assert.False(t, r.Success)
214+
assert.Equal(t, diff, r.Diff)
215+
216+
matcher.ReadFile = ioutil.ReadFile
217+
}
218+
195219
func Test_ValidateExpectedOut_ValidateXML(t *testing.T) {
196220
xml := `<book>
197221
<author>J. R. R. Tolkien</author>

0 commit comments

Comments
 (0)