Skip to content

Commit efab91a

Browse files
committed
Optimize test code
1 parent 2e677c7 commit efab91a

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

Diff for: cmd/clusterctl/cmd/plugin_test.go

+34-36
Original file line numberDiff line numberDiff line change
@@ -37,43 +37,41 @@ const (
3737
)
3838

3939
func Test_plugin(t *testing.T) {
40-
g := NewWithT(t)
41-
tt := []struct {
42-
name string
43-
command string
44-
expected string
45-
}{
46-
{
47-
name: "base plugin command test",
48-
expected: "I am a plugin named clusterctl-foo",
49-
},
50-
{
51-
name: "plugin version command test",
52-
command: "version",
53-
expected: "1.0.0",
54-
},
55-
}
56-
57-
tmpDir := os.TempDir()
58-
defer os.Remove(tmpDir)
59-
60-
pluginPath := filepath.Join(tmpDir, "clusterctl-foo")
61-
path := os.Getenv("PATH")
62-
g.Expect(os.WriteFile(pluginPath, []byte(pluginCode), 0755)).To(Succeed()) //nolint:gosec
63-
64-
for _, tc := range tt {
65-
t.Run(tc.name, func(t *testing.T) {
66-
g := NewWithT(t)
67-
stdout, _, err := runForkTest("TestExecutePlugin", fmt.Sprintf("%s=%s", pluginCommandEnvVar, tc.command), fmt.Sprintf("PATH=%s:%s", tmpDir, path))
68-
g.Expect(err).NotTo(HaveOccurred())
69-
g.Expect(stdout).To(ContainSubstring(tc.expected))
70-
})
71-
}
72-
}
73-
74-
func TestExecutePlugin(t *testing.T) {
7540
if goutils.DefaultString(os.Getenv(forkEnvVar), "false") == strconv.FormatBool(false) {
76-
t.Skip("FORK environment variable isn't set. Skipping test")
41+
g := NewWithT(t)
42+
tt := []struct {
43+
name string
44+
command string
45+
expected string
46+
}{
47+
{
48+
name: "base plugin command test",
49+
expected: "I am a plugin named clusterctl-foo",
50+
},
51+
{
52+
name: "plugin version command test",
53+
command: "version",
54+
expected: "1.0.0",
55+
},
56+
}
57+
58+
tmpDir := os.TempDir()
59+
defer os.Remove(tmpDir)
60+
61+
pluginPath := filepath.Join(tmpDir, "clusterctl-foo")
62+
path := os.Getenv("PATH")
63+
g.Expect(os.WriteFile(pluginPath, []byte(pluginCode), 0755)).To(Succeed()) //nolint:gosec
64+
65+
for _, tc := range tt {
66+
t.Run(tc.name, func(t *testing.T) {
67+
g := NewWithT(t)
68+
stdout, _, err := runForkTest("Test_plugin", fmt.Sprintf("%s=%s", pluginCommandEnvVar, tc.command), fmt.Sprintf("PATH=%s:%s", tmpDir, path))
69+
g.Expect(err).NotTo(HaveOccurred())
70+
g.Expect(stdout).To(ContainSubstring(tc.expected))
71+
})
72+
}
73+
74+
return
7775
}
7876

7977
os.Args = append(baseArgs, os.Getenv(pluginCommandEnvVar))

0 commit comments

Comments
 (0)