|
| 1 | +package helper |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "runtime" |
| 9 | + "sync" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + buildOnce sync.Once |
| 15 | + binPath string |
| 16 | + buildErr error |
| 17 | +) |
| 18 | + |
| 19 | +// NamerBinary compiles the namer binary to a temp directory (once) and returns its path. |
| 20 | +func NamerBinary(t *testing.T) string { |
| 21 | + t.Helper() |
| 22 | + |
| 23 | + buildOnce.Do(func() { |
| 24 | + tmpDir, err := os.MkdirTemp("", "namer-integration-*") |
| 25 | + if err != nil { |
| 26 | + buildErr = err |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + bin := filepath.Join(tmpDir, "namer") |
| 31 | + if runtime.GOOS == "windows" { |
| 32 | + bin += ".exe" |
| 33 | + } |
| 34 | + |
| 35 | + cmd := exec.Command("go", "build", "-o", bin, ".") |
| 36 | + cmd.Dir = projectRoot() |
| 37 | + out, err := cmd.CombinedOutput() |
| 38 | + if err != nil { |
| 39 | + buildErr = &BuildError{Output: string(out), Err: err} |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + binPath = bin |
| 44 | + }) |
| 45 | + |
| 46 | + if buildErr != nil { |
| 47 | + t.Fatalf("failed to build namer binary: %v", buildErr) |
| 48 | + } |
| 49 | + |
| 50 | + return binPath |
| 51 | +} |
| 52 | + |
| 53 | +// BuildError wraps a build failure with compiler output. |
| 54 | +type BuildError struct { |
| 55 | + Output string |
| 56 | + Err error |
| 57 | +} |
| 58 | + |
| 59 | +func (e *BuildError) Error() string { |
| 60 | + return e.Err.Error() + "\n" + e.Output |
| 61 | +} |
| 62 | + |
| 63 | +// Run executes the namer binary with the given args and returns stdout, stderr, and error. |
| 64 | +func Run(t *testing.T, args ...string) (stdout, stderr string, err error) { |
| 65 | + t.Helper() |
| 66 | + |
| 67 | + bin := NamerBinary(t) |
| 68 | + cmd := exec.Command(bin, args...) |
| 69 | + cmd.Env = append(os.Environ(), "HOME="+t.TempDir()) // isolate from user config |
| 70 | + |
| 71 | + var outBuf, errBuf bytes.Buffer |
| 72 | + cmd.Stdout = &outBuf |
| 73 | + cmd.Stderr = &errBuf |
| 74 | + |
| 75 | + err = cmd.Run() |
| 76 | + return outBuf.String(), errBuf.String(), err |
| 77 | +} |
| 78 | + |
| 79 | +// RunWithStdin executes the namer binary with the given args and stdin input. |
| 80 | +func RunWithStdin(t *testing.T, stdin string, args ...string) (stdout, stderr string, err error) { |
| 81 | + t.Helper() |
| 82 | + |
| 83 | + bin := NamerBinary(t) |
| 84 | + cmd := exec.Command(bin, args...) |
| 85 | + cmd.Env = append(os.Environ(), "HOME="+t.TempDir()) |
| 86 | + cmd.Stdin = bytes.NewBufferString(stdin) |
| 87 | + |
| 88 | + var outBuf, errBuf bytes.Buffer |
| 89 | + cmd.Stdout = &outBuf |
| 90 | + cmd.Stderr = &errBuf |
| 91 | + |
| 92 | + err = cmd.Run() |
| 93 | + return outBuf.String(), errBuf.String(), err |
| 94 | +} |
| 95 | + |
| 96 | +// RunWithEnv executes the namer binary with extra environment variables. |
| 97 | +func RunWithEnv(t *testing.T, env []string, args ...string) (stdout, stderr string, err error) { |
| 98 | + t.Helper() |
| 99 | + |
| 100 | + bin := NamerBinary(t) |
| 101 | + cmd := exec.Command(bin, args...) |
| 102 | + cmd.Env = append(os.Environ(), env...) |
| 103 | + |
| 104 | + var outBuf, errBuf bytes.Buffer |
| 105 | + cmd.Stdout = &outBuf |
| 106 | + cmd.Stderr = &errBuf |
| 107 | + |
| 108 | + err = cmd.Run() |
| 109 | + return outBuf.String(), errBuf.String(), err |
| 110 | +} |
| 111 | + |
| 112 | +// WriteConfigFile creates a config file with the given JSON content and returns the path. |
| 113 | +func WriteConfigFile(t *testing.T, content string) string { |
| 114 | + t.Helper() |
| 115 | + |
| 116 | + dir := t.TempDir() |
| 117 | + path := filepath.Join(dir, "namer.json") |
| 118 | + if err := os.WriteFile(path, []byte(content), 0644); err != nil { |
| 119 | + t.Fatalf("failed to write config file: %v", err) |
| 120 | + } |
| 121 | + |
| 122 | + return path |
| 123 | +} |
| 124 | + |
| 125 | +func projectRoot() string { |
| 126 | + // helper.go is at integrations/helper/helper.go |
| 127 | + _, f, _, _ := runtime.Caller(0) |
| 128 | + return filepath.Join(filepath.Dir(f), "..", "..") |
| 129 | +} |
0 commit comments