diff --git a/server/channels/api4/plugin_test.go b/server/channels/api4/plugin_test.go index ec21c4dd9d5..e6b137fc8a6 100644 --- a/server/channels/api4/plugin_test.go +++ b/server/channels/api4/plugin_test.go @@ -13,7 +13,6 @@ import ( "net/http" "net/http/httptest" "os" - "path" "path/filepath" "sort" "strings" @@ -27,7 +26,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" "github.com/mattermost/mattermost/server/v8/channels/testlib" "github.com/mattermost/mattermost/server/v8/channels/utils/fileutils" ) @@ -1991,7 +1990,7 @@ func TestPluginWebSocketSession(t *testing.T) { pluginID := "com.mattermost.websocket_session_test" // Compile plugin - fullPath := path.Join(plugin_api_tests.GetPackagePath(), "manual.test_websocket_session", "main.go") + fullPath := filepath.Join(server.GetPackagePath(), "channels", "app", "plugin_api_tests", "manual.test_websocket_session", "main.go") pluginCode, err := os.ReadFile(fullPath) require.NoError(t, err) require.NotEmpty(t, pluginCode) diff --git a/server/channels/app/plugin_api_test.go b/server/channels/app/plugin_api_test.go index ba168a2b1e3..04bc572c1fb 100644 --- a/server/channels/app/plugin_api_test.go +++ b/server/channels/app/plugin_api_test.go @@ -16,7 +16,6 @@ import ( "net/http" "net/http/httptest" "os" - "path" "path/filepath" "strings" "sync" @@ -32,9 +31,8 @@ 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/app/plugin_api_tests" + "github.com/mattermost/mattermost/server/v8" "github.com/mattermost/mattermost/server/v8/einterfaces/mocks" - "github.com/mattermost/mattermost/server/v8/tests" ) func getDefaultPluginSettingsSchema() string { @@ -858,7 +856,7 @@ func TestPluginAPILoadPluginConfiguration(t *testing.T) { cfg.PluginSettings.Plugins["testloadpluginconfig"] = pluginJson }) - fullPath := path.Join(plugin_api_tests.GetPackagePath(), "manual.test_load_configuration_plugin", "main.go") + fullPath := filepath.Join(server.GetPackagePath(), "channels", "app", "plugin_api_tests", "manual.test_load_configuration_plugin", "main.go") err = pluginAPIHookTest(t, th, fullPath, "testloadpluginconfig", `{"id": "testloadpluginconfig", "server": {"executable": "backend.exe"}, "settings_schema": { "settings": [ @@ -891,7 +889,7 @@ func TestPluginAPILoadPluginConfigurationDefaults(t *testing.T) { cfg.PluginSettings.Plugins["testloadpluginconfig"] = pluginJson }) - fullPath := path.Join(plugin_api_tests.GetPackagePath(), "manual.test_load_configuration_defaults_plugin", "main.go") + fullPath := filepath.Join(server.GetPackagePath(), "channels", "app", "plugin_api_tests", "manual.test_load_configuration_defaults_plugin", "main.go") err = pluginAPIHookTest(t, th, fullPath, "testloadpluginconfig", `{ "settings": [ @@ -979,7 +977,7 @@ func TestPluginAPIInstallPlugin(t *testing.T) { defer th.TearDown() api := th.SetupPluginAPI() - tarData, err := os.ReadFile(filepath.Join(tests.GetPackagePath(), "testplugin.tar.gz")) + tarData, err := os.ReadFile(filepath.Join(server.GetPackagePath(), "tests", "testplugin.tar.gz")) require.NoError(t, err) _, appErr := api.InstallPlugin(bytes.NewReader(tarData), true) @@ -1055,7 +1053,7 @@ func TestInstallPlugin(t *testing.T) { defer th.TearDown() // start an http server to serve plugin's tarball to the test. - ts := httptest.NewServer(http.FileServer(http.Dir(tests.GetPackagePath()))) + ts := httptest.NewServer(http.FileServer(http.Dir(filepath.Join(server.GetPackagePath(), "tests")))) defer ts.Close() th.App.UpdateConfig(func(cfg *model.Config) { @@ -1253,14 +1251,14 @@ func pluginAPIHookTest(t *testing.T, th *TestHelper, fileName string, id string, func TestBasicAPIPlugins(t *testing.T) { defaultSchema := getDefaultPluginSettingsSchema() - testFolder := plugin_api_tests.GetPackagePath() + testFolder := filepath.Join(server.GetPackagePath(), "channels", "app", "plugin_api_tests") dirs, err := os.ReadDir(testFolder) require.NoError(t, err, "Cannot read test folder %v", testFolder) for _, dir := range dirs { d := dir.Name() if dir.IsDir() && !strings.HasPrefix(d, "manual.") { t.Run(d, func(t *testing.T) { - mainPath := path.Join(testFolder, d, "main.go") + mainPath := filepath.Join(testFolder, d, "main.go") _, err := os.Stat(mainPath) require.NoError(t, err, "Cannot find plugin main file at %v", mainPath) th := Setup(t).InitBasic().DeleteBots() @@ -1838,7 +1836,7 @@ func TestPluginHTTPConnHijack(t *testing.T) { th := Setup(t) defer th.TearDown() - fullPath := path.Join(plugin_api_tests.GetPackagePath(), "manual.test_http_hijack_plugin", "main.go") + fullPath := filepath.Join(server.GetPackagePath(), "channels", "app", "plugin_api_tests", "manual.test_http_hijack_plugin", "main.go") pluginCode, err := os.ReadFile(fullPath) require.NoError(t, err) @@ -1871,7 +1869,7 @@ func TestPluginHTTPUpgradeWebSocket(t *testing.T) { th := Setup(t) defer th.TearDown() - fullPath := path.Join(plugin_api_tests.GetPackagePath(), "manual.test_http_upgrade_websocket_plugin", "main.go") + fullPath := filepath.Join(server.GetPackagePath(), "channels", "app", "plugin_api_tests", "manual.test_http_upgrade_websocket_plugin", "main.go") pluginCode, err := os.ReadFile(fullPath) require.NoError(t, err) @@ -2418,7 +2416,7 @@ func TestPluginServeMetrics(t *testing.T) { cfg.MetricsSettings.ListenAddress = prevAddress }) - fullPath := path.Join(plugin_api_tests.GetPackagePath(), "manual.test_serve_metrics_plugin", "main.go") + fullPath := filepath.Join(server.GetPackagePath(), "channels", "app", "plugin_api_tests", "manual.test_serve_metrics_plugin", "main.go") pluginCode, err := os.ReadFile(fullPath) require.NoError(t, err) diff --git a/server/channels/app/plugin_api_tests/path.go b/server/channels/app/plugin_api_tests/path.go deleted file mode 100644 index 94a8a6be508..00000000000 --- a/server/channels/app/plugin_api_tests/path.go +++ /dev/null @@ -1,18 +0,0 @@ -// 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) -} diff --git a/server/channels/testlib/helper.go b/server/channels/testlib/helper.go index b55756864cc..d83ee801e34 100644 --- a/server/channels/testlib/helper.go +++ b/server/channels/testlib/helper.go @@ -15,11 +15,11 @@ import ( "github.com/mattermost/mattermost/server/public/model" "github.com/mattermost/mattermost/server/public/shared/mlog" + "github.com/mattermost/mattermost/server/v8" "github.com/mattermost/mattermost/server/v8/channels/store" "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" ) @@ -198,13 +198,13 @@ func (h *MainHelper) PreloadMigrations() { switch *h.Settings.DriverName { case model.DatabaseDriverPostgres: - finalPath := filepath.Join(testdata.GetPackagePath(), "postgres_migration_warmup.sql") + finalPath := filepath.Join(server.GetPackagePath(), "channels", "testlib", "testdata", "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(testdata.GetPackagePath(), "mysql_migration_warmup.sql") + finalPath := filepath.Join(server.GetPackagePath(), "channels", "testlib", "testdata", "mysql_migration_warmup.sql") buf, err = os.ReadFile(finalPath) if err != nil { panic(fmt.Errorf("cannot read file: %v", err)) diff --git a/server/channels/testlib/resources.go b/server/channels/testlib/resources.go index cb69abece67..3468c4278f3 100644 --- a/server/channels/testlib/resources.go +++ b/server/channels/testlib/resources.go @@ -14,7 +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" "github.com/mattermost/mattermost/server/v8/channels/utils/fileutils" "github.com/mattermost/mattermost/server/v8/platform/shared/filestore" ) @@ -41,7 +41,7 @@ type testResourceDetails struct { func findFile(path string) string { // Use the testdata path to search from the root of the monorepo. searchPaths := fileutils.CommonBaseSearchPaths() - searchPaths = append(searchPaths, filepath.Join(testdata.GetPackagePath(), "../../../")) + searchPaths = append(searchPaths, filepath.Join(server.GetPackagePath(), "channels", "testlib", "testdata", "../../../")) return fileutils.FindPath(path, searchPaths, func(fileInfo os.FileInfo) bool { return !fileInfo.IsDir() @@ -60,7 +60,7 @@ func findDir(dir string) (string, bool) { // Use the testdata path to search from the root of the monorepo. searchPaths := fileutils.CommonBaseSearchPaths() - searchPaths = append(searchPaths, filepath.Join(testdata.GetPackagePath(), "../../../")) + searchPaths = append(searchPaths, filepath.Join(server.GetPackagePath(), "channels", "testlib", "testdata", "../../../")) found := fileutils.FindPath(dir, searchPaths, func(fileInfo os.FileInfo) bool { return fileInfo.IsDir() diff --git a/server/channels/testlib/testdata/path.go b/server/channels/testlib/testdata/path.go deleted file mode 100644 index a3188ac9afd..00000000000 --- a/server/channels/testlib/testdata/path.go +++ /dev/null @@ -1,18 +0,0 @@ -// 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) -} diff --git a/server/channels/utils/fileutils/fileutils.go b/server/channels/utils/fileutils/fileutils.go index 69a17ea943c..9844a68cf2e 100644 --- a/server/channels/utils/fileutils/fileutils.go +++ b/server/channels/utils/fileutils/fileutils.go @@ -6,6 +6,8 @@ package fileutils import ( "os" "path/filepath" + + "github.com/mattermost/mattermost/server/v8" ) func CommonBaseSearchPaths() []string { @@ -17,6 +19,8 @@ func CommonBaseSearchPaths() []string { "../../../..", } + // this enables the server to be used in tests from a different repository + paths = append(paths, server.GetPackagePath()) return paths } diff --git a/server/cmd/mmctl/commands/export_e2e_test.go b/server/cmd/mmctl/commands/export_e2e_test.go index 233584c61ec..ee8a24bf8d2 100644 --- a/server/cmd/mmctl/commands/export_e2e_test.go +++ b/server/cmd/mmctl/commands/export_e2e_test.go @@ -9,9 +9,9 @@ import ( "path/filepath" "time" + "github.com/mattermost/mattermost/server/v8" "github.com/mattermost/mattermost/server/v8/cmd/mmctl/client" "github.com/mattermost/mattermost/server/v8/cmd/mmctl/printer" - "github.com/mattermost/mattermost/server/v8/tests" "github.com/mattermost/mattermost/server/public/model" "github.com/mattermost/mattermost/server/public/utils" @@ -21,7 +21,7 @@ import ( func (s *MmctlE2ETestSuite) TestExportListCmdF() { s.SetupTestHelper() importName := "import_test.zip" - importFilePath := filepath.Join(tests.GetPackagePath(), importName) + importFilePath := filepath.Join(server.GetPackagePath(), "tests", importName) exportPath, err := filepath.Abs(filepath.Join(*s.th.App.Config().FileSettings.Directory, *s.th.App.Config().ExportSettings.Directory)) s.Require().Nil(err) @@ -73,7 +73,7 @@ func (s *MmctlE2ETestSuite) TestExportListCmdF() { func (s *MmctlE2ETestSuite) TestExportDeleteCmdF() { s.SetupTestHelper() importName := "import_test.zip" - importFilePath := filepath.Join(tests.GetPackagePath(), importName) + importFilePath := filepath.Join(server.GetPackagePath(), "tests", importName) exportPath, err := filepath.Abs(filepath.Join(*s.th.App.Config().FileSettings.Directory, *s.th.App.Config().ExportSettings.Directory)) s.Require().Nil(err) @@ -179,7 +179,7 @@ func (s *MmctlE2ETestSuite) TestExportCreateCmdF() { func (s *MmctlE2ETestSuite) TestExportDownloadCmdF() { s.SetupTestHelper() importName := "import_test.zip" - importFilePath := filepath.Join(tests.GetPackagePath(), importName) + importFilePath := filepath.Join(server.GetPackagePath(), "tests", importName) exportPath, err := filepath.Abs(filepath.Join(*s.th.App.Config().FileSettings.Directory, *s.th.App.Config().ExportSettings.Directory)) s.Require().Nil(err) diff --git a/server/cmd/mmctl/commands/extract_e2e_test.go b/server/cmd/mmctl/commands/extract_e2e_test.go index 311183a6926..2dfa4358199 100644 --- a/server/cmd/mmctl/commands/extract_e2e_test.go +++ b/server/cmd/mmctl/commands/extract_e2e_test.go @@ -9,9 +9,9 @@ import ( "path/filepath" "time" + "github.com/mattermost/mattermost/server/v8" "github.com/mattermost/mattermost/server/v8/cmd/mmctl/client" "github.com/mattermost/mattermost/server/v8/cmd/mmctl/printer" - "github.com/mattermost/mattermost/server/v8/tests" "github.com/mattermost/mattermost/server/public/model" "github.com/spf13/cobra" @@ -20,7 +20,7 @@ import ( func (s *MmctlE2ETestSuite) TestExtractRunCmdF() { s.SetupTestHelper().InitBasic() docName := "sample-doc.pdf" - docFilePath := filepath.Join(tests.GetPackagePath(), docName) + docFilePath := filepath.Join(server.GetPackagePath(), "tests", docName) s.Run("no permissions", func() { printer.Clean() diff --git a/server/cmd/mmctl/commands/import_e2e_test.go b/server/cmd/mmctl/commands/import_e2e_test.go index edf9a435f42..1a50556be3f 100644 --- a/server/cmd/mmctl/commands/import_e2e_test.go +++ b/server/cmd/mmctl/commands/import_e2e_test.go @@ -9,9 +9,9 @@ import ( "path/filepath" "time" + "github.com/mattermost/mattermost/server/v8" "github.com/mattermost/mattermost/server/v8/cmd/mmctl/client" "github.com/mattermost/mattermost/server/v8/cmd/mmctl/printer" - "github.com/mattermost/mattermost/server/v8/tests" "github.com/mattermost/mattermost/server/public/model" "github.com/spf13/cobra" @@ -20,7 +20,7 @@ import ( func (s *MmctlE2ETestSuite) TestImportUploadCmdF() { s.SetupTestHelper().InitBasic() importName := "import_test.zip" - importFilePath := filepath.Join(tests.GetPackagePath(), importName) + importFilePath := filepath.Join(server.GetPackagePath(), "tests", importName) info, err := os.Stat(importFilePath) s.Require().NoError(err) @@ -92,7 +92,7 @@ func (s *MmctlE2ETestSuite) TestImportUploadCmdF() { func (s *MmctlE2ETestSuite) TestImportProcessCmdF() { s.SetupTestHelper().InitBasic() importName := "import_test.zip" - importFilePath := filepath.Join(tests.GetPackagePath(), "import_test.zip") + importFilePath := filepath.Join(server.GetPackagePath(), "tests", "import_test.zip") s.Run("no permissions", func() { printer.Clean() @@ -131,7 +131,7 @@ func (s *MmctlE2ETestSuite) TestImportProcessCmdF() { func (s *MmctlE2ETestSuite) TestImportListAvailableCmdF() { s.SetupTestHelper().InitBasic() importName := "import_test.zip" - importFilePath := filepath.Join(tests.GetPackagePath(), importName) + importFilePath := filepath.Join(server.GetPackagePath(), "tests", importName) s.Run("no permissions", func() { printer.Clean() @@ -371,7 +371,7 @@ func (s *MmctlE2ETestSuite) TestImportValidateCmdF() { s.SetupTestHelper().InitBasic() importName := "import_test.zip" - importFilePath := filepath.Join(tests.GetPackagePath(), importName) + importFilePath := filepath.Join(server.GetPackagePath(), "tests", importName) s.RunForSystemAdminAndLocal("defaults", func(c client.Client) { printer.Clean() diff --git a/server/cmd/mmctl/commands/plugin_e2e_test.go b/server/cmd/mmctl/commands/plugin_e2e_test.go index 4186e529ec5..617b8b12da5 100644 --- a/server/cmd/mmctl/commands/plugin_e2e_test.go +++ b/server/cmd/mmctl/commands/plugin_e2e_test.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/go-multierror" "github.com/mattermost/mattermost/server/public/model" + "github.com/mattermost/mattermost/server/v8" "github.com/pkg/errors" "path/filepath" @@ -16,13 +17,12 @@ import ( "github.com/mattermost/mattermost/server/v8/cmd/mmctl/client" "github.com/mattermost/mattermost/server/v8/cmd/mmctl/printer" - "github.com/mattermost/mattermost/server/v8/tests" ) func (s *MmctlE2ETestSuite) TestPluginAddCmd() { s.SetupTestHelper().InitBasic() - pluginPath := filepath.Join(tests.GetPackagePath(), "testplugin.tar.gz") + pluginPath := filepath.Join(server.GetPackagePath(), "tests", "testplugin.tar.gz") s.RunForSystemAdminAndLocal("add an already installed plugin without force", func(c client.Client) { printer.Clean() diff --git a/server/tests/path.go b/server/path.go similarity index 96% rename from server/tests/path.go rename to server/path.go index 850fb1ab0d5..5d58f295db9 100644 --- a/server/tests/path.go +++ b/server/path.go @@ -1,7 +1,7 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -package tests +package server import ( "path/filepath"