test: start the updates package's Postgres only without -short
go test passes -short as -test.short=true, so scanning os.Args for -test.short never matched and every -short run started a container (and failed without Docker). Read the flag through flag.Parse and testing.Short instead.
This commit is contained in:
@@ -3,6 +3,7 @@ package updates
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
@@ -22,9 +23,13 @@ var (
|
|||||||
userPGErr error
|
userPGErr error
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TestMain starts the Postgres container only without -short. go test
|
||||||
|
// passes -short as -test.short=true, so the flag is read through
|
||||||
|
// flag.Parse and testing.Short, never by scanning os.Args.
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
|
flag.Parse()
|
||||||
code := 1
|
code := 1
|
||||||
if !testShort() {
|
if !testing.Short() {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
||||||
userPGErr = startUserPostgres(ctx)
|
userPGErr = startUserPostgres(ctx)
|
||||||
cancel()
|
cancel()
|
||||||
@@ -39,15 +44,6 @@ func TestMain(m *testing.M) {
|
|||||||
os.Exit(code)
|
os.Exit(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testShort() bool {
|
|
||||||
for _, a := range os.Args {
|
|
||||||
if a == "-test.short" {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func startUserPostgres(ctx context.Context) error {
|
func startUserPostgres(ctx context.Context) error {
|
||||||
ctr, err := postgres.Run(ctx,
|
ctr, err := postgres.Run(ctx,
|
||||||
"postgres:16-alpine",
|
"postgres:16-alpine",
|
||||||
|
|||||||
Reference in New Issue
Block a user