MM-16506: conditionally build go.mod for plugins (#11430)
* MM-16506: conditionally build go.mod for plugins Unless `GO111MODULE=off`, generate a `go.mod` that points at the local copy of `mattermost-server` to ensure plugin tests that compile source code on demand always test with the local copy of mattermost-server. This also fixes an issue with early adopters of `GO111MODULE=on` on the server failing to find the right version of go-i18n dependencies. * plugin: enable testlib resource management * customize fileutils.Find* for testlib
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
11f1accac6
Коммит
25f3bf4a2b
@@ -12,7 +12,10 @@ import (
|
|||||||
var mainHelper *testlib.MainHelper
|
var mainHelper *testlib.MainHelper
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
mainHelper = testlib.NewMainHelperWithOptions(nil)
|
var options = testlib.HelperOptions{
|
||||||
|
EnableResources: true,
|
||||||
|
}
|
||||||
|
mainHelper = testlib.NewMainHelperWithOptions(&options)
|
||||||
defer mainHelper.Close()
|
defer mainHelper.Close()
|
||||||
|
|
||||||
mainHelper.Main(m)
|
mainHelper.Main(m)
|
||||||
|
|||||||
@@ -34,11 +34,38 @@ type testResourceDetails struct {
|
|||||||
action int8
|
action int8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// commonBaseSearchPaths is a custom version of what fileutils exposes. At some point, consolidate.
|
||||||
|
var commonBaseSearchPaths = []string{
|
||||||
|
".",
|
||||||
|
"..",
|
||||||
|
"../..",
|
||||||
|
"../../..",
|
||||||
|
"../../../..",
|
||||||
|
}
|
||||||
|
|
||||||
|
func findFile(path string) string {
|
||||||
|
return fileutils.FindPath(path, commonBaseSearchPaths, func(fileInfo os.FileInfo) bool {
|
||||||
|
return !fileInfo.IsDir()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func findDir(dir string) (string, bool) {
|
||||||
|
found := fileutils.FindPath(dir, commonBaseSearchPaths, func(fileInfo os.FileInfo) bool {
|
||||||
|
return fileInfo.IsDir()
|
||||||
|
})
|
||||||
|
if found == "" {
|
||||||
|
return "./", false
|
||||||
|
}
|
||||||
|
|
||||||
|
return found, true
|
||||||
|
}
|
||||||
|
|
||||||
func getTestResourcesToSetup() []testResourceDetails {
|
func getTestResourcesToSetup() []testResourceDetails {
|
||||||
var srcPath string
|
var srcPath string
|
||||||
var found bool
|
var found bool
|
||||||
|
|
||||||
var testResourcesToSetup = []testResourceDetails{
|
var testResourcesToSetup = []testResourceDetails{
|
||||||
|
{"mattermost-server", "mattermost-server", resourceTypeFolder, actionSymlink},
|
||||||
{"i18n", "i18n", resourceTypeFolder, actionSymlink},
|
{"i18n", "i18n", resourceTypeFolder, actionSymlink},
|
||||||
{"templates", "templates", resourceTypeFolder, actionSymlink},
|
{"templates", "templates", resourceTypeFolder, actionSymlink},
|
||||||
{"tests", "tests", resourceTypeFolder, actionSymlink},
|
{"tests", "tests", resourceTypeFolder, actionSymlink},
|
||||||
@@ -49,14 +76,14 @@ func getTestResourcesToSetup() []testResourceDetails {
|
|||||||
// Finding resources and setting full path to source to be used for further processing
|
// Finding resources and setting full path to source to be used for further processing
|
||||||
for i, testResource := range testResourcesToSetup {
|
for i, testResource := range testResourcesToSetup {
|
||||||
if testResource.resType == resourceTypeFile {
|
if testResource.resType == resourceTypeFile {
|
||||||
srcPath = fileutils.FindFile(testResource.src)
|
srcPath = findFile(testResource.src)
|
||||||
if srcPath == "" {
|
if srcPath == "" {
|
||||||
panic(fmt.Sprintf("Failed to find file %s", testResource.src))
|
panic(fmt.Sprintf("Failed to find file %s", testResource.src))
|
||||||
}
|
}
|
||||||
|
|
||||||
testResourcesToSetup[i].src = srcPath
|
testResourcesToSetup[i].src = srcPath
|
||||||
} else if testResource.resType == resourceTypeFolder {
|
} else if testResource.resType == resourceTypeFolder {
|
||||||
srcPath, found = fileutils.FindDir(testResource.src)
|
srcPath, found = findDir(testResource.src)
|
||||||
if found == false {
|
if found == false {
|
||||||
panic(fmt.Sprintf("Failed to find folder %s", testResource.src))
|
panic(fmt.Sprintf("Failed to find folder %s", testResource.src))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,23 +4,56 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func goMod(t *testing.T, dir string, args ...string) {
|
||||||
|
cmd := exec.Command("go", append([]string{"mod"}, args...)...)
|
||||||
|
cmd.Dir = dir
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to %s: %s", strings.Join(args, " "), string(output))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func CompileGo(t *testing.T, sourceCode, outputPath string) {
|
func CompileGo(t *testing.T, sourceCode, outputPath string) {
|
||||||
dir, err := ioutil.TempDir(".", "")
|
dir, err := ioutil.TempDir(".", "")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer os.RemoveAll(dir)
|
defer os.RemoveAll(dir)
|
||||||
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "main.go"), []byte(sourceCode), 0600))
|
|
||||||
|
dir, err = filepath.Abs(dir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Write out main.go given the source code.
|
||||||
|
main := filepath.Join(dir, "main.go")
|
||||||
|
err = ioutil.WriteFile(main, []byte(sourceCode), 0600)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
if os.Getenv("GO111MODULE") != "off" {
|
||||||
|
var mattermostServerPath string
|
||||||
|
|
||||||
|
// Generate a go.mod file relying on the local copy of the mattermost-server.
|
||||||
|
// testlib linked mattermost-server into the general temporary directory for this test.
|
||||||
|
mattermostServerPath, err = filepath.Abs("mattermost-server")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
goMod(t, dir, "init", "mattermost.com/test")
|
||||||
|
goMod(t, dir, "edit", "-require", "github.com/mattermost/mattermost-server@v0.0.0")
|
||||||
|
goMod(t, dir, "edit", "-replace", fmt.Sprintf("github.com/mattermost/mattermost-server@v0.0.0=%s", mattermostServerPath))
|
||||||
|
}
|
||||||
|
|
||||||
cmd := exec.Command("go", "build", "-o", outputPath, "main.go")
|
cmd := exec.Command("go", "build", "-o", outputPath, "main.go")
|
||||||
cmd.Dir = dir
|
cmd.Dir = dir
|
||||||
cmd.Stdout = os.Stdout
|
cmd.Stdout = os.Stdout
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
require.NoError(t, cmd.Run(), "failed to compile go")
|
err = cmd.Run()
|
||||||
|
require.NoError(t, err, "failed to compile go")
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user