diff --git a/cli/command/container/create.go b/cli/command/container/create.go index 6453a60998..5d9dab5332 100644 --- a/cli/command/container/create.go +++ b/cli/command/container/create.go @@ -177,8 +177,8 @@ func (cid *cidFile) Close() error { if cid.written { return nil } - if err := os.Remove(cid.path); err != nil { - return fmt.Errorf("failed to remove the CID file '%s': %w", cid.path, err) + if err := os.Remove(cid.path); err != nil && !errors.Is(err, os.ErrNotExist) { + return fmt.Errorf("failed to remove the CID file: %w", err) } return nil diff --git a/cli/command/container/create_test.go b/cli/command/container/create_test.go index 597218cf7f..1c6ec92649 100644 --- a/cli/command/container/create_test.go +++ b/cli/command/container/create_test.go @@ -5,6 +5,7 @@ import ( "errors" "io" "os" + "path/filepath" "runtime" "sort" "strings" @@ -42,17 +43,31 @@ func TestNewCIDFileWhenFileAlreadyExists(t *testing.T) { } func TestCIDFileCloseWithNoWrite(t *testing.T) { - tempdir := fs.NewDir(t, "test-cid-file") - defer tempdir.Remove() + // Closing should remove the file if it was not written to. + t.Run("closing should remove file", func(t *testing.T) { + filename := filepath.Join(t.TempDir(), "cidfile-1") + file, err := newCIDFile(filename) + assert.NilError(t, err) + assert.Check(t, is.Equal(file.path, filename)) - path := tempdir.Join("cidfile") - file, err := newCIDFile(path) - assert.NilError(t, err) - assert.Check(t, is.Equal(file.path, path)) + assert.NilError(t, file.Close()) + _, err = os.Stat(filename) + assert.Check(t, os.IsNotExist(err)) + }) - assert.NilError(t, file.Close()) - _, err = os.Stat(path) - assert.Check(t, os.IsNotExist(err)) + // Closing (and removing) the file should not produce an error if the file no longer exists. + t.Run("close should remove file", func(t *testing.T) { + filename := filepath.Join(t.TempDir(), "cidfile-2") + file, err := newCIDFile(filename) + assert.NilError(t, err) + assert.Check(t, is.Equal(file.path, filename)) + + assert.NilError(t, os.Remove(filename)) + _, err = os.Stat(filename) + assert.Check(t, os.IsNotExist(err)) + + assert.NilError(t, file.Close()) + }) } func TestCIDFileCloseWithWrite(t *testing.T) {