@@ -313,6 +313,21 @@ func TestProcessWithMatches(t *testing.T) {
313
313
matches : map [string ]struct {}{"FOO" : {}, "BAR" : {}},
314
314
unmatched : map [string ]struct {}{"BAZ" : {}},
315
315
},
316
+ {
317
+ input : "${FOO:-}" ,
318
+ envs : map [string ]string {
319
+ "FOO" : "xxx" ,
320
+ "BAR" : "" ,
321
+ },
322
+ expected : "xxx" ,
323
+ matches : map [string ]struct {}{"FOO" : {}},
324
+ },
325
+ {
326
+ input : "${FOO:-}" ,
327
+ envs : map [string ]string {},
328
+ expected : "" ,
329
+ unmatched : map [string ]struct {}{"FOO" : {}},
330
+ },
316
331
317
332
{
318
333
input : "${FOO+aaa} ${BAR+bbb} ${BAZ+ccc}" ,
@@ -388,6 +403,15 @@ func TestProcessWithMatches(t *testing.T) {
388
403
expectedErr : true ,
389
404
unmatched : map [string ]struct {}{"BAZ" : {}},
390
405
},
406
+ {
407
+ input : "${BAZ:?}" ,
408
+ envs : map [string ]string {
409
+ "FOO" : "xxx" ,
410
+ "BAR" : "" ,
411
+ },
412
+ expectedErr : true ,
413
+ unmatched : map [string ]struct {}{"BAZ" : {}},
414
+ },
391
415
392
416
{
393
417
input : "${FOO=aaa}" ,
@@ -405,6 +429,11 @@ func TestProcessWithMatches(t *testing.T) {
405
429
},
406
430
expectedErr : true ,
407
431
},
432
+ {
433
+ input : "${FOO=}" ,
434
+ envs : map [string ]string {},
435
+ expectedErr : true ,
436
+ },
408
437
{
409
438
// special characters in regular expressions
410
439
// } needs to be escaped so it doesn't match the
@@ -426,12 +455,42 @@ func TestProcessWithMatches(t *testing.T) {
426
455
expected : "y" ,
427
456
matches : map [string ]struct {}{"FOO" : {}},
428
457
},
458
+ {
459
+ input : "${FOO#*}" ,
460
+ envs : map [string ]string {"FOO" : "xxyy" },
461
+ expected : "xxyy" ,
462
+ matches : map [string ]struct {}{"FOO" : {}},
463
+ },
464
+ {
465
+ input : "${FOO#$BAR}" ,
466
+ envs : map [string ]string {"FOO" : "xxyy" , "BAR" : "x" },
467
+ expected : "xyy" ,
468
+ matches : map [string ]struct {}{"FOO" : {}, "BAR" : {}},
469
+ },
470
+ {
471
+ input : "${FOO#$BAR}" ,
472
+ envs : map [string ]string {"FOO" : "xxyy" , "BAR" : "" },
473
+ expected : "xxyy" ,
474
+ matches : map [string ]struct {}{"FOO" : {}, "BAR" : {}},
475
+ },
476
+ {
477
+ input : "${FOO#}" ,
478
+ envs : map [string ]string {"FOO" : "xxyy" },
479
+ expected : "xxyy" ,
480
+ matches : map [string ]struct {}{"FOO" : {}},
481
+ },
429
482
{
430
483
input : "${FOO##*x}" ,
431
484
envs : map [string ]string {"FOO" : "xxyy" },
432
485
expected : "yy" ,
433
486
matches : map [string ]struct {}{"FOO" : {}},
434
487
},
488
+ {
489
+ input : "${FOO##}" ,
490
+ envs : map [string ]string {"FOO" : "xxyy" },
491
+ expected : "xxyy" ,
492
+ matches : map [string ]struct {}{"FOO" : {}},
493
+ },
435
494
{
436
495
input : "${FOO#?\\ ?}" ,
437
496
envs : map [string ]string {"FOO" : "???y" },
@@ -451,6 +510,18 @@ func TestProcessWithMatches(t *testing.T) {
451
510
expected : "a" ,
452
511
matches : map [string ]struct {}{"FOO" : {}},
453
512
},
513
+ {
514
+ input : "${FOO%}" ,
515
+ envs : map [string ]string {"FOO" : "xxyy" },
516
+ expected : "xxyy" ,
517
+ matches : map [string ]struct {}{"FOO" : {}},
518
+ },
519
+ {
520
+ input : "${FOO%%$BAR}" ,
521
+ envs : map [string ]string {"FOO" : "xxyy" , "BAR" : "" },
522
+ expected : "xxyy" ,
523
+ matches : map [string ]struct {}{"FOO" : {}, "BAR" : {}},
524
+ },
454
525
{
455
526
// test: wildcards
456
527
input : "${FOO/$NEEDLE/.} - ${FOO//$NEEDLE/.}" ,
@@ -484,6 +555,38 @@ func TestProcessWithMatches(t *testing.T) {
484
555
expected : "\\ /tmp\\ /foo.txt" ,
485
556
matches : map [string ]struct {}{"FOO" : {}},
486
557
},
558
+
559
+ // Following cases with empty/partial values are currently not
560
+ // guaranteed behavior. Tests are provided to make sure partial
561
+ // input does not cause runtime error.
562
+ {
563
+ input : "${FOO/$BAR/ww}" ,
564
+ envs : map [string ]string {"FOO" : "xxyy" , "BAR" : "" },
565
+ expected : "wwxxyy" ,
566
+ matches : map [string ]struct {}{"FOO" : {}, "BAR" : {}},
567
+ },
568
+ {
569
+ input : "${FOO//ww}" ,
570
+ envs : map [string ]string {"FOO" : "xxyy" },
571
+ expectedErr : true ,
572
+ },
573
+ {
574
+ input : "${FOO//}" ,
575
+ envs : map [string ]string {"FOO" : "xxyy" },
576
+ expectedErr : true ,
577
+ },
578
+ {
579
+ input : "${FOO///}" ,
580
+ envs : map [string ]string {"FOO" : "xxyy" },
581
+ expected : "xxyy" ,
582
+ matches : map [string ]struct {}{"FOO" : {}},
583
+ },
584
+ {
585
+ input : "${FOO///}" ,
586
+ envs : map [string ]string {},
587
+ expected : "" ,
588
+ unmatched : map [string ]struct {}{"FOO" : {}},
589
+ },
487
590
}
488
591
489
592
for _ , c := range tc {
@@ -500,12 +603,12 @@ func TestProcessWithMatches(t *testing.T) {
500
603
require .NoError (t , err )
501
604
require .Equal (t , c .expected , w )
502
605
503
- require .Equal (t , len (c .matches ), len ( matches ) )
606
+ require .Len (t , matches , len (c .matches ), c . matches )
504
607
for k := range c .matches {
505
608
require .Contains (t , matches , k )
506
609
}
507
610
508
- require .Equal (t , len (c .unmatched ), len ( unmatched ) )
611
+ require .Len (t , unmatched , len (c .unmatched ), c . unmatched )
509
612
for k := range c .unmatched {
510
613
require .Contains (t , unmatched , k )
511
614
}
0 commit comments