2024-02-21 10:36:17 -05:00
|
|
|
package command_test
|
2019-04-17 18:09:29 -04:00
|
|
|
|
|
|
|
import (
|
2024-02-21 10:36:17 -05:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-10-30 15:07:18 -04:00
|
|
|
"os"
|
2024-04-08 04:11:09 -04:00
|
|
|
"os/signal"
|
2019-10-30 15:07:18 -04:00
|
|
|
"path/filepath"
|
2024-02-21 10:36:17 -05:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
2019-04-17 18:09:29 -04:00
|
|
|
"testing"
|
2024-02-21 10:36:17 -05:00
|
|
|
"time"
|
2019-04-17 18:09:29 -04:00
|
|
|
|
2024-02-21 10:36:17 -05:00
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/internal/test"
|
2019-10-30 15:07:18 -04:00
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2019-04-17 18:09:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStringSliceReplaceAt(t *testing.T) {
|
2024-02-21 10:36:17 -05:00
|
|
|
out, ok := command.StringSliceReplaceAt([]string{"abc", "foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, -1)
|
2019-04-17 18:09:29 -04:00
|
|
|
assert.Assert(t, ok)
|
|
|
|
assert.DeepEqual(t, []string{"abc", "baz", "bax"}, out)
|
|
|
|
|
2024-02-21 10:36:17 -05:00
|
|
|
out, ok = command.StringSliceReplaceAt([]string{"foo"}, []string{"foo", "bar"}, []string{"baz"}, -1)
|
2019-04-17 18:09:29 -04:00
|
|
|
assert.Assert(t, !ok)
|
|
|
|
assert.DeepEqual(t, []string{"foo"}, out)
|
|
|
|
|
2024-02-21 10:36:17 -05:00
|
|
|
out, ok = command.StringSliceReplaceAt([]string{"abc", "foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, 0)
|
2019-04-17 18:09:29 -04:00
|
|
|
assert.Assert(t, !ok)
|
|
|
|
assert.DeepEqual(t, []string{"abc", "foo", "bar", "bax"}, out)
|
|
|
|
|
2024-02-21 10:36:17 -05:00
|
|
|
out, ok = command.StringSliceReplaceAt([]string{"foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, 0)
|
2019-04-17 18:09:29 -04:00
|
|
|
assert.Assert(t, ok)
|
|
|
|
assert.DeepEqual(t, []string{"baz", "bax"}, out)
|
|
|
|
|
2024-02-21 10:36:17 -05:00
|
|
|
out, ok = command.StringSliceReplaceAt([]string{"abc", "foo", "bar", "baz"}, []string{"foo", "bar"}, nil, -1)
|
2019-04-17 18:09:29 -04:00
|
|
|
assert.Assert(t, ok)
|
|
|
|
assert.DeepEqual(t, []string{"abc", "baz"}, out)
|
|
|
|
|
2024-02-21 10:36:17 -05:00
|
|
|
out, ok = command.StringSliceReplaceAt([]string{"foo"}, nil, []string{"baz"}, -1)
|
2019-04-17 18:09:29 -04:00
|
|
|
assert.Assert(t, !ok)
|
|
|
|
assert.DeepEqual(t, []string{"foo"}, out)
|
|
|
|
}
|
2019-10-30 15:07:18 -04:00
|
|
|
|
|
|
|
func TestValidateOutputPath(t *testing.T) {
|
2022-02-25 08:35:28 -05:00
|
|
|
basedir := t.TempDir()
|
2019-10-30 15:07:18 -04:00
|
|
|
dir := filepath.Join(basedir, "dir")
|
|
|
|
notexist := filepath.Join(basedir, "notexist")
|
2022-09-30 13:13:22 -04:00
|
|
|
err := os.MkdirAll(dir, 0o755)
|
2019-10-30 15:07:18 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
file := filepath.Join(dir, "file")
|
2022-09-30 13:13:22 -04:00
|
|
|
err = os.WriteFile(file, []byte("hi"), 0o644)
|
2019-10-30 15:07:18 -04:00
|
|
|
assert.NilError(t, err)
|
2022-09-29 11:21:51 -04:00
|
|
|
testcases := []struct {
|
2019-10-30 15:07:18 -04:00
|
|
|
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) {
|
2024-02-21 10:36:17 -05:00
|
|
|
err := command.ValidateOutputPath(testcase.path)
|
2019-10-30 15:07:18 -04:00
|
|
|
if testcase.err == nil {
|
|
|
|
assert.NilError(t, err)
|
|
|
|
} else {
|
|
|
|
assert.ErrorContains(t, err, testcase.err.Error())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-02-21 10:36:17 -05:00
|
|
|
|
|
|
|
func TestPromptForConfirmation(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
type promptResult struct {
|
|
|
|
result bool
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
bufioWriter := bufio.NewWriter(buf)
|
|
|
|
|
|
|
|
var (
|
|
|
|
promptWriter *io.PipeWriter
|
|
|
|
promptReader *io.PipeReader
|
|
|
|
)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if promptWriter != nil {
|
|
|
|
promptWriter.Close()
|
|
|
|
}
|
|
|
|
if promptReader != nil {
|
|
|
|
promptReader.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
2024-03-26 05:07:01 -04:00
|
|
|
desc string
|
|
|
|
f func() error
|
|
|
|
expected promptResult
|
2024-02-21 10:36:17 -05:00
|
|
|
}{
|
2024-03-26 05:07:01 -04:00
|
|
|
{"SIGINT", func() error {
|
2024-02-21 10:36:17 -05:00
|
|
|
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
|
2024-03-26 05:07:01 -04:00
|
|
|
return nil
|
|
|
|
}, promptResult{false, command.ErrPromptTerminated}},
|
|
|
|
{"no", func() error {
|
2024-02-21 10:36:17 -05:00
|
|
|
_, err := fmt.Fprint(promptWriter, "n\n")
|
2024-03-26 05:07:01 -04:00
|
|
|
return err
|
|
|
|
}, promptResult{false, nil}},
|
|
|
|
{"yes", func() error {
|
2024-02-21 10:36:17 -05:00
|
|
|
_, err := fmt.Fprint(promptWriter, "y\n")
|
2024-03-26 05:07:01 -04:00
|
|
|
return err
|
|
|
|
}, promptResult{true, nil}},
|
|
|
|
{"any", func() error {
|
2024-02-21 10:36:17 -05:00
|
|
|
_, err := fmt.Fprint(promptWriter, "a\n")
|
2024-03-26 05:07:01 -04:00
|
|
|
return err
|
|
|
|
}, promptResult{false, nil}},
|
|
|
|
{"with space", func() error {
|
2024-02-21 10:36:17 -05:00
|
|
|
_, err := fmt.Fprint(promptWriter, " y\n")
|
2024-03-26 05:07:01 -04:00
|
|
|
return err
|
|
|
|
}, promptResult{true, nil}},
|
|
|
|
{"reader closed", func() error {
|
|
|
|
return promptReader.Close()
|
|
|
|
}, promptResult{false, nil}},
|
2024-02-21 10:36:17 -05:00
|
|
|
} {
|
|
|
|
t.Run("case="+tc.desc, func(t *testing.T) {
|
2024-04-08 04:11:09 -04:00
|
|
|
notifyCtx, notifyCancel := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
t.Cleanup(notifyCancel)
|
|
|
|
|
2024-02-21 10:36:17 -05:00
|
|
|
buf.Reset()
|
|
|
|
promptReader, promptWriter = io.Pipe()
|
|
|
|
|
|
|
|
wroteHook := make(chan struct{}, 1)
|
|
|
|
promptOut := test.NewWriterWithHook(bufioWriter, func(p []byte) {
|
|
|
|
wroteHook <- struct{}{}
|
|
|
|
})
|
|
|
|
|
|
|
|
result := make(chan promptResult, 1)
|
|
|
|
go func() {
|
2024-04-08 04:11:09 -04:00
|
|
|
r, err := command.PromptForConfirmation(notifyCtx, promptReader, promptOut, "")
|
2024-02-21 10:36:17 -05:00
|
|
|
result <- promptResult{r, err}
|
|
|
|
}()
|
|
|
|
|
2024-03-26 05:07:01 -04:00
|
|
|
select {
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
case <-wroteHook:
|
|
|
|
}
|
2024-02-21 10:36:17 -05:00
|
|
|
|
|
|
|
assert.NilError(t, bufioWriter.Flush())
|
|
|
|
assert.Equal(t, strings.TrimSpace(buf.String()), "Are you sure you want to proceed? [y/N]")
|
|
|
|
|
2024-03-26 05:07:01 -04:00
|
|
|
// wait for the Prompt to write to the buffer
|
|
|
|
drainChannel(ctx, wroteHook)
|
|
|
|
|
|
|
|
assert.NilError(t, tc.f())
|
2024-02-21 10:36:17 -05:00
|
|
|
|
2024-03-26 05:07:01 -04:00
|
|
|
select {
|
|
|
|
case <-time.After(500 * time.Millisecond):
|
|
|
|
t.Fatal("timeout waiting for prompt result")
|
|
|
|
case r := <-result:
|
|
|
|
assert.Equal(t, r, tc.expected)
|
|
|
|
}
|
2024-02-21 10:36:17 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func drainChannel(ctx context.Context, ch <-chan struct{}) {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-ch:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|