Source file
src/os/path_windows_test.go
1
2
3
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
19
20
21
22
23
24 veryLong := "l" + strings.Repeat("o", 248) + "ng"
25 for _, test := range []struct{ in, want string }{
26
27 {`C:\short.txt`, `C:\short.txt`},
28 {`C:\`, `C:\`},
29 {`C:`, `C:`},
30
31
32
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 func TestMkdirAllLongPath(t *testing.T) {
53 tmpDir := t.TempDir()
54 path := tmpDir
55 for i := 0; i < 100; i++ {
56 path += `\another-path-component`
57 }
58 if err := os.MkdirAll(path, 0777); err != nil {
59 t.Fatalf("MkdirAll(%q) failed; %v", path, err)
60 }
61 if err := os.RemoveAll(tmpDir); err != nil {
62 t.Fatalf("RemoveAll(%q) failed; %v", tmpDir, err)
63 }
64 }
65
66 func TestMkdirAllExtendedLength(t *testing.T) {
67 tmpDir := t.TempDir()
68
69 const prefix = `\\?\`
70 if len(tmpDir) < 4 || tmpDir[:4] != prefix {
71 fullPath, err := syscall.FullPath(tmpDir)
72 if err != nil {
73 t.Fatalf("FullPath(%q) fails: %v", tmpDir, err)
74 }
75 tmpDir = prefix + fullPath
76 }
77 path := tmpDir + `\dir\`
78 if err := os.MkdirAll(path, 0777); err != nil {
79 t.Fatalf("MkdirAll(%q) failed: %v", path, err)
80 }
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 }
87
88 func TestOpenRootSlash(t *testing.T) {
89 tests := []string{
90 `/`,
91 `\`,
92 }
93
94 for _, test := range tests {
95 dir, err := os.Open(test)
96 if err != nil {
97 t.Fatalf("Open(%q) failed: %v", test, err)
98 }
99 dir.Close()
100 }
101 }
102
View as plain text