@@ -21,9 +21,14 @@ import (
21
21
"encoding/json"
22
22
"errors"
23
23
"flag"
24
+ "fmt"
24
25
"io/ioutil"
26
+ "os"
25
27
"os/exec"
26
28
"path/filepath"
29
+ "regexp"
30
+ "runtime"
31
+ "strconv"
27
32
"strings"
28
33
"testing"
29
34
@@ -217,21 +222,63 @@ func runTest(t *testing.T, test *mainTest) {
217
222
}
218
223
}
219
224
225
+ func expandEscapedFilenames (t * testing.T ) error {
226
+ // Escaped filenames exist because their unescaped forms are invalid on
227
+ // Windows. We have no choice but to skip these in testing.
228
+ if runtime .GOOS == "windows" {
229
+ return nil
230
+ }
231
+
232
+ match , err := filepath .Glob ("testdata/*%*" )
233
+ if err != nil {
234
+ return err
235
+ }
236
+
237
+ filenameEscapeRE := regexp .MustCompile (`%[0-9A-Fa-f]{2}` )
238
+
239
+ for _ , input := range match {
240
+ file := filenameEscapeRE .ReplaceAllStringFunc (input , func (s string ) string {
241
+ code , err := strconv .ParseUint (s [1 :], 16 , 8 )
242
+ if err != nil {
243
+ panic (err )
244
+ }
245
+ return string ([]byte {byte (code )})
246
+ })
247
+ if file != input {
248
+ if err := os .Link (input , file ); err != nil {
249
+ if ! os .IsExist (err ) {
250
+ return fmt .Errorf ("linking %s -> %s: %v" , file , input , err )
251
+ }
252
+ }
253
+ t .Logf ("Linked %s -> %s" , input , file )
254
+ }
255
+ }
256
+
257
+ return nil
258
+ }
259
+
220
260
func TestMain (t * testing.T ) {
221
261
flag .Parse ()
262
+
263
+ if err := expandEscapedFilenames (t ); err != nil {
264
+ t .Fatal (err )
265
+ }
266
+
222
267
var mainTests []mainTest
223
268
match , err := filepath .Glob ("testdata/*.jsonnet" )
224
269
if err != nil {
225
270
t .Fatal (err )
226
271
}
227
272
273
+ jsonnetExtRE := regexp .MustCompile (`\.jsonnet$` )
274
+
228
275
for _ , input := range match {
229
- golden := input
230
- name := input
231
- if strings .HasSuffix (input , ".jsonnet" ) {
232
- name = input [:len (input )- len (".jsonnet" )]
233
- golden = name + ".golden"
276
+ // Skip escaped filenames.
277
+ if strings .ContainsRune (input , '%' ) {
278
+ continue
234
279
}
280
+ name := jsonnetExtRE .ReplaceAllString (input , "" )
281
+ golden := jsonnetExtRE .ReplaceAllString (input , ".golden" )
235
282
var meta testMetadata
236
283
if val , exists := metadataForTests [name ]; exists {
237
284
meta = val
0 commit comments