...
Run Format

Source file src/os/path_windows_test.go

Documentation: os

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

View as plain text