diff options
Diffstat (limited to 'testrig')
-rw-r--r-- | testrig/config.go | 36 | ||||
-rw-r--r-- | testrig/db.go | 13 | ||||
-rw-r--r-- | testrig/email.go | 5 | ||||
-rw-r--r-- | testrig/router.go | 9 |
4 files changed, 22 insertions, 41 deletions
diff --git a/testrig/config.go b/testrig/config.go index ccc28d17c..64b8649aa 100644 --- a/testrig/config.go +++ b/testrig/config.go @@ -19,45 +19,19 @@ package testrig import ( - "reflect" - "github.com/coreos/go-oidc/v3/oidc" - "github.com/spf13/viper" "github.com/superseriousbusiness/gotosocial/internal/config" ) -// InitTestConfig resets + initializes the viper configuration with test defaults. +// InitTestConfig initializes viper configuration with test defaults. func InitTestConfig() { - // reset viper to an empty state - viper.Reset() - - // get the field names of config.Keys - keyFields := reflect.VisibleFields(reflect.TypeOf(config.Keys)) - - // get the field values of config.Keys - keyValues := reflect.ValueOf(config.Keys) - - // get the field values of TestDefaults - testDefaults := reflect.ValueOf(TestDefaults) - - // for each config field... - for _, field := range keyFields { - // the viper config key should be the value of the key - key, ok := keyValues.FieldByName(field.Name).Interface().(string) - if !ok { - panic("could not convert config.Keys value to string") - } - - // the value should be the test default corresponding to the given fieldName - value := testDefaults.FieldByName(field.Name).Interface() - - // actually set the value in viper -- this will override everything - viper.Set(key, value) - } + config.Config(func(cfg *config.Configuration) { + *cfg = TestDefaults + }) } // TestDefaults returns a Values struct with values set that are suitable for local testing. -var TestDefaults = config.Values{ +var TestDefaults = config.Configuration{ LogLevel: "trace", LogDbQueries: true, ApplicationName: "gotosocial", diff --git a/testrig/db.go b/testrig/db.go index 61d80e6ae..67bc681c6 100644 --- a/testrig/db.go +++ b/testrig/db.go @@ -24,7 +24,6 @@ import ( "strconv" "github.com/sirupsen/logrus" - "github.com/spf13/viper" "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/db/bundb" @@ -69,11 +68,15 @@ var testModels = []interface{}{ // value as the port instead. func NewTestDB() db.DB { if alternateAddress := os.Getenv("GTS_DB_ADDRESS"); alternateAddress != "" { - viper.Set(config.Keys.DbAddress, alternateAddress) + config.Config(func(cfg *config.Configuration) { + cfg.DbAddress = alternateAddress + }) } if alternateDBType := os.Getenv("GTS_DB_TYPE"); alternateDBType != "" { - viper.Set(config.Keys.DbType, alternateDBType) + config.Config(func(cfg *config.Configuration) { + cfg.DbType = alternateDBType + }) } if alternateDBPort := os.Getenv("GTS_DB_PORT"); alternateDBPort != "" { @@ -81,7 +84,9 @@ func NewTestDB() db.DB { if err != nil { panic(err) } - viper.Set(config.Keys.DbPort, port) + config.Config(func(cfg *config.Configuration) { + cfg.DbPort = int(port) + }) } testDB, err := bundb.NewBunDBService(context.Background()) diff --git a/testrig/email.go b/testrig/email.go index 5873ee442..eb1ac4e06 100644 --- a/testrig/email.go +++ b/testrig/email.go @@ -19,7 +19,6 @@ package testrig import ( - "github.com/spf13/viper" "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/email" ) @@ -30,7 +29,9 @@ import ( // the map, with email address of the recipient as the key, and the value as the // parsed email message as it would have been sent. func NewEmailSender(templateBaseDir string, sentEmails map[string]string) email.Sender { - viper.Set(config.Keys.WebTemplateBaseDir, templateBaseDir) + config.Config(func(cfg *config.Configuration) { + cfg.WebTemplateBaseDir = templateBaseDir + }) var sendCallback func(toAddress string, message string) diff --git a/testrig/router.go b/testrig/router.go index 468b468c6..e06e21bd4 100644 --- a/testrig/router.go +++ b/testrig/router.go @@ -26,7 +26,6 @@ import ( "runtime" "github.com/gin-gonic/gin" - "github.com/spf13/viper" "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/router" @@ -38,11 +37,13 @@ import ( // value as the template base directory instead. func NewTestRouter(db db.DB) router.Router { if alternativeTemplateBaseDir := os.Getenv("GTS_WEB_TEMPLATE_BASE_DIR"); alternativeTemplateBaseDir != "" { - viper.Set(config.Keys.WebTemplateBaseDir, alternativeTemplateBaseDir) + config.Config(func(cfg *config.Configuration) { + cfg.WebTemplateBaseDir = alternativeTemplateBaseDir + }) } if alternativeBindAddress := os.Getenv("GTS_BIND_ADDRESS"); alternativeBindAddress != "" { - viper.Set(config.Keys.BindAddress, alternativeBindAddress) + config.SetBindAddress(alternativeBindAddress) } r, err := router.New(context.Background(), db) @@ -56,7 +57,7 @@ func NewTestRouter(db db.DB) router.Router { func ConfigureTemplatesWithGin(engine *gin.Engine) { router.LoadTemplateFunctions(engine) - templateBaseDir := viper.GetString(config.Keys.WebTemplateBaseDir) + templateBaseDir := config.GetWebTemplateBaseDir() if !filepath.IsAbs(templateBaseDir) { // https://stackoverflow.com/questions/31873396/is-it-possible-to-get-the-current-root-of-package-structure-as-a-string-in-golan |