github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/plugin/pluginenv/search_path_test.go (about)

     1  package pluginenv
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/mattermost/mattermost-server/model"
    13  )
    14  
    15  func TestScanSearchPath(t *testing.T) {
    16  	dir := initTmpDir(t, map[string]string{
    17  		".foo/plugin.json": `{"id": "foo"}`,
    18  		"foo/bar":          "asdf",
    19  		"foo/plugin.json":  `{"id": "foo"}`,
    20  		"bar/zxc":          "qwer",
    21  		"baz/plugin.yaml":  "id: baz",
    22  		"bad/plugin.json":  "asd",
    23  		"qwe":              "asd",
    24  	})
    25  	defer os.RemoveAll(dir)
    26  
    27  	plugins, err := ScanSearchPath(dir)
    28  	require.NoError(t, err)
    29  	assert.Len(t, plugins, 3)
    30  	assert.Contains(t, plugins, &model.BundleInfo{
    31  		Path:         filepath.Join(dir, "foo"),
    32  		ManifestPath: filepath.Join(dir, "foo", "plugin.json"),
    33  		Manifest: &model.Manifest{
    34  			Id: "foo",
    35  		},
    36  	})
    37  	assert.Contains(t, plugins, &model.BundleInfo{
    38  		Path:         filepath.Join(dir, "baz"),
    39  		ManifestPath: filepath.Join(dir, "baz", "plugin.yaml"),
    40  		Manifest: &model.Manifest{
    41  			Id: "baz",
    42  		},
    43  	})
    44  	foundError := false
    45  	for _, x := range plugins {
    46  		if x.ManifestError != nil {
    47  			assert.Equal(t, x.Path, filepath.Join(dir, "bad"))
    48  			assert.Equal(t, x.ManifestPath, filepath.Join(dir, "bad", "plugin.json"))
    49  			syntexError, ok := x.ManifestError.(*json.SyntaxError)
    50  			assert.True(t, ok)
    51  			assert.EqualValues(t, 1, syntexError.Offset)
    52  			foundError = true
    53  		}
    54  	}
    55  	assert.True(t, foundError)
    56  }
    57  
    58  func TestScanSearchPath_Error(t *testing.T) {
    59  	plugins, err := ScanSearchPath("not a valid path!")
    60  	assert.Nil(t, plugins)
    61  	assert.Error(t, err)
    62  }