From 0d4de2392b702226ca3b779b4337772aff3c22eb Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 9 Jun 2024 14:06:13 +0200 Subject: [PATCH] cli/command/container: inline some variables to prevent shadowing Signed-off-by: Sebastiaan van Stijn --- cli/command/container/cp_test.go | 70 ++++++++++++++++---------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/cli/command/container/cp_test.go b/cli/command/container/cp_test.go index 6a40518a27..3dd6b315a4 100644 --- a/cli/command/container/cp_test.go +++ b/cli/command/container/cp_test.go @@ -51,15 +51,16 @@ func TestRunCopyWithInvalidArguments(t *testing.T) { func TestRunCopyFromContainerToStdout(t *testing.T) { tarContent := "the tar content" - fakeClient := &fakeClient{ - containerCopyFromFunc: func(container, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) { - assert.Check(t, is.Equal("container", container)) + cli := test.NewFakeCli(&fakeClient{ + containerCopyFromFunc: func(ctr, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) { + assert.Check(t, is.Equal("container", ctr)) return io.NopCloser(strings.NewReader(tarContent)), types.ContainerPathStat{}, nil }, - } - options := copyOptions{source: "container:/path", destination: "-"} - cli := test.NewFakeCli(fakeClient) - err := runCopy(context.TODO(), cli, options) + }) + err := runCopy(context.TODO(), cli, copyOptions{ + source: "container:/path", + destination: "-", + }) assert.NilError(t, err) assert.Check(t, is.Equal(tarContent, cli.OutBuffer().String())) assert.Check(t, is.Equal("", cli.ErrBuffer().String())) @@ -70,16 +71,18 @@ func TestRunCopyFromContainerToFilesystem(t *testing.T) { fs.WithFile("file1", "content\n")) defer destDir.Remove() - fakeClient := &fakeClient{ - containerCopyFromFunc: func(container, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) { - assert.Check(t, is.Equal("container", container)) + cli := test.NewFakeCli(&fakeClient{ + containerCopyFromFunc: func(ctr, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) { + assert.Check(t, is.Equal("container", ctr)) readCloser, err := archive.TarWithOptions(destDir.Path(), &archive.TarOptions{}) return readCloser, types.ContainerPathStat{}, err }, - } - options := copyOptions{source: "container:/path", destination: destDir.Path(), quiet: true} - cli := test.NewFakeCli(fakeClient) - err := runCopy(context.TODO(), cli, options) + }) + err := runCopy(context.TODO(), cli, copyOptions{ + source: "container:/path", + destination: destDir.Path(), + quiet: true, + }) assert.NilError(t, err) assert.Check(t, is.Equal("", cli.OutBuffer().String())) assert.Check(t, is.Equal("", cli.ErrBuffer().String())) @@ -94,20 +97,17 @@ func TestRunCopyFromContainerToFilesystemMissingDestinationDirectory(t *testing. fs.WithFile("file1", "content\n")) defer destDir.Remove() - fakeClient := &fakeClient{ - containerCopyFromFunc: func(container, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) { - assert.Check(t, is.Equal("container", container)) + cli := test.NewFakeCli(&fakeClient{ + containerCopyFromFunc: func(ctr, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) { + assert.Check(t, is.Equal("container", ctr)) readCloser, err := archive.TarWithOptions(destDir.Path(), &archive.TarOptions{}) return readCloser, types.ContainerPathStat{}, err }, - } - - options := copyOptions{ + }) + err := runCopy(context.TODO(), cli, copyOptions{ source: "container:/path", destination: destDir.Join("missing", "foo"), - } - cli := test.NewFakeCli(fakeClient) - err := runCopy(context.TODO(), cli, options) + }) assert.ErrorContains(t, err, destDir.Join("missing")) } @@ -115,12 +115,11 @@ func TestRunCopyToContainerFromFileWithTrailingSlash(t *testing.T) { srcFile := fs.NewFile(t, t.Name()) defer srcFile.Remove() - options := copyOptions{ + cli := test.NewFakeCli(&fakeClient{}) + err := runCopy(context.TODO(), cli, copyOptions{ source: srcFile.Path() + string(os.PathSeparator), destination: "container:/path", - } - cli := test.NewFakeCli(&fakeClient{}) - err := runCopy(context.TODO(), cli, options) + }) expectedError := "not a directory" if runtime.GOOS == "windows" { @@ -130,12 +129,11 @@ func TestRunCopyToContainerFromFileWithTrailingSlash(t *testing.T) { } func TestRunCopyToContainerSourceDoesNotExist(t *testing.T) { - options := copyOptions{ + cli := test.NewFakeCli(&fakeClient{}) + err := runCopy(context.TODO(), cli, copyOptions{ source: "/does/not/exist", destination: "container:/path", - } - cli := test.NewFakeCli(&fakeClient{}) - err := runCopy(context.TODO(), cli, options) + }) expected := "no such file or directory" if runtime.GOOS == "windows" { expected = "cannot find the file specified" @@ -184,17 +182,19 @@ func TestSplitCpArg(t *testing.T) { t.Run(testcase.doc, func(t *testing.T) { skip.If(t, testcase.os != "" && testcase.os != runtime.GOOS) - container, path := splitCpArg(testcase.path) - assert.Check(t, is.Equal(testcase.expectedContainer, container)) + ctr, path := splitCpArg(testcase.path) + assert.Check(t, is.Equal(testcase.expectedContainer, ctr)) assert.Check(t, is.Equal(testcase.expectedPath, path)) }) } } func TestRunCopyFromContainerToFilesystemIrregularDestination(t *testing.T) { - options := copyOptions{source: "container:/dev/null", destination: "/dev/random"} 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) expected := `"/dev/random" must be a directory or a regular file` assert.ErrorContains(t, err, expected)