Centralize the GetPackagePath (#27004)

This commit is contained in:
Jesse Hallam 2024-05-15 12:05:13 -03:00 committed by GitHub
parent e96db725ea
commit cd51dec6e5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 36 additions and 71 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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)
}

View file

@ -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))

View file

@ -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()

View file

@ -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)
}

View file

@ -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
}

View file

@ -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)

View file

@ -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()

View file

@ -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()

View file

@ -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()

View file

@ -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"