From b38f0dd804c722081ea212ae08cee80fe7cb29fe Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 18 Feb 2025 21:16:20 +0100 Subject: [PATCH] pkg/archive: fix naked returns, output variables in tests pkg/archive/copy_unix_test.go:54:3: naked return in func `fileContentsEqual` with 35 lines of code (nakedret) return ^ pkg/archive/copy_unix_test.go:60:3: naked return in func `fileContentsEqual` with 35 lines of code (nakedret) return ^ pkg/archive/copy_unix_test.go:67:3: naked return in func `fileContentsEqual` with 35 lines of code (nakedret) return ^ pkg/archive/copy_unix_test.go:74:3: naked return in func `fileContentsEqual` with 35 lines of code (nakedret) return ^ pkg/archive/copy_unix_test.go:83:2: naked return in func `fileContentsEqual` with 35 lines of code (nakedret) return ^ pkg/archive/diff_test.go:314:3: naked return in func `makeTestLayer` with 35 lines of code (nakedret) return ^ pkg/archive/diff_test.go:326:5: naked return in func `makeTestLayer` with 35 lines of code (nakedret) return ^ pkg/archive/diff_test.go:330:5: naked return in func `makeTestLayer` with 35 lines of code (nakedret) return ^ pkg/archive/diff_test.go:336:3: naked return in func `makeTestLayer` with 35 lines of code (nakedret) return ^ pkg/archive/copy_unix_test.go:36:2: naked return in func `getTestTempDirs` with 10 lines of code (nakedret) return ^ pkg/stdcopy/stdcopy_test.go:93:3: naked return in func `getSrcBuffer` with 10 lines of code (nakedret) return ^ Signed-off-by: Sebastiaan van Stijn --- pkg/archive/copy_unix_test.go | 37 +++++++++++++++++------------------ pkg/archive/diff_test.go | 12 ++++++------ 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/pkg/archive/copy_unix_test.go b/pkg/archive/copy_unix_test.go index e2bc870968..180be43811 100644 --- a/pkg/archive/copy_unix_test.go +++ b/pkg/archive/copy_unix_test.go @@ -33,7 +33,7 @@ func getTestTempDirs(t *testing.T) (tmpDirA, tmpDirB string) { tmpDirB, err = os.MkdirTemp("", "archive-copy-test") assert.NilError(t, err) - return + return tmpDirA, tmpDirB } func isNotDir(err error) bool { @@ -46,57 +46,56 @@ func joinTrailingSep(pathElements ...string) string { return fmt.Sprintf("%s%c", joined, filepath.Separator) } -func fileContentsEqual(t *testing.T, filenameA, filenameB string) (err error) { +func fileContentsEqual(t *testing.T, filenameA, filenameB string) error { t.Logf("checking for equal file contents: %q and %q\n", filenameA, filenameB) fileA, err := os.Open(filenameA) if err != nil { - return + return err } defer fileA.Close() fileB, err := os.Open(filenameB) if err != nil { - return + return err } defer fileB.Close() hasher := sha256.New() - if _, err = io.Copy(hasher, fileA); err != nil { - return + if _, err := io.Copy(hasher, fileA); err != nil { + return err } hashA := hasher.Sum(nil) hasher.Reset() - if _, err = io.Copy(hasher, fileB); err != nil { - return + if _, err := io.Copy(hasher, fileB); err != nil { + return err } hashB := hasher.Sum(nil) if !bytes.Equal(hashA, hashB) { - err = fmt.Errorf("file content hashes not equal - expected %s, got %s", hex.EncodeToString(hashA), hex.EncodeToString(hashB)) + return fmt.Errorf("file content hashes not equal - expected %s, got %s", hex.EncodeToString(hashA), hex.EncodeToString(hashB)) } - return + return nil } -func dirContentsEqual(t *testing.T, newDir, oldDir string) (err error) { - t.Logf("checking for equal directory contents: %q and %q\n", newDir, oldDir) +func dirContentsEqual(t *testing.T, newDir, oldDir string) error { + t.Logf("checking for equal directory contents: %q and %q", newDir, oldDir) - var changes []Change - - if changes, err = ChangesDirs(newDir, oldDir); err != nil { - return + c, err := ChangesDirs(newDir, oldDir) + if err != nil { + return err } - if len(changes) != 0 { - err = fmt.Errorf("expected no changes between directories, but got: %v", changes) + if len(c) != 0 { + return fmt.Errorf("expected no changes between directories, but got: %v", c) } - return + return nil } func logDirContents(t *testing.T, dirPath string) { diff --git a/pkg/archive/diff_test.go b/pkg/archive/diff_test.go index e98eaa5a8e..0d61a36561 100644 --- a/pkg/archive/diff_test.go +++ b/pkg/archive/diff_test.go @@ -308,13 +308,13 @@ func TestApplyLayerWhiteouts(t *testing.T) { } } -func makeTestLayer(paths []string) (rc io.ReadCloser, err error) { +func makeTestLayer(paths []string) (_ io.ReadCloser, retErr error) { tmpDir, err := os.MkdirTemp("", "graphdriver-test-mklayer") if err != nil { - return + return nil, err } defer func() { - if err != nil { + if retErr != nil { os.RemoveAll(tmpDir) } }() @@ -323,17 +323,17 @@ func makeTestLayer(paths []string) (rc io.ReadCloser, err error) { // creation to be platform agnostic. if p[len(p)-1] == '/' { if err = os.MkdirAll(filepath.Join(tmpDir, p), 0o700); err != nil { - return + return nil, err } } else { if err = os.WriteFile(filepath.Join(tmpDir, p), nil, 0o600); err != nil { - return + return nil, err } } } archive, err := Tar(tmpDir, Uncompressed) if err != nil { - return + return nil, err } return &readCloserWrapper{ Reader: archive,