Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

test(server): resolve repo dirs relative to the package dir, not runtime.Caller (#9850)

Jiayuan Zhangabdb6124a03f
diff --git a/server/cmd/backfill_codex_usage_cache/integration_test.go b/server/cmd/backfill_codex_usage_cache/integration_test.go
index 477bf9bb02..28c143bdf6 100644
--- a/server/cmd/backfill_codex_usage_cache/integration_test.go
+++ b/server/cmd/backfill_codex_usage_cache/integration_test.go
@@ -5,7 +5,6 @@ import (
"fmt"
"os"
"path/filepath"
- "runtime"
"sort"
"strings"
"testing"
@@ -232,12 +231,12 @@ func seedBackfillTaskUsageFixture(t *testing.T, ctx context.Context, pool *pgxpo
}
func testResolveMigrationsDir() (string, error) {
- _, here, _, ok := runtime.Caller(0)
- if !ok {
- return "", fmt.Errorf("runtime.Caller(0) failed")
+ // Relative to the package directory go test runs in; runtime.Caller
+ // would return a module path under -trimpath.
+ dir, err := filepath.Abs(filepath.Join("..", "..", "migrations"))
+ if err != nil {
+ return "", err
}
- dir := filepath.Join(filepath.Dir(here), "..", "..", "migrations")
- dir = filepath.Clean(dir)
if info, err := os.Stat(dir); err != nil || !info.IsDir() {
return "", fmt.Errorf("migrations dir not at %s", dir)
}
diff --git a/server/cmd/server/plugin_schedule_pulse_example_test.go b/server/cmd/server/plugin_schedule_pulse_example_test.go
index a50d000927..f94c98bf01 100644
--- a/server/cmd/server/plugin_schedule_pulse_example_test.go
+++ b/server/cmd/server/plugin_schedule_pulse_example_test.go
@@ -12,7 +12,6 @@ import (
"net/url"
"os"
"path/filepath"
- "runtime"
"strings"
"sync"
"testing"
@@ -53,11 +52,12 @@ type schedulePulseHookBody struct {
func schedulePulseExampleDir(t *testing.T) string {
t.Helper()
- _, thisFile, _, ok := runtime.Caller(0)
- if !ok {
- t.Fatal("cannot locate this test file")
+ // Relative to the package directory go test runs in; runtime.Caller
+ // would return a module path under -trimpath.
+ dir, err := filepath.Abs(filepath.Join("..", "..", "..", "examples", "plugins", "schedule-pulse"))
+ if err != nil {
+ t.Fatalf("resolve the schedule-pulse example dir: %v", err)
}
- dir := filepath.Join(filepath.Dir(thisFile), "..", "..", "..", "examples", "plugins", "schedule-pulse")
if _, err := os.Stat(filepath.Join(dir, plugincontract.ManifestFilename)); err != nil {
t.Fatalf("the schedule-pulse example is missing: %v", err)
}
diff --git a/server/internal/handler/plugin_example_test.go b/server/internal/handler/plugin_example_test.go
index cccb7ae2b1..2866fd60f4 100644
--- a/server/internal/handler/plugin_example_test.go
+++ b/server/internal/handler/plugin_example_test.go
@@ -11,7 +11,6 @@ import (
"net/url"
"os"
"path/filepath"
- "runtime"
"strings"
"testing"
@@ -39,12 +38,12 @@ import (
func exampleRepoDir(t *testing.T) string {
t.Helper()
- _, thisFile, _, ok := runtime.Caller(0)
- if !ok {
- t.Fatal("cannot locate this test file")
+ // server/internal/handler -> repo root, relative to the package directory
+ // go test runs in; runtime.Caller would return a module path under -trimpath.
+ dir, err := filepath.Abs(filepath.Join("..", "..", "..", "examples", "plugins", "deploy-sentinel"))
+ if err != nil {
+ t.Fatalf("resolve the example plugin dir: %v", err)
}
- // server/internal/handler -> repo root
- dir := filepath.Join(filepath.Dir(thisFile), "..", "..", "..", "examples", "plugins", "deploy-sentinel")
if _, err := os.Stat(filepath.Join(dir, plugincontract.ManifestFilename)); err != nil {
t.Fatalf("the example plugin is missing: %v", err)
}
diff --git a/server/internal/migrations/migrations_lint_test.go b/server/internal/migrations/migrations_lint_test.go
index 5488bbfb22..15d107274c 100644
--- a/server/internal/migrations/migrations_lint_test.go
+++ b/server/internal/migrations/migrations_lint_test.go
@@ -3,7 +3,6 @@ package migrations
import (
"fmt"
"path/filepath"
- "runtime"
"sort"
"strconv"
"strings"
@@ -79,11 +78,13 @@ func migrationFilesForLint(t *testing.T, pattern string) []string {
func realMigrationsDir(t *testing.T) string {
t.Helper()
- _, self, _, ok := runtime.Caller(0)
- if !ok {
- t.Fatal("resolve migration lint test path")
+ // Relative to the package directory go test runs in; runtime.Caller
+ // would return a module path under -trimpath.
+ dir, err := filepath.Abs(filepath.Join("..", "..", "migrations"))
+ if err != nil {
+ t.Fatalf("resolve migrations dir: %v", err)
}
- return filepath.Clean(filepath.Join(filepath.Dir(self), "..", "..", "migrations"))
+ return dir
}
func splitMigrationFilename(name string) (stem, direction string, ok bool) {
diff --git a/server/internal/taskusagebackfill/hook_test.go b/server/internal/taskusagebackfill/hook_test.go
index 42780caff3..8c4434e291 100644
--- a/server/internal/taskusagebackfill/hook_test.go
+++ b/server/internal/taskusagebackfill/hook_test.go
@@ -6,7 +6,6 @@ import (
"fmt"
"os"
"path/filepath"
- "runtime"
"sort"
"strings"
"testing"
@@ -208,20 +207,18 @@ func TestHook_FreshDatabaseStampsWatermarkOnly(t *testing.T) {
// database without coupling to cmd/migrate's exit-on-error model.
// ---------------------------------------------------------------------
-// resolveMigrationsDir locates server/migrations relative to this test
-// file's source path. The shared migrations.ResolveDir() walks parent
+// resolveMigrationsDir locates server/migrations relative to this
+// package's directory. The shared migrations.ResolveDir() walks parent
// directories looking for any folder literally named "migrations",
// which collides with the Go package server/internal/migrations and
-// returns the wrong directory. Anchoring on runtime.Caller's file path
-// avoids that ambiguity entirely.
+// returns the wrong directory. go test runs in the package directory, so
+// a fixed relative path avoids that ambiguity; runtime.Caller is not used
+// because -trimpath turns its result into a module path.
func resolveMigrationsDir() (string, error) {
- _, here, _, ok := runtime.Caller(0)
- if !ok {
- return "", fmt.Errorf("runtime.Caller(0) failed")
+ dir, err := filepath.Abs(filepath.Join("..", "..", "migrations"))
+ if err != nil {
+ return "", err
}
- // here = .../server/internal/taskusagebackfill/hook_test.go
- dir := filepath.Join(filepath.Dir(here), "..", "..", "migrations")
- dir = filepath.Clean(dir)
if info, err := os.Stat(dir); err != nil || !info.IsDir() {
return "", fmt.Errorf("migrations dir not at %s", dir)
}