Skip to content

Commit 0a02076

Browse files
7ttpsweatybridge
andauthored
fix(db): support arbitrary test directories (#4590)
* fix(db test): resolve absolute paths before computing relative path when running supabase db test from a docker-outside-of-docker environment, test file paths may be absolute host paths while utils.DbTestsDir is relative. filepath.Rel fails when the base path is relative and the target is absolute. this change converts both paths to absolute before computing the relative path, which handles all path combinations correctly: relative+relative, absolute+absolute, and mixed scenarios like docker-outside-of-docker. closes #3194 * fix: support arbitrary test directories * chore: unset container working dir * chore: refactor bind mount --------- Co-authored-by: Han Qiao <sweatybridge@gmail.com>
1 parent ac1c59a commit 0a02076

1 file changed

Lines changed: 11 additions & 14 deletions

File tree

internal/db/test/test.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,22 @@ const (
2626

2727
func Run(ctx context.Context, testFiles []string, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
2828
// Build test command
29+
if len(testFiles) == 0 {
30+
testFiles = append(testFiles, utils.DbTestsDir)
31+
}
32+
binds := make([]string, len(testFiles))
2933
cmd := []string{"pg_prove", "--ext", ".pg", "--ext", ".sql", "-r"}
30-
for _, fp := range testFiles {
31-
relPath, err := filepath.Rel(utils.DbTestsDir, fp)
32-
if err != nil {
33-
return errors.Errorf("failed to resolve relative path: %w", err)
34+
for i, fp := range testFiles {
35+
if !filepath.IsAbs(fp) {
36+
fp = filepath.Join(utils.CurrentDirAbs, fp)
3437
}
35-
cmd = append(cmd, relPath)
38+
dockerPath := utils.ToDockerPath(fp)
39+
cmd = append(cmd, dockerPath)
40+
binds[i] = fmt.Sprintf("%s:%s:ro", fp, dockerPath)
3641
}
3742
if viper.GetBool("DEBUG") {
3843
cmd = append(cmd, "--verbose")
3944
}
40-
// Mount tests directory into container as working directory
41-
srcPath, err := filepath.Abs(utils.DbTestsDir)
42-
if err != nil {
43-
return errors.Errorf("failed to resolve absolute path: %w", err)
44-
}
45-
dstPath := "/tmp"
46-
binds := []string{fmt.Sprintf("%s:%s:ro", srcPath, dstPath)}
4745
// Enable pgTAP if not already exists
4846
alreadyExists := false
4947
options = append(options, func(cc *pgx.ConnConfig) {
@@ -87,8 +85,7 @@ func Run(ctx context.Context, testFiles []string, config pgconn.Config, fsys afe
8785
"PGPASSWORD=" + config.Password,
8886
"PGDATABASE=" + config.Database,
8987
},
90-
Cmd: cmd,
91-
WorkingDir: dstPath,
88+
Cmd: cmd,
9289
},
9390
hostConfig,
9491
network.NetworkingConfig{},

0 commit comments

Comments
 (0)