github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/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  	t.Parallel()
    19  
    20  	// 248 is long enough to trigger the longer-than-248 checks in
    21  	// fixLongPath, but short enough not to make a path component
    22  	// longer than 255, which is illegal on Windows. (which
    23  	// doesn't really matter anyway, since this is purely a string
    24  	// function we're testing, and it's not actually being used to
    25  	// do a system call)
    26  	veryLong := "l" + strings.Repeat("o", 248) + "ng"
    27  	for _, test := range []struct{ in, want string }{
    28  		// Short; unchanged:
    29  		{`C:\short.txt`, `C:\short.txt`},
    30  		{`C:\`, `C:\`},
    31  		{`C:`, `C:`},
    32  		// The "long" substring is replaced by a looooooong
    33  		// string which triggers the rewriting. Except in the
    34  		// cases below where it doesn't.
    35  		{`C:\long\foo.txt`, `\\?\C:\long\foo.txt`},
    36  		{`C:/long/foo.txt`, `\\?\C:\long\foo.txt`},
    37  		{`C:\long\foo\\bar\.\baz\\`, `\\?\C:\long\foo\bar\baz`},
    38  		{`\\unc\path`, `\\unc\path`},
    39  		{`long.txt`, `long.txt`},
    40  		{`C:long.txt`, `C:long.txt`},
    41  		{`c:\long\..\bar\baz`, `c:\long\..\bar\baz`},
    42  		{`\\?\c:\long\foo.txt`, `\\?\c:\long\foo.txt`},
    43  		{`\\?\c:\long/foo.txt`, `\\?\c:\long/foo.txt`},
    44  	} {
    45  		in := strings.ReplaceAll(test.in, "long", veryLong)
    46  		want := strings.ReplaceAll(test.want, "long", veryLong)
    47  		if got := os.FixLongPath(in); got != want {
    48  			got = strings.ReplaceAll(got, veryLong, "long")
    49  			t.Errorf("fixLongPath(%q) = %q; want %q", test.in, got, test.want)
    50  		}
    51  	}
    52  }
    53  
    54  func TestMkdirAllLongPath(t *testing.T) {
    55  	t.Parallel()
    56  
    57  	tmpDir := t.TempDir()
    58  	path := tmpDir
    59  	for i := 0; i < 100; i++ {
    60  		path += `\another-path-component`
    61  	}
    62  	if err := os.MkdirAll(path, 0777); err != nil {
    63  		t.Fatalf("MkdirAll(%q) failed; %v", path, err)
    64  	}
    65  	if err := os.RemoveAll(tmpDir); err != nil {
    66  		t.Fatalf("RemoveAll(%q) failed; %v", tmpDir, err)
    67  	}
    68  }
    69  
    70  func TestMkdirAllExtendedLength(t *testing.T) {
    71  	t.Parallel()
    72  	tmpDir := t.TempDir()
    73  
    74  	const prefix = `\\?\`
    75  	if len(tmpDir) < 4 || tmpDir[:4] != prefix {
    76  		fullPath, err := syscall.FullPath(tmpDir)
    77  		if err != nil {
    78  			t.Fatalf("FullPath(%q) fails: %v", tmpDir, err)
    79  		}
    80  		tmpDir = prefix + fullPath
    81  	}
    82  	path := tmpDir + `\dir\`
    83  	if err := os.MkdirAll(path, 0777); err != nil {
    84  		t.Fatalf("MkdirAll(%q) failed: %v", path, err)
    85  	}
    86  
    87  	path = path + `.\dir2`
    88  	if err := os.MkdirAll(path, 0777); err == nil {
    89  		t.Fatalf("MkdirAll(%q) should have failed, but did not", path)
    90  	}
    91  }
    92  
    93  func TestOpenRootSlash(t *testing.T) {
    94  	t.Parallel()
    95  
    96  	tests := []string{
    97  		`/`,
    98  		`\`,
    99  	}
   100  
   101  	for _, test := range tests {
   102  		dir, err := os.Open(test)
   103  		if err != nil {
   104  			t.Fatalf("Open(%q) failed: %v", test, err)
   105  		}
   106  		dir.Close()
   107  	}
   108  }