Quentin Smith | 231aa9d | 2016-10-28 13:01:51 -0400 | [diff] [blame] | 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 | |
| 5 | package os_test |
| 6 | |
| 7 | import ( |
| 8 | "os" |
Brad Fitzpatrick | 2925427 | 2016-11-08 01:06:06 +0000 | [diff] [blame] | 9 | "strings" |
Quentin Smith | 231aa9d | 2016-10-28 13:01:51 -0400 | [diff] [blame] | 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | func TestFixLongPath(t *testing.T) { |
Brad Fitzpatrick | 2925427 | 2016-11-08 01:06:06 +0000 | [diff] [blame] | 14 | // 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 Smith | 231aa9d | 2016-10-28 13:01:51 -0400 | [diff] [blame] | 21 | for _, test := range []struct{ in, want string }{ |
Brad Fitzpatrick | 2925427 | 2016-11-08 01:06:06 +0000 | [diff] [blame] | 22 | // 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 Smith | 231aa9d | 2016-10-28 13:01:51 -0400 | [diff] [blame] | 32 | {`\\unc\path`, `\\unc\path`}, |
Brad Fitzpatrick | 2925427 | 2016-11-08 01:06:06 +0000 | [diff] [blame] | 33 | {`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 Smith | 231aa9d | 2016-10-28 13:01:51 -0400 | [diff] [blame] | 38 | } { |
Brad Fitzpatrick | 2925427 | 2016-11-08 01:06:06 +0000 | [diff] [blame] | 39 | 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 Smith | 231aa9d | 2016-10-28 13:01:51 -0400 | [diff] [blame] | 43 | t.Errorf("fixLongPath(%q) = %q; want %q", test.in, got, test.want) |
| 44 | } |
| 45 | } |
| 46 | } |