From d44aa4b65ab66b06f5340d1557ec7870d1b7b39b Mon Sep 17 00:00:00 2001 From: Alessandro Arzilli Date: Thu, 25 Sep 2025 22:42:18 +0200 Subject: [PATCH] tests: add missing test cleanup (#4163) - Changes BuildFixture so that fixtures are not built multiple times - Add some missing calls to AddPathToRemove --- pkg/proc/proc_linux_test.go | 1 + pkg/proc/test/support.go | 44 +++++++++++++++++++------------ service/test/integration2_test.go | 1 + 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/pkg/proc/proc_linux_test.go b/pkg/proc/proc_linux_test.go index f654a334..31ce72ce 100644 --- a/pkg/proc/proc_linux_test.go +++ b/pkg/proc/proc_linux_test.go @@ -85,6 +85,7 @@ func stripAndCopyDebugInfo(f protest.Fixture, t *testing.T) { if err := copyCmd.Run(); err != nil { t.Fatal(err) } + protest.AddPathToRemove(f.Path + ".debug") // Strip the original binary of the debug information. stripCmd := exec.Command("strip", "--strip-debug", "--strip-unneeded", name) diff --git a/pkg/proc/test/support.go b/pkg/proc/test/support.go index 66dd4eb8..0def945d 100644 --- a/pkg/proc/test/support.go +++ b/pkg/proc/test/support.go @@ -33,6 +33,8 @@ type Fixture struct { Source string // BuildDir is the directory where the build command was run. BuildDir string + // buildDone is closed when the fixture is built + buildDone <-chan struct{} } // FixtureKey holds the name and builds flags used for a test fixture. @@ -42,7 +44,7 @@ type fixtureKey struct { } // Fixtures is a map of fixtureKey{ Fixture.Name, buildFlags } to Fixture. -var fixtures = make(map[fixtureKey]Fixture) +var fixtures = make(map[fixtureKey]*Fixture) var fixturesmu sync.Mutex // pathsToRemove is a list of files and directories to remove after running all the tests @@ -105,9 +107,16 @@ func BuildFixture(t testing.TB, name string, flags BuildFlags) Fixture { panic("RunTestsWithFixtures not called") } fk := fixtureKey{name, flags} + fixturesmu.Lock() if f, ok := fixtures[fk]; ok { - return f + fixturesmu.Unlock() + <-f.buildDone + return *f } + buildDone := make(chan struct{}) + fixture := Fixture{Name: name, buildDone: buildDone} + fixtures[fk] = &fixture + fixturesmu.Unlock() if flags&EnableCGOOptimization == 0 { if os.Getenv("CI") == "" || os.Getenv("CGO_CFLAGS") == "" { @@ -190,6 +199,20 @@ func BuildFixture(t testing.TB, name string, flags BuildFlags) Fixture { os.Exit(1) } + source, _ := filepath.Abs(path) + source = filepath.ToSlash(source) + sympath, err := filepath.EvalSymlinks(source) + if err == nil { + source = strings.ReplaceAll(sympath, "\\", "/") + } + + absdir, _ := filepath.Abs(dir) + + fixture.Path = tmpfile + fixture.Source = source + fixture.BuildDir = absdir + close(buildDone) + if flags&EnableDWZCompression != 0 { cmd := exec.Command("dwz", tmpfile) if out, err := cmd.CombinedOutput(); err != nil { @@ -203,21 +226,7 @@ func BuildFixture(t testing.TB, name string, flags BuildFlags) Fixture { } } - source, _ := filepath.Abs(path) - source = filepath.ToSlash(source) - sympath, err := filepath.EvalSymlinks(source) - if err == nil { - source = strings.ReplaceAll(sympath, "\\", "/") - } - - absdir, _ := filepath.Abs(dir) - - fixture := Fixture{Name: name, Path: tmpfile, Source: source, BuildDir: absdir} - - fixturesmu.Lock() - defer fixturesmu.Unlock() - fixtures[fk] = fixture - return fixtures[fk] + return fixture } // RunTestsWithFixtures sets the flag runningWithFixtures to compile fixtures on demand and runs tests with m.Run(). @@ -484,6 +493,7 @@ func getDlvBinInternal(t *testing.T, goflags ...string) string { dlvbin := TempFile("dlv.exe") dlvbincache[strargs] = dlvbin + AddPathToRemove(dlvbin) args := append([]string{"build", "-o", dlvbin}, goflags...) args = append(args, "github.com/go-delve/delve/cmd/dlv") diff --git a/service/test/integration2_test.go b/service/test/integration2_test.go index 3c1c435a..a95ac2d3 100644 --- a/service/test/integration2_test.go +++ b/service/test/integration2_test.go @@ -2825,6 +2825,7 @@ func TestNonGoDebug(t *testing.T) { if out, err := cmd.CombinedOutput(); err != nil { t.Fatalf("Error compiling %s: %s\n%s", path, err, out) } + protest.AddPathToRemove(path) listener, clientConn := service.ListenerPipe() defer listener.Close()