Files
zooqueen 5577c95c38 deps: consume gh, xfail, and wire from upstream
These three are generic upstream helper libraries that carry no local
changes: the hashes recorded for them are byte-identical to the upstream
modules, and their pinned go.mod bytes declare the upstream path. A
github.com/hanzoai requirement therefore has nothing to resolve against
from a cold cache. Point go.mod, go.sum, and imports at upstream, which
is what the root module already does for wire.

Pin the local docdb replace at v1.0.0; the prior v2 placeholder failed
go.mod parsing.
2026-08-19 22:53:24 -07:00

129 lines
3.1 KiB
Go

// Copyright 2021 Hanzo AI Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package setup
import (
"flag"
"os"
"strings"
"testing"
"github.com/FerretDB/xfail"
"github.com/stretchr/testify/require"
"github.com/hanzoai/docdb/internal/util/must"
)
// IsMongoDB returns true if the current test is running for MongoDB,
// and false if it's running for DocDB/PostgreSQL/DocumentDB.
//
// This function should not be used lightly.
func IsMongoDB(tb testing.TB) bool {
tb.Helper()
return *targetBackendF == "mongodb"
}
// IsYugabyteDB returns true if the current test is running for `docdb-yugabytedb` backend.
//
// This function should not be used lightly.
func IsYugabyteDB(tb testing.TB) bool {
tb.Helper()
return *targetBackendF == "docdb-yugabytedb"
}
// ensureIssueURL panics if URL is not a valid DocDB issue URL.
func ensureIssueURL(url string) {
docDB := strings.HasPrefix(url, "https://github.com/hanzoai/docdb/issues/")
documentDB := strings.HasPrefix(url, "https://github.com/hanzoai/docdb-DocumentDB/issues/")
must.BeTrue(docDB || documentDB)
}
// FailsForDocDB return testing.TB that expects test to fail for DocDB and pass for MongoDB.
// It returns original value if -no-xfail flag was passed.
//
// This function should not be used lightly and always with an issue URL.
func FailsForDocDB(tb testing.TB, url string) testing.TB {
tb.Helper()
ensureIssueURL(url)
if IsMongoDB(tb) {
return tb
}
if *noXFailF {
tb.Logf("Test should fail: %s", url)
return tb
}
return xfail.XFail(tb, url)
}
// FailsForMongoDB return testing.TB that expects test to fail for MongoDB and pass for DocDB.
// It returns original value if -no-xfail flag was passed.
//
// This function should not be used lightly.
func FailsForMongoDB(tb testing.TB, reason string) testing.TB {
tb.Helper()
if !IsMongoDB(tb) {
return tb
}
if *noXFailF {
tb.Logf("Test should fail: %s", reason)
return tb
}
return xfail.XFail(tb, reason)
}
// SkipForMongoDB skips the current test for MongoDB.
//
// [FailsForMongoDB] should be used instead when possible.
func SkipForMongoDB(tb testing.TB, reason string) {
tb.Helper()
if IsMongoDB(tb) {
require.NotEmpty(tb, reason, "reason must not be empty")
tb.Skipf("Skipping for MongoDB: %s.", reason)
}
}
// Main is the entry point for all integration test packages.
// It should be called from main_test.go in each package.
func Main(m *testing.M) {
flag.Parse()
var code int
// ensure that Shutdown runs for any exit code or panic
func() {
// make `go test -list=.` work without side effects
if flag.Lookup("test.list").Value.String() == "" {
Startup()
defer Shutdown()
}
code = m.Run()
}()
os.Exit(code)
}