@@ -462,3 +462,194 @@ impl OutputAssertionBuilder {
462
462
self . not ( ) . is ( output)
463
463
}
464
464
}
465
+
466
+ #[ cfg( test) ]
467
+ mod test {
468
+ use super :: * ;
469
+ use std:: ffi:: OsString ;
470
+
471
+ fn command ( ) -> Assert {
472
+ Assert :: command ( & [ "printenv" ] )
473
+ }
474
+
475
+ #[ test]
476
+ fn take_ownership ( ) {
477
+ let x = Environment :: inherit ( ) ;
478
+
479
+ command ( ) . with_env ( x. clone ( ) ) . with_env ( & x) . with_env ( x) ;
480
+ }
481
+
482
+ #[ test]
483
+ fn in_place_mod ( ) {
484
+ let y = Environment :: empty ( ) ;
485
+
486
+ let y = y. insert ( "key" , "value" ) ;
487
+
488
+ assert_eq ! (
489
+ y. compile( ) ,
490
+ vec![ ( OsString :: from( "key" ) , OsString :: from( "value" ) ) ]
491
+ ) ;
492
+ }
493
+
494
+ #[ test]
495
+ fn in_place_mod2 ( ) {
496
+ let x = Environment :: inherit ( ) ;
497
+
498
+ assert ! (
499
+ command( )
500
+ . with_env( & x. insert( "key" , "value" ) . insert( "key" , "vv" ) )
501
+ . stdout( )
502
+ . contains( "key=vv" )
503
+ . execute( )
504
+ . is_ok( )
505
+ ) ;
506
+ // Granted, `insert` moved `x`, so we can no longer reference it, even
507
+ // though only a reference was passed to `with_env`
508
+ }
509
+
510
+ #[ test]
511
+ fn in_place_mod3 ( ) {
512
+ // In-place modification while allowing later accesses to the `Environment`
513
+ let y = Environment :: empty ( ) ;
514
+
515
+ assert_eq ! (
516
+ y. clone( ) . insert( "key" , "value" ) . compile( ) ,
517
+ vec![ ( OsString :: from( "key" ) , OsString :: from( "value" ) ) ]
518
+ ) ;
519
+
520
+ assert ! (
521
+ command( )
522
+ . with_env( y)
523
+ . stdout( )
524
+ . not( )
525
+ . contains( "key=value" )
526
+ . execute( )
527
+ . is_ok( )
528
+ ) ;
529
+ }
530
+
531
+ #[ test]
532
+ fn empty_env ( ) {
533
+ // In-place modification while allowing later accesses to the `Environment`
534
+ let y = Environment :: empty ( ) ;
535
+
536
+ assert ! ( command( ) . with_env( y) . stdout( ) . is( "" ) . execute( ) . is_ok( ) ) ;
537
+ }
538
+ #[ test]
539
+ fn take_vec ( ) {
540
+ let v = vec ! [ ( "bar" . to_string( ) , "baz" . to_string( ) ) ] ;
541
+
542
+ assert ! (
543
+ command( )
544
+ . with_env( & vec![ ( "bar" , "baz" ) ] )
545
+ . stdout( )
546
+ . contains( "bar=baz" )
547
+ . execute( )
548
+ . is_ok( )
549
+ ) ;
550
+
551
+ assert ! (
552
+ command( )
553
+ . with_env( & v)
554
+ . stdout( )
555
+ . contains( "bar=baz" )
556
+ . execute( )
557
+ . is_ok( )
558
+ ) ;
559
+
560
+ assert ! (
561
+ command( )
562
+ . with_env( & vec![ ( "bar" , "baz" ) ] )
563
+ . stdout( )
564
+ . isnt( "" )
565
+ . execute( )
566
+ . is_ok( )
567
+ ) ;
568
+ }
569
+
570
+ #[ test]
571
+ fn take_slice_of_strs ( ) {
572
+ assert ! (
573
+ command( )
574
+ . with_env( & [ ( "bar" , "BAZ" ) ] )
575
+ . stdout( )
576
+ . contains( "bar=BAZ" )
577
+ . execute( )
578
+ . is_ok( )
579
+ ) ;
580
+
581
+ assert ! (
582
+ command( )
583
+ . with_env( & [ ( "bar" , "BAZ" ) ] [ ..] )
584
+ . stdout( )
585
+ . contains( "bar=BAZ" )
586
+ . execute( )
587
+ . is_ok( )
588
+ ) ;
589
+
590
+ assert ! (
591
+ command( )
592
+ . with_env( [ ( "bar" , "BAZ" ) ] . as_ref( ) )
593
+ . stdout( )
594
+ . contains( "bar=BAZ" )
595
+ . execute( )
596
+ . is_ok( )
597
+ ) ;
598
+ }
599
+
600
+ #[ test]
601
+ fn take_slice_of_strings ( ) {
602
+ // same deal as above
603
+
604
+ assert ! (
605
+ command( )
606
+ . with_env( & [ ( "bar" . to_string( ) , "BAZ" . to_string( ) ) ] )
607
+ . stdout( )
608
+ . contains( "bar=BAZ" )
609
+ . execute( )
610
+ . is_ok( )
611
+ ) ;
612
+
613
+ assert ! (
614
+ command( )
615
+ . with_env( & [ ( "bar" . to_string( ) , "BAZ" . to_string( ) ) ] [ ..] )
616
+ . stdout( )
617
+ . contains( "bar=BAZ" )
618
+ . execute( )
619
+ . is_ok( )
620
+ ) ;
621
+ }
622
+
623
+ #[ test]
624
+ fn take_slice ( ) {
625
+ assert ! (
626
+ command( )
627
+ . with_env( & [ ( "hey" , "ho" ) ] )
628
+ . stdout( )
629
+ . contains( "hey=ho" )
630
+ . execute( )
631
+ . is_ok( )
632
+ ) ;
633
+
634
+ assert ! (
635
+ command( )
636
+ . with_env( & [ ( "hey" , "ho" . to_string( ) ) ] )
637
+ . stdout( )
638
+ . contains( "hey=ho" )
639
+ . execute( )
640
+ . is_ok( )
641
+ ) ;
642
+ }
643
+
644
+ #[ test]
645
+ fn take_string_i32 ( ) {
646
+ assert ! (
647
+ command( )
648
+ . with_env( & [ ( "bar" , 3 as i32 ) ] )
649
+ . stdout( )
650
+ . contains( "bar=3" )
651
+ . execute( )
652
+ . is_ok( )
653
+ ) ;
654
+ }
655
+ }
0 commit comments