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.
14 func TestFixLongPath(t
*testing
.T
) {
15 if os
.CanUseLongPaths
{
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
24 veryLong
:= "l" + strings
.Repeat("o", 248) + "ng"
25 for _
, test
:= range []struct{ in
, want
string }{
27 {`C:\short.txt`, `C:\short.txt`},
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`},
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
)
52 func TestMkdirAllLongPath(t
*testing
.T
) {
55 for i
:= 0; i
< 100; i
++ {
56 path
+= `\another-path-component`
58 if err
:= os
.MkdirAll(path
, 0777); err
!= nil {
59 t
.Fatalf("MkdirAll(%q) failed; %v", path
, err
)
61 if err
:= os
.RemoveAll(tmpDir
); err
!= nil {
62 t
.Fatalf("RemoveAll(%q) failed; %v", tmpDir
, err
)
66 func TestMkdirAllExtendedLength(t
*testing
.T
) {
70 if len(tmpDir
) < 4 || tmpDir
[:4] != prefix
{
71 fullPath
, err
:= syscall
.FullPath(tmpDir
)
73 t
.Fatalf("FullPath(%q) fails: %v", tmpDir
, err
)
75 tmpDir
= prefix
+ fullPath
77 path
:= tmpDir
+ `\dir\`
78 if err
:= os
.MkdirAll(path
, 0777); err
!= nil {
79 t
.Fatalf("MkdirAll(%q) failed: %v", path
, err
)
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
)
88 func TestOpenRootSlash(t
*testing
.T
) {
94 for _
, test
:= range tests
{
95 dir
, err
:= os
.Open(test
)
97 t
.Fatalf("Open(%q) failed: %v", test
, err
)