-
Notifications
You must be signed in to change notification settings - Fork 473
/
matcher_test.go
89 lines (79 loc) · 1.91 KB
/
matcher_test.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//go:build linux
package goss
import (
"bytes"
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"testing"
"github.com/goss-org/goss/util"
"github.com/stretchr/testify/assert"
)
var (
// This will generate the "golden files" prior to running the tests.
// helpful when the output is changed and a user doesn't want to update every single expectation file by hand
update = flag.Bool("update", false, "update the golden files of this test")
)
func TestMain(m *testing.M) {
flag.Parse()
os.Exit(m.Run())
}
func TestMatchers(t *testing.T) {
files, err := filepath.Glob(filepath.Join("testdata", "out_matching_*"))
if err != nil {
t.Fatal(err)
}
for _, outFile := range files {
outFile := outFile
parts := strings.Split(outFile, ".")
specName := fmt.Sprintf("%s.yaml", strings.TrimPrefix(parts[0], "testdata/out_"))
specFile := filepath.Join("testdata", specName)
outFormat := parts[2]
wantCode, err := strconv.Atoi(parts[1])
if err != nil {
t.Fatal(err)
}
tn := outFile
t.Run(tn, func(t *testing.T) {
output := &bytes.Buffer{}
cfg, err := util.NewConfig(
util.WithOutputFormat(outFormat),
util.WithResultWriter(output),
util.WithSpecFile(specFile),
util.WithFormatOptions("sort", "pretty"),
)
if err != nil {
t.Fatal(err)
}
exitCode, err := Validate(cfg)
if err != nil {
t.Fatal(err)
}
actualOut := output.String()
actualOut = sanitizeOutput(actualOut)
if *update {
os.WriteFile(outFile, []byte(actualOut), 0644)
}
wantOutB, err := os.ReadFile(outFile)
if err != nil {
t.Fatal(err)
}
wantOut := string(wantOutB)
if actualOut != wantOut {
assert.Equal(t, wantOut, actualOut)
}
if exitCode != wantCode {
assert.Equal(t, wantCode, exitCode)
}
})
}
}
func sanitizeOutput(s string) string {
// Remove duration time
re := regexp.MustCompile(`\d\.\d\d\ds`)
return re.ReplaceAllString(s, "")
}