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,8 +21,13 @@ const (
2021 NotContains = "notcontains"
2122 JSON = "json"
2223 XML = "xml"
24+ File = "file"
2325)
2426
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+
2531// NewMatcher creates a new matcher by type
2632func NewMatcher (matcher string ) Matcher {
2733 switch matcher {
@@ -37,6 +43,8 @@ func NewMatcher(matcher string) Matcher {
3743 return JSONMatcher {}
3844 case XML :
3945 return XMLMatcher {}
46+ case File :
47+ return FileMatcher {}
4048 default :
4149 panic (fmt .Sprintf ("Validator '%s' does not exist!" , matcher ))
4250 }
@@ -240,3 +248,32 @@ to be equal to
240248
241249 return result
242250}
251+
252+ // FileMatcher matches output captured from stdout or stderr
253+ // against the contents of a file
254+ type FileMatcher struct {
255+ }
256+
257+ func (m FileMatcher ) Match (got interface {}, expected interface {}) MatcherResult {
258+ expectedText , err := ReadFile (expected .(string ))
259+ if err != nil {
260+ panic (err .Error ())
261+ }
262+ expectedString := string (expectedText )
263+
264+ result := got == expectedString
265+
266+ diff := difflib.UnifiedDiff {
267+ A : difflib .SplitLines (got .(string )),
268+ B : difflib .SplitLines (expectedString ),
269+ FromFile : "Got" ,
270+ ToFile : "Expected" ,
271+ Context : 3 ,
272+ }
273+ diffText , _ := difflib .GetUnifiedDiffString (diff )
274+
275+ return MatcherResult {
276+ Diff : diffText ,
277+ Success : result ,
278+ }
279+ }
0 commit comments