path_windows_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. package os_test
  5. import (
  6. "os"
  7. "strings"
  8. "syscall"
  9. "testing"
  10. )
  11. func TestFixLongPath(t *testing.T) {
  12. if os.CanUseLongPaths {
  13. return
  14. }
  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. func TestMkdirAllLongPath(t *testing.T) {
  49. tmpDir := t.TempDir()
  50. path := tmpDir
  51. for i := 0; i < 100; i++ {
  52. path += `\another-path-component`
  53. }
  54. if err := os.MkdirAll(path, 0777); err != nil {
  55. t.Fatalf("MkdirAll(%q) failed; %v", path, err)
  56. }
  57. if err := os.RemoveAll(tmpDir); err != nil {
  58. t.Fatalf("RemoveAll(%q) failed; %v", tmpDir, err)
  59. }
  60. }
  61. func TestMkdirAllExtendedLength(t *testing.T) {
  62. tmpDir := t.TempDir()
  63. const prefix = `\\?\`
  64. if len(tmpDir) < 4 || tmpDir[:4] != prefix {
  65. fullPath, err := syscall.FullPath(tmpDir)
  66. if err != nil {
  67. t.Fatalf("FullPath(%q) fails: %v", tmpDir, err)
  68. }
  69. tmpDir = prefix + fullPath
  70. }
  71. path := tmpDir + `\dir\`
  72. if err := os.MkdirAll(path, 0777); err != nil {
  73. t.Fatalf("MkdirAll(%q) failed: %v", path, err)
  74. }
  75. path = path + `.\dir2`
  76. if err := os.MkdirAll(path, 0777); err == nil {
  77. t.Fatalf("MkdirAll(%q) should have failed, but did not", path)
  78. }
  79. }
  80. func TestOpenRootSlash(t *testing.T) {
  81. tests := []string{
  82. `/`,
  83. `\`,
  84. }
  85. for _, test := range tests {
  86. dir, err := os.Open(test)
  87. if err != nil {
  88. t.Fatalf("Open(%q) failed: %v", test, err)
  89. }
  90. dir.Close()
  91. }
  92. }