diff --git a/pkg/config/config/config.go b/pkg/config/config/config.go index 9614176108c..2127e6f984b 100644 --- a/pkg/config/config/config.go +++ b/pkg/config/config/config.go @@ -1373,6 +1373,14 @@ func createTestViper() *viper.Viper { func UseTestFile(t *testing.T) { t.Helper() + if err := LoadTestFile(); err != nil { + t.Fatalf("fatal error test config file: %s", err) + } +} + +// LoadTestFile injects the standard test configuration without requiring +// testing.T. It is useful from TestMain implementations. +func LoadTestFile() error { build.BuildMode = build.ModeProd v := createTestViper() @@ -1380,13 +1388,11 @@ func UseTestFile(t *testing.T) { if _, ok := err.(viper.ConfigFileNotFoundError); ok { v = createTestViper() } else { - t.Fatalf("fatal error test config file: %s", err) + return err } } - if err := UseViper(v); err != nil { - t.Fatalf("fatal error test config file: %s", err) - } + return UseViper(v) } // FindConfigFile search in the Paths directories for the file with the given diff --git a/pkg/rabbitmq/main_test.go b/pkg/rabbitmq/main_test.go new file mode 100644 index 00000000000..9244bda10ee --- /dev/null +++ b/pkg/rabbitmq/main_test.go @@ -0,0 +1,12 @@ +package rabbitmq_test + +import ( + "os" + "testing" + + "github.com/cozy/cozy-stack/tests/testutils" +) + +func TestMain(m *testing.M) { + os.Exit(testutils.RunTestMainWithCouchDB(m)) +} diff --git a/tests/testutils/test_utils.go b/tests/testutils/test_utils.go index 90c8f6f2c85..d48f1b63e81 100644 --- a/tests/testutils/test_utils.go +++ b/tests/testutils/test_utils.go @@ -8,6 +8,7 @@ import ( "flag" "fmt" "io" + "log" "net/http/httptest" "net/url" "path" @@ -87,6 +88,24 @@ func NeedCouchdb(t *testing.T) { } } +// RunTestMainWithCouchDB loads the standard test configuration and initializes +// global CouchDB views when CouchDB is available. +func RunTestMainWithCouchDB(m *testing.M) int { + if err := config.LoadTestFile(); err != nil { + log.Printf("test setup: could not load test config: %s", err) + return 1 + } + + ctx := context.Background() + if _, err := couchdb.CheckStatus(ctx); err == nil { + if err := couchdb.InitGlobalDB(ctx); err != nil { + log.Printf("test setup: could not initialize global CouchDB: %s", err) + } + } + + return m.Run() +} + // TODO can be used as a reminder to do something in the future. The test that // calls TODO will fail after the limit date, which is an efficient way to not // forget about it.