cli/command/container: inline some variables to prevent shadowing

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2024-06-09 14:06:13 +02:00
parent 6b58179017
commit 0d4de2392b
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 35 additions and 35 deletions

View File

@ -51,15 +51,16 @@ func TestRunCopyWithInvalidArguments(t *testing.T) {
func TestRunCopyFromContainerToStdout(t *testing.T) { func TestRunCopyFromContainerToStdout(t *testing.T) {
tarContent := "the tar content" tarContent := "the tar content"
fakeClient := &fakeClient{ cli := test.NewFakeCli(&fakeClient{
containerCopyFromFunc: func(container, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) { containerCopyFromFunc: func(ctr, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) {
assert.Check(t, is.Equal("container", container)) assert.Check(t, is.Equal("container", ctr))
return io.NopCloser(strings.NewReader(tarContent)), types.ContainerPathStat{}, nil return io.NopCloser(strings.NewReader(tarContent)), types.ContainerPathStat{}, nil
}, },
} })
options := copyOptions{source: "container:/path", destination: "-"} err := runCopy(context.TODO(), cli, copyOptions{
cli := test.NewFakeCli(fakeClient) source: "container:/path",
err := runCopy(context.TODO(), cli, options) destination: "-",
})
assert.NilError(t, err) assert.NilError(t, err)
assert.Check(t, is.Equal(tarContent, cli.OutBuffer().String())) assert.Check(t, is.Equal(tarContent, cli.OutBuffer().String()))
assert.Check(t, is.Equal("", cli.ErrBuffer().String())) assert.Check(t, is.Equal("", cli.ErrBuffer().String()))
@ -70,16 +71,18 @@ func TestRunCopyFromContainerToFilesystem(t *testing.T) {
fs.WithFile("file1", "content\n")) fs.WithFile("file1", "content\n"))
defer destDir.Remove() defer destDir.Remove()
fakeClient := &fakeClient{ cli := test.NewFakeCli(&fakeClient{
containerCopyFromFunc: func(container, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) { containerCopyFromFunc: func(ctr, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) {
assert.Check(t, is.Equal("container", container)) assert.Check(t, is.Equal("container", ctr))
readCloser, err := archive.TarWithOptions(destDir.Path(), &archive.TarOptions{}) readCloser, err := archive.TarWithOptions(destDir.Path(), &archive.TarOptions{})
return readCloser, types.ContainerPathStat{}, err return readCloser, types.ContainerPathStat{}, err
}, },
} })
options := copyOptions{source: "container:/path", destination: destDir.Path(), quiet: true} err := runCopy(context.TODO(), cli, copyOptions{
cli := test.NewFakeCli(fakeClient) source: "container:/path",
err := runCopy(context.TODO(), cli, options) destination: destDir.Path(),
quiet: true,
})
assert.NilError(t, err) assert.NilError(t, err)
assert.Check(t, is.Equal("", cli.OutBuffer().String())) assert.Check(t, is.Equal("", cli.OutBuffer().String()))
assert.Check(t, is.Equal("", cli.ErrBuffer().String())) assert.Check(t, is.Equal("", cli.ErrBuffer().String()))
@ -94,20 +97,17 @@ func TestRunCopyFromContainerToFilesystemMissingDestinationDirectory(t *testing.
fs.WithFile("file1", "content\n")) fs.WithFile("file1", "content\n"))
defer destDir.Remove() defer destDir.Remove()
fakeClient := &fakeClient{ cli := test.NewFakeCli(&fakeClient{
containerCopyFromFunc: func(container, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) { containerCopyFromFunc: func(ctr, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) {
assert.Check(t, is.Equal("container", container)) assert.Check(t, is.Equal("container", ctr))
readCloser, err := archive.TarWithOptions(destDir.Path(), &archive.TarOptions{}) readCloser, err := archive.TarWithOptions(destDir.Path(), &archive.TarOptions{})
return readCloser, types.ContainerPathStat{}, err return readCloser, types.ContainerPathStat{}, err
}, },
} })
err := runCopy(context.TODO(), cli, copyOptions{
options := copyOptions{
source: "container:/path", source: "container:/path",
destination: destDir.Join("missing", "foo"), destination: destDir.Join("missing", "foo"),
} })
cli := test.NewFakeCli(fakeClient)
err := runCopy(context.TODO(), cli, options)
assert.ErrorContains(t, err, destDir.Join("missing")) assert.ErrorContains(t, err, destDir.Join("missing"))
} }
@ -115,12 +115,11 @@ func TestRunCopyToContainerFromFileWithTrailingSlash(t *testing.T) {
srcFile := fs.NewFile(t, t.Name()) srcFile := fs.NewFile(t, t.Name())
defer srcFile.Remove() defer srcFile.Remove()
options := copyOptions{ cli := test.NewFakeCli(&fakeClient{})
err := runCopy(context.TODO(), cli, copyOptions{
source: srcFile.Path() + string(os.PathSeparator), source: srcFile.Path() + string(os.PathSeparator),
destination: "container:/path", destination: "container:/path",
} })
cli := test.NewFakeCli(&fakeClient{})
err := runCopy(context.TODO(), cli, options)
expectedError := "not a directory" expectedError := "not a directory"
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
@ -130,12 +129,11 @@ func TestRunCopyToContainerFromFileWithTrailingSlash(t *testing.T) {
} }
func TestRunCopyToContainerSourceDoesNotExist(t *testing.T) { func TestRunCopyToContainerSourceDoesNotExist(t *testing.T) {
options := copyOptions{ cli := test.NewFakeCli(&fakeClient{})
err := runCopy(context.TODO(), cli, copyOptions{
source: "/does/not/exist", source: "/does/not/exist",
destination: "container:/path", destination: "container:/path",
} })
cli := test.NewFakeCli(&fakeClient{})
err := runCopy(context.TODO(), cli, options)
expected := "no such file or directory" expected := "no such file or directory"
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
expected = "cannot find the file specified" expected = "cannot find the file specified"
@ -184,17 +182,19 @@ func TestSplitCpArg(t *testing.T) {
t.Run(testcase.doc, func(t *testing.T) { t.Run(testcase.doc, func(t *testing.T) {
skip.If(t, testcase.os != "" && testcase.os != runtime.GOOS) skip.If(t, testcase.os != "" && testcase.os != runtime.GOOS)
container, path := splitCpArg(testcase.path) ctr, path := splitCpArg(testcase.path)
assert.Check(t, is.Equal(testcase.expectedContainer, container)) assert.Check(t, is.Equal(testcase.expectedContainer, ctr))
assert.Check(t, is.Equal(testcase.expectedPath, path)) assert.Check(t, is.Equal(testcase.expectedPath, path))
}) })
} }
} }
func TestRunCopyFromContainerToFilesystemIrregularDestination(t *testing.T) { func TestRunCopyFromContainerToFilesystemIrregularDestination(t *testing.T) {
options := copyOptions{source: "container:/dev/null", destination: "/dev/random"}
cli := test.NewFakeCli(nil) cli := test.NewFakeCli(nil)
err := runCopy(context.TODO(), cli, options) err := runCopy(context.TODO(), cli, copyOptions{
source: "container:/dev/null",
destination: "/dev/random",
})
assert.Assert(t, err != nil) assert.Assert(t, err != nil)
expected := `"/dev/random" must be a directory or a regular file` expected := `"/dev/random" must be a directory or a regular file`
assert.ErrorContains(t, err, expected) assert.ErrorContains(t, err, expected)