Files
coder/coderd/database/dbtestutil/cleaner_posix.go
T
Spike Curtis e2076beb9f test: change to explicitly compiling dbcleaner on posix (#20206)
relates to: https://github.com/coder/internal/issues/1026

On POSIX systems (macOS and Linux) we compile the dbcleaner binary into a temp directory. This allows us to explicitly separate the compilation step and report the time it takes. We suspect this might be a contributing factor in the above linked flakes we see on macOS.

This doesn't work on Windows because Go tests clean up the temp directory at the end of the test and the dbcleaner binary will still be executing. On Windows you cannot delete a file being executed nor the directory. However, we are not seeing any flakes on Windows so the old behavior seems to be OK.
2025-10-08 16:18:02 +04:00

35 lines
1.3 KiB
Go

//go:build !windows
package dbtestutil
import (
"os/exec"
"path/filepath"
"time"
)
const timeFormat = "2006-01-02 15:04:05.000"
// cleanerCmd builds the cleaner binary in a temporary directory and returns a command to execute it. We can do this on
// POSIX because it's OK to delete the temporary directory after the test: the binary can still run. This is not
// possible on Windows because cleaning the temporary directory will fail if the binary is still running.
// c.f. cleaner_windows.go.
func cleanerCmd(t TBSubset) *exec.Cmd {
start := time.Now()
t.Logf("[%s] starting cleaner binary build", start.Format(timeFormat))
tempDir := t.TempDir()
cleanerBinary := filepath.Join(tempDir, "cleaner")
buildCmd := exec.Command("go", "build", "-o", cleanerBinary, "github.com/coder/coder/v2/coderd/database/dbtestutil/cleanercmd")
output, err := buildCmd.CombinedOutput()
if err != nil {
t.Logf("failed to build cleaner binary: %v", err)
t.Logf("output: %s", string(output))
// Fall back to go run if build fails
return exec.Command("go", "run", "github.com/coder/coder/v2/coderd/database/dbtestutil/cleanercmd")
}
t.Logf("[%s] cleaner binary %s built in %s", time.Now().Format(timeFormat), cleanerBinary, time.Since(start))
return exec.Command(cleanerBinary)
}