2016-10-28 11:01:51 -06:00
|
|
|
// Copyright 2016 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package os_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2016-11-07 18:06:06 -07:00
|
|
|
"strings"
|
2018-01-28 14:19:13 -07:00
|
|
|
"syscall"
|
2016-10-28 11:01:51 -06:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFixLongPath(t *testing.T) {
|
2016-11-07 18:06:06 -07:00
|
|
|
// 248 is long enough to trigger the longer-than-248 checks in
|
|
|
|
// fixLongPath, but short enough not to make a path component
|
|
|
|
// longer than 255, which is illegal on Windows. (which
|
|
|
|
// doesn't really matter anyway, since this is purely a string
|
|
|
|
// function we're testing, and it's not actually being used to
|
|
|
|
// do a system call)
|
|
|
|
veryLong := "l" + strings.Repeat("o", 248) + "ng"
|
2016-10-28 11:01:51 -06:00
|
|
|
for _, test := range []struct{ in, want string }{
|
2016-11-07 18:06:06 -07:00
|
|
|
// Short; unchanged:
|
|
|
|
{`C:\short.txt`, `C:\short.txt`},
|
|
|
|
{`C:\`, `C:\`},
|
|
|
|
{`C:`, `C:`},
|
|
|
|
// The "long" substring is replaced by a looooooong
|
|
|
|
// string which triggers the rewriting. Except in the
|
|
|
|
// cases below where it doesn't.
|
|
|
|
{`C:\long\foo.txt`, `\\?\C:\long\foo.txt`},
|
|
|
|
{`C:/long/foo.txt`, `\\?\C:\long\foo.txt`},
|
|
|
|
{`C:\long\foo\\bar\.\baz\\`, `\\?\C:\long\foo\bar\baz`},
|
2016-10-28 11:01:51 -06:00
|
|
|
{`\\unc\path`, `\\unc\path`},
|
2016-11-07 18:06:06 -07:00
|
|
|
{`long.txt`, `long.txt`},
|
|
|
|
{`C:long.txt`, `C:long.txt`},
|
|
|
|
{`c:\long\..\bar\baz`, `c:\long\..\bar\baz`},
|
|
|
|
{`\\?\c:\long\foo.txt`, `\\?\c:\long\foo.txt`},
|
|
|
|
{`\\?\c:\long/foo.txt`, `\\?\c:\long/foo.txt`},
|
2016-10-28 11:01:51 -06:00
|
|
|
} {
|
2018-09-26 15:10:21 -06:00
|
|
|
in := strings.ReplaceAll(test.in, "long", veryLong)
|
|
|
|
want := strings.ReplaceAll(test.want, "long", veryLong)
|
2016-11-07 18:06:06 -07:00
|
|
|
if got := os.FixLongPath(in); got != want {
|
2018-09-26 15:10:21 -06:00
|
|
|
got = strings.ReplaceAll(got, veryLong, "long")
|
2016-10-28 11:01:51 -06:00
|
|
|
t.Errorf("fixLongPath(%q) = %q; want %q", test.in, got, test.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-28 14:19:13 -07:00
|
|
|
|
|
|
|
func TestMkdirAllExtendedLength(t *testing.T) {
|
2020-10-29 12:17:47 -06:00
|
|
|
tmpDir, err := os.MkdirTemp("", "TestMkdirAllExtendedLength")
|
2018-01-28 14:19:13 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
const prefix = `\\?\`
|
|
|
|
if len(tmpDir) < 4 || tmpDir[:4] != prefix {
|
|
|
|
fullPath, err := syscall.FullPath(tmpDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("FullPath(%q) fails: %v", tmpDir, err)
|
|
|
|
}
|
|
|
|
tmpDir = prefix + fullPath
|
|
|
|
}
|
|
|
|
path := tmpDir + `\dir\`
|
|
|
|
err = os.MkdirAll(path, 0777)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("MkdirAll(%q) failed: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
path = path + `.\dir2`
|
|
|
|
err = os.MkdirAll(path, 0777)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("MkdirAll(%q) should have failed, but did not", path)
|
|
|
|
}
|
|
|
|
}
|
2019-11-12 18:02:17 -07:00
|
|
|
|
|
|
|
func TestOpenRootSlash(t *testing.T) {
|
|
|
|
tests := []string{
|
|
|
|
`/`,
|
|
|
|
`\`,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
dir, err := os.Open(test)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Open(%q) failed: %v", test, err)
|
|
|
|
}
|
|
|
|
dir.Close()
|
|
|
|
}
|
|
|
|
}
|