Skip to content

Commit

Permalink
fix: remove unused file
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <[email protected]>
  • Loading branch information
JeyJeyGao committed Dec 30, 2024
1 parent 43878ed commit fad665c
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 59 deletions.
12 changes: 0 additions & 12 deletions internal/osutil/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package osutil
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"io/fs"
Expand Down Expand Up @@ -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)
Expand Down
47 changes: 0 additions & 47 deletions internal/osutil/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ package osutil

import (
"bytes"
"errors"
"io/fs"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -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")
}
})
}

0 comments on commit fad665c

Please sign in to comment.