Этот коммит содержится в:
Jesse Hallam
2024-05-10 18:13:05 -03:00
коммит произвёл GitHub
родитель daf84488cc
Коммит 630bd40141
17 изменённых файлов: 95 добавлений и 91 удалений

Просмотреть файл

@@ -27,6 +27,7 @@ import (
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin"
"github.com/mattermost/mattermost/server/public/plugin/utils"
"github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests"
"github.com/mattermost/mattermost/server/v8/channels/testlib"
"github.com/mattermost/mattermost/server/v8/channels/utils/fileutils"
)
@@ -1990,9 +1991,7 @@ func TestPluginWebSocketSession(t *testing.T) {
pluginID := "com.mattermost.websocket_session_test"
// Compile plugin
testFolder, found := fileutils.FindDir("channels/app/plugin_api_tests")
require.True(t, found, "Cannot find tests folder")
fullPath := path.Join(testFolder, "manual.test_websocket_session", "main.go")
fullPath := path.Join(plugin_api_tests.GetPackagePath(), "manual.test_websocket_session", "main.go")
pluginCode, err := os.ReadFile(fullPath)
require.NoError(t, err)
require.NotEmpty(t, pluginCode)

Просмотреть файл

@@ -32,8 +32,9 @@ import (
"github.com/mattermost/mattermost/server/public/plugin/utils"
"github.com/mattermost/mattermost/server/public/shared/i18n"
"github.com/mattermost/mattermost/server/public/shared/request"
"github.com/mattermost/mattermost/server/v8/channels/utils/fileutils"
"github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests"
"github.com/mattermost/mattermost/server/v8/einterfaces/mocks"
"github.com/mattermost/mattermost/server/v8/tests"
)
func getDefaultPluginSettingsSchema() string {
@@ -802,9 +803,7 @@ func TestPluginAPILoadPluginConfiguration(t *testing.T) {
cfg.PluginSettings.Plugins["testloadpluginconfig"] = pluginJson
})
testFolder, found := fileutils.FindDir("channels/app/plugin_api_tests")
require.True(t, found, "Cannot find tests folder")
fullPath := path.Join(testFolder, "manual.test_load_configuration_plugin", "main.go")
fullPath := path.Join(plugin_api_tests.GetPackagePath(), "manual.test_load_configuration_plugin", "main.go")
err = pluginAPIHookTest(t, th, fullPath, "testloadpluginconfig", `{"id": "testloadpluginconfig", "server": {"executable": "backend.exe"}, "settings_schema": {
"settings": [
@@ -837,9 +836,7 @@ func TestPluginAPILoadPluginConfigurationDefaults(t *testing.T) {
cfg.PluginSettings.Plugins["testloadpluginconfig"] = pluginJson
})
testFolder, found := fileutils.FindDir("channels/app/plugin_api_tests")
require.True(t, found, "Cannot find tests folder")
fullPath := path.Join(testFolder, "manual.test_load_configuration_defaults_plugin", "main.go")
fullPath := path.Join(plugin_api_tests.GetPackagePath(), "manual.test_load_configuration_defaults_plugin", "main.go")
err = pluginAPIHookTest(t, th, fullPath, "testloadpluginconfig", `{
"settings": [
@@ -927,8 +924,7 @@ func TestPluginAPIInstallPlugin(t *testing.T) {
defer th.TearDown()
api := th.SetupPluginAPI()
path, _ := fileutils.FindDir("tests")
tarData, err := os.ReadFile(filepath.Join(path, "testplugin.tar.gz"))
tarData, err := os.ReadFile(filepath.Join(tests.GetPackagePath(), "testplugin.tar.gz"))
require.NoError(t, err)
_, appErr := api.InstallPlugin(bytes.NewReader(tarData), true)
@@ -1004,8 +1000,7 @@ func TestInstallPlugin(t *testing.T) {
defer th.TearDown()
// start an http server to serve plugin's tarball to the test.
path, _ := fileutils.FindDir("tests")
ts := httptest.NewServer(http.FileServer(http.Dir(path)))
ts := httptest.NewServer(http.FileServer(http.Dir(tests.GetPackagePath())))
defer ts.Close()
th.App.UpdateConfig(func(cfg *model.Config) {
@@ -1203,8 +1198,7 @@ func pluginAPIHookTest(t *testing.T, th *TestHelper, fileName string, id string,
func TestBasicAPIPlugins(t *testing.T) {
defaultSchema := getDefaultPluginSettingsSchema()
testFolder, found := fileutils.FindDir("channels/app/plugin_api_tests")
require.True(t, found, "Cannot read find app folder")
testFolder := plugin_api_tests.GetPackagePath()
dirs, err := os.ReadDir(testFolder)
require.NoError(t, err, "Cannot read test folder %v", testFolder)
for _, dir := range dirs {
@@ -1789,9 +1783,7 @@ func TestPluginHTTPConnHijack(t *testing.T) {
th := Setup(t)
defer th.TearDown()
testFolder, found := fileutils.FindDir("channels/app/plugin_api_tests")
require.True(t, found, "Cannot find tests folder")
fullPath := path.Join(testFolder, "manual.test_http_hijack_plugin", "main.go")
fullPath := path.Join(plugin_api_tests.GetPackagePath(), "manual.test_http_hijack_plugin", "main.go")
pluginCode, err := os.ReadFile(fullPath)
require.NoError(t, err)
@@ -1824,9 +1816,7 @@ func TestPluginHTTPUpgradeWebSocket(t *testing.T) {
th := Setup(t)
defer th.TearDown()
testFolder, found := fileutils.FindDir("channels/app/plugin_api_tests")
require.True(t, found, "Cannot find tests folder")
fullPath := path.Join(testFolder, "manual.test_http_upgrade_websocket_plugin", "main.go")
fullPath := path.Join(plugin_api_tests.GetPackagePath(), "manual.test_http_upgrade_websocket_plugin", "main.go")
pluginCode, err := os.ReadFile(fullPath)
require.NoError(t, err)
@@ -2373,9 +2363,7 @@ func TestPluginServeMetrics(t *testing.T) {
cfg.MetricsSettings.ListenAddress = prevAddress
})
testFolder, found := fileutils.FindDir("channels/app/plugin_api_tests")
require.True(t, found, "Cannot find tests folder")
fullPath := path.Join(testFolder, "manual.test_serve_metrics_plugin", "main.go")
fullPath := path.Join(plugin_api_tests.GetPackagePath(), "manual.test_serve_metrics_plugin", "main.go")
pluginCode, err := os.ReadFile(fullPath)
require.NoError(t, err)

Просмотреть файл

@@ -0,0 +1,18 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package plugin_api_tests
import (
"path/filepath"
"runtime"
)
// GetPackagePath returns the filepath to this package for use in tests that need to read data here.
func GetPackagePath() string {
// Find the path to this file
_, filename, _, _ := runtime.Caller(0)
// Return the containing directory
return filepath.Dir(filename)
}

Просмотреть файл

@@ -322,7 +322,7 @@ func NewServer(options ...Option) (*Server, error) {
templatesDir, ok := templates.GetTemplateDirectory()
if !ok {
return nil, errors.New("Failed find server templates in \"templates\" directory or MM_SERVER_PATH")
return nil, errors.New("Failed find server templates in \"templates\" directory")
}
htmlTemplateWatcher, errorsChan, err2 := templates.NewWithWatcher(templatesDir)
if err2 != nil {

Просмотреть файл

@@ -19,6 +19,7 @@ import (
"github.com/mattermost/mattermost/server/v8/channels/store/searchlayer"
"github.com/mattermost/mattermost/server/v8/channels/store/sqlstore"
"github.com/mattermost/mattermost/server/v8/channels/store/storetest"
"github.com/mattermost/mattermost/server/v8/channels/testlib/testdata"
"github.com/mattermost/mattermost/server/v8/channels/utils"
"github.com/mattermost/mattermost/server/v8/platform/services/searchengine"
)
@@ -195,25 +196,15 @@ func (h *MainHelper) PreloadMigrations() {
var buf []byte
var err error
basePath := os.Getenv("MM_SERVER_PATH")
if basePath == "" {
_, errFile := os.Stat("mattermost-server/server")
if os.IsNotExist(errFile) {
basePath = "mattermost/server"
} else {
basePath = "mattermost-server/server"
}
}
relPath := "channels/testlib/testdata"
switch *h.Settings.DriverName {
case model.DatabaseDriverPostgres:
finalPath := filepath.Join(basePath, relPath, "postgres_migration_warmup.sql")
finalPath := filepath.Join(testdata.GetPackagePath(), "postgres_migration_warmup.sql")
buf, err = os.ReadFile(finalPath)
if err != nil {
panic(fmt.Errorf("cannot read file: %v", err))
}
case model.DatabaseDriverMysql:
finalPath := filepath.Join(basePath, relPath, "mysql_migration_warmup.sql")
finalPath := filepath.Join(testdata.GetPackagePath(), "mysql_migration_warmup.sql")
buf, err = os.ReadFile(finalPath)
if err != nil {
panic(fmt.Errorf("cannot read file: %v", err))

Просмотреть файл

@@ -14,6 +14,7 @@ import (
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/utils"
"github.com/mattermost/mattermost/server/v8/channels/testlib/testdata"
"github.com/mattermost/mattermost/server/v8/channels/utils/fileutils"
"github.com/mattermost/mattermost/server/v8/platform/shared/filestore"
)
@@ -38,7 +39,11 @@ type testResourceDetails struct {
}
func findFile(path string) string {
return fileutils.FindPath(path, fileutils.CommonBaseSearchPaths(), func(fileInfo os.FileInfo) bool {
// Use the testdata path to search from the root of the monorepo.
searchPaths := fileutils.CommonBaseSearchPaths()
searchPaths = append(searchPaths, filepath.Join(testdata.GetPackagePath(), "../../../"))
return fileutils.FindPath(path, searchPaths, func(fileInfo os.FileInfo) bool {
return !fileInfo.IsDir()
})
}
@@ -53,7 +58,11 @@ func findDir(dir string) (string, bool) {
return path.Dir(srcPath), true
}
found := fileutils.FindPath(dir, fileutils.CommonBaseSearchPaths(), func(fileInfo os.FileInfo) bool {
// Use the testdata path to search from the root of the monorepo.
searchPaths := fileutils.CommonBaseSearchPaths()
searchPaths = append(searchPaths, filepath.Join(testdata.GetPackagePath(), "../../../"))
found := fileutils.FindPath(dir, searchPaths, func(fileInfo os.FileInfo) bool {
return fileInfo.IsDir()
})
if found == "" {

18
server/channels/testlib/testdata/path.go поставляемый Обычный файл
Просмотреть файл

@@ -0,0 +1,18 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package testdata
import (
"path/filepath"
"runtime"
)
// GetPackagePath returns the filepath to this package for in tests that need to read data here.
func GetPackagePath() string {
// Find the path to this file
_, filename, _, _ := runtime.Caller(0)
// Return the containing directory
return filepath.Dir(filename)
}

Просмотреть файл

@@ -17,11 +17,6 @@ func CommonBaseSearchPaths() []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
}

Просмотреть файл

@@ -5,8 +5,6 @@ package utils
import (
"fmt"
"os"
"path/filepath"
"github.com/mattermost/mattermost/server/public/shared/i18n"
"github.com/mattermost/mattermost/server/v8/channels/utils/fileutils"
@@ -16,9 +14,6 @@ import (
// loaded already and assigns english while loading server config.
func TranslationsPreInit() error {
translationsDir := "i18n"
if mattermostPath := os.Getenv("MM_SERVER_PATH"); mattermostPath != "" {
translationsDir = filepath.Join(mattermostPath, "i18n")
}
i18nDirectory, found := fileutils.FindDirRelBinary(translationsDir)
if !found {