github.com/Serizao/go-winio@v0.0.0-20230906082528-f02f7f4ad6e8/internal/fs/fs_test.go (about)

     1  //go:build windows
     2  
     3  package fs
     4  
     5  import (
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  
    11  	"golang.org/x/sys/windows"
    12  )
    13  
    14  func Test_GetFinalPathNameByHandle(t *testing.T) {
    15  	d := t.TempDir()
    16  	// open f via a relative path
    17  	name := t.Name() + ".txt"
    18  	fullPath := filepath.Join(d, name)
    19  
    20  	w, err := os.Getwd()
    21  	if err != nil {
    22  		t.Fatalf("could not get working directory: %v", err)
    23  	}
    24  	if err := os.Chdir(d); err != nil {
    25  		t.Fatalf("could not chdir to %s: %v", d, err)
    26  	}
    27  	defer os.Chdir(w) //nolint:errcheck
    28  
    29  	f, err := os.Create(name)
    30  	if err != nil {
    31  		t.Fatalf("could not open %s: %v", fullPath, err)
    32  	}
    33  	defer f.Close()
    34  
    35  	path, err := GetFinalPathNameByHandle(windows.Handle(f.Fd()), GetFinalPathDefaultFlag)
    36  	if err != nil {
    37  		t.Fatalf("could not get final path for %s: %v", fullPath, err)
    38  	}
    39  	if strings.EqualFold(fullPath, path) {
    40  		t.Fatalf("expected %s, got %s", fullPath, path)
    41  	}
    42  }