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

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package os_test
     6  
     7  import (
     8  	"os"
     9  	"strings"
    10  	"syscall"
    11  	"testing"
    12  )
    13  
    14  func TestFixLongPath(t *testing.T) {
    15  	if os.CanUseLongPaths {
    16  		return
    17  	}
    18  	// 248 is long enough to trigger the longer-than-248 checks in
    19  	// fixLongPath, but short enough not to make a path component
    20  	// longer than 255, which is illegal on Windows. (which
    21  	// doesn't really matter anyway, since this is purely a string
    22  	// function we're testing, and it's not actually being used to
    23  	// do a system call)
    24  	veryLong := "l" + strings.Repeat("o", 248) + "ng"
    25  	for _, test := range []struct{ in, want string }{
    26  		// Short; unchanged:
    27  		{`C:\short.txt`, `C:\short.txt`},
    28  		{`C:\`, `C:\`},
    29  		{`C:`, `C:`},
    30  		// The "long" substring is replaced by a looooooong
    31  		// string which triggers the rewriting. Except in the
    32  		// cases below where it doesn't.
    33  		{`C:\long\foo.txt`, `\\?\C:\long\foo.txt`},
    34  		{`C:/long/foo.txt`, `\\?\C:\long\foo.txt`},
    35  		{`C:\long\foo\\bar\.\baz\\`, `\\?\C:\long\foo\bar\baz`},
    36  		{`\\unc\path`, `\\unc\path`},
    37  		{`long.txt`, `long.txt`},
    38  		{`C:long.txt`, `C:long.txt`},
    39  		{`c:\long\..\bar\baz`, `c:\long\..\bar\baz`},
    40  		{`\\?\c:\long\foo.txt`, `\\?\c:\long\foo.txt`},
    41  		{`\\?\c:\long/foo.txt`, `\\?\c:\long/foo.txt`},
    42  	} {
    43  		in := strings.ReplaceAll(test.in, "long", veryLong)
    44  		want := strings.ReplaceAll(test.want, "long", veryLong)
    45  		if got := os.FixLongPath(in); got != want {
    46  			got = strings.ReplaceAll(got, veryLong, "long")
    47  			t.Errorf("fixLongPath(%q) = %q; want %q", test.in, got, test.want)
    48  		}
    49  	}
    50  }
    51  
    52  // TODO: bring back upstream version's TestMkdirAllLongPath once os.RemoveAll and t.TempDir implemented
    53  
    54  // isWine returns true if executing on wine (Wine Is Not an Emulator), which
    55  // is compatible with windows but does not reproduce all its quirks.
    56  func isWine() bool {
    57  	return os.Getenv("WINECONFIGDIR") != ""
    58  }
    59  
    60  func TestMkdirAllExtendedLength(t *testing.T) {
    61  	// TODO: revert to upstream version once os.RemoveAll and t.TempDir implemented
    62  	tmpDir := os.TempDir()
    63  
    64  	const prefix = `\\?\`
    65  	if len(tmpDir) < 4 || tmpDir[:4] != prefix {
    66  		fullPath, err := syscall.FullPath(tmpDir)
    67  		if err != nil {
    68  			t.Fatalf("FullPath(%q) fails: %v", tmpDir, err)
    69  		}
    70  		tmpDir = prefix + fullPath
    71  	}
    72  	path := tmpDir + `\dir\`
    73  	if err := os.MkdirAll(path, 0777); err != nil {
    74  		t.Fatalf("MkdirAll(%q) failed: %v", path, err)
    75  	}
    76  
    77  	if isWine() {
    78  		// TODO: use t.Skip once implemented
    79  		t.Log("wine: Skipping check for no-dots-for-you quirk in windows extended paths")
    80  		return
    81  	}
    82  	path = path + `.\dir2`
    83  	if err := os.MkdirAll(path, 0777); err == nil {
    84  		t.Fatalf("MkdirAll(%q) should have failed, but did not", path)
    85  	}
    86  }