blob: cce0bdd522dc2fc43328a951efda97eaa1225693 [file] [log] [blame]
Quentin Smith231aa9d2016-10-28 13:01:51 -04001// 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
5package os_test
6
7import (
8 "os"
Brad Fitzpatrick29254272016-11-08 01:06:06 +00009 "strings"
Quentin Smith231aa9d2016-10-28 13:01:51 -040010 "testing"
11)
12
13func TestFixLongPath(t *testing.T) {
Brad Fitzpatrick29254272016-11-08 01:06:06 +000014 // 248 is long enough to trigger the longer-than-248 checks in
15 // fixLongPath, but short enough not to make a path component
16 // longer than 255, which is illegal on Windows. (which
17 // doesn't really matter anyway, since this is purely a string
18 // function we're testing, and it's not actually being used to
19 // do a system call)
20 veryLong := "l" + strings.Repeat("o", 248) + "ng"
Quentin Smith231aa9d2016-10-28 13:01:51 -040021 for _, test := range []struct{ in, want string }{
Brad Fitzpatrick29254272016-11-08 01:06:06 +000022 // Short; unchanged:
23 {`C:\short.txt`, `C:\short.txt`},
24 {`C:\`, `C:\`},
25 {`C:`, `C:`},
26 // The "long" substring is replaced by a looooooong
27 // string which triggers the rewriting. Except in the
28 // cases below where it doesn't.
29 {`C:\long\foo.txt`, `\\?\C:\long\foo.txt`},
30 {`C:/long/foo.txt`, `\\?\C:\long\foo.txt`},
31 {`C:\long\foo\\bar\.\baz\\`, `\\?\C:\long\foo\bar\baz`},
Quentin Smith231aa9d2016-10-28 13:01:51 -040032 {`\\unc\path`, `\\unc\path`},
Brad Fitzpatrick29254272016-11-08 01:06:06 +000033 {`long.txt`, `long.txt`},
34 {`C:long.txt`, `C:long.txt`},
35 {`c:\long\..\bar\baz`, `c:\long\..\bar\baz`},
36 {`\\?\c:\long\foo.txt`, `\\?\c:\long\foo.txt`},
37 {`\\?\c:\long/foo.txt`, `\\?\c:\long/foo.txt`},
Quentin Smith231aa9d2016-10-28 13:01:51 -040038 } {
Brad Fitzpatrick29254272016-11-08 01:06:06 +000039 in := strings.Replace(test.in, "long", veryLong, -1)
40 want := strings.Replace(test.want, "long", veryLong, -1)
41 if got := os.FixLongPath(in); got != want {
42 got = strings.Replace(got, veryLong, "long", -1)
Quentin Smith231aa9d2016-10-28 13:01:51 -040043 t.Errorf("fixLongPath(%q) = %q; want %q", test.in, got, test.want)
44 }
45 }
46}