-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestscript_test.go
More file actions
92 lines (80 loc) · 2.58 KB
/
testscript_test.go
File metadata and controls
92 lines (80 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package apx_test
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
"github.com/rogpeppe/go-internal/testscript"
)
func TestScript(t *testing.T) {
// Ensure the binary exists before running tests.
binPath, err := filepath.Abs(filepath.Join("bin", getBinaryName("apx")))
if err != nil {
t.Fatalf("failed to resolve bin path: %v", err)
}
if _, err := os.Stat(binPath); os.IsNotExist(err) {
if err := os.MkdirAll(filepath.Dir(binPath), 0755); err != nil {
t.Fatalf("failed to create bin directory: %v", err)
}
if err := buildBinary(binPath); err != nil {
t.Fatalf("failed to build binary: %v", err)
}
}
testscript.Run(t, testscript.Params{
Dir: "testdata/script",
Setup: setupTestScript,
RequireExplicitExec: true,
Condition: func(cond string) (bool, error) {
// Support conditional test execution based on environment
switch cond {
case "e2e":
// E2E tests require E2E_ENABLED=1 environment variable
// This prevents running E2E tests in regular CI/local runs
return os.Getenv("E2E_ENABLED") == "1", nil
default:
return false, nil
}
},
})
}
func setupTestScript(env *testscript.Env) error {
// Use the absolute path to the pre-built binary directory directly on PATH.
// This avoids copying the binary per-test, which can trigger ETXTBSY
// (text file busy) on Linux when parallel tests share the same source
// binary while other packages rebuild it.
absDir, err := filepath.Abs("bin")
if err != nil {
return fmt.Errorf("failed to resolve bin directory: %w", err)
}
if os.Getenv("CI") != "" {
println("DEBUG: GOOS =", runtime.GOOS)
println("DEBUG: absDir =", absDir)
println("DEBUG: PathListSeparator =", string(os.PathListSeparator))
}
// Add the absolute bin directory to PATH
newPath := absDir + string(os.PathListSeparator) + env.Getenv("PATH")
env.Setenv("PATH", newPath)
// Set HOME to a writable directory so tools like buf can create caches.
// testscript defaults HOME to /no-home which is read-only.
env.Setenv("HOME", env.WorkDir)
// Set testing environment variables
env.Setenv("APX_DISABLE_TTY", "1")
env.Setenv("NO_COLOR", "1")
env.Setenv("CI", "1")
return nil
}
// buildBinary builds the apx binary to the specified path
func buildBinary(destPath string) error {
cmd := exec.Command("go", "build", "-o", destPath, "./cmd/apx")
cmd.Env = os.Environ()
return cmd.Run()
}
// getBinaryName returns the correct binary name for the current OS
func getBinaryName(baseName string) string {
if runtime.GOOS == "windows" {
return baseName + ".exe"
}
return baseName
}