Skip to content

Commit

Permalink
Add validator test and allow fake ReadFile
Browse files Browse the repository at this point in the history
  • Loading branch information
glesica committed Oct 20, 2020
1 parent fe89129 commit 2a616dc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
6 changes: 5 additions & 1 deletion pkg/matcher/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const (
File = "file"
)

// The function used to open files when necessary for matching
// Allows the file IO to be overridden during tests
var ReadFile = ioutil.ReadFile

// NewMatcher creates a new matcher by type
func NewMatcher(matcher string) Matcher {
switch matcher {
Expand Down Expand Up @@ -251,7 +255,7 @@ type FileMatcher struct {
}

func (m FileMatcher) Match(got interface{}, expected interface{}) MatcherResult {
expectedText, err := ioutil.ReadFile(expected.(string))
expectedText, err := ReadFile(expected.(string))
if err != nil {
panic(err.Error())
}
Expand Down
17 changes: 11 additions & 6 deletions pkg/matcher/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,23 @@ another`
assert.Equal(t, diff, r.Diff)
}

func TestFileMatcher_Validate(t *testing.T) {
func TestFileMatcher_Match(t *testing.T) {
ReadFile = func(filename string) ([]byte, error) {
return []byte("line one\nline two"), nil
}
m := FileMatcher{}
got := m.Match("zoom", "zoom.txt")
got := m.Match("line one\nline two", "fake.txt")
assert.True(t, got.Success)
assert.Equal(t, "", got.Diff)
}

func TestFileMatcher_ValidateFails(t *testing.T) {
ReadFile = func(filename string) ([]byte, error) {
return []byte("line one\nline two"), nil
}
m := FileMatcher{}
got := m.Match("zoom", "zoologist.txt")
got := m.Match("line one\nline three", "fake.txt")
assert.False(t, got.Success)
println(got.Diff)
assert.Contains(t, got.Diff, "+zoologist")
assert.Contains(t, got.Diff, "-zoom")
assert.Contains(t, got.Diff, "+line two")
assert.Contains(t, got.Diff, "-line three")
}
1 change: 0 additions & 1 deletion pkg/matcher/zoologist.txt

This file was deleted.

1 change: 0 additions & 1 deletion pkg/matcher/zoom.txt

This file was deleted.

24 changes: 24 additions & 0 deletions pkg/runtime/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package runtime
import (
"github.com/commander-cli/commander/pkg/matcher"
"github.com/stretchr/testify/assert"
"io/ioutil"
"testing"
)

Expand Down Expand Up @@ -192,6 +193,29 @@ test`
assert.Equal(t, diff, r.Diff)
}

func Test_ValidateExpectedOut_ValidateFile(t *testing.T) {
content := "line one"
matcher.ReadFile = func(filename string) ([]byte, error) {
return []byte(content), nil
}
r := validateExpectedOut(content, ExpectedOut{File: "fake.txt"})
assert.True(t, r.Success)
assert.Equal(t, "", r.Diff)

diff := `--- Got
+++ Expected
@@ -1,2 +1 @@
line one
-line two
`

r = validateExpectedOut(content+"\nline two", ExpectedOut{File: "fake.txt"})
assert.False(t, r.Success)
assert.Equal(t, diff, r.Diff)

matcher.ReadFile = ioutil.ReadFile
}

func Test_ValidateExpectedOut_ValidateXML(t *testing.T) {
xml := `<book>
<author>J. R. R. Tolkien</author>
Expand Down

0 comments on commit 2a616dc

Please sign in to comment.