1
1
package limatmpl
2
2
3
3
import (
4
+ "path/filepath"
5
+ "runtime"
4
6
"strings"
5
7
"testing"
6
8
@@ -18,8 +20,8 @@ var useAbsLocatorsTestCases = []useAbsLocatorsTestCase{
18
20
{
19
21
"Template without basedOn or script file" ,
20
22
"template://foo" ,
21
- `foo: bar ` ,
22
- `foo: bar ` ,
23
+ `arch: aarch64 ` ,
24
+ `arch: aarch64 ` ,
23
25
},
24
26
{
25
27
"Single string base template" ,
@@ -102,98 +104,161 @@ func RunUseAbsLocatorTest(t *testing.T, tc useAbsLocatorsTestCase) {
102
104
}
103
105
104
106
func TestBasePath (t * testing.T ) {
105
- actual , err := basePath ("/foo" )
107
+ // On Windows the root will be something like "C:\"
108
+ root , err := filepath .Abs ("/" )
106
109
assert .NilError (t , err )
107
- assert . Equal ( t , actual , "/" )
110
+ volume := filepath . VolumeName ( root )
108
111
109
- actual , err = basePath ("/foo/bar" )
110
- assert .NilError (t , err )
111
- assert .Equal (t , actual , "/foo" )
112
+ t .Run ("" , func (t * testing.T ) {
113
+ actual , err := basePath ("/foo" )
114
+ assert .NilError (t , err )
115
+ assert .Equal (t , actual , root )
116
+ })
112
117
113
- actual , err = basePath ("template://foo" )
114
- assert .NilError (t , err )
115
- assert .Equal (t , actual , "template://" )
118
+ t .Run ("" , func (t * testing.T ) {
119
+ actual , err := basePath ("/foo/bar" )
120
+ assert .NilError (t , err )
121
+ assert .Equal (t , actual , filepath .Clean (volume + "/foo" ))
122
+ })
116
123
117
- actual , err = basePath ("template://foo/bar" )
118
- assert .NilError (t , err )
119
- assert .Equal (t , actual , "template://foo" )
124
+ t .Run ("" , func (t * testing.T ) {
125
+ actual , err := basePath ("template://foo" )
126
+ assert .NilError (t , err )
127
+ assert .Equal (t , actual , "template://" )
128
+ })
120
129
121
- actual , err = basePath ("http://host/foo" )
122
- assert .NilError (t , err )
123
- assert .Equal (t , actual , "http://host" )
130
+ t .Run ("" , func (t * testing.T ) {
131
+ actual , err := basePath ("template://foo/bar" )
132
+ assert .NilError (t , err )
133
+ assert .Equal (t , actual , "template://foo" )
134
+ })
124
135
125
- actual , err = basePath ("http://host/foo/bar" )
126
- assert .NilError (t , err )
127
- assert .Equal (t , actual , "http://host/foo" )
136
+ t .Run ("" , func (t * testing.T ) {
137
+ actual , err := basePath ("http://host/foo" )
138
+ assert .NilError (t , err )
139
+ assert .Equal (t , actual , "http://host" )
140
+ })
128
141
129
- actual , err = basePath ("file:///foo" )
130
- assert .NilError (t , err )
131
- assert .Equal (t , actual , "file:///" )
142
+ t .Run ("" , func (t * testing.T ) {
143
+ actual , err := basePath ("http://host/foo/bar" )
144
+ assert .NilError (t , err )
145
+ assert .Equal (t , actual , "http://host/foo" )
146
+ })
132
147
133
- actual , err = basePath ("file:///foo/bar" )
134
- assert .NilError (t , err )
135
- assert .Equal (t , actual , "file:///foo" )
148
+ t .Run ("" , func (t * testing.T ) {
149
+ actual , err := basePath ("file:///foo" )
150
+ assert .NilError (t , err )
151
+ assert .Equal (t , actual , "file:///" )
152
+ })
153
+
154
+ t .Run ("" , func (t * testing.T ) {
155
+ actual , err := basePath ("file:///foo/bar" )
156
+ assert .NilError (t , err )
157
+ assert .Equal (t , actual , "file:///foo" )
158
+ })
136
159
}
137
160
138
161
func TestAbsPath (t * testing.T ) {
139
- // If the locator is already an absolute path, it is returned unchanged (no extension appended either)
140
- actual , err := absPath ("/foo" , "/root" )
162
+ root , err := filepath .Abs ("/" )
141
163
assert .NilError (t , err )
142
- assert . Equal ( t , actual , "/foo" )
164
+ volume := filepath . VolumeName ( root )
143
165
144
- actual , err = absPath ("template://foo" , "/root" )
145
- assert .NilError (t , err )
146
- assert .Equal (t , actual , "template://foo" )
166
+ t .Run ("If the locator is already an absolute path, it is returned unchanged" , func (t * testing.T ) {
167
+ actual , err := absPath (volume + "/foo" , volume + "/root" )
168
+ assert .NilError (t , err )
169
+ assert .Equal (t , actual , filepath .Clean (volume + "/foo" ))
170
+ })
147
171
148
- actual , err = absPath ("http://host/foo" , "/root" )
149
- assert .NilError (t , err )
150
- assert .Equal (t , actual , "http://host/foo" )
172
+ t .Run ("If the locator is a rooted path without volume name, then the volume will be added" , func (t * testing.T ) {
173
+ actual , err := absPath ("/foo" , volume + "/root" )
174
+ assert .NilError (t , err )
175
+ assert .Equal (t , actual , filepath .Clean (volume + "/foo" ))
176
+ })
151
177
152
- actual , err = absPath ("file:///foo" , "/root" )
153
- assert .NilError (t , err )
154
- assert .Equal (t , actual , "file:///foo" )
178
+ t .Run ("" , func (t * testing.T ) {
179
+ actual , err := absPath ("template://foo" , volume + "/root" )
180
+ assert .NilError (t , err )
181
+ assert .Equal (t , actual , "template://foo" )
182
+ })
155
183
156
- // Can't have relative path when reading from STDIN
157
- _ , err = absPath ("foo" , "-" )
158
- assert .ErrorContains (t , err , "STDIN" )
184
+ t .Run ("" , func (t * testing.T ) {
185
+ actual , err := absPath ("http://host/foo" , volume + "/root" )
186
+ assert .NilError (t , err )
187
+ assert .Equal (t , actual , "http://host/foo" )
188
+ })
159
189
160
- // Relative paths must be underneath the basePath
161
- _ , err = absPath ("../foo" , "/root" )
162
- assert .ErrorContains (t , err , "'../'" )
190
+ t .Run ("" , func (t * testing.T ) {
191
+ actual , err := absPath ("file:///foo" , volume + "/root" )
192
+ assert .NilError (t , err )
193
+ assert .Equal (t , actual , "file:///foo" )
194
+ })
163
195
164
- // basePath must not be empty
165
- _ , err = absPath ("foo" , "" )
166
- assert .ErrorContains (t , err , "empty" )
196
+ t .Run ("Can't have relative path when reading from STDIN" , func (t * testing.T ) {
197
+ _ , err = absPath ("foo" , "-" )
198
+ assert .ErrorContains (t , err , "STDIN" )
199
+ })
167
200
168
- _ , err = absPath ("./foo" , "" )
169
- assert .ErrorContains (t , err , "empty" )
201
+ t .Run ("Relative paths must be underneath the basePath" , func (t * testing.T ) {
202
+ _ , err = absPath ("../foo" , volume + "/root" )
203
+ assert .ErrorContains (t , err , "'../'" )
204
+ })
170
205
171
- // Check relative paths with all the supported schemes
172
- actual , err = absPath ("./ foo" , "/root " )
173
- assert .NilError (t , err )
174
- assert . Equal ( t , actual , "/root/foo" )
206
+ t . Run ( "basePath must not be empty" , func ( t * testing. T ) {
207
+ _ , err = absPath ("foo" , "" )
208
+ assert .ErrorContains (t , err , "empty" )
209
+ } )
175
210
176
- actual , err = absPath ("foo" , "template://" )
177
- assert .NilError (t , err )
178
- assert .Equal (t , actual , "template://foo" )
211
+ t .Run ("" , func (t * testing.T ) {
212
+ _ , err = absPath ("./foo" , "" )
213
+ assert .ErrorContains (t , err , "empty" )
214
+ })
179
215
180
- actual , err = absPath ("bar" , "template://foo" )
181
- assert .NilError (t , err )
182
- assert .Equal (t , actual , "template://foo/bar" )
216
+ t .Run ("" , func (t * testing.T ) {
217
+ actual , err := absPath ("./foo" , volume + "/root" )
218
+ assert .NilError (t , err )
219
+ assert .Equal (t , actual , filepath .Clean (volume + "/root/foo" ))
220
+ })
183
221
184
- actual , err = absPath ("foo" , "http://host" )
185
- assert .NilError (t , err )
186
- assert .Equal (t , actual , "http://host/foo" )
222
+ if runtime .GOOS == "windows" {
223
+ t .Run ("Relative locators must not include volume names" , func (t * testing.T ) {
224
+ _ , err := absPath (volume + "foo" , volume + "/root" )
225
+ assert .ErrorContains (t , err , "volume" )
226
+ })
227
+ }
228
+
229
+ t .Run ("" , func (t * testing.T ) {
230
+ actual , err := absPath ("foo" , "template://" )
231
+ assert .NilError (t , err )
232
+ assert .Equal (t , actual , "template://foo" )
233
+ })
187
234
188
- actual , err = absPath ("bar" , "http://host/foo" )
189
- assert .NilError (t , err )
190
- assert .Equal (t , actual , "http://host/foo/bar" )
235
+ t .Run ("" , func (t * testing.T ) {
236
+ actual , err := absPath ("bar" , "template://foo" )
237
+ assert .NilError (t , err )
238
+ assert .Equal (t , actual , "template://foo/bar" )
239
+ })
191
240
192
- actual , err = absPath ("foo" , "file:///" )
193
- assert .NilError (t , err )
194
- assert .Equal (t , actual , "file:///foo" )
241
+ t .Run ("" , func (t * testing.T ) {
242
+ actual , err := absPath ("foo" , "http://host" )
243
+ assert .NilError (t , err )
244
+ assert .Equal (t , actual , "http://host/foo" )
245
+ })
195
246
196
- actual , err = absPath ("bar" , "file:///foo" )
197
- assert .NilError (t , err )
198
- assert .Equal (t , actual , "file:///foo/bar" )
247
+ t .Run ("" , func (t * testing.T ) {
248
+ actual , err := absPath ("bar" , "http://host/foo" )
249
+ assert .NilError (t , err )
250
+ assert .Equal (t , actual , "http://host/foo/bar" )
251
+ })
252
+
253
+ t .Run ("" , func (t * testing.T ) {
254
+ actual , err := absPath ("foo" , "file:///" )
255
+ assert .NilError (t , err )
256
+ assert .Equal (t , actual , "file:///foo" )
257
+ })
258
+
259
+ t .Run ("" , func (t * testing.T ) {
260
+ actual , err := absPath ("bar" , "file:///foo" )
261
+ assert .NilError (t , err )
262
+ assert .Equal (t , actual , "file:///foo/bar" )
263
+ })
199
264
}
0 commit comments