Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pipeline/backend/local/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
24 changes: 12 additions & 12 deletions pipeline/backend/local/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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]})
Expand All @@ -69,36 +69,36 @@ 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])
})
})

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])
assert.Equal(t, "-c", args[1])
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])
Expand All @@ -107,15 +107,15 @@ 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])
assert.Contains(t, args[1], "echo test")
})

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)
Expand All @@ -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])
Expand Down