(t *testing.T, stdout string, stderr string, exitCode int)
| 66 | } |
| 67 | |
| 68 | func createMockExecutable(t *testing.T, stdout string, stderr string, exitCode int) string { |
| 69 | tmpDir := t.TempDir() |
| 70 | sourcePath := filepath.Join(tmpDir, "main.go") |
| 71 | binaryPath := filepath.Join(tmpDir, "mockexec") |
| 72 | if runtime.GOOS == "windows" { |
| 73 | binaryPath += ".exe" |
| 74 | } |
| 75 | |
| 76 | // Create Go source |
| 77 | source := buildCommandSourceCode(exitCode, stdout, stderr) |
| 78 | |
| 79 | // Write source file |
| 80 | if err := os.WriteFile(sourcePath, []byte(source), 0600); err != nil { |
| 81 | t.Fatal(err) |
| 82 | } |
| 83 | |
| 84 | // Compile |
| 85 | cmd := exec.Command("go", "build", "-o", binaryPath, sourcePath) |
| 86 | if out, err := cmd.CombinedOutput(); err != nil { |
| 87 | t.Fatalf("failed to compile: %v\n%s", err, out) |
| 88 | } |
| 89 | return binaryPath |
| 90 | |
| 91 | } |
| 92 | |
| 93 | func buildCommandSourceCode(exitCode int, stdout, stderr string) string { |
| 94 | return fmt.Sprintf(`package main |
no test coverage detected