Skip to content

Commit

Permalink
Run acceptance and unit tests when test fixtures change (GoogleCloudP…
Browse files Browse the repository at this point in the history
  • Loading branch information
trodge authored and pengq-google committed May 21, 2024
1 parent 222ffae commit e708893
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
14 changes: 9 additions & 5 deletions .ci/magician/cmd/test_terraform_vcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ func execTestTerraformVCR(prNumber, mmCommitSha, buildID, projectID, buildStep,
}

if len(services) == 0 && !runFullVCR {
fmt.Println("Skipping tests: No go files changed")
fmt.Println("Skipping tests: No go files or test fixtures changed")
os.Exit(0)
}
fmt.Println("Running tests: Go files changed")
fmt.Println("Running tests: Go files or test fixtures changed")

if err := vt.FetchCassettes(provider.Beta, baseBranch, prNumber); err != nil {
fmt.Println("Error fetching cassettes: ", err)
Expand Down Expand Up @@ -281,7 +281,7 @@ Please fix these to complete your PR. If you believe these test failures to be i
}

func modifiedPackages(newBranch, oldBranch string, rnr ExecRunner) (map[string]struct{}, bool, error) {
fmt.Println("Checking for modified go files")
fmt.Println("Checking for modified go files or test fixtures")

if _, err := rnr.Run("git", []string{"fetch", "origin", fmt.Sprintf("%s:%s", oldBranch, oldBranch), "--depth", "1"}, nil); err != nil {

Expand All @@ -290,9 +290,13 @@ func modifiedPackages(newBranch, oldBranch string, rnr ExecRunner) (map[string]s
if err != nil {
return nil, false, err
}
return modifiedPackagesFromDiffs(strings.Split(diffs, "\n"))
}

func modifiedPackagesFromDiffs(diffs []string) (map[string]struct{}, bool, error) {
var goFiles []string
for _, line := range strings.Split(diffs, "\n") {
if strings.HasSuffix(line, ".go") || line == "go.mod" || line == "go.sum" {
for _, line := range diffs {
if strings.HasSuffix(line, ".go") || strings.Contains(line, "test-fixtures") || strings.HasSuffix(line, "go.mod") || strings.HasSuffix(line, "go.sum") {
goFiles = append(goFiles, line)
}
}
Expand Down
66 changes: 66 additions & 0 deletions .ci/magician/cmd/test_terraform_vcr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

import (
"reflect"
"testing"
)

func TestModifiedPackagesFromDiffs(t *testing.T) {
for _, tc := range []struct {
name string
diffs []string
packages map[string]struct{}
all bool
}{
{
name: "one-package",
diffs: []string{"google-beta/services/servicename/resource.go"},
packages: map[string]struct{}{"servicename": struct{}{}},
all: false,
},
{
name: "multiple-packages",
diffs: []string{
"google-beta/services/serviceone/resource.go",
"google-beta/services/servicetwo/test-fixtures/fixture.txt",
"google-beta/services/servicethree/resource_test.go",
},
packages: map[string]struct{}{
"serviceone": struct{}{},
"servicetwo": struct{}{},
"servicethree": struct{}{},
},
all: false,
},
{
name: "all-packages",
diffs: []string{"google-beta/provider/provider.go"},
packages: map[string]struct{}{},
all: true,
},
{
name: "all-packages-go-mod",
diffs: []string{"scripts/go.mod"},
packages: map[string]struct{}{},
all: true,
},
{
name: "all-packages-go-sum",
diffs: []string{"go.sum"},
packages: map[string]struct{}{},
all: true,
},
{
name: "no-packages",
diffs: []string{"website/docs/d/notebooks_runtime_iam_policy.html.markdown"},
packages: map[string]struct{}{},
all: false,
},
} {
if packages, all, _ := modifiedPackagesFromDiffs(tc.diffs); !reflect.DeepEqual(packages, tc.packages) {
t.Errorf("Unexpected packages found for test %s: %v, expected %v", tc.name, packages, tc.packages)
} else if all != tc.all {
t.Errorf("Unexpected value for all packages for test %s: %v, expected %v", tc.name, all, tc.all)
}
}
}
2 changes: 1 addition & 1 deletion .github/workflows/test-tpg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
- name: Check for Code Changes
id: pull_request
run: |
gofiles=$(git diff --name-only HEAD~1 | { grep -e "\.go$" -e "go.mod$" -e "go.sum$" || test $? = 1; })
gofiles=$(git diff --name-only HEAD~1 | { grep -e "\.go$" -e "test-fixtures" -e "go.mod$" -e "go.sum$" || test $? = 1; })
if [ -z "$gofiles" ]; then
echo "has_changes=false" >> $GITHUB_OUTPUT
else
Expand Down

0 comments on commit e708893

Please sign in to comment.