diff --git a/pipeline/backend/local/command.go b/pipeline/backend/local/command.go index 099b9d3c4f2..e1956e5db3b 100644 --- a/pipeline/backend/local/command.go +++ b/pipeline/backend/local/command.go @@ -39,7 +39,7 @@ func (e *local) execCommands(ctx context.Context, step *types.Step, state *workf // Prepare commands // TODO: support `entrypoint` from pipeline config - args, err := e.genCmdByShell(step.Image, step.Commands) + args, err := e.genCmdByShell(step.Image, step.Commands, state.baseDir) if err != nil { return fmt.Errorf("could not convert commands into args: %w", err) } @@ -77,7 +77,7 @@ func checkShellExistence(shell string) error { return err } -func (e *local) genCmdByShell(shell string, cmdList []string) (args []string, err error) { +func (e *local) genCmdByShell(shell string, cmdList []string, baseDir string) (args []string, err error) { if len(cmdList) == 0 { return nil, ErrNoCmdSet } @@ -116,7 +116,7 @@ func (e *local) genCmdByShell(shell string, cmdList []string) (args []string, er script += fmt.Sprintf("@%s\n", cmd) script += "@IF NOT %ERRORLEVEL% == 0 exit %ERRORLEVEL%\n" } - cmd, err := os.CreateTemp(e.tempDir, "*.cmd") + cmd, err := os.CreateTemp(baseDir, "*.cmd") if err != nil { return nil, err } diff --git a/pipeline/backend/local/command_test.go b/pipeline/backend/local/command_test.go index 4a4afdc8aa9..24c84827270 100644 --- a/pipeline/backend/local/command_test.go +++ b/pipeline/backend/local/command_test.go @@ -29,18 +29,18 @@ func TestGenCmdByShell(t *testing.T) { e := local{tempDir: tmpDir} t.Run("error cases", func(t *testing.T) { - args, err := e.genCmdByShell("", []string{"echo hi"}) + args, err := e.genCmdByShell("", []string{"echo hi"}, t.TempDir()) assert.Nil(t, args) assert.ErrorIs(t, err, ErrNoShellSet) - args, err = e.genCmdByShell("sh", []string{}) + args, err = e.genCmdByShell("sh", []string{}, t.TempDir()) assert.Nil(t, args) assert.ErrorIs(t, err, ErrNoCmdSet) }) t.Run("windows shells", func(t *testing.T) { t.Run("cmd", func(t *testing.T) { - args, err := e.genCmdByShell("cmd.exe", []string{"echo hi", "call build.bat"}) + args, err := e.genCmdByShell("cmd.exe", []string{"echo hi", "call build.bat"}, t.TempDir()) require.NoError(t, err) require.Len(t, args, 2) assert.Equal(t, "/c", args[0]) @@ -60,7 +60,7 @@ func TestGenCmdByShell(t *testing.T) { }) t.Run("powershell", func(t *testing.T) { - args, err := e.genCmdByShell("powershell", []string{"Write-Host 'test'", "echo test"}) + args, err := e.genCmdByShell("powershell", []string{"Write-Host 'test'", "echo test"}, t.TempDir()) require.NoError(t, err) require.Len(t, args, 4) assert.EqualValues(t, []string{"-noprofile", "-noninteractive", "-c"}, []string{args[0], args[1], args[2]}) @@ -69,7 +69,7 @@ Write-Host 'test' echo '+ echo test' echo test`, args[3]) - args, err = e.genCmdByShell("pwsh", []string{"Get-Process"}) + args, err = e.genCmdByShell("pwsh", []string{"Get-Process"}, t.TempDir()) require.NoError(t, err) assert.Len(t, args, 4) assert.Equal(t, "-noprofile", args[0]) @@ -77,7 +77,7 @@ echo test`, args[3]) }) t.Run("unix shells", func(t *testing.T) { - args, err := e.genCmdByShell("sh", []string{"echo hello", "pwd"}) + args, err := e.genCmdByShell("sh", []string{"echo hello", "pwd"}, t.TempDir()) require.NoError(t, err) assert.Len(t, args, 3) assert.Equal(t, "-e", args[0]) @@ -85,20 +85,20 @@ echo test`, args[3]) assert.Contains(t, args[2], "echo hello") assert.Contains(t, args[2], "pwd") - args, err = e.genCmdByShell("bash", []string{"ls -la"}) + args, err = e.genCmdByShell("bash", []string{"ls -la"}, t.TempDir()) require.NoError(t, err) assert.Len(t, args, 3) assert.Equal(t, "-e", args[0]) assert.Equal(t, "-c", args[1]) - args, err = e.genCmdByShell("zsh", []string{"echo test"}) + args, err = e.genCmdByShell("zsh", []string{"echo test"}, t.TempDir()) require.NoError(t, err) assert.Len(t, args, 3) assert.Equal(t, "-e", args[0]) }) t.Run("fish shell", func(t *testing.T) { - args, err := e.genCmdByShell("fish", []string{"echo test", "ls"}) + args, err := e.genCmdByShell("fish", []string{"echo test", "ls"}, t.TempDir()) require.NoError(t, err) assert.Len(t, args, 2) assert.Equal(t, "-c", args[0]) @@ -107,7 +107,7 @@ echo test`, args[3]) }) t.Run("nu shell", func(t *testing.T) { - args, err := e.genCmdByShell("nu", []string{"echo test"}) + args, err := e.genCmdByShell("nu", []string{"echo test"}, t.TempDir()) require.NoError(t, err) assert.Len(t, args, 2) assert.Equal(t, "--commands", args[0]) @@ -115,7 +115,7 @@ echo test`, args[3]) }) t.Run("command escaping", func(t *testing.T) { - args, err := e.genCmdByShell("cmd", []string{"echo 'test with | pipe'", "echo 'test & ampersand'\n\necho new line"}) + args, err := e.genCmdByShell("cmd", []string{"echo 'test with | pipe'", "echo 'test & ampersand'\n\necho new line"}, t.TempDir()) require.NoError(t, err) content, err := os.ReadFile(args[1]) require.NoError(t, err) @@ -132,7 +132,7 @@ echo new line }) t.Run("shell with .exe suffix", func(t *testing.T) { - args, err := e.genCmdByShell("bash.exe", []string{"echo test"}) + args, err := e.genCmdByShell("bash.exe", []string{"echo test"}, t.TempDir()) require.NoError(t, err) assert.Len(t, args, 3) assert.Equal(t, "-e", args[0])