/
utils_test.go
218 lines (178 loc) · 5.77 KB
/
utils_test.go
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package command_test
import (
"bufio"
"bytes"
"context"
"os"
"path/filepath"
"strings"
"syscall"
"testing"
"time"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/streams"
"github.com/docker/cli/internal/test"
"github.com/pkg/errors"
"gotest.tools/v3/assert"
)
func TestStringSliceReplaceAt(t *testing.T) {
out, ok := command.StringSliceReplaceAt([]string{"abc", "foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, -1)
assert.Assert(t, ok)
assert.DeepEqual(t, []string{"abc", "baz", "bax"}, out)
out, ok = command.StringSliceReplaceAt([]string{"foo"}, []string{"foo", "bar"}, []string{"baz"}, -1)
assert.Assert(t, !ok)
assert.DeepEqual(t, []string{"foo"}, out)
out, ok = command.StringSliceReplaceAt([]string{"abc", "foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, 0)
assert.Assert(t, !ok)
assert.DeepEqual(t, []string{"abc", "foo", "bar", "bax"}, out)
out, ok = command.StringSliceReplaceAt([]string{"foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, 0)
assert.Assert(t, ok)
assert.DeepEqual(t, []string{"baz", "bax"}, out)
out, ok = command.StringSliceReplaceAt([]string{"abc", "foo", "bar", "baz"}, []string{"foo", "bar"}, nil, -1)
assert.Assert(t, ok)
assert.DeepEqual(t, []string{"abc", "baz"}, out)
out, ok = command.StringSliceReplaceAt([]string{"foo"}, nil, []string{"baz"}, -1)
assert.Assert(t, !ok)
assert.DeepEqual(t, []string{"foo"}, out)
}
func TestValidateOutputPath(t *testing.T) {
basedir := t.TempDir()
dir := filepath.Join(basedir, "dir")
notexist := filepath.Join(basedir, "notexist")
err := os.MkdirAll(dir, 0o755)
assert.NilError(t, err)
file := filepath.Join(dir, "file")
err = os.WriteFile(file, []byte("hi"), 0o644)
assert.NilError(t, err)
testcases := []struct {
path string
err error
}{
{basedir, nil},
{file, nil},
{dir, nil},
{dir + string(os.PathSeparator), nil},
{notexist, nil},
{notexist + string(os.PathSeparator), nil},
{filepath.Join(notexist, "file"), errors.New("does not exist")},
}
for _, testcase := range testcases {
t.Run(testcase.path, func(t *testing.T) {
err := command.ValidateOutputPath(testcase.path)
if testcase.err == nil {
assert.NilError(t, err)
} else {
assert.ErrorContains(t, err, testcase.err.Error())
}
})
}
}
func TestPromptForConfirmation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
t.Run("case=prompt should wait for user input", func(t *testing.T) {
buf := new(bytes.Buffer)
wroteHook := make(chan struct{}, 1)
bufioWriter := bufio.NewWriter(buf)
w := test.NewWriterWithHook(bufioWriter, func(p []byte) {
wroteHook <- struct{}{}
})
r, _, err := os.Pipe()
assert.NilError(t, err)
in := streams.NewIn(r)
result := make(chan bool, 1)
go func() {
result <- command.PromptForConfirmation(ctx, in, w, "")
}()
// wait for the prompt to write to the buffer
pollForPromptOutput(ctx, t, wroteHook)
assert.NilError(t, bufioWriter.Flush())
assert.Check(t, strings.Contains(buf.String(), "Are you sure you want to proceed? [y/N]"))
writeCtx, writeCancel := context.WithTimeout(ctx, 100*time.Millisecond)
defer writeCancel()
writeInputChan := make(chan struct{})
go func() {
_, err := w.Write([]byte("n"))
assert.NilError(t, err)
writeInputChan <- struct{}{}
}()
select {
case <-writeCtx.Done():
t.Fatal("PromptForConfirmation did not receive user input")
case <-result:
t.Fatal("PromptForConfirmation returned before user input")
case <-writeInputChan:
}
})
t.Run("case=terminate the prompt with SIGINT", func(t *testing.T) {
wroteHook := make(chan struct{}, 1)
buf := new(bytes.Buffer)
bufioWriter := bufio.NewWriter(buf)
w := test.NewWriterWithHook(bufioWriter, func(p []byte) {
wroteHook <- struct{}{}
})
r, _, err := os.Pipe()
assert.NilError(t, err)
in := streams.NewIn(r)
result := make(chan bool, 1)
defer close(result)
go func() {
result <- command.PromptForConfirmation(ctx, in, w, "")
}()
// wait for the Prompt to write to the buffer
pollForPromptOutput(ctx, t, wroteHook)
assert.NilError(t, bufioWriter.Flush())
assert.Equal(t, strings.TrimSpace(buf.String()), "Are you sure you want to proceed? [y/N]")
resultCtx, resultCancel := context.WithTimeout(ctx, 100*time.Millisecond)
defer resultCancel()
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
select {
case <-resultCtx.Done():
t.Fatal("PromptForConfirmation did not return after SIGINT")
case r := <-result:
assert.Check(t, !r)
}
})
t.Run("case=prompt should return on io.EOF", func(t *testing.T) {
buf := new(bytes.Buffer)
wroteHook := make(chan struct{}, 1)
bufioWriter := bufio.NewWriter(buf)
w := test.NewWriterWithHook(bufioWriter, func(p []byte) {
wroteHook <- struct{}{}
})
r, _, err := os.Pipe()
assert.NilError(t, err)
in := streams.NewIn(r)
result := make(chan bool, 1)
go func() {
result <- command.PromptForConfirmation(ctx, in, w, "")
}()
// wait for the prompt to write to the buffer
pollForPromptOutput(ctx, t, wroteHook)
assert.NilError(t, bufioWriter.Flush())
assert.Check(t, strings.Contains(buf.String(), "Are you sure you want to proceed? [y/N]"))
assert.NilError(t, in.Close())
resultCtx, resultCancel := context.WithTimeout(ctx, 100*time.Millisecond)
defer resultCancel()
select {
case <-resultCtx.Done():
t.Fatal("PromptForConfirmation did not return after io.EOF")
case r := <-result:
assert.Check(t, !r)
}
})
}
func pollForPromptOutput(ctx context.Context, t *testing.T, wroteHook <-chan struct{}) {
t.Helper()
ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
defer cancel()
for {
select {
case <-ctx.Done():
t.Fatal("Buffered output was not written to before ctx was cancelled")
return
case <-wroteHook:
return
}
}
}