@@ -6,23 +6,40 @@ package os_test
6
6
7
7
import (
8
8
"os"
9
+ "strings"
9
10
"testing"
10
11
)
11
12
12
13
func TestFixLongPath (t * testing.T ) {
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"
13
21
for _ , test := range []struct { in , want string }{
14
- {`C:\foo.txt` , `\\?\C:\foo.txt` },
15
- {`C:/foo.txt` , `\\?\C:\foo.txt` },
16
- {`C:\foo\\bar\.\baz\\` , `\\?\C:\foo\bar\baz` },
17
- {`C:\` , `\\?\C:\` }, // drives must have a trailing slash
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` },
18
32
{`\\unc\path` , `\\unc\path` },
19
- {`foo .txt` , `foo .txt` },
20
- {`C:foo .txt` , `C:foo .txt` },
21
- {`c:\foo \..\bar\baz` , `c:\foo \..\bar\baz` },
22
- {`\\?\c:\windows \foo.txt` , `\\?\c:\windows \foo.txt` },
23
- {`\\?\c:\windows /foo.txt` , `\\?\c:\windows /foo.txt` },
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` },
24
38
} {
25
- if got := os .FixLongPath (test .in ); got != test .want {
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 )
26
43
t .Errorf ("fixLongPath(%q) = %q; want %q" , test .in , got , test .want )
27
44
}
28
45
}
0 commit comments