This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins_test.go
158 lines (147 loc) · 4.15 KB
/
plugins_test.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package pink
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func makeManifestPaths(pluginPaths []string) error {
for _, pluginPath := range pluginPaths {
err := os.MkdirAll(pluginPath, 0777)
if err != nil {
return err
}
f, err := os.Create(filepath.Join(pluginPath, "manifest.json"))
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
}
return nil
}
func setUp(t *testing.T) (func(), string) {
tmpDir, err := ioutil.TempDir("", "pink-test")
if err != nil {
t.Fatalf(err.Error())
}
cleanUp := func() { os.RemoveAll(tmpDir) }
pluginRoot := GetPluginsDir(tmpDir)
paths := []string{
filepath.Join(pluginRoot, "system", "run"),
filepath.Join(pluginRoot, "system", "stop"),
filepath.Join(pluginRoot, "foo"),
}
err = makeManifestPaths(paths)
if err != nil {
t.Fatalf(err.Error())
}
return cleanUp, pluginRoot
}
// findPlugins returns the location of all intalled manifest.json files below a given root.
func TestFindPlugin(t *testing.T) {
tearDown, pluginRoot := setUp(t)
defer tearDown()
assertPlugins := func(t *testing.T, root string, expected []string) {
t.Run(root, func(t *testing.T) {
plugins, err := findPlugins(root)
if err != nil {
t.Fatalf(err.Error())
}
require.ElementsMatch(t, expected, plugins)
})
}
assertPlugins(t, pluginRoot, []string{"system", "foo"})
assertPlugins(t, filepath.Join(pluginRoot, "system"), []string{"run", "stop"})
assertPlugins(t, filepath.Join(pluginRoot, "system", "run"), []string{})
assertPlugins(t, filepath.Join(pluginRoot, "system", "stop"), []string{})
assertPlugins(t, filepath.Join(pluginRoot, "foo"), []string{})
}
// IsInvokable indicates the leaves of the command tree.
func TestIsInvokable(t *testing.T) {
tearDown, pluginRoot := setUp(t)
defer tearDown()
inv, err := isInvokable(filepath.Join(pluginRoot, "foo"))
require.NoError(t, err)
require.True(t, inv)
inv, err = isInvokable(filepath.Join(pluginRoot, "system"))
require.False(t, inv)
inv, err = isInvokable(filepath.Join(pluginRoot, "system", "run"))
require.True(t, inv)
inv, err = isInvokable(filepath.Join(pluginRoot, "system", "stop"))
require.True(t, inv)
inv, err = isInvokable(filepath.Join(pluginRoot, "system", "run", "up"))
require.False(t, inv)
}
// DispatchCommand finds the appropriate plugins and passes it the right portion of the arguments.
func TestDispatchCommand(t *testing.T) {
cases := []struct {
name string
inArgs []string
err string
manifest string
outArgs []string
runHelp bool
}{
{
name: "Tier 1 manifest, with zero args",
inArgs: []string{"foo"},
manifest: "foo/manifest.json",
outArgs: []string{},
},
{
name: "Tier 1 manifest, with multiple args",
inArgs: []string{"foo", "-v", "-f", "Bernard", "-n", "Cribbins"},
manifest: "foo/manifest.json",
outArgs: []string{"-v", "-f", "Bernard", "-n", "Cribbins"},
},
{
name: "Tier 2 manifest, with single arg",
inArgs: []string{"system", "run", "postgres"},
manifest: "system/run/manifest.json",
outArgs: []string{"postgres"},
},
{
name: "Non-existant Tier-1 manifest",
inArgs: []string{"rather", "--name", "Terry Thomas"},
err: `No plug-in called "rather" is installed`,
},
{
name: "Non-existant Tier-2 manifest",
inArgs: []string{"system", "ding-dong", "--name", "Leslie Phillips"},
err: `No plug-in called "system ding-dong" is installed`,
},
{
name: "Tier-1 with no manifest (run help)",
inArgs: []string{"system"},
runHelp: true,
},
{
name: "Tier-1, explicitly run help",
inArgs: []string{"system", "-h"},
runHelp: true,
},
}
tearDown, pluginRoot := setUp(t)
defer tearDown()
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
manifest, args, runHelp, err := DispatchCommand(c.inArgs, []string{}, pluginRoot)
if c.err != "" {
require.EqualError(t, err, c.err)
return
}
require.NoError(t, err)
if c.runHelp {
require.True(t, runHelp)
return
}
require.False(t, runHelp)
require.Equal(t, c.outArgs, args)
require.Equal(t, filepath.Join(pluginRoot, c.manifest), manifest)
})
}
}