Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions pkg/config/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1373,20 +1373,26 @@ 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()

if err := v.ReadInConfig(); err != nil {
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
Expand Down
12 changes: 12 additions & 0 deletions pkg/rabbitmq/main_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
19 changes: 19 additions & 0 deletions tests/testutils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"flag"
"fmt"
"io"
"log"
"net/http/httptest"
"net/url"
"path"
Expand Down Expand Up @@ -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.
Expand Down
Loading