From 49a75059c51194bfc86cc2f8744c37203e9155f5 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Tue, 25 Aug 2020 17:29:40 +0200 Subject: [PATCH] [MM-27919] Allow i18n and resources to be loaded using an environment variable (#15341) * [MM-27919] Allow i18n and resources to be loaded using an environment variable * Improve error message * Modify environment variable name to be less ambiguous Co-authored-by: Mattermod --- testlib/resources.go | 27 ++++++++++++++++++--------- utils/i18n.go | 11 +++++++++-- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/testlib/resources.go b/testlib/resources.go index 681e6607df..39645104a3 100644 --- a/testlib/resources.go +++ b/testlib/resources.go @@ -36,17 +36,26 @@ type testResourceDetails struct { action int8 } -// commonBaseSearchPaths is a custom version of what fileutils exposes. At some point, consolidate. -var commonBaseSearchPaths = []string{ - ".", - "..", - "../..", - "../../..", - "../../../..", +// getCommonBaseSearchPaths() is a custom version of what fileutils exposes. At some point, consolidate. +func getCommonBaseSearchPaths() []string { + paths := []string{ + ".", + "..", + "../..", + "../../..", + "../../../..", + } + + // this enables the server to be used in tests from a different repository + if mmPath := os.Getenv("MM_SERVER_PATH"); mmPath != "" { + paths = append(paths, mmPath) + } + + return paths } func findFile(path string) string { - return fileutils.FindPath(path, commonBaseSearchPaths, func(fileInfo os.FileInfo) bool { + return fileutils.FindPath(path, getCommonBaseSearchPaths(), func(fileInfo os.FileInfo) bool { return !fileInfo.IsDir() }) } @@ -61,7 +70,7 @@ func findDir(dir string) (string, bool) { return path.Dir(srcPath), true } - found := fileutils.FindPath(dir, commonBaseSearchPaths, func(fileInfo os.FileInfo) bool { + found := fileutils.FindPath(dir, getCommonBaseSearchPaths(), func(fileInfo os.FileInfo) bool { return fileInfo.IsDir() }) if found == "" { diff --git a/utils/i18n.go b/utils/i18n.go index 6668da5059..43ef776123 100644 --- a/utils/i18n.go +++ b/utils/i18n.go @@ -7,6 +7,7 @@ import ( "fmt" "io/ioutil" "net/http" + "os" "path/filepath" "strings" @@ -32,7 +33,13 @@ func TranslationsPreInit() error { // segfault trying to handle the error, and the untranslated IDs are strictly better. T = TfuncWithFallback("en") TDefault = TfuncWithFallback("en") - return InitTranslationsWithDir("i18n") + + translationsDir := "i18n" + if mattermostPath := os.Getenv("MM_SERVER_PATH"); mattermostPath != "" { + translationsDir = filepath.Join(mattermostPath, "i18n") + } + + return InitTranslationsWithDir(translationsDir) } func InitTranslations(localizationSettings model.LocalizationSettings) error { @@ -46,7 +53,7 @@ func InitTranslations(localizationSettings model.LocalizationSettings) error { func InitTranslationsWithDir(dir string) error { i18nDirectory, found := fileutils.FindDirRelBinary(dir) if !found { - return fmt.Errorf("Unable to find i18n directory") + return fmt.Errorf(fmt.Sprintf("Unable to find i18n directory at %q", dir)) } files, _ := ioutil.ReadDir(i18nDirectory)