refactor: improve test command execution with runCmd and captureOutput helpers

This commit is contained in:
Denis Bilenko (aider) 2025-03-03 16:51:02 +01:00
parent 8b91c207db
commit 86d7606304
1 changed files with 29 additions and 22 deletions

View File

@ -47,16 +47,35 @@ func writeProjectFiles(baseDir string, files map[string]string) error {
return nil return nil
} }
// runCmd runs a command in the given directory and returns its combined output. // runCmd runs a command in the given directory and fails the test if it fails
// AI TODO: This should accept testing.T directly and fail the test if command fails func runCmd(t *testing.T, dir, name string, args ...string) {
func runCmd(dir, name string, args ...string) (string, error) {
cmd := exec.Command(name, args...) cmd := exec.Command(name, args...)
cmd.Dir = dir cmd.Dir = dir
var out bytes.Buffer var out bytes.Buffer
cm.Stdout = &out cmd.Stdout = &out
cmd.Stderr = &out cmd.Stderr = &out
err := cmd.Run() err := cmd.Run()
return out.String(), err if err != nil {
t.Logf("Command failed: %s %s", name, strings.Join(args, " "))
t.Logf("Output:\n%s", out.String())
t.Fatal(err)
}
}
// captureOutput runs a command and returns its output
func captureOutput(t *testing.T, dir, name string, args ...string) string {
cmd := exec.Command(name, args...)
cmd.Dir = dir
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out
err := cmd.Run()
if err != nil {
t.Logf("Command failed: %s %s", name, strings.Join(args, " "))
t.Logf("Output:\n%s", out.String())
t.Fatal(err)
}
return out.String()
} }
// TestPatchWheel tests PatchWheel with several Python versions. // TestPatchWheel tests PatchWheel with several Python versions.
@ -74,10 +93,7 @@ func TestPatchWheel(t *testing.T) {
} }
// Create a virtual environment using uv // Create a virtual environment using uv
// AI TODO: we re not us out here. So do not return out from runCmd; instead if there is stdout or stderr, log it with t.Error(). Note, in one case you need stdout below. So add separate helper captureOutpout to do that. runCmd(t, tempDir, "uv", "venv", "--python", py, "venv")
if out, err := runCmd(tempDir, "uv", "venv", "--python", py, "venv"); err != nil {
t.Fatalf("uv venv creation failed: %v, output: %s", err, out)
}
// Determine the pip and python paths inside the venv. // Determine the pip and python paths inside the venv.
venvBin := filepath.Join(tempDir, "venv", "bin") venvBin := filepath.Join(tempDir, "venv", "bin")
@ -85,14 +101,10 @@ func TestPatchWheel(t *testing.T) {
pipExec := filepath.Join(venvBin, "pip") pipExec := filepath.Join(venvBin, "pip")
// Install build using uv // Install build using uv
if out, err := runCmd(tempDir, pipExec, "install", "build"); err != nil { runCmd(t, tempDir, pipExec, "install", "build")
t.Fatalf("uv pip install failed: %v, output: %s", err, out)
}
// Build the wheel. // Build the wheel.
if out, err := runCmd(tempDir, pyExec, "-m", "build", "--wheel"); err != nil { runCmd(t, tempDir, pyExec, "-m", "build", "--wheel")
t.Fatalf("wheel build failed: %v, output: %s", err, out)
}
distDir := filepath.Join(tempDir, "dist") distDir := filepath.Join(tempDir, "dist")
entries, err := ioutil.ReadDir(distDir) entries, err := ioutil.ReadDir(distDir)
if err != nil || len(entries) == 0 { if err != nil || len(entries) == 0 {
@ -113,15 +125,10 @@ func TestPatchWheel(t *testing.T) {
t.Logf("origWheel=%s patchedWheel=%s", origWheel, patchedWheel) t.Logf("origWheel=%s patchedWheel=%s", origWheel, patchedWheel)
// Install the patched wheel using uv // Install the patched wheel using uv
if out, err := runCmd(tempDir, pipExec, "install", "--reinstall", patchedWheel); err != nil { runCmd(t, tempDir, pipExec, "install", "--reinstall", patchedWheel)
t.Fatalf("failed to install patched wheel with uv: %v, output: %s", err, out)
}
// Run a small command to import the package and print its version. // Run a small command to import the package and print its version.
cmdOut, err := runCmd(tempDir, pyExec, "-c", "import myproj; print(myproj.__version__)") cmdOut := captureOutput(t, tempDir, pyExec, "-c", "import myproj; print(myproj.__version__)")
if err != nil {
t.Fatalf("importing patched package failed: %v, output: %s", err, cmdOut)
}
version := strings.TrimSpace(cmdOut) version := strings.TrimSpace(cmdOut)
if !strings.HasPrefix(version, "0.1.0+") { if !strings.HasPrefix(version, "0.1.0+") {
t.Fatalf("expected version to start with 0.1.0+, got %s", version) t.Fatalf("expected version to start with 0.1.0+, got %s", version)