summaryrefslogtreecommitdiffstats
path: root/plugin/rpcplugin/main_test.go
blob: 06423106c664cc2b44b0981da0eca2271528e932 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package rpcplugin

import (
	"context"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/mattermost/mattermost-server/mlog"
	"github.com/mattermost/mattermost-server/plugin/plugintest"
	"github.com/mattermost/mattermost-server/plugin/rpcplugin/rpcplugintest"
)

func TestMain(t *testing.T) {
	// Setup a global logger to catch tests logging outside of app context
	// The global logger will be stomped by apps initalizing but that's fine for testing. Ideally this won't happen.
	mlog.InitGlobalLogger(mlog.NewLogger(&mlog.LoggerConfiguration{
		EnableConsole: true,
		ConsoleJson:   true,
		ConsoleLevel:  "error",
		EnableFile:    false,
	}))

	dir, err := ioutil.TempDir("", "")
	require.NoError(t, err)
	defer os.RemoveAll(dir)

	plugin := filepath.Join(dir, "plugin.exe")
	rpcplugintest.CompileGo(t, `
		package main

		import (
			"github.com/mattermost/mattermost-server/plugin/rpcplugin"
		)

		type MyPlugin struct {}

		func main() {
			rpcplugin.Main(&MyPlugin{})
		}
	`, plugin)

	ctx, cancel := context.WithCancel(context.Background())
	p, ipc, err := NewProcess(ctx, plugin)
	require.NoError(t, err)
	defer p.Wait()

	muxer := NewMuxer(ipc, false)
	defer muxer.Close()

	defer cancel()

	var api plugintest.API

	hooks, err := ConnectMain(muxer, "plugin_id")
	require.NoError(t, err)
	assert.NoError(t, hooks.OnActivate(&api))
	assert.NoError(t, hooks.OnDeactivate())
}