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