@@ -11,12 +11,17 @@ import (
11
11
12
12
func TestBuilder (t * testing.T ) {
13
13
ctrl := gomock .NewController (t )
14
+ getWindowsString := func (t * testing.T , builder * path.Builder ) string {
15
+ s , err := builder .GetWindowsString ()
16
+ require .NoError (t , err )
17
+ return s
18
+ }
14
19
15
20
// The following paths should remain completely identical when
16
21
// resolved without making any assumptions about the layout of
17
22
// the underlying file system. ".." elements should not be
18
23
// removed from paths.
19
- t .Run ("Identity " , func (t * testing.T ) {
24
+ t .Run ("UNIXIdentity " , func (t * testing.T ) {
20
25
for _ , p := range []string {
21
26
"." ,
22
27
".." ,
@@ -31,6 +36,7 @@ func TestBuilder(t *testing.T) {
31
36
"/hello/../world/foo" ,
32
37
} {
33
38
t .Run (p , func (t * testing.T ) {
39
+ // Unix Parser
34
40
builder1 , scopewalker1 := path .EmptyBuilder .Join (path .VoidScopeWalker )
35
41
require .NoError (t , path .Resolve (path .MustNewUNIXParser (p ), scopewalker1 ))
36
42
require .Equal (t , p , builder1 .GetUNIXString ())
@@ -42,6 +48,81 @@ func TestBuilder(t *testing.T) {
42
48
}
43
49
})
44
50
51
+ t .Run ("WindowsParseUNIXPaths" , func (t * testing.T ) {
52
+ for _ , data := range [][]string {
53
+ {"." , "." },
54
+ {".." , ".." },
55
+ {"/" , "\\ " },
56
+ {"hello" , "hello" },
57
+ {"hello/" , "hello\\ " },
58
+ {"hello/.." , "hello\\ .." },
59
+ {"/hello/" , "\\ hello\\ " },
60
+ {"/hello/.." , "\\ hello\\ .." },
61
+ {"/hello/../world" , "\\ hello\\ ..\\ world" },
62
+ {"/hello/../world/" , "\\ hello\\ ..\\ world\\ " },
63
+ {"/hello/../world/foo" , "\\ hello\\ ..\\ world\\ foo" },
64
+ } {
65
+ p := data [0 ]
66
+ expected := data [1 ]
67
+ t .Run (p , func (t * testing.T ) {
68
+ // Windows Parser, compare Windows and UNIX string identity.
69
+ builder1 , scopewalker1 := path .EmptyBuilder .Join (path .VoidScopeWalker )
70
+ require .NoError (t , path .Resolve (path .MustNewWindowsParser (p ), scopewalker1 ))
71
+ require .Equal (t , expected , getWindowsString (t , builder1 ))
72
+ require .Equal (t , p , builder1 .GetUNIXString ())
73
+
74
+ builder2 , scopewalker2 := path .EmptyBuilder .Join (path .VoidScopeWalker )
75
+ require .NoError (t , path .Resolve (builder1 , scopewalker2 ))
76
+ require .Equal (t , expected , getWindowsString (t , builder2 ))
77
+ require .Equal (t , p , builder2 .GetUNIXString ())
78
+ })
79
+ }
80
+ })
81
+
82
+ t .Run ("WindowsIdentity" , func (t * testing.T ) {
83
+ for _ , p := range []string {
84
+ "C:\\ " ,
85
+ "C:\\ hello\\ " ,
86
+ "C:\\ hello\\ .." ,
87
+ "C:\\ hello\\ ..\\ world" ,
88
+ "C:\\ hello\\ ..\\ world\\ " ,
89
+ "C:\\ hello\\ ..\\ world\\ foo" ,
90
+ } {
91
+ t .Run (p , func (t * testing.T ) {
92
+ builder1 , scopewalker1 := path .EmptyBuilder .Join (path .VoidScopeWalker )
93
+ require .NoError (t , path .Resolve (path .MustNewWindowsParser (p ), scopewalker1 ))
94
+ require .Equal (t , p , getWindowsString (t , builder1 ))
95
+
96
+ builder2 , scopewalker2 := path .EmptyBuilder .Join (path .VoidScopeWalker )
97
+ require .NoError (t , path .Resolve (builder1 , scopewalker2 ))
98
+ require .Equal (t , p , getWindowsString (t , builder2 ))
99
+ })
100
+ }
101
+ })
102
+
103
+ t .Run ("WindowsParseAndWriteUNIXPaths" , func (t * testing.T ) {
104
+ for _ , data := range [][]string {
105
+ {"C:\\ " , "/" , },
106
+ {"C:\\ hello\\ " , "/hello/" , },
107
+ {"C:\\ hello\\ .." , "/hello/.." , },
108
+ {"C:\\ hello\\ ..\\ world" , "/hello/../world" , },
109
+ {"C:\\ hello\\ ..\\ world\\ " , "/hello/../world/" , },
110
+ {"C:\\ hello\\ ..\\ world\\ foo" , "/hello/../world/foo" , },
111
+ } {
112
+ p := data [0 ]
113
+ expected := data [1 ]
114
+ t .Run (p , func (t * testing.T ) {
115
+ builder1 , scopewalker1 := path .EmptyBuilder .Join (path .VoidScopeWalker )
116
+ require .NoError (t , path .Resolve (path .MustNewWindowsParser (p ), scopewalker1 ))
117
+ require .Equal (t , expected , builder1 .GetUNIXString ())
118
+
119
+ builder2 , scopewalker2 := path .EmptyBuilder .Join (path .VoidScopeWalker )
120
+ require .NoError (t , path .Resolve (builder1 , scopewalker2 ))
121
+ require .Equal (t , expected , builder2 .GetUNIXString ())
122
+ })
123
+ }
124
+ })
125
+
45
126
// The following paths can be normalized, even when making no
46
127
// assumptions about the layout of the underlying file system.
47
128
t .Run ("Normalized" , func (t * testing.T ) {
0 commit comments