tree-optimization/116481 - avoid building function_type[]
[official-gcc.git] / libgo / go / os / path_windows_test.go
blobe960bcb19c69476352a9765ba3e5877a6fd9a1db
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.
5 package os_test
7 import (
8 "os"
9 "strings"
10 "syscall"
11 "testing"
14 func TestFixLongPath(t *testing.T) {
15 if os.CanUseLongPaths {
16 return
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
23 // do a system call)
24 veryLong := "l" + strings.Repeat("o", 248) + "ng"
25 for _, test := range []struct{ in, want string }{
26 // Short; unchanged:
27 {`C:\short.txt`, `C:\short.txt`},
28 {`C:\`, `C:\`},
29 {`C:`, `C:`},
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`},
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)
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`
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) {
67 tmpDir := t.TempDir()
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)
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) {
89 tests := []string{
90 `/`,
91 `\`,
94 for _, test := range tests {
95 dir, err := os.Open(test)
96 if err != nil {
97 t.Fatalf("Open(%q) failed: %v", test, err)
99 dir.Close()