Skip to content

Commit 97d3d29

Browse files
committed
Adds testing for PR creation
1 parent 42e3fb5 commit 97d3d29

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

tools/flakeguard/golang/golang.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,11 @@ type SkipTest struct {
213213
// SkipTests finds all package/test pairs provided and skips them
214214
func SkipTests(repoPath string, testsToSkip []SkipTest) error {
215215
for _, testToSkip := range testsToSkip {
216-
cmd := exec.Command("go", "list", "-json", testToSkip.Package)
216+
cmd := exec.Command("go", "list", "-f", "{{.Dir}}", testToSkip.Package)
217+
cmd.Dir = repoPath
217218
out, err := cmd.CombinedOutput()
218219
if err != nil {
219-
return fmt.Errorf("error getting package path for %s: %w", testToSkip.Package, err)
220+
return fmt.Errorf("error getting package path for %s: %w\nOutput:\n%s", testToSkip.Package, err, string(out))
220221
}
221222
packagePath := strings.TrimSpace(string(out))
222223
log.Debug().Str("package", testToSkip.Package).Str("test", testToSkip.Name).Str("packagePath", packagePath).Msg("Skipping test")

tools/flakeguard/golang/golang_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package golang
22

33
import (
44
"errors"
5+
"os"
56
"testing"
67

78
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
810
)
911

1012
// Mock version of hasTests function to simulate various scenarios
@@ -54,3 +56,27 @@ func TestFilterPackagesWithTests(t *testing.T) {
5456
assert.Equal(t, expected, result, "Expected empty slice for packages with errors")
5557
})
5658
}
59+
60+
func TestSkipTests(t *testing.T) {
61+
// Create a temp dir copying over the testdata directory
62+
tempDir, err := os.MkdirTemp("./", "testdata-*")
63+
require.NoError(t, err, "Failed to create temp dir")
64+
t.Cleanup(func() {
65+
if !t.Failed() {
66+
if err := os.RemoveAll(tempDir); err != nil {
67+
t.Fatalf("Failed to remove temp dir: %v", err)
68+
}
69+
return
70+
}
71+
t.Logf("Skipping cleanup of temp dir %s because test failed, leaving it for debugging", tempDir)
72+
})
73+
74+
// Copy the testdata directory to the temp dir for running the test on
75+
err = os.CopyFS(tempDir, os.DirFS("testdata"))
76+
testsToSkip := []SkipTest{
77+
{Package: "package_a", Name: "TestPackAFail"},
78+
{Package: "package_b", Name: "TestPackBFail"},
79+
}
80+
err = SkipTests(tempDir, testsToSkip)
81+
assert.NoError(t, err)
82+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/owner/repo/testdata
2+
3+
go 1.24.3
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package package_a
2+
3+
import "testing"
4+
5+
// This test should not get skipped by flakeguard
6+
func TestPackAPass(t *testing.T) {
7+
t.Log("This is a passing test")
8+
}
9+
10+
// This test should get skipped by flakeguard
11+
func TestPackAFail(t *testing.T) {
12+
t.Log("This is a failing test")
13+
t.FailNow()
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package package_b
2+
3+
import "testing"
4+
5+
// This test should get skipped by flakeguard
6+
func TestPackBFail(t *testing.T) {
7+
t.Log("This is a failing test")
8+
t.FailNow()
9+
}
10+
11+
// This test should not get skipped by flakeguard
12+
func TestPackBPass(t *testing.T) {
13+
t.Log("This is a passing test")
14+
}

0 commit comments

Comments
 (0)