From e116b30b403d79d6d182090b192de8317e8211aa Mon Sep 17 00:00:00 2001 From: akuma06 Date: Wed, 31 May 2017 10:49:01 +0200 Subject: [PATCH] Better handle of test files After some thoughts, it is better to use the config from config files than default ones --- cache/native/native_test.go | 10 ++++++++++ config/config.go | 18 ++++++++++++++---- config/parser.go | 5 ++--- db/gorm_test.go | 16 +++++++++------- router/template_test.go | 5 +++++ .../torrent/metainfoFetcher/operation_test.go | 10 ++++++++++ util/publicSettings/publicSettings_test.go | 12 +++++++++--- 7 files changed, 59 insertions(+), 17 deletions(-) diff --git a/cache/native/native_test.go b/cache/native/native_test.go index 13a2fb50..0b39bd12 100644 --- a/cache/native/native_test.go +++ b/cache/native/native_test.go @@ -1,13 +1,23 @@ package native import ( + "path" "sync" "testing" "github.com/NyaaPantsu/nyaa/common" + "github.com/NyaaPantsu/nyaa/config" "github.com/NyaaPantsu/nyaa/model" ) +// run before config/parse.go:init() +var _ = func() (_ struct{}) { + config.ConfigPath = path.Join("..", "..", config.ConfigPath) + config.DefaultConfigPath = path.Join("..", "..", config.DefaultConfigPath) + config.Parse() + return +}() + // Basic test for deadlocks and race conditions func TestConcurrency(t *testing.T) { c := New(0.000001) diff --git a/config/config.go b/config/config.go index d3c4a66c..625d1bbe 100644 --- a/config/config.go +++ b/config/config.go @@ -12,6 +12,13 @@ import ( yaml "gopkg.in/yaml.v2" ) +var ( + // DefaultConfigPath : path to the default config file (please do not change it) + DefaultConfigPath = "config/default_config.yml" + // ConfigPath : path to the user specific config file (please do not change it) + ConfigPath = "config/config.yml" +) + // Conf : Modified configuration var Conf *Config var privateConf Config @@ -34,8 +41,12 @@ var allowedDBLogModes = map[string]bool{ "silent": true, } -// Construct a new config variable func init() { + Parse() +} + +// Parse : Parse config into a config variable +func Parse() { getDefaultConfig() privateConf = *DefaultConfig Conf = &privateConf @@ -43,10 +54,9 @@ func init() { } func overrideDefaults() { - path := "config/config.yml" - data, err := ioutil.ReadFile(path) + data, err := ioutil.ReadFile(ConfigPath) if err != nil { - log.Printf("can't read file '%s'", path) + log.Printf("can't read file '%s'", ConfigPath) } err = yaml.Unmarshal(data, &Conf) if err != nil { diff --git a/config/parser.go b/config/parser.go index 2fcf730c..310c2dc3 100644 --- a/config/parser.go +++ b/config/parser.go @@ -12,10 +12,9 @@ var DefaultConfig *Config func getDefaultConfig() *Config { DefaultConfig = &Config{} - path := "config/default_config.yml" - data, err := ioutil.ReadFile(path) + data, err := ioutil.ReadFile(DefaultConfigPath) if err != nil { - log.Printf("can't read file '%s'", path) + log.Printf("can't read file '%s'", DefaultConfigPath) } err = yaml.Unmarshal(data, DefaultConfig) if err != nil { diff --git a/db/gorm_test.go b/db/gorm_test.go index 5938677f..a790453c 100644 --- a/db/gorm_test.go +++ b/db/gorm_test.go @@ -2,12 +2,21 @@ package db import ( "fmt" + "path" "testing" "github.com/NyaaPantsu/nyaa/config" "github.com/azhao12345/gorm" ) +// run before config/parse.go:init() +var _ = func() (_ struct{}) { + config.ConfigPath = path.Join("..", config.ConfigPath) + config.DefaultConfigPath = path.Join("..", config.DefaultConfigPath) + config.Parse() + return +}() + type errorLogger struct { t *testing.T } @@ -29,13 +38,6 @@ func TestGormInitSqlite(t *testing.T) { config.Conf.DBType = SqliteType config.Conf.DBParams = ":memory:?cache=shared&mode=memory" config.Conf.DBLogMode = "detailed" - config.Conf.Models.CommentsTableName = "comments" - config.Conf.Models.FilesTableName = "files" - config.Conf.Models.NotificationsTableName = "notifications" - config.Conf.Models.ReportsTableName = "torrent_reports" - config.Conf.Models.TorrentsTableName = "torrents" - config.Conf.Models.UploadsOldTableName = "user_uploads_old" - config.Conf.Models.LastOldTorrentID = 90000 db, err := GormInit(config.Conf, &errorLogger{t}) if err != nil { diff --git a/router/template_test.go b/router/template_test.go index be6756e9..94f60111 100644 --- a/router/template_test.go +++ b/router/template_test.go @@ -3,11 +3,16 @@ package router import ( "path" "testing" + + "github.com/NyaaPantsu/nyaa/config" ) // run before router/init.go:init() var _ = func() (_ struct{}) { TemplateDir = path.Join("..", TemplateDir) + config.ConfigPath = path.Join("..", config.ConfigPath) + config.DefaultConfigPath = path.Join("..", config.DefaultConfigPath) + config.Parse() return }() diff --git a/service/torrent/metainfoFetcher/operation_test.go b/service/torrent/metainfoFetcher/operation_test.go index 5e3babcd..d5163bce 100644 --- a/service/torrent/metainfoFetcher/operation_test.go +++ b/service/torrent/metainfoFetcher/operation_test.go @@ -1,12 +1,22 @@ package metainfoFetcher import ( + "path" "testing" + "github.com/NyaaPantsu/nyaa/config" "github.com/NyaaPantsu/nyaa/model" "github.com/anacrolix/torrent" ) +// run before config/parse.go:init() +var _ = func() (_ struct{}) { + config.ConfigPath = path.Join("..", "..", "..", config.ConfigPath) + config.DefaultConfigPath = path.Join("..", "..", "..", config.DefaultConfigPath) + config.Parse() + return +}() + func TestInvalidHash(t *testing.T) { client, err := torrent.NewClient(nil) if err != nil { diff --git a/util/publicSettings/publicSettings_test.go b/util/publicSettings/publicSettings_test.go index 1b120337..b3335ef2 100644 --- a/util/publicSettings/publicSettings_test.go +++ b/util/publicSettings/publicSettings_test.go @@ -7,10 +7,16 @@ import ( "github.com/NyaaPantsu/nyaa/config" ) +// run before config/parse.go:init() +var _ = func() (_ struct{}) { + config.ConfigPath = path.Join("..", "..", config.ConfigPath) + config.DefaultConfigPath = path.Join("..", "..", config.DefaultConfigPath) + config.Parse() + return +}() + func TestInitI18n(t *testing.T) { - conf := config.I18nConfig{} - conf.Directory = "translations" - conf.DefaultLanguage = "en-us" + conf := config.Conf.I18n conf.Directory = path.Join("..", "..", conf.Directory) var retriever UserRetriever // not required during initialization