Skip to content

Commit 3f9fd01

Browse files
authored
Merge pull request #6 from devbuddy/better-tests
Table driven test
2 parents 7840f21 + 93f7b6e commit 3f9fd01

File tree

3 files changed

+48
-40
lines changed

3 files changed

+48
-40
lines changed

.github/workflows/tests.yml

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ name: tests
22

33
on:
44
push:
5-
pull_request:
65

76
jobs:
87
linters:

dev.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ up:
55
- homebrew:
66
- golangci/tap/golangci-lint
77
- go:
8-
version: 1.16.4
8+
version: 1.17.7
99
modules: true
1010

1111
commands:

shell_test.go

+47-38
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,63 @@
11
package expect
22

33
import (
4+
"os/exec"
45
"testing"
56

67
"github.com/stretchr/testify/require"
78
)
89

910
const DockerImage = "docker-test-image"
1011

11-
func Test_ShellExpect_Bash(t *testing.T) {
12-
env := []string{
13-
"PS1=##\n",
14-
"TESTVAR=foobar",
12+
func Test_ShellExpect(t *testing.T) {
13+
tests := []struct {
14+
shell string
15+
shellArgs []string
16+
env []string
17+
}{
18+
{
19+
shell: "zsh",
20+
shellArgs: []string{"--no-globalrcs", "--no-rcs", "--no-zle", "--no-promptcr"},
21+
env: []string{
22+
"PROMPT=##\n",
23+
"TESTVAR=foobar",
24+
},
25+
},
26+
{
27+
shell: "bash",
28+
shellArgs: []string{"--noprofile", "--norc"},
29+
env: []string{
30+
"PS1=##\n",
31+
"TESTVAR=foobar",
32+
},
33+
},
1534
}
1635

17-
ep := NewExpectWithEnv("bash", []string{"--noprofile", "--norc"}, env)
18-
err := ep.Start()
19-
require.NoError(t, err)
20-
21-
shell := NewShellExpect(ep, "##\n")
22-
23-
err = shell.Init()
24-
require.NoError(t, err)
25-
26-
output, err := shell.Run("echo $TESTVAR")
27-
require.NoError(t, err)
28-
require.Equal(t, []string{"foobar"}, output)
29-
}
30-
31-
func Test_ShellExpect_Zsh(t *testing.T) {
32-
t.SkipNow()
33-
34-
env := []string{
35-
"PROMPT=##\n",
36-
"TESTVAR=foobar",
36+
for _, tt := range tests {
37+
t.Run(tt.shell, func(t *testing.T) {
38+
shellPath, err := exec.LookPath(tt.shell)
39+
if err != nil {
40+
t.Skipf("shell executable not found for %s (%s)", tt.shell, err)
41+
}
42+
43+
ep := NewExpectWithEnv(shellPath, tt.shellArgs, tt.env)
44+
err = ep.Start()
45+
require.NoError(t, err)
46+
47+
shell := NewShellExpect(ep, "##\n")
48+
49+
t.Run("init", func(t *testing.T) {
50+
err = shell.Init()
51+
require.NoError(t, err)
52+
})
53+
54+
t.Run("echo", func(t *testing.T) {
55+
output, err := shell.Run("echo $TESTVAR")
56+
require.NoError(t, err)
57+
require.Equal(t, []string{"foobar"}, output)
58+
})
59+
})
3760
}
38-
39-
ep := NewExpectWithEnv("zsh", []string{"--no-globalrcs", "--no-rcs", "--no-zle", "--no-promptcr"}, env)
40-
err := ep.Start()
41-
require.NoError(t, err)
42-
ep.Debug = true
43-
44-
shell := NewShellExpect(ep, "##\n")
45-
46-
err = shell.Init()
47-
require.NoError(t, err)
48-
49-
output, err := shell.Run("echo $TESTVAR")
50-
require.NoError(t, err)
51-
require.Equal(t, []string{"foobar"}, output)
5261
}
5362

5463
func Test_ShellExpect_Docker_Bash(t *testing.T) {

0 commit comments

Comments
 (0)