1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
//go:build !integration
// +build !integration
package shells
import (
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-runner/common"
)
func TestBash_CommandShellEscapesLegacy(t *testing.T) {
writer := &BashWriter{useNewEscape: false}
writer.Command("foo", "x&(y)")
assert.Equal(t, `$'foo' "x&(y)"`+"\n", writer.String())
}
func TestBash_IfCmdShellEscapesLegacy(t *testing.T) {
writer := &BashWriter{useNewEscape: false}
writer.IfCmd("foo", "x&(y)")
assert.Equal(t, `if $'foo' "x&(y)" >/dev/null 2>&1; then`+"\n", writer.String())
}
func TestBash_CommandShellEscapes(t *testing.T) {
tests := []struct {
command string
args []string
expected string
}{
{
command: "foo",
args: []string{"x&(y)"},
expected: "foo \"x&(y)\"\n",
},
{
command: "echo",
args: []string{"c:\\windows"},
expected: "echo \"c:\\\\windows\"\n",
},
}
for _, tc := range tests {
writer := &BashWriter{useNewEscape: true}
writer.Command(tc.command, tc.args...)
assert.Equal(t, tc.expected, writer.String())
}
}
func TestBash_IfCmdShellEscapes(t *testing.T) {
writer := &BashWriter{useNewEscape: true}
writer.IfCmd("foo", "x&(y)")
assert.Equal(t, "if foo \"x&(y)\" >/dev/null 2>&1; then\n", writer.String())
}
func TestBash_CheckForErrors(t *testing.T) {
tests := map[string]struct {
checkForErrors bool
expected string
}{
"enabled": {
checkForErrors: true,
// nolint:lll
expected: "$'echo \\'hello world\\''\n_runner_exit_code=$?; if [ $_runner_exit_code -ne 0 ]; then exit $_runner_exit_code; fi\n",
},
"disabled": {
checkForErrors: false,
expected: "$'echo \\'hello world\\''\n",
},
}
for tn, tc := range tests {
t.Run(tn, func(t *testing.T) {
writer := &BashWriter{checkForErrors: tc.checkForErrors}
writer.Command("echo 'hello world'")
assert.Equal(t, tc.expected, writer.String())
})
}
}
func TestBash_GetConfiguration(t *testing.T) {
tests := map[string]struct {
info common.ShellScriptInfo
cmd string
args []string
os string
}{
`bash`: {
info: common.ShellScriptInfo{Shell: "bash", Type: common.NormalShell},
cmd: "bash",
},
`bash -l`: {
info: common.ShellScriptInfo{Shell: "bash", Type: common.LoginShell},
cmd: "bash",
args: []string{"-l"},
},
`su -s /bin/bash foobar -c bash`: {
info: common.ShellScriptInfo{Shell: "bash", User: "foobar", Type: common.NormalShell},
cmd: "su",
args: []string{"-s", "/bin/bash", "foobar", "-c", "bash"},
os: OSLinux,
},
`su -s /bin/bash foobar -c $'bash -l'`: {
info: common.ShellScriptInfo{Shell: "bash", User: "foobar", Type: common.LoginShell},
cmd: "su",
args: []string{"-s", "/bin/bash", "foobar", "-c", "bash -l"},
os: OSLinux,
},
`su -s /bin/sh foobar -c $'sh -l'`: {
info: common.ShellScriptInfo{Shell: "sh", User: "foobar", Type: common.LoginShell},
cmd: "su",
args: []string{"-s", "/bin/sh", "foobar", "-c", "sh -l"},
os: OSLinux,
},
`su foobar -c $'bash -l'`: {
info: common.ShellScriptInfo{Shell: "bash", User: "foobar", Type: common.LoginShell},
cmd: "su",
args: []string{"foobar", "-c", "bash -l"},
os: "darwin",
},
}
for tn, tc := range tests {
t.Run(tn, func(t *testing.T) {
if tc.os != "" && tc.os != runtime.GOOS {
t.Skipf("test only runs on %s", tc.os)
}
sh := BashShell{Shell: tc.info.Shell}
config, err := sh.GetConfiguration(tc.info)
require.NoError(t, err)
assert.Equal(t, tc.cmd, config.Command)
assert.Equal(t, tc.args, config.Arguments)
assert.Equal(t, tn, config.CmdLine)
})
}
}
|