|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + nonTestFile = "not_test_file.go" |
| 15 | + goTestFileNameOne = "first_go_file_test.go" |
| 16 | + goTestFileNameTwo = "second_go_file_test.go" |
| 17 | +) |
| 18 | + |
| 19 | +func TestGetGithubActionMatrixForTests(t *testing.T) { |
| 20 | + t.Run("empty dir does not fail", func(t *testing.T) { |
| 21 | + testingDir := t.TempDir() |
| 22 | + _, err := getGithubActionMatrixForTests(testingDir) |
| 23 | + assert.NoError(t, err) |
| 24 | + }) |
| 25 | + |
| 26 | + t.Run("only test functions are picked up", func(t *testing.T) { |
| 27 | + testingDir := t.TempDir() |
| 28 | + createFileWithTestSuiteAndTests(t, "FeeMiddlewareTestSuite", "TestA", "TestB", testingDir, goTestFileNameOne) |
| 29 | + |
| 30 | + gh, err := getGithubActionMatrixForTests(testingDir) |
| 31 | + assert.NoError(t, err) |
| 32 | + |
| 33 | + expected := GithubActionTestMatrix{ |
| 34 | + Include: []TestSuitePair{ |
| 35 | + { |
| 36 | + Suite: "TestFeeMiddlewareTestSuite", |
| 37 | + Test: "TestA", |
| 38 | + }, |
| 39 | + { |
| 40 | + Suite: "TestFeeMiddlewareTestSuite", |
| 41 | + Test: "TestB", |
| 42 | + }, |
| 43 | + }, |
| 44 | + } |
| 45 | + assertGithubActionTestMatricesEqual(t, expected, gh) |
| 46 | + }) |
| 47 | + |
| 48 | + t.Run("all files are picked up", func(t *testing.T) { |
| 49 | + testingDir := t.TempDir() |
| 50 | + createFileWithTestSuiteAndTests(t, "FeeMiddlewareTestSuite", "TestA", "TestB", testingDir, goTestFileNameOne) |
| 51 | + createFileWithTestSuiteAndTests(t, "TransferTestSuite", "TestC", "TestD", testingDir, goTestFileNameTwo) |
| 52 | + |
| 53 | + gh, err := getGithubActionMatrixForTests(testingDir) |
| 54 | + assert.NoError(t, err) |
| 55 | + |
| 56 | + expected := GithubActionTestMatrix{ |
| 57 | + Include: []TestSuitePair{ |
| 58 | + { |
| 59 | + Suite: "TestTransferTestSuite", |
| 60 | + Test: "TestC", |
| 61 | + }, |
| 62 | + { |
| 63 | + Suite: "TestFeeMiddlewareTestSuite", |
| 64 | + Test: "TestA", |
| 65 | + }, |
| 66 | + { |
| 67 | + Suite: "TestFeeMiddlewareTestSuite", |
| 68 | + Test: "TestB", |
| 69 | + }, |
| 70 | + { |
| 71 | + Suite: "TestTransferTestSuite", |
| 72 | + Test: "TestD", |
| 73 | + }, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + assertGithubActionTestMatricesEqual(t, expected, gh) |
| 78 | + }) |
| 79 | + |
| 80 | + t.Run("non test files are not picked up", func(t *testing.T) { |
| 81 | + testingDir := t.TempDir() |
| 82 | + createFileWithTestSuiteAndTests(t, "FeeMiddlewareTestSuite", "TestA", "TestB", testingDir, nonTestFile) |
| 83 | + |
| 84 | + gh, err := getGithubActionMatrixForTests(testingDir) |
| 85 | + assert.NoError(t, err) |
| 86 | + assert.Empty(t, gh.Include) |
| 87 | + }) |
| 88 | + |
| 89 | + t.Run("fails when there are multiple suite runs", func(t *testing.T) { |
| 90 | + testingDir := t.TempDir() |
| 91 | + createFileWithTestSuiteAndTests(t, "FeeMiddlewareTestSuite", "TestA", "TestB", testingDir, nonTestFile) |
| 92 | + |
| 93 | + fileWithTwoSuites := `package foo |
| 94 | +func SuiteOne(t *testing.T) { |
| 95 | + suite.Run(t, new(FeeMiddlewareTestSuite)) |
| 96 | +} |
| 97 | +
|
| 98 | +func SuiteTwo(t *testing.T) { |
| 99 | + suite.Run(t, new(FeeMiddlewareTestSuite)) |
| 100 | +} |
| 101 | +
|
| 102 | +type FeeMiddlewareTestSuite struct {} |
| 103 | +` |
| 104 | + |
| 105 | + err := os.WriteFile(path.Join(testingDir, goTestFileNameOne), []byte(fileWithTwoSuites), os.FileMode(777)) |
| 106 | + assert.NoError(t, err) |
| 107 | + |
| 108 | + _, err = getGithubActionMatrixForTests(testingDir) |
| 109 | + assert.Error(t, err) |
| 110 | + }) |
| 111 | +} |
| 112 | + |
| 113 | +func assertGithubActionTestMatricesEqual(t *testing.T, expected, actual GithubActionTestMatrix) { |
| 114 | + // sort by both suite and test as the order of the end result does not matter as |
| 115 | + // all tests will be run. |
| 116 | + sort.SliceStable(expected.Include, func(i, j int) bool { |
| 117 | + memberI := expected.Include[i] |
| 118 | + memberJ := expected.Include[j] |
| 119 | + if memberI.Suite == memberJ.Suite { |
| 120 | + return memberI.Test < memberJ.Test |
| 121 | + } |
| 122 | + return memberI.Suite < memberJ.Suite |
| 123 | + }) |
| 124 | + |
| 125 | + sort.SliceStable(actual.Include, func(i, j int) bool { |
| 126 | + memberI := actual.Include[i] |
| 127 | + memberJ := actual.Include[j] |
| 128 | + if memberI.Suite == memberJ.Suite { |
| 129 | + return memberI.Test < memberJ.Test |
| 130 | + } |
| 131 | + return memberI.Suite < memberJ.Suite |
| 132 | + }) |
| 133 | + assert.Equal(t, expected.Include, actual.Include) |
| 134 | +} |
| 135 | + |
| 136 | +func goTestFileContents(suiteName, fnName1, fnName2 string) string { |
| 137 | + |
| 138 | + replacedSuiteName := strings.ReplaceAll(`package foo |
| 139 | +
|
| 140 | +func TestSuiteName(t *testing.T) { |
| 141 | + suite.Run(t, new(SuiteName)) |
| 142 | +} |
| 143 | +
|
| 144 | +type SuiteName struct {} |
| 145 | +
|
| 146 | +func (s *SuiteName) fnName1() {} |
| 147 | +func (s *SuiteName) fnName2() {} |
| 148 | +
|
| 149 | +func (s *SuiteName) suiteHelper() {} |
| 150 | +
|
| 151 | +func helper() {} |
| 152 | +`, "SuiteName", suiteName) |
| 153 | + |
| 154 | + replacedFn1Name := strings.ReplaceAll(replacedSuiteName, "fnName1", fnName1) |
| 155 | + return strings.ReplaceAll(replacedFn1Name, "fnName2", fnName2) |
| 156 | +} |
| 157 | + |
| 158 | +func createFileWithTestSuiteAndTests(t *testing.T, suiteName, fn1Name, fn2Name, dir, filename string) { |
| 159 | + goFileContents := goTestFileContents(suiteName, fn1Name, fn2Name) |
| 160 | + err := os.WriteFile(path.Join(dir, filename), []byte(goFileContents), os.FileMode(777)) |
| 161 | + assert.NoError(t, err) |
| 162 | +} |
0 commit comments