github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/internal/sysfs/unlink_test.go (about)

     1  package sysfs
     2  
     3  import (
     4  	"os"
     5  	"path"
     6  	"testing"
     7  
     8  	"github.com/tetratelabs/wazero/experimental/sys"
     9  	"github.com/tetratelabs/wazero/internal/testing/require"
    10  )
    11  
    12  func TestUnlink(t *testing.T) {
    13  	t.Run("doesn't exist", func(t *testing.T) {
    14  		name := "non-existent"
    15  		errno := unlink(name)
    16  		require.EqualErrno(t, sys.ENOENT, errno)
    17  	})
    18  
    19  	t.Run("target: dir", func(t *testing.T) {
    20  		tmpDir := t.TempDir()
    21  
    22  		dir := path.Join(tmpDir, "dir")
    23  		require.NoError(t, os.Mkdir(dir, 0o700))
    24  
    25  		errno := unlink(dir)
    26  		require.EqualErrno(t, sys.EISDIR, errno)
    27  
    28  		require.NoError(t, os.Remove(dir))
    29  	})
    30  
    31  	t.Run("target: symlink to dir", func(t *testing.T) {
    32  		tmpDir := t.TempDir()
    33  
    34  		// Create link target dir.
    35  		subDirRealPath := path.Join(tmpDir, "subdir")
    36  		require.NoError(t, os.Mkdir(subDirRealPath, 0o700))
    37  
    38  		// Create a symlink to the subdirectory.
    39  		const symlinkName = "symlink-to-dir"
    40  		require.NoError(t, os.Symlink("subdir", symlinkName))
    41  
    42  		// Unlinking the symlink should suceed.
    43  		errno := unlink(symlinkName)
    44  		require.EqualErrno(t, 0, errno)
    45  	})
    46  
    47  	t.Run("file exists", func(t *testing.T) {
    48  		tmpDir := t.TempDir()
    49  
    50  		name := path.Join(tmpDir, "unlink")
    51  
    52  		require.NoError(t, os.WriteFile(name, []byte{}, 0o600))
    53  
    54  		require.EqualErrno(t, 0, unlink(name))
    55  		_, err := os.Stat(name)
    56  		require.Error(t, err)
    57  	})
    58  }