forked from cosmos/ibc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
161 lines (132 loc) · 4.42 KB
/
main_test.go
File metadata and controls
161 lines (132 loc) · 4.42 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"os"
"path"
"sort"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
const (
nonTestFile = "not_test_file.go"
goTestFileNameOne = "first_go_file_test.go"
goTestFileNameTwo = "second_go_file_test.go"
)
func TestGetGithubActionMatrixForTests(t *testing.T) {
t.Run("empty dir does not fail", func(t *testing.T) {
testingDir := t.TempDir()
_, err := getGithubActionMatrixForTests(testingDir, "", nil)
assert.NoError(t, err)
})
t.Run("only test functions are picked up", func(t *testing.T) {
testingDir := t.TempDir()
createFileWithTestSuiteAndTests(t, "FeeMiddlewareTestSuite", "TestA", "TestB", testingDir, goTestFileNameOne)
gh, err := getGithubActionMatrixForTests(testingDir, "", nil)
assert.NoError(t, err)
expected := GithubActionTestMatrix{
Include: []TestSuitePair{
{
EntryPoint: "TestFeeMiddlewareTestSuite",
Test: "TestA",
},
{
EntryPoint: "TestFeeMiddlewareTestSuite",
Test: "TestB",
},
},
}
assertGithubActionTestMatricesEqual(t, expected, gh)
})
t.Run("all files are picked up", func(t *testing.T) {
testingDir := t.TempDir()
createFileWithTestSuiteAndTests(t, "FeeMiddlewareTestSuite", "TestA", "TestB", testingDir, goTestFileNameOne)
createFileWithTestSuiteAndTests(t, "TransferTestSuite", "TestC", "TestD", testingDir, goTestFileNameTwo)
gh, err := getGithubActionMatrixForTests(testingDir, "", nil)
assert.NoError(t, err)
expected := GithubActionTestMatrix{
Include: []TestSuitePair{
{
EntryPoint: "TestTransferTestSuite",
Test: "TestC",
},
{
EntryPoint: "TestFeeMiddlewareTestSuite",
Test: "TestA",
},
{
EntryPoint: "TestFeeMiddlewareTestSuite",
Test: "TestB",
},
{
EntryPoint: "TestTransferTestSuite",
Test: "TestD",
},
},
}
assertGithubActionTestMatricesEqual(t, expected, gh)
})
t.Run("non test files are not picked up", func(t *testing.T) {
testingDir := t.TempDir()
createFileWithTestSuiteAndTests(t, "FeeMiddlewareTestSuite", "TestA", "TestB", testingDir, nonTestFile)
gh, err := getGithubActionMatrixForTests(testingDir, "", nil)
assert.NoError(t, err)
assert.Empty(t, gh.Include)
})
t.Run("fails when there are multiple suite runs", func(t *testing.T) {
testingDir := t.TempDir()
createFileWithTestSuiteAndTests(t, "FeeMiddlewareTestSuite", "TestA", "TestB", testingDir, nonTestFile)
fileWithTwoSuites := `package foo
func SuiteOne(t *testing.T) {
suite.Run(t, new(FeeMiddlewareTestSuite))
}
func SuiteTwo(t *testing.T) {
suite.Run(t, new(FeeMiddlewareTestSuite))
}
type FeeMiddlewareTestSuite struct {}
`
err := os.WriteFile(path.Join(testingDir, goTestFileNameOne), []byte(fileWithTwoSuites), os.FileMode(0o777))
assert.NoError(t, err)
_, err = getGithubActionMatrixForTests(testingDir, "", nil)
assert.Error(t, err)
})
}
func assertGithubActionTestMatricesEqual(t *testing.T, expected, actual GithubActionTestMatrix) {
// sort by both suite and test as the order of the end result does not matter as
// all tests will be run.
sort.SliceStable(expected.Include, func(i, j int) bool {
memberI := expected.Include[i]
memberJ := expected.Include[j]
if memberI.EntryPoint == memberJ.EntryPoint {
return memberI.Test < memberJ.Test
}
return memberI.EntryPoint < memberJ.EntryPoint
})
sort.SliceStable(actual.Include, func(i, j int) bool {
memberI := actual.Include[i]
memberJ := actual.Include[j]
if memberI.EntryPoint == memberJ.EntryPoint {
return memberI.Test < memberJ.Test
}
return memberI.EntryPoint < memberJ.EntryPoint
})
assert.Equal(t, expected.Include, actual.Include)
}
func goTestFileContents(suiteName, fnName1, fnName2 string) string {
replacedSuiteName := strings.ReplaceAll(`package foo
func TestSuiteName(t *testing.T) {
suite.Run(t, new(SuiteName))
}
type SuiteName struct {}
func (s *SuiteName) fnName1() {}
func (s *SuiteName) fnName2() {}
func (s *SuiteName) suiteHelper() {}
func helper() {}
`, "SuiteName", suiteName)
replacedFn1Name := strings.ReplaceAll(replacedSuiteName, "fnName1", fnName1)
return strings.ReplaceAll(replacedFn1Name, "fnName2", fnName2)
}
func createFileWithTestSuiteAndTests(t *testing.T, suiteName, fn1Name, fn2Name, dir, filename string) {
goFileContents := goTestFileContents(suiteName, fn1Name, fn2Name)
err := os.WriteFile(path.Join(dir, filename), []byte(goFileContents), os.FileMode(0o777))
assert.NoError(t, err)
}