@@ -37,6 +37,7 @@ const (
3737)
3838
3939func Test_plugin (t * testing.T ) {
40+ g := NewWithT (t )
4041 tt := []struct {
4142 name string
4243 command string
@@ -53,33 +54,33 @@ func Test_plugin(t *testing.T) {
5354 },
5455 }
5556
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+
5664 for _ , tc := range tt {
5765 t .Run (tc .name , func (t * testing.T ) {
5866 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 ) )
6068 g .Expect (err ).NotTo (HaveOccurred ())
6169 g .Expect (stdout ).To (ContainSubstring (tc .expected ))
6270 })
6371 }
6472}
6573
6674func TestExecutePlugin (t * testing.T ) {
67- g := NewWithT (t )
6875 if goutils .DefaultString (os .Getenv (forkEnvVar ), "false" ) == strconv .FormatBool (false ) {
6976 t .Skip ("FORK environment variable isn't set. Skipping test" )
7077 }
7178
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-
7779 os .Args = append (baseArgs , os .Getenv (pluginCommandEnvVar ))
7880 Execute ()
7981}
8082
81- const pluginCode = `
82- #!/bin/bash
83+ var pluginCode = `#!/bin/bash
8384
8485# optional argument handling
8586if [[ "$1" == "version" ]]
9192echo "I am a plugin named clusterctl-foo"
9293`
9394
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 ... )
9799
98100 var stdoutB , stderrB bytes.Buffer
99101 cmd .Stdout = & stdoutB
0 commit comments