github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/os/os_symlink_test.go (about)

     1  //go:build !windows && !baremetal && !js && !wasip1
     2  
     3  // Copyright 2009 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package os_test
     8  
     9  import (
    10  	. "os"
    11  	"testing"
    12  )
    13  
    14  // TODO: Move this back into os_anyos_test.go when wasi supports symlink
    15  
    16  func TestSymlink(t *testing.T) {
    17  	//testenv.MustHaveSymlink(t)
    18  
    19  	defer chtmpdir(t)()
    20  	from, to := "symlinktestfrom", "symlinktestto"
    21  	file, err := Create(to)
    22  	if err != nil {
    23  		t.Fatalf("Create(%q) failed: %v", to, err)
    24  	}
    25  	if err = file.Close(); err != nil {
    26  		t.Errorf("Close(%q) failed: %v", to, err)
    27  	}
    28  	err = Symlink(to, from)
    29  	if err != nil {
    30  		t.Fatalf("Symlink(%q, %q) failed: %v", to, from, err)
    31  	}
    32  	tostat, err := Lstat(to)
    33  	if err != nil {
    34  		t.Fatalf("Lstat(%q) failed: %v", to, err)
    35  	}
    36  	if tostat.Mode()&ModeSymlink != 0 {
    37  		t.Fatalf("Lstat(%q).Mode()&ModeSymlink = %v, want 0", to, tostat.Mode()&ModeSymlink)
    38  	}
    39  	fromstat, err := Stat(from)
    40  	if err != nil {
    41  		t.Fatalf("Stat(%q) failed: %v", from, err)
    42  	}
    43  	if !SameFile(tostat, fromstat) {
    44  		t.Errorf("Symlink(%q, %q) did not create symlink", to, from)
    45  	}
    46  	fromstat, err = Lstat(from)
    47  	if err != nil {
    48  		t.Fatalf("Lstat(%q) failed: %v", from, err)
    49  	}
    50  	if fromstat.Mode()&ModeSymlink == 0 {
    51  		t.Fatalf("Lstat(%q).Mode()&ModeSymlink = 0, want %v", from, ModeSymlink)
    52  	}
    53  	fromstat, err = Stat(from)
    54  	if err != nil {
    55  		t.Fatalf("Stat(%q) failed: %v", from, err)
    56  	}
    57  	if fromstat.Name() != from {
    58  		t.Errorf("Stat(%q).Name() = %q, want %q", from, fromstat.Name(), from)
    59  	}
    60  	if fromstat.Mode()&ModeSymlink != 0 {
    61  		t.Fatalf("Stat(%q).Mode()&ModeSymlink = %v, want 0", from, fromstat.Mode()&ModeSymlink)
    62  	}
    63  	s, err := Readlink(from)
    64  	if err != nil {
    65  		t.Fatalf("Readlink(%q) failed: %v", from, err)
    66  	}
    67  	if s != to {
    68  		t.Fatalf("Readlink(%q) = %q, want %q", from, s, to)
    69  	}
    70  	file, err = Open(from)
    71  	if err != nil {
    72  		t.Fatalf("Open(%q) failed: %v", from, err)
    73  	}
    74  	file.Close()
    75  }