forked from helm-unittest/helm-unittest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_runner_test.go
110 lines (98 loc) · 3.06 KB
/
test_runner_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package unittest_test
import (
"bytes"
"regexp"
"sort"
"strings"
"testing"
"github.com/bradleyjkemp/cupaloy"
. "github.com/lrills/helm-unittest/unittest"
"github.com/stretchr/testify/assert"
)
var sectionBeginPattern = regexp.MustCompile("( PASS | FAIL |\n*###|\n*Charts:|\n*Snapshot Summary:)")
var timePattern = regexp.MustCompile("Time:\\s+([\\d\\.]+)ms")
func makeOutputSnapshotable(output string) []interface{} {
timeLoc := timePattern.FindStringSubmatchIndex(output)[2:4]
timeAgnosticOutput := output[:timeLoc[0]] + "XX.XXX" + output[timeLoc[1]:]
sectionBeggingLocs := sectionBeginPattern.FindAllStringIndex(timeAgnosticOutput, -1)
sections := make([]string, len(sectionBeggingLocs))
suiteBeginIdx := -1
for sectionIdx := 0; sectionIdx < len(sections); sectionIdx++ {
start := sectionBeggingLocs[sectionIdx][0]
var end int
if sectionIdx >= len(sections)-1 {
end = len(timeAgnosticOutput)
} else {
end = sectionBeggingLocs[sectionIdx+1][0]
}
sectionContent := timeAgnosticOutput[start:end]
sectionBegin := sectionContent[:6]
if sectionBegin == " PASS " || sectionBegin == " FAIL " {
sections[sectionIdx] = strings.TrimRight(sectionContent, "\n")
if suiteBeginIdx == -1 {
suiteBeginIdx = sectionIdx
}
} else {
sections[sectionIdx] = sectionContent
if suiteBeginIdx != -1 {
sort.Strings(sections[suiteBeginIdx:sectionIdx])
suiteBeginIdx = -1
}
}
}
sectionsToRetrun := make([]interface{}, len(sections))
for idx, section := range sections {
sectionsToRetrun[idx] = section
}
return sectionsToRetrun
}
func TestRunnerOkWithPassedTests(t *testing.T) {
buffer := new(bytes.Buffer)
runner := TestRunner{
Printer: NewPrinter(buffer, nil),
Config: TestConfig{
TestFiles: []string{"tests/*_test.yaml"},
},
}
passed := runner.Run([]string{"../__fixtures__/basic"})
assert.True(t, passed)
cupaloy.SnapshotT(t, makeOutputSnapshotable(buffer.String())...)
}
func TestRunnerOkWithFailedTests(t *testing.T) {
buffer := new(bytes.Buffer)
runner := TestRunner{
Printer: NewPrinter(buffer, nil),
Config: TestConfig{
TestFiles: []string{"tests_failed/*_test.yaml"},
},
}
passed := runner.Run([]string{"../__fixtures__/basic"})
assert.False(t, passed)
cupaloy.SnapshotT(t, makeOutputSnapshotable(buffer.String())...)
}
func TestRunnerWithTestsInSubchart(t *testing.T) {
buffer := new(bytes.Buffer)
runner := TestRunner{
Printer: NewPrinter(buffer, nil),
Config: TestConfig{
WithSubChart: true,
TestFiles: []string{"tests/*_test.yaml"},
},
}
passed := runner.Run([]string{"../__fixtures__/with-subchart"})
assert.True(t, passed)
cupaloy.SnapshotT(t, makeOutputSnapshotable(buffer.String())...)
}
func TestRunnerWithTestsInSubchartButFlagFalse(t *testing.T) {
buffer := new(bytes.Buffer)
runner := TestRunner{
Printer: NewPrinter(buffer, nil),
Config: TestConfig{
WithSubChart: false,
TestFiles: []string{"tests/*_test.yaml"},
},
}
passed := runner.Run([]string{"../__fixtures__/with-subchart"})
assert.True(t, passed)
cupaloy.SnapshotT(t, makeOutputSnapshotable(buffer.String())...)
}