1
0
mirror of https://github.com/golang/go synced 2024-10-02 02:28:32 -06:00
go/src/os/path_windows_test.go
Brad Fitzpatrick 2925427a47 os: on Windows, don't fix long paths that aren't long
Notably, don't allocate.

Follow-up to https://golang.org/cl/32451 which added long path
cleaning.

Updates #3358

Change-Id: I89c59cbd660d0a030f31b6acd070fa9f3250683b
Reviewed-on: https://go-review.googlesource.com/32886
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
2016-11-08 23:06:17 +00:00

47 lines
1.6 KiB
Go

// 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"
"strings"
"testing"
)
func TestFixLongPath(t *testing.T) {
// 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"
for _, test := range []struct{ in, want string }{
// 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`},
{`\\unc\path`, `\\unc\path`},
{`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`},
} {
in := strings.Replace(test.in, "long", veryLong, -1)
want := strings.Replace(test.want, "long", veryLong, -1)
if got := os.FixLongPath(in); got != want {
got = strings.Replace(got, veryLong, "long", -1)
t.Errorf("fixLongPath(%q) = %q; want %q", test.in, got, test.want)
}
}
}