|
1 | 1 | package expect
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "os/exec" |
4 | 5 | "testing"
|
5 | 6 |
|
6 | 7 | "github.com/stretchr/testify/require"
|
7 | 8 | )
|
8 | 9 |
|
9 | 10 | const DockerImage = "docker-test-image"
|
10 | 11 |
|
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 | + }, |
15 | 34 | }
|
16 | 35 |
|
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 | + }) |
37 | 60 | }
|
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) |
52 | 61 | }
|
53 | 62 |
|
54 | 63 | func Test_ShellExpect_Docker_Bash(t *testing.T) {
|
|
0 commit comments