From fad665c5e8dada994def136c8ddc62efcb1e1117 Mon Sep 17 00:00:00 2001 From: Junjie Gao Date: Mon, 30 Dec 2024 07:33:44 +0000 Subject: [PATCH] fix: remove unused file Signed-off-by: Junjie Gao --- internal/osutil/file.go | 12 --------- internal/osutil/file_test.go | 47 ------------------------------------ 2 files changed, 59 deletions(-) diff --git a/internal/osutil/file.go b/internal/osutil/file.go index 1242f0941..06f69792a 100644 --- a/internal/osutil/file.go +++ b/internal/osutil/file.go @@ -16,7 +16,6 @@ package osutil import ( "crypto/sha256" "encoding/hex" - "errors" "fmt" "io" "io/fs" @@ -61,17 +60,6 @@ func WriteFileWithPermission(path string, data []byte, perm fs.FileMode, overwri return file.Close() } -// RemoveIfExists removes a file if it exists. -func RemoveIfExists(filepath string) error { - if _, err := os.Stat(filepath); err != nil { - if errors.Is(err, fs.ErrNotExist) { - return nil - } - return err - } - return os.Remove(filepath) -} - // CopyToDir copies the src file to dst. Existing file will be overwritten. func CopyToDir(src, dst string) (int64, error) { sourceFileStat, err := os.Stat(src) diff --git a/internal/osutil/file_test.go b/internal/osutil/file_test.go index 8d5865b8a..59226f9b1 100644 --- a/internal/osutil/file_test.go +++ b/internal/osutil/file_test.go @@ -15,8 +15,6 @@ package osutil import ( "bytes" - "errors" - "io/fs" "os" "path/filepath" "runtime" @@ -271,48 +269,3 @@ func TestValidateChecksum(t *testing.T) { t.Fatalf("expected nil err, but got %v", err) } } - -func TestRemoveIfExists(t *testing.T) { - t.Run("remove existing file", func(t *testing.T) { - tempDir := t.TempDir() - filename := filepath.Join(tempDir, "file.txt") - data := []byte("data") - if err := WriteFile(filename, data); err != nil { - t.Fatal(err) - } - if err := RemoveIfExists(filename); err != nil { - t.Fatal(err) - } - if _, err := os.Stat(filename); !errors.Is(err, fs.ErrNotExist) { - t.Fatal("file should be removed") - } - }) - - t.Run("remove non-existing file", func(t *testing.T) { - tempDir := t.TempDir() - filename := filepath.Join(tempDir, "file.txt") - if err := RemoveIfExists(filename); err != nil { - t.Fatal(err) - } - }) - - t.Run("remove file in directory without permission", func(t *testing.T) { - if runtime.GOOS == "windows" { - t.Skip("skipping test on Windows") - } - tempDir := t.TempDir() - filename := filepath.Join(tempDir, "file.txt") - data := []byte("data") - if err := WriteFile(filename, data); err != nil { - t.Fatal(err) - } - if err := os.Chmod(tempDir, 0000); err != nil { - t.Fatal(err) - } - defer os.Chmod(tempDir, 0700) - - if err := RemoveIfExists(filename); err == nil { - t.Fatal("expected an error when removing file from restricted directory") - } - }) -}