-
Notifications
You must be signed in to change notification settings - Fork 1
/
assert.go
62 lines (49 loc) · 1.97 KB
/
assert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package invokers
import (
"fmt"
"regexp"
"strings"
"github.com/jromero/ugo/pkg/ugo/internal/tasks"
"github.com/jromero/ugo/pkg/ugo/types"
"github.com/sergi/go-diff/diffmatchpatch"
)
var ansiPattern = regexp.MustCompile("[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))")
var _ types.Invoker = (*AssertInvoker)(nil)
type AssertInvoker struct {
Logger types.Logger
}
func NewAssertInvoker(logger types.Logger) *AssertInvoker {
return &AssertInvoker{Logger: logger}
}
func (a *AssertInvoker) Supports(task types.Task) bool {
_, ok := task.(*tasks.AssertContainsTask)
return ok
}
func (a *AssertInvoker) Invoke(task types.Task, _, prevOutput string) (output string, err error) {
containsTask := task.(*tasks.AssertContainsTask)
return "", a.executeAssertContains(prevOutput, containsTask)
}
func (a *AssertInvoker) executeAssertContains(priorOutput string, task *tasks.AssertContainsTask) error {
expected := task.Expected()
sanitized := ansiPattern.ReplaceAllString(priorOutput, "")
if task.IgnoreLines() != "" {
rx := regexp.MustCompile(fmt.Sprintf(`(?m)^%s(\n|$)`, regexp.QuoteMeta(task.IgnoreLines())))
expected = regexp.QuoteMeta(rx.ReplaceAllString(expected, "~~WILDCARD~~${1}"))
expected = strings.ReplaceAll(expected, "~~WILDCARD~~", ".*")
expected = "(?ms).*" + expected + ".*"
a.Logger.Debug("Checking that output matches pattern:\n%s", expected)
if !regexp.MustCompile(expected).MatchString(sanitized) {
a.Logger.Info("Content:\n%s", sanitized)
return fmt.Errorf("Output didn't match pattern:\n%s", expected)
}
} else {
a.Logger.Debug("Checking that output contained:\n%s", expected)
if !strings.Contains(sanitized, expected) {
dmp := diffmatchpatch.New()
diff := dmp.DiffPrettyText(dmp.DiffMain(sanitized, expected, false))
a.Logger.Info("Diff between output and expected:\n%s", diff)
return fmt.Errorf("Output did not contain:\n%s", expected)
}
}
return nil
}