-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_test.go
More file actions
171 lines (148 loc) · 3.75 KB
/
shell_test.go
File metadata and controls
171 lines (148 loc) · 3.75 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package tish_test
import (
"bytes"
"context"
"fmt"
"io"
"os"
"testing"
"github.com/midbel/tish"
)
const cmd = "go mod graph"
type stdio struct {
Out bytes.Buffer
Err bytes.Buffer
}
func (s *stdio) Reset() {
s.Out.Reset()
s.Err.Reset()
}
type ShellCase struct {
Script string
Out []string
Err []string
Args []string
}
func TestShellBis(t *testing.T) {
data := []ShellCase{
{
Script: `echo foobar`,
Out: []string{"foobar"},
},
{
Script: `FOO=foobar; echo ${FOO} | cut -f 1 -d 'b'`,
Out: []string{"foo"},
},
}
for _, d := range data {
t.Run(d.Script, func(t *testing.T) {
pair := Pair(d.Out, d.Err)
sh, err := createShell(pair.Out, pair.Err)
if err != nil {
t.Fatalf("fail to create shell: %s", err)
}
if err := sh.Execute(context.TODO(), d.Script, "test", d.Args); err != nil {
t.Fatalf("error while executing script: %s", err)
}
})
}
}
func TestShell(t *testing.T) {
var (
sio stdio
sh, err = createShell(&sio.Out, &sio.Err)
)
if err != nil {
t.Fatalf("unexpected error creating shell: %s", err)
}
t.Run("default", func(t *testing.T) {
defer sio.Reset()
executeScript(t, sh, cmd, &sio)
})
t.Run("redirection", func(t *testing.T) {
defer sio.Reset()
executeScript(t, sh, "echo foobar > testdata/foobar.txt; if [[ -s testdata/foobar.txt ]]; then echo ok fi", &sio)
os.Remove("testdata/foobar.txt")
})
t.Run("alias", func(t *testing.T) {
defer sio.Reset()
sh.Alias("showgraph", cmd)
executeScript(t, sh, "showgraph", &sio)
})
t.Run("assign", func(t *testing.T) {
defer sio.Reset()
executeScript(t, sh, "foobar = foobar; echo ${foobar} | cut -f 1 -d 'b'", &sio)
})
t.Run("conditional", func(t *testing.T) {
defer sio.Reset()
executeScript(t, sh, "true && echo foo", &sio)
executeScript(t, sh, "false && echo foo; echo foo", &sio)
executeScript(t, sh, "false || echo bar", &sio)
executeScript(t, sh, "echo foo || echo bar", &sio)
})
t.Run("for-loop", func(t *testing.T) {
defer sio.Reset()
sh.Define("empty", []string{})
executeScript(t, sh, "for label in foo bar; do echo $label; done", &sio)
executeScript(t, sh, "for label in foo bar; do echo $label; break; done", &sio)
executeScript(t, sh, "for label in foo bar; do echo $label; continue; done", &sio)
executeScript(t, sh, "for label in $empty; do echo $label; else echo empty; done", &sio)
})
t.Run("test", func(t *testing.T) {
defer sio.Reset()
executeScript(t, sh, "if [[ -d testdata ]]; then echo ok else echo ko fi", &sio)
executeScript(t, sh, "if [[ ! -d testdata ]]; then echo ko else echo ok fi", &sio)
})
}
func executeScript(t *testing.T, sh *tish.Shell, script string, sio *stdio) {
t.Helper()
err := sh.Execute(context.TODO(), script, "test", nil)
if err != nil {
t.Fatalf("unexpected error executing command: %s", err)
}
t.Logf("length stdout: %d", sio.Out.Len())
t.Logf("length stderr: %d", sio.Err.Len())
if sio.Out.Len() == 0 {
t.Errorf("stdout is empty")
}
if sio.Err.Len() != 0 {
t.Errorf("stderr is not empty")
}
}
func createShell(out, err io.Writer) (*tish.Shell, error) {
options := []tish.ShellOption{
tish.WithStdout(out),
tish.WithStderr(err),
}
return tish.New(options...)
}
type stdpair struct {
Out io.Writer
Err io.Writer
}
func Pair(out, err []string) stdpair {
return stdpair{
Out: empty(out),
Err: empty(err),
}
}
type writer struct {
curr int
want []string
}
func empty(values []string) io.Writer {
return &writer{
want: values,
}
}
func (w *writer) Write(b []byte) (int, error) {
if w.curr >= len(w.want) {
return 0, fmt.Errorf("too many values written")
}
got := bytes.TrimSpace(b)
if w.want[w.curr] != string(got) {
return 0, fmt.Errorf("strings mismatched! want %s, got %s", w.want[w.curr], got)
}
w.curr++
return len(b), nil
}