github.com/safing/portbase@v0.19.5/utils/renameio/symlink_test.go (about)

     1  //go:build darwin || dragonfly || freebsd || linux || nacl || netbsd || openbsd || solaris || windows
     2  
     3  package renameio
     4  
     5  import (
     6  	"bytes"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  )
    11  
    12  func TestSymlink(t *testing.T) {
    13  	t.Parallel()
    14  
    15  	d, err := os.MkdirTemp("", "test-renameio-testsymlink")
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  	t.Cleanup(func() {
    20  		_ = os.RemoveAll(d)
    21  	})
    22  
    23  	want := []byte("Hello World")
    24  	if err := os.WriteFile(filepath.Join(d, "hello.txt"), want, 0o0600); err != nil {
    25  		t.Fatal(err)
    26  	}
    27  
    28  	for i := 0; i < 2; i++ {
    29  		if err := Symlink("hello.txt", filepath.Join(d, "hi.txt")); err != nil {
    30  			t.Fatal(err)
    31  		}
    32  
    33  		got, err := os.ReadFile(filepath.Join(d, "hi.txt"))
    34  		if err != nil {
    35  			t.Fatal(err)
    36  		}
    37  		if !bytes.Equal(got, want) {
    38  			t.Fatalf("unexpected content: got %q, want %q", string(got), string(want))
    39  		}
    40  	}
    41  }