From 5d2cb67fb19ad305e32acabd0e55a551d97700b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Tue, 8 Jul 2025 21:36:51 +0200 Subject: [PATCH] Use t.Cleanup in fileFromFixture MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ... to simplify users. Should not change test behavior. Signed-off-by: Miloslav Trmač --- integration/copy_test.go | 5 ----- integration/utils_test.go | 5 +++-- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/integration/copy_test.go b/integration/copy_test.go index 56f30c5f..18d0bdff 100644 --- a/integration/copy_test.go +++ b/integration/copy_test.go @@ -745,7 +745,6 @@ func (s *copySuite) TestCopySignatures() { dirDest := "dir:" + dir policy := fileFromFixture(t, "fixtures/policy.json", map[string]string{"@keydir@": s.gpgHome}) - defer os.Remove(policy) // type: reject assertSkopeoFails(t, fmt.Sprintf(".*Source image rejected: Running image %s:latest is rejected by policy.*", testFQIN), @@ -809,7 +808,6 @@ func (s *copySuite) TestCopyDirSignatures() { // Note the "/@dirpath@": The value starts with a slash so that it is not rejected in other tests which do not replace it, // but we must ensure that the result is a canonical path, not something starting with a "//". policy := fileFromFixture(t, "fixtures/policy.json", map[string]string{"@keydir@": s.gpgHome, "/@dirpath@": topDir + "/restricted"}) - defer os.Remove(policy) // Get some images. assertSkopeoSucceeds(t, "", "copy", "--retry-times", "3", testFQIN+":armfh", topDirDest+"/dir1") @@ -917,7 +915,6 @@ func (s *copySuite) TestCopyDockerLookaside() { defer splitLookasideReadServer.Close() policy := fileFromFixture(t, "fixtures/policy.json", map[string]string{"@keydir@": s.gpgHome}) - defer os.Remove(policy) registriesDir := filepath.Join(tmpDir, "registries.d") err = os.Mkdir(registriesDir, 0755) require.NoError(t, err) @@ -978,7 +975,6 @@ func (s *copySuite) TestCopyAtomicExtension() { registriesDir := filepath.Join(topDir, "registries.d") dirDest := "dir:" + topDir policy := fileFromFixture(t, "fixtures/policy.json", map[string]string{"@keydir@": s.gpgHome}) - defer os.Remove(policy) // Get an image to work with to an atomic: destination. Also verifies that we can use Docker repositories without X-Registry-Supports-Signatures assertSkopeoSucceeds(t, "", "--tls-verify=false", "--registries.d", registriesDir, "copy", "--retry-times", "3", @@ -1036,7 +1032,6 @@ func (s *copySuite) TestCopyVerifyingMirroredSignatures() { dirDest := "dir:" + filepath.Join(topDir, "unused-dest") policy := fileFromFixture(t, "fixtures/policy.json", map[string]string{"@keydir@": s.gpgHome}) - defer os.Remove(policy) // We use X-R-S-S for this testing to avoid having to deal with the lookasides. // A downside is that OpenShift records signatures per image, so the error messages below diff --git a/integration/utils_test.go b/integration/utils_test.go index 1d8dfc55..d30ba3ac 100644 --- a/integration/utils_test.go +++ b/integration/utils_test.go @@ -174,8 +174,8 @@ func modifyEnviron(env []string, name, value string) []string { return append(res, prefix+value) } -// fileFromFixture applies edits to inputPath and returns a path to the temporary file. -// Callers should defer os.Remove(the_returned_path) +// fileFromFixture applies edits to inputPath and returns a path to the temporary file with the edits, +// which will be automatically removed when the test completes. func fileFromFixture(t *testing.T, inputPath string, edits map[string]string) string { contents, err := os.ReadFile(inputPath) require.NoError(t, err) @@ -188,6 +188,7 @@ func fileFromFixture(t *testing.T, inputPath string, edits map[string]string) st file, err := os.CreateTemp("", "policy.json") require.NoError(t, err) path := file.Name() + t.Cleanup(func() { os.Remove(path) }) _, err = file.Write(contents) require.NoError(t, err)