-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplugin_test.go
More file actions
138 lines (116 loc) · 3.57 KB
/
Copy pathplugin_test.go
File metadata and controls
138 lines (116 loc) · 3.57 KB
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
package integration_test
import (
"os"
"path/filepath"
"strings"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
)
var _ = Describe("plugin", func() {
var workingDir string
var r *runner
BeforeEach(func() {
var err error
workingDir, err = os.MkdirTemp("", "kratix-test")
Expect(err).NotTo(HaveOccurred())
r = &runner{exitCode: 0, dir: workingDir}
})
AfterEach(func() {
Expect(os.RemoveAll(workingDir)).To(Succeed())
})
When("called without a subcommand", func() {
It("prints the help", func() {
session := r.run("plugin")
Expect(session.Out).To(SatisfyAll(
gbytes.Say("Provides utilities for interacting with plugins"),
gbytes.Say("list List all visible plugin executables on a user's PATH"),
))
})
})
Context("list", func() {
When("there is no plugin found in PATH", func() {
It("errors and prints a message", func() {
r.Path = "does-not-contain-a-plugin"
r.exitCode = 1
session := r.run("plugin", "list")
Expect(session.Err).To(gbytes.Say("error: unable to find any kratix plugins in your PATH"))
})
})
When("there are plugins in PATH", func() {
BeforeEach(func() {
createPlugins(workingDir)
r = &runner{exitCode: 0, dir: workingDir, Path: workingDir}
})
It("lists them", func() {
sess := r.run("plugin", "list")
Expect(sess.Out).To(SatisfyAll(
gbytes.Say("The following compatible plugins are available:"),
gbytes.Say("kratix cat"),
gbytes.Say("kratix error"),
gbytes.Say("kratix hi"),
))
Expect(string(sess.Out.Contents())).To(ContainSubstring(workingDir))
})
})
When("PATH has duplicate directory entries that resolve to the same location", func() {
BeforeEach(func() {
createPlugins(workingDir)
r = &runner{
exitCode: 0,
dir: workingDir,
Path: workingDir + ":" + workingDir + "/" + ":" + workingDir,
}
})
It("does not report a plugin as overshadowing itself", func() {
sess := r.run("plugin", "list")
Expect(sess.Err).NotTo(gbytes.Say("overshadowed by a similarly named plugin"))
Expect(strings.Count(string(sess.Out.Contents()), "kratix hi")).To(Equal(1))
})
})
})
Context("execute", func() {
When("there are plugins in PATH", func() {
BeforeEach(func() {
createPlugins(workingDir)
r = &runner{exitCode: 0, dir: workingDir, Path: workingDir}
})
It("can execute them", func() {
By("running plugins successfully")
sess := r.run("hi")
Expect(sess.Out).To(gbytes.Say("have a nice day"))
sess = r.run("cat")
Expect(sess.Out).To(gbytes.Say("meow meow meow"))
By("preserving the exit code when plugin failed")
r.exitCode = 127
sess = r.run("error")
Expect(sess.Out).To(gbytes.Say("blah"))
})
})
})
})
func createPlugins(dir string) {
script1 := "#!/bin/sh\n" +
"echo \"meow meow meow\"\n"
script2 := "#!/bin/sh\n" +
"echo \"have a nice day\"\n"
script3 := "#!/bin/sh\n" +
"echo \"blah\" && exit 127\n"
writePluginFile(filepath.Join(dir, "kratix-cat"), script1)
writePluginFile(filepath.Join(dir, "kratix-hi"), script2)
writePluginFile(filepath.Join(dir, "kratix-error"), script3)
}
func writePluginFile(path, script string) {
var f *os.File
var err error
if f, err = os.Create(path); err != nil {
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}
defer f.Close()
_, err = f.WriteString(script)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
ExpectWithOffset(1, f.Sync()).To(Succeed())
ExpectWithOffset(1, f.Close()).To(Succeed())
// rwxr-xr-x
ExpectWithOffset(1, os.Chmod(f.Name(), 0o755)).To(Succeed())
}