Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix github workflow matrix generation #3172

Merged
merged 2 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions cmd/build_test_matrix/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type TestSuitePair struct {
}

func main() {
githubActionMatrix, err := getGithubActionMatrixForTests(e2eTestDirectory, getTestFunctionToRun(), getExcludedTestFunctions())
githubActionMatrix, err := getGithubActionMatrixForTests(e2eTestDirectory, getTestToRun(), getTestEntrypointToRun(), getExcludedTestFunctions())
if err != nil {
fmt.Printf("error generating github action json: %s", err)
os.Exit(1)
Expand All @@ -50,16 +50,26 @@ func main() {
fmt.Println(string(ghBytes))
}

// getTestFunctionToRun returns the specified test function to run if present, otherwise
// getTestEntrypointToRun returns the specified test function to run if present, otherwise
// it returns an empty string which will result in running all test suites.
func getTestFunctionToRun() string {
func getTestEntrypointToRun() string {
testSuite, ok := os.LookupEnv(testEntryPointEnv)
if !ok {
return ""
}
return testSuite
}

// getTestToRun returns the specified test function to run if present.
// If specified, only this test will be run.
func getTestToRun() string {
testName, ok := os.LookupEnv(testNameEnv)
if !ok {
return ""
}
return testName
}

// getExcludedTestFunctions returns a list of test functions that we don't want to run.
func getExcludedTestFunctions() []string {
exclusions, ok := os.LookupEnv(testExclusionsEnv)
Expand All @@ -81,7 +91,7 @@ func contains(s string, items []string) bool {
// getGithubActionMatrixForTests returns a json string representing the contents that should go in the matrix
// field in a github action workflow. This string can be used with `fromJSON(str)` to dynamically build
// the workflow matrix to include all E2E tests under the e2eRootDirectory directory.
func getGithubActionMatrixForTests(e2eRootDirectory, suite string, exlcudedItems []string) (GithubActionTestMatrix, error) {
func getGithubActionMatrixForTests(e2eRootDirectory, testName string, suite string, excludedItems []string) (GithubActionTestMatrix, error) {
testSuiteMapping := map[string][]string{}
fset := token.NewFileSet()
err := filepath.Walk(e2eRootDirectory, func(path string, info fs.FileInfo, err error) error {
Expand All @@ -104,14 +114,11 @@ func getGithubActionMatrixForTests(e2eRootDirectory, suite string, exlcudedItems
return fmt.Errorf("failed extracting test suite name and test cases: %s", err)
}

testName := os.Getenv(testNameEnv)
if testName != "" && contains(testName, testCases) {
testCases = []string{testName}
} else if testName != "" {
return fmt.Errorf("failed to find test case: %s", testName)
}

if contains(suiteNameForFile, exlcudedItems) {
if contains(suiteNameForFile, excludedItems) {
return nil
}

Expand All @@ -138,6 +145,10 @@ func getGithubActionMatrixForTests(e2eRootDirectory, suite string, exlcudedItems
}
}

if testName != "" && len(gh.Include) != 1 {
return GithubActionTestMatrix{}, fmt.Errorf("expected exactly 1 test in the output matrix but got %d", len(gh.Include))
}

return gh, nil
}

Expand Down
38 changes: 33 additions & 5 deletions cmd/build_test_matrix/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ const (
func TestGetGithubActionMatrixForTests(t *testing.T) {
t.Run("empty dir does not fail", func(t *testing.T) {
testingDir := t.TempDir()
_, err := getGithubActionMatrixForTests(testingDir, "", nil)
_, 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)
gh, err := getGithubActionMatrixForTests(testingDir, "", "", nil)
assert.NoError(t, err)

expected := GithubActionTestMatrix{
Expand All @@ -50,7 +50,7 @@ func TestGetGithubActionMatrixForTests(t *testing.T) {
createFileWithTestSuiteAndTests(t, "FeeMiddlewareTestSuite", "TestA", "TestB", testingDir, goTestFileNameOne)
createFileWithTestSuiteAndTests(t, "TransferTestSuite", "TestC", "TestD", testingDir, goTestFileNameTwo)

gh, err := getGithubActionMatrixForTests(testingDir, "", nil)
gh, err := getGithubActionMatrixForTests(testingDir, "", "", nil)
assert.NoError(t, err)

expected := GithubActionTestMatrix{
Expand All @@ -77,11 +77,39 @@ func TestGetGithubActionMatrixForTests(t *testing.T) {
assertGithubActionTestMatricesEqual(t, expected, gh)
})

t.Run("single test can be specified", 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, "TestA", "TestFeeMiddlewareTestSuite", nil)
assert.NoError(t, err)

expected := GithubActionTestMatrix{
Include: []TestSuitePair{
{
EntryPoint: "TestFeeMiddlewareTestSuite",
Test: "TestA",
},
},
}

assertGithubActionTestMatricesEqual(t, expected, gh)
})

t.Run("error if single test doesn't exist", func(t *testing.T) {
testingDir := t.TempDir()
createFileWithTestSuiteAndTests(t, "FeeMiddlewareTestSuite", "TestA", "TestB", testingDir, goTestFileNameOne)

_, err := getGithubActionMatrixForTests(testingDir, "TestThatDoesntExist", "TestFeeMiddlewareTestSuite", nil)
assert.Error(t, err)
})

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)
gh, err := getGithubActionMatrixForTests(testingDir, "", "", nil)
assert.NoError(t, err)
assert.Empty(t, gh.Include)
})
Expand All @@ -105,7 +133,7 @@ type FeeMiddlewareTestSuite struct {}
err := os.WriteFile(path.Join(testingDir, goTestFileNameOne), []byte(fileWithTwoSuites), os.FileMode(0o777))
assert.NoError(t, err)

_, err = getGithubActionMatrixForTests(testingDir, "", nil)
_, err = getGithubActionMatrixForTests(testingDir, "", "", nil)
assert.Error(t, err)
})
}
Expand Down