@@ -37,6 +37,7 @@ const (
37
37
)
38
38
39
39
func Test_plugin (t * testing.T ) {
40
+ g := NewWithT (t )
40
41
tt := []struct {
41
42
name string
42
43
command string
@@ -53,33 +54,33 @@ func Test_plugin(t *testing.T) {
53
54
},
54
55
}
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
+
56
64
for _ , tc := range tt {
57
65
t .Run (tc .name , func (t * testing.T ) {
58
66
g := NewWithT (t )
59
- stdout , _ , err := runForkTest ("TestExecutePlugin" , fmt .Sprintf ("%s=%s" , pluginCommandEnvVar , tc .command ))
67
+ stdout , _ , err := runForkTest ("TestExecutePlugin" , fmt .Sprintf ("%s=%s" , pluginCommandEnvVar , tc .command ), fmt . Sprintf ( "PATH=%s:%s" , tmpDir , path ) )
60
68
g .Expect (err ).NotTo (HaveOccurred ())
61
69
g .Expect (stdout ).To (ContainSubstring (tc .expected ))
62
70
})
63
71
}
64
72
}
65
73
66
74
func TestExecutePlugin (t * testing.T ) {
67
- g := NewWithT (t )
68
75
if goutils .DefaultString (os .Getenv (forkEnvVar ), "false" ) == strconv .FormatBool (false ) {
69
76
t .Skip ("FORK environment variable isn't set. Skipping test" )
70
77
}
71
78
72
- tmpDir := t .TempDir ()
73
- path := filepath .Join (tmpDir , "clusterctl-foo" )
74
- g .Expect (os .WriteFile (path , []byte (pluginCode ), 0755 )).To (Succeed ()) //nolint:gosec
75
- g .Expect (os .Setenv ("PATH" , fmt .Sprintf ("%s:%s" , os .Getenv ("PATH" ), tmpDir ))).ToNot (HaveOccurred ())
76
-
77
79
os .Args = append (baseArgs , os .Getenv (pluginCommandEnvVar ))
78
80
Execute ()
79
81
}
80
82
81
- const pluginCode = `
82
- #!/bin/bash
83
+ var pluginCode = `#!/bin/bash
83
84
84
85
# optional argument handling
85
86
if [[ "$1" == "version" ]]
91
92
echo "I am a plugin named clusterctl-foo"
92
93
`
93
94
94
- func runForkTest (testName string , option string ) (string , string , error ) {
95
- cmd := exec .Command (os .Args [0 ], fmt .Sprintf ("-test.run=%v" , testName )) //nolint:gosec
96
- cmd .Env = append (os .Environ (), fmt .Sprintf ("%s=%v" , forkEnvVar , true ), option )
95
+ func runForkTest (testName string , options ... string ) (string , string , error ) {
96
+ cmd := exec .Command (os .Args [0 ], "-test.run" , testName ) //nolint:gosec
97
+ cmd .Env = append (os .Environ (), fmt .Sprintf ("%s=%v" , forkEnvVar , true ))
98
+ cmd .Env = append (cmd .Env , options ... )
97
99
98
100
var stdoutB , stderrB bytes.Buffer
99
101
cmd .Stdout = & stdoutB
0 commit comments