WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 1bc3308

Browse files
committed
chore: bring in some custom code fixes and optimize integration tests
1 parent d2459cc commit 1bc3308

File tree

7 files changed

+68
-71
lines changed

7 files changed

+68
-71
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ bin/
66
summary.md
77
/.speakeasy/
88
temp/
9-
integration/temp
9+
integrationTests/
1010
.DS_Store
1111
.vscode/launch.json
1212
__debug_bin*

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ require (
3838
github.com/speakeasy-api/huh v1.1.2
3939
github.com/speakeasy-api/jq v0.1.1-0.20251107233444-84d7e49e84a4
4040
github.com/speakeasy-api/openapi v1.11.3
41-
github.com/speakeasy-api/openapi-generation/v2 v2.778.1
41+
github.com/speakeasy-api/openapi-generation/v2 v2.778.5
4242
github.com/speakeasy-api/sdk-gen-config v1.43.1
4343
github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.26.7
4444
github.com/speakeasy-api/speakeasy-core v0.21.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,8 @@ github.com/speakeasy-api/libopenapi v0.21.9-fixhiddencomps-fixed h1:PL/kpBY5vkBm
535535
github.com/speakeasy-api/libopenapi v0.21.9-fixhiddencomps-fixed/go.mod h1:Gc8oQkjr2InxwumK0zOBtKN9gIlv9L2VmSVIUk2YxcU=
536536
github.com/speakeasy-api/openapi v1.11.3 h1:7ExJYuJM3hiV35EROK3i+/w8SdEep3Ct9BivGaOkbNI=
537537
github.com/speakeasy-api/openapi v1.11.3/go.mod h1:ITV3em4IFe1Hd4gX5Peq9TE7+Rfd/WIHZE/aqxNgihg=
538-
github.com/speakeasy-api/openapi-generation/v2 v2.778.1 h1:F1XIyov3HKjrbZf5GUku4ucXrf68jOKOxZjCGENhcLc=
539-
github.com/speakeasy-api/openapi-generation/v2 v2.778.1/go.mod h1:ol7GV+VKS4rlkH1pkIdw55n0gXa3OOL+V32h65JRruA=
538+
github.com/speakeasy-api/openapi-generation/v2 v2.778.5 h1:Dl6A8vNWryhJkOmtSg7uYFAN0pgJert11f/ClIH9PaY=
539+
github.com/speakeasy-api/openapi-generation/v2 v2.778.5/go.mod h1:ol7GV+VKS4rlkH1pkIdw55n0gXa3OOL+V32h65JRruA=
540540
github.com/speakeasy-api/sdk-gen-config v1.43.1 h1:rhhv6mAVV2yl1I6TqHkpunnOnSXyH5milgzEA9vJPEo=
541541
github.com/speakeasy-api/sdk-gen-config v1.43.1/go.mod h1:kD0NPNX5yaG4j+dcCpLL0hHKQbFk6X93obp+v1XlK5E=
542542
github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.26.7 h1:SoWZkRlpFlv8qibCfXWrBZay1JeLS9uqJ+1cu+DFgXo=

integration/main_test.go

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,74 +6,80 @@ import (
66
"os/exec"
77
"path/filepath"
88
"runtime"
9+
"sync"
910
"testing"
1011

1112
"github.com/stretchr/testify/assert"
1213
)
1314

1415
// prebuiltBinary holds the path to the pre-built speakeasy binary.
1516
// This avoids recompiling on every `go run main.go` call, saving ~20s per invocation.
16-
var prebuiltBinary string
17+
var (
18+
prebuiltBinary string
19+
buildOnce sync.Once
20+
buildErr error
21+
)
22+
23+
// ensureBinary builds the speakeasy binary once on first call.
24+
// Subsequent calls return immediately. This is called lazily by execute()
25+
// so that executeI() invocations don't pay the build cost.
26+
func ensureBinary() (string, error) {
27+
buildOnce.Do(func() {
28+
_, filename, _, _ := runtime.Caller(0)
29+
baseFolder := filepath.Join(filepath.Dir(filename), "..")
30+
// Use PID to avoid collision between parallel test runs on the same machine
31+
binaryName := fmt.Sprintf("speakeasy-test-binary-%d", os.Getpid())
32+
if runtime.GOOS == "windows" {
33+
binaryName += ".exe"
34+
}
35+
binaryPath := filepath.Join(os.TempDir(), binaryName)
36+
37+
fmt.Println("Pre-building speakeasy binary for integration tests...")
38+
buildCmd := exec.Command("go", "build", "-o", binaryPath, filepath.Join(baseFolder, "main.go"))
39+
buildCmd.Dir = baseFolder
40+
buildCmd.Stdout = os.Stdout
41+
buildCmd.Stderr = os.Stderr
42+
if err := buildCmd.Run(); err != nil {
43+
buildErr = fmt.Errorf("failed to pre-build speakeasy binary: %w", err)
44+
return
45+
}
46+
prebuiltBinary = binaryPath
47+
fmt.Println("Pre-built speakeasy binary:", prebuiltBinary)
48+
})
49+
return prebuiltBinary, buildErr
50+
}
1751

1852
// Entrypoint for CLI integration tests
1953
func TestMain(m *testing.M) {
20-
// Create a temporary directory
21-
if _, err := os.Stat(tempDir); err == nil {
22-
if err := os.RemoveAll(tempDir); err != nil {
23-
panic(err)
24-
}
25-
}
54+
testDir := integrationTestsDir()
2655

27-
if err := os.Mkdir(tempDir, 0o755); err != nil {
56+
// Create the integrationTests directory (MkdirAll is safe for parallel test processes)
57+
if err := os.MkdirAll(testDir, 0o755); err != nil {
2858
panic(err)
2959
}
3060

31-
// Pre-build the speakeasy binary once to avoid ~20s compilation overhead per test
32-
_, filename, _, _ := runtime.Caller(0)
33-
baseFolder := filepath.Join(filepath.Dir(filename), "..")
34-
binaryName := "speakeasy-test-binary"
35-
if runtime.GOOS == "windows" {
36-
binaryName += ".exe"
37-
}
38-
binaryPath := filepath.Join(os.TempDir(), binaryName)
39-
40-
fmt.Println("Pre-building speakeasy binary for integration tests...")
41-
buildCmd := exec.Command("go", "build", "-o", binaryPath, filepath.Join(baseFolder, "main.go"))
42-
buildCmd.Dir = baseFolder
43-
buildCmd.Stdout = os.Stdout
44-
buildCmd.Stderr = os.Stderr
45-
if err := buildCmd.Run(); err != nil {
46-
panic(fmt.Sprintf("failed to pre-build speakeasy binary: %v", err))
47-
}
48-
prebuiltBinary = binaryPath
49-
fmt.Println("Pre-built speakeasy binary:", prebuiltBinary)
61+
code := m.Run()
5062

51-
// Defer the removal of the temp directory and binary
52-
defer func() {
53-
if err := os.RemoveAll(tempDir); err != nil {
54-
panic(err)
55-
}
63+
// Cleanup must happen before os.Exit (defer is not executed with os.Exit)
64+
if prebuiltBinary != "" {
5665
os.Remove(prebuiltBinary)
57-
}()
66+
}
5867

59-
code := m.Run()
6068
os.Exit(code)
6169
}
6270

6371
func setupTestDir(t *testing.T) string {
6472
t.Helper()
65-
_, filename, _, _ := runtime.Caller(0)
66-
workingDir := filepath.Dir(filename)
67-
temp, err := createTempDir(workingDir)
73+
temp, err := createTempDir("")
6874
assert.NoError(t, err)
69-
registerCleanup(t, workingDir, temp)
75+
registerCleanup(t, temp)
7076

7177
return temp
7278
}
7379

74-
func registerCleanup(t *testing.T, workingDir string, temp string) {
80+
func registerCleanup(t *testing.T, temp string) {
7581
t.Helper()
7682
t.Cleanup(func() {
77-
os.RemoveAll(filepath.Join(workingDir, temp))
83+
os.RemoveAll(temp)
7884
})
7985
}

integration/patches_git_test.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os/exec"
88
"path/filepath"
99
"regexp"
10-
"runtime"
1110
"strings"
1211
"testing"
1312

@@ -695,14 +694,9 @@ func TestGitArchitecture_DeltaCompressionEfficiency(t *testing.T) {
695694
func TestGitArchitecture_ImplicitFetchFromRemote(t *testing.T) {
696695
t.Parallel()
697696

698-
// Create temp directories inside the module tree (required for `go run` to work)
699-
// Using the integration folder as base ensures go.mod is findable
700-
_, filename, _, _ := runtime.Caller(0)
701-
integrationDir := filepath.Dir(filename)
702-
703-
remoteDir := filepath.Join(integrationDir, "temp", "remote-"+randStringBytes(7)+".git")
704-
envADir := filepath.Join(integrationDir, "temp", "envA-"+randStringBytes(7))
705-
envBDir := filepath.Join(integrationDir, "temp", "envB-"+randStringBytes(7))
697+
remoteDir := filepath.Join(integrationTestsDir(), "remote-"+randStringBytes(7)+".git")
698+
envADir := filepath.Join(integrationTestsDir(), "envA-"+randStringBytes(7))
699+
envBDir := filepath.Join(integrationTestsDir(), "envB-"+randStringBytes(7))
706700

707701
// Create directories
708702
require.NoError(t, os.MkdirAll(remoteDir, 0755))
@@ -1216,10 +1210,7 @@ func TestGitArchitecture_MultipleTypeScriptTargetsSameRepo(t *testing.T) {
12161210
func setupDualTypeScriptTargetsTestDir(t *testing.T) string {
12171211
t.Helper()
12181212

1219-
// Create temp directory using runtime.Caller pattern (required for go run to work)
1220-
_, filename, _, _ := runtime.Caller(0)
1221-
integrationDir := filepath.Dir(filename)
1222-
temp := filepath.Join(integrationDir, "temp", "dual-ts-"+randStringBytes(7))
1213+
temp := filepath.Join(integrationTestsDir(), "dual-ts-"+randStringBytes(7))
12231214

12241215
require.NoError(t, os.MkdirAll(temp, 0755))
12251216
t.Cleanup(func() {

integration/utils.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ import (
1111
)
1212

1313
const (
14-
tempDir = "temp"
1514
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
1615
version = "0.0.1"
1716
artifactArch = "linux_amd64"
1817
)
1918

20-
func createTempDir(wd string) (string, error) {
21-
target := filepath.Join(wd, tempDir, randStringBytes(7))
19+
// integrationTestsDir returns the path to the integrationTests directory at repo root.
20+
// This is outside the integration/ package directory to avoid interference with `go test ./integration/...`.
21+
func integrationTestsDir() string {
22+
_, filename, _, _ := runtime.Caller(0)
23+
return filepath.Join(filepath.Dir(filename), "..", "integrationTests")
24+
}
25+
26+
func createTempDir(_ string) (string, error) {
27+
target := filepath.Join(integrationTestsDir(), randStringBytes(7))
2228
if err := os.Mkdir(target, 0o755); err != nil {
2329
return "", err
2430
}

integration/workflow_test.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"os"
99
"os/exec"
1010
"path/filepath"
11-
"runtime"
1211
"strings"
1312
"sync"
1413
"testing"
@@ -282,16 +281,11 @@ func (r *subprocessRunner) Run() error {
282281
func execute(t *testing.T, wd string, args ...string) Runnable {
283282
t.Helper()
284283

285-
// Use pre-built binary if available (set by TestMain), otherwise fall back to go run
286-
var execCmd *exec.Cmd
287-
if prebuiltBinary != "" {
288-
execCmd = exec.Command(prebuiltBinary, args...)
289-
} else {
290-
_, filename, _, _ := runtime.Caller(0)
291-
baseFolder := filepath.Join(filepath.Dir(filename), "..")
292-
mainGo := filepath.Join(baseFolder, "main.go")
293-
execCmd = exec.Command("go", append([]string{"run", mainGo}, args...)...)
294-
}
284+
// Build the binary lazily on first execute() call
285+
binaryPath, err := ensureBinary()
286+
require.NoError(t, err, "failed to build speakeasy binary")
287+
288+
execCmd := exec.Command(binaryPath, args...)
295289
execCmd.Env = os.Environ()
296290
execCmd.Dir = wd
297291

0 commit comments

Comments
 (0)