Source File
test_helper.go
Belonging Package
golang.org/x/pkgsite/internal/postgres
package postgres
import (
_
)
func () string {
:= testhelper.TestDataPath("../../migrations")
return "file://" + filepath.ToSlash()
}
func ( string) ( bool, error) {
:= dbtest.DBConnURI()
:= migrationsSource()
, := migrate.New(, )
if != nil {
return false, fmt.Errorf("migrate.New(): %v", )
}
defer func() {
if , := .Close(); != nil || != nil {
= dbtest.MultiErr{, , }
}
}()
if := .Up(); != nil && != migrate.ErrNoChange {
return true, fmt.Errorf("m.Up(): %v", )
}
return false, nil
}
log.Printf("Migration failed for %s: %v, recreating database.", , )
if := recreateDB(); != nil {
return nil, fmt.Errorf("recreateDB(%q): %v", , )
}
_, = tryToMigrate()
}
if != nil {
return nil, fmt.Errorf("unfixable error migrating database: %v.\nConsider running ./devtools/drop_test_dbs.sh", )
}
}
:= os.Getenv("GO_DISCOVERY_DATABASE_DRIVER")
if == "" {
= "pgx"
}
, := database.Open(, dbtest.DBConnURI(), "test")
if != nil {
return nil,
}
return New(), nil
}
func ( *DB, *testing.T) {
:= context.Background()
.Helper()
if := .db.Transact(, sql.LevelDefault, func( *database.DB) error {
if , := .Exec(, `
TRUNCATE modules CASCADE;
TRUNCATE search_documents;
TRUNCATE version_map;
TRUNCATE paths CASCADE;
TRUNCATE symbol_names CASCADE;
TRUNCATE imports_unique;
TRUNCATE latest_module_versions;`); != nil {
return
}
if , := .Exec(, `TRUNCATE module_version_states CASCADE;`); != nil {
return
}
if , := .Exec(, `TRUNCATE excluded_prefixes;`); != nil {
return
}
return nil
}); != nil {
.Fatalf("error resetting test DB: %v", )
}
.expoller.Poll() // clear excluded prefixes
}
func ( string, *testing.M, **DB) {
database.QueryLoggingDisabled = true
, := SetupTestDB()
if != nil {
if errors.Is(, derrors.NotFound) && os.Getenv("GO_DISCOVERY_TESTDB") != "true" {
log.Printf("SKIPPING: could not connect to DB (see doc/postgres.md to set up): %v", )
return
}
log.Fatal()
}
* =
:= .Run()
if := .Close(); != nil {
log.Fatal()
}
os.Exit()
}
func ( string, int, *testing.M, *func(*testing.T) (*DB, func())) {
:= time.Now()
database.QueryLoggingDisabled = true
:= make(chan *DB, )
for := 0; < ; ++ {
, := SetupTestDB(fmt.Sprintf("%s-%d", , ))
if != nil {
if errors.Is(, derrors.NotFound) && os.Getenv("GO_DISCOVERY_TESTDB") != "true" {
log.Printf("SKIPPING: could not connect to DB (see doc/postgres.md to set up): %v", )
return
}
log.Fatal()
}
<-
}
* = func( *testing.T) (*DB, func()) {
:= <-
:= func() {
ResetTestDB(, )
<-
}
return ,
}
log.Printf("parallel test setup for %d DBs took %s", , time.Since())
:= .Run()
if len() != cap() {
log.Fatal("not all DBs were released")
}
for := 0; < ; ++ {
:= <-
if := .Close(); != nil {
log.Fatal()
}
}
os.Exit()
}
func ( context.Context, *testing.T, *DB, *internal.Module) {
mustInsertModule(, , , , "", true)
}
func ( context.Context, *testing.T, *DB, *internal.Module, string) {
mustInsertModule(, , , , , true)
}
func ( context.Context, *testing.T, *DB, *internal.Module) {
mustInsertModule(, , , , "", false)
}
func ( context.Context, *testing.T, *DB, *internal.Module, string, bool) {
.Helper()
var *internal.LatestModuleVersions
if == "-" {
if := .UpdateLatestModuleVersionsStatus(, .ModulePath, 404); != nil {
.Fatal()
}
} else if {
= addLatest(, , , .ModulePath, .Version, )
}
if , := .InsertModule(, , ); != nil {
.Fatal()
}
}
func ( context.Context, *testing.T, *DB, , , string) *internal.LatestModuleVersions {
if !strings.HasPrefix(strings.TrimSpace(), "module") {
= "module " + + "\n" +
}
, := internal.NewLatestModuleVersions(, , , "", []byte())
if != nil {
.Fatal()
}
, := .UpdateLatestModuleVersions(, )
if != nil {
.Fatal()
}
return
}
func ( context.Context, *testing.T, *DB) {
.Helper()
for , := range []struct {
, string
[]string
}{
{
"std",
"v1.13.4",
[]string{
"archive/tar",
"archive/zip",
"cmd/go",
"cmd/internal/obj",
"cmd/internal/obj/arm",
"cmd/internal/obj/arm64",
},
},
{
"std",
"v1.13.0",
[]string{
"archive/tar",
"archive/zip",
"cmd/go",
"cmd/internal/obj",
"cmd/internal/obj/arm",
"cmd/internal/obj/arm64",
},
},
{
"github.com/hashicorp/vault/api",
"v1.1.2",
[]string{""},
},
{
"github.com/hashicorp/vault",
"v1.1.2",
[]string{
"api",
"builtin/audit/file",
"builtin/audit/socket",
"vault/replication",
"vault/seal/transit",
},
},
{
"github.com/hashicorp/vault",
"v1.2.3",
[]string{
"builtin/audit/file",
"builtin/audit/socket",
"internal/foo",
"vault/replication",
"vault/seal/transit",
},
},
{
"github.com/hashicorp/vault",
"v1.0.3",
[]string{
"api",
"builtin/audit/file",
"builtin/audit/socket",
},
},
} {
:= sample.Module(., ., ....)
MustInsertModule(, , , )
}
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |