summaryrefslogtreecommitdiffstats
path: root/plugin/pluginenv/search_path_test.go
blob: d9a18cf56bfd3f542727e87d5f75f1255cb82b07 (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
package pluginenv

import (
	"encoding/json"
	"os"
	"path/filepath"
	"testing"

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

	"github.com/mattermost/platform/plugin"
)

func TestScanSearchPath(t *testing.T) {
	dir := initTmpDir(t, map[string]string{
		".foo/plugin.json": `{"id": "foo"}`,
		"foo/bar":          "asdf",
		"foo/plugin.json":  `{"id": "foo"}`,
		"bar/zxc":          "qwer",
		"baz/plugin.yaml":  "id: baz",
		"bad/plugin.json":  "asd",
		"qwe":              "asd",
	})
	defer os.RemoveAll(dir)

	plugins, err := ScanSearchPath(dir)
	require.NoError(t, err)
	assert.Len(t, plugins, 3)
	assert.Contains(t, plugins, &plugin.BundleInfo{
		Path:         filepath.Join(dir, "foo"),
		ManifestPath: filepath.Join(dir, "foo", "plugin.json"),
		Manifest: &plugin.Manifest{
			Id: "foo",
		},
	})
	assert.Contains(t, plugins, &plugin.BundleInfo{
		Path:         filepath.Join(dir, "baz"),
		ManifestPath: filepath.Join(dir, "baz", "plugin.yaml"),
		Manifest: &plugin.Manifest{
			Id: "baz",
		},
	})
	foundError := false
	for _, x := range plugins {
		if x.ManifestError != nil {
			assert.Equal(t, x.Path, filepath.Join(dir, "bad"))
			assert.Equal(t, x.ManifestPath, filepath.Join(dir, "bad", "plugin.json"))
			syntexError, ok := x.ManifestError.(*json.SyntaxError)
			assert.True(t, ok)
			assert.EqualValues(t, 1, syntexError.Offset)
			foundError = true
		}
	}
	assert.True(t, foundError)
}

func TestScanSearchPath_Error(t *testing.T) {
	plugins, err := ScanSearchPath("not a valid path!")
	assert.Nil(t, plugins)
	assert.Error(t, err)
}