Files
wazero/internal/sysfs/unlink_test.go
Crypt Keeper 34324031cb extracts FS interfaces into fsapi package and consolidates impls (#1477)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
Co-authored-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
2023-05-17 07:19:54 +03:00

59 lines
1.3 KiB
Go

package sysfs
import (
"os"
"path"
"syscall"
"testing"
"github.com/tetratelabs/wazero/internal/testing/require"
)
func TestUnlink(t *testing.T) {
t.Run("doesn't exist", func(t *testing.T) {
name := "non-existent"
errno := Unlink(name)
require.EqualErrno(t, syscall.ENOENT, errno)
})
t.Run("target: dir", func(t *testing.T) {
tmpDir := t.TempDir()
dir := path.Join(tmpDir, "dir")
require.NoError(t, os.Mkdir(dir, 0o700))
errno := Unlink(dir)
require.EqualErrno(t, syscall.EISDIR, errno)
require.NoError(t, os.Remove(dir))
})
t.Run("target: symlink to dir", func(t *testing.T) {
tmpDir := t.TempDir()
// Create link target dir.
subDirRealPath := path.Join(tmpDir, "subdir")
require.NoError(t, os.Mkdir(subDirRealPath, 0o700))
// Create a symlink to the subdirectory.
const symlinkName = "symlink-to-dir"
require.NoError(t, os.Symlink("subdir", symlinkName))
// Unlinking the symlink should suceed.
errno := Unlink(symlinkName)
require.EqualErrno(t, 0, errno)
})
t.Run("file exists", func(t *testing.T) {
tmpDir := t.TempDir()
name := path.Join(tmpDir, "unlink")
require.NoError(t, os.WriteFile(name, []byte{}, 0o600))
require.EqualErrno(t, 0, Unlink(name))
_, err := os.Stat(name)
require.Error(t, err)
})
}