@@ -2469,3 +2469,296 @@ version = "0.1.0"
2469
2469
& [ ( "Cargo.toml" , & rewritten_toml) ] ,
2470
2470
) ;
2471
2471
}
2472
+
2473
+ fn verify_packaged_status_line (
2474
+ output : std:: process:: Output ,
2475
+ num_files : usize ,
2476
+ uncompressed_size : usize ,
2477
+ compressed_size : usize ,
2478
+ ) {
2479
+ use cargo:: util:: human_readable_bytes;
2480
+
2481
+ let stderr = String :: from_utf8 ( output. stderr ) . unwrap ( ) ;
2482
+ let mut packaged_lines = stderr
2483
+ . lines ( )
2484
+ . filter ( |line| line. trim ( ) . starts_with ( "Packaged" ) ) ;
2485
+ let packaged_line = packaged_lines
2486
+ . next ( )
2487
+ . expect ( "`Packaged` status line should appear in stderr" ) ;
2488
+ assert ! (
2489
+ packaged_lines. next( ) . is_none( ) ,
2490
+ "Only one `Packaged` status line should appear in stderr"
2491
+ ) ;
2492
+ let size_info = packaged_line. trim ( ) . trim_start_matches ( "Packaged" ) . trim ( ) ;
2493
+ let uncompressed = human_readable_bytes ( uncompressed_size) ;
2494
+ let compressed = human_readable_bytes ( compressed_size) ;
2495
+ let expected = format ! (
2496
+ "{} files, {:.1}{} ({:.1}{} compressed)" ,
2497
+ num_files, uncompressed. 0 , uncompressed. 1 , compressed. 0 , compressed. 1
2498
+ ) ;
2499
+ assert_eq ! ( size_info, expected) ;
2500
+ }
2501
+
2502
+ #[ cargo_test]
2503
+ fn basic_filesizes ( ) {
2504
+ let cargo_toml_orig_contents = r#"
2505
+ [package]
2506
+ name = "foo"
2507
+ version = "0.0.1"
2508
+ authors = []
2509
+ exclude = ["*.txt"]
2510
+ license = "MIT"
2511
+ description = "foo"
2512
+ "# ;
2513
+ let main_rs_contents = r#"fn main() { println!("🦀"); }"# ;
2514
+ let cargo_toml_contents = format ! (
2515
+ r#"{}
2516
+ [package]
2517
+ name = "foo"
2518
+ version = "0.0.1"
2519
+ authors = []
2520
+ exclude = ["*.txt"]
2521
+ description = "foo"
2522
+ license = "MIT"
2523
+ "# ,
2524
+ cargo:: core:: package:: MANIFEST_PREAMBLE
2525
+ ) ;
2526
+ let cargo_lock_contents = r#"# This file is automatically @generated by Cargo.
2527
+ # It is not intended for manual editing.
2528
+ version = 3
2529
+
2530
+ [[package]]
2531
+ name = "foo"
2532
+ version = "0.0.1"
2533
+ "# ;
2534
+ let p = project ( )
2535
+ . file ( "Cargo.toml" , cargo_toml_orig_contents)
2536
+ . file ( "src/main.rs" , main_rs_contents)
2537
+ . file ( "src/bar.txt" , "Ignored text file contents" ) // should be ignored when packaging
2538
+ . build ( ) ;
2539
+
2540
+ let uncompressed_size = cargo_toml_orig_contents. len ( )
2541
+ + main_rs_contents. len ( )
2542
+ + cargo_toml_contents. len ( )
2543
+ + cargo_lock_contents. len ( ) ;
2544
+ let output = p. cargo ( "package" ) . exec_with_output ( ) . unwrap ( ) ;
2545
+
2546
+ assert ! ( p. root( ) . join( "target/package/foo-0.0.1.crate" ) . is_file( ) ) ;
2547
+ p. cargo ( "package -l" )
2548
+ . with_stdout (
2549
+ "\
2550
+ Cargo.lock
2551
+ Cargo.toml
2552
+ Cargo.toml.orig
2553
+ src/main.rs
2554
+ " ,
2555
+ )
2556
+ . run ( ) ;
2557
+ p. cargo ( "package" ) . with_stdout ( "" ) . run ( ) ;
2558
+
2559
+ let f = File :: open ( & p. root ( ) . join ( "target/package/foo-0.0.1.crate" ) ) . unwrap ( ) ;
2560
+ let compressed_size = f. metadata ( ) . unwrap ( ) . len ( ) as usize ;
2561
+ verify_packaged_status_line ( output, 4 , uncompressed_size, compressed_size) ;
2562
+ validate_crate_contents (
2563
+ f,
2564
+ "foo-0.0.1.crate" ,
2565
+ & [ "Cargo.lock" , "Cargo.toml" , "Cargo.toml.orig" , "src/main.rs" ] ,
2566
+ & [
2567
+ ( "Cargo.lock" , cargo_lock_contents) ,
2568
+ ( "Cargo.toml" , & cargo_toml_contents) ,
2569
+ ( "Cargo.toml.orig" , cargo_toml_orig_contents) ,
2570
+ ( "src/main.rs" , main_rs_contents) ,
2571
+ ] ,
2572
+ ) ;
2573
+ }
2574
+
2575
+ #[ cargo_test]
2576
+ fn larger_filesizes ( ) {
2577
+ let cargo_toml_orig_contents = r#"
2578
+ [package]
2579
+ name = "foo"
2580
+ version = "0.0.1"
2581
+ authors = []
2582
+ license = "MIT"
2583
+ description = "foo"
2584
+ "# ;
2585
+ let lots_of_crabs = std:: iter:: repeat ( "🦀" ) . take ( 1337 ) . collect :: < String > ( ) ;
2586
+ let main_rs_contents = format ! ( r#"fn main() {{ println!("{}"); }}"# , lots_of_crabs) ;
2587
+ let bar_txt_contents = "This file is relatively uncompressible, to increase the compressed
2588
+ package size beyond 1KiB.
2589
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
2590
+ ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
2591
+ ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
2592
+ reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur
2593
+ sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
2594
+ laborum." ;
2595
+ let cargo_toml_contents = format ! (
2596
+ r#"{}
2597
+ [package]
2598
+ name = "foo"
2599
+ version = "0.0.1"
2600
+ authors = []
2601
+ description = "foo"
2602
+ license = "MIT"
2603
+ "# ,
2604
+ cargo:: core:: package:: MANIFEST_PREAMBLE
2605
+ ) ;
2606
+ let cargo_lock_contents = r#"# This file is automatically @generated by Cargo.
2607
+ # It is not intended for manual editing.
2608
+ version = 3
2609
+
2610
+ [[package]]
2611
+ name = "foo"
2612
+ version = "0.0.1"
2613
+ "# ;
2614
+ let p = project ( )
2615
+ . file ( "Cargo.toml" , cargo_toml_orig_contents)
2616
+ . file ( "src/main.rs" , & main_rs_contents)
2617
+ . file ( "src/bar.txt" , bar_txt_contents)
2618
+ . build ( ) ;
2619
+
2620
+ let uncompressed_size = cargo_toml_orig_contents. len ( )
2621
+ + main_rs_contents. len ( )
2622
+ + cargo_toml_contents. len ( )
2623
+ + cargo_lock_contents. len ( )
2624
+ + bar_txt_contents. len ( ) ;
2625
+
2626
+ let output = p. cargo ( "package" ) . exec_with_output ( ) . unwrap ( ) ;
2627
+ assert ! ( p. root( ) . join( "target/package/foo-0.0.1.crate" ) . is_file( ) ) ;
2628
+ p. cargo ( "package -l" )
2629
+ . with_stdout (
2630
+ "\
2631
+ Cargo.lock
2632
+ Cargo.toml
2633
+ Cargo.toml.orig
2634
+ src/bar.txt
2635
+ src/main.rs
2636
+ " ,
2637
+ )
2638
+ . run ( ) ;
2639
+ p. cargo ( "package" ) . with_stdout ( "" ) . run ( ) ;
2640
+
2641
+ let f = File :: open ( & p. root ( ) . join ( "target/package/foo-0.0.1.crate" ) ) . unwrap ( ) ;
2642
+ let compressed_size = f. metadata ( ) . unwrap ( ) . len ( ) as usize ;
2643
+ verify_packaged_status_line ( output, 5 , uncompressed_size, compressed_size) ;
2644
+ validate_crate_contents (
2645
+ f,
2646
+ "foo-0.0.1.crate" ,
2647
+ & [
2648
+ "Cargo.lock" ,
2649
+ "Cargo.toml" ,
2650
+ "Cargo.toml.orig" ,
2651
+ "src/bar.txt" ,
2652
+ "src/main.rs" ,
2653
+ ] ,
2654
+ & [
2655
+ ( "Cargo.lock" , cargo_lock_contents) ,
2656
+ ( "Cargo.toml" , & cargo_toml_contents) ,
2657
+ ( "Cargo.toml.orig" , cargo_toml_orig_contents) ,
2658
+ ( "src/bar.txt" , bar_txt_contents) ,
2659
+ ( "src/main.rs" , & main_rs_contents) ,
2660
+ ] ,
2661
+ ) ;
2662
+ }
2663
+
2664
+ #[ cargo_test]
2665
+ fn symlink_filesizes ( ) {
2666
+ if !symlink_supported ( ) {
2667
+ return ;
2668
+ }
2669
+
2670
+ let cargo_toml_orig_contents = r#"
2671
+ [package]
2672
+ name = "foo"
2673
+ version = "0.0.1"
2674
+ authors = []
2675
+ license = "MIT"
2676
+ description = "foo"
2677
+ "# ;
2678
+ let lots_of_crabs = std:: iter:: repeat ( "🦀" ) . take ( 1337 ) . collect :: < String > ( ) ;
2679
+ let main_rs_contents = format ! ( r#"fn main() {{ println!("{}"); }}"# , lots_of_crabs) ;
2680
+ let bar_txt_contents = "This file is relatively uncompressible, to increase the compressed
2681
+ package size beyond 1KiB.
2682
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
2683
+ ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
2684
+ ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
2685
+ reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur
2686
+ sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
2687
+ laborum." ;
2688
+ let cargo_toml_contents = format ! (
2689
+ r#"{}
2690
+ [package]
2691
+ name = "foo"
2692
+ version = "0.0.1"
2693
+ authors = []
2694
+ description = "foo"
2695
+ license = "MIT"
2696
+ "# ,
2697
+ cargo:: core:: package:: MANIFEST_PREAMBLE
2698
+ ) ;
2699
+ let cargo_lock_contents = r#"# This file is automatically @generated by Cargo.
2700
+ # It is not intended for manual editing.
2701
+ version = 3
2702
+
2703
+ [[package]]
2704
+ name = "foo"
2705
+ version = "0.0.1"
2706
+ "# ;
2707
+
2708
+ let p = project ( )
2709
+ . file ( "Cargo.toml" , cargo_toml_orig_contents)
2710
+ . file ( "src/main.rs" , & main_rs_contents)
2711
+ . file ( "bla/bar.txt" , bar_txt_contents)
2712
+ . symlink ( "src/main.rs" , "src/main.rs.bak" )
2713
+ . symlink_dir ( "bla" , "foo" )
2714
+ . build ( ) ;
2715
+
2716
+ let uncompressed_size = cargo_toml_orig_contents. len ( )
2717
+ + main_rs_contents. len ( ) * 2
2718
+ + cargo_toml_contents. len ( )
2719
+ + cargo_lock_contents. len ( )
2720
+ + bar_txt_contents. len ( ) * 2 ;
2721
+
2722
+ let output = p. cargo ( "package" ) . exec_with_output ( ) . unwrap ( ) ;
2723
+ assert ! ( p. root( ) . join( "target/package/foo-0.0.1.crate" ) . is_file( ) ) ;
2724
+ p. cargo ( "package -l" )
2725
+ . with_stdout (
2726
+ "\
2727
+ Cargo.lock
2728
+ Cargo.toml
2729
+ Cargo.toml.orig
2730
+ bla/bar.txt
2731
+ foo/bar.txt
2732
+ src/main.rs
2733
+ src/main.rs.bak
2734
+ " ,
2735
+ )
2736
+ . run ( ) ;
2737
+ p. cargo ( "package" ) . with_stdout ( "" ) . run ( ) ;
2738
+
2739
+ let f = File :: open ( & p. root ( ) . join ( "target/package/foo-0.0.1.crate" ) ) . unwrap ( ) ;
2740
+ let compressed_size = f. metadata ( ) . unwrap ( ) . len ( ) as usize ;
2741
+ verify_packaged_status_line ( output, 7 , uncompressed_size, compressed_size) ;
2742
+ validate_crate_contents (
2743
+ f,
2744
+ "foo-0.0.1.crate" ,
2745
+ & [
2746
+ "Cargo.lock" ,
2747
+ "Cargo.toml" ,
2748
+ "Cargo.toml.orig" ,
2749
+ "bla/bar.txt" ,
2750
+ "foo/bar.txt" ,
2751
+ "src/main.rs" ,
2752
+ "src/main.rs.bak" ,
2753
+ ] ,
2754
+ & [
2755
+ ( "Cargo.lock" , cargo_lock_contents) ,
2756
+ ( "Cargo.toml" , & cargo_toml_contents) ,
2757
+ ( "Cargo.toml.orig" , cargo_toml_orig_contents) ,
2758
+ ( "bla/bar.txt" , bar_txt_contents) ,
2759
+ ( "foo/bar.txt" , bar_txt_contents) ,
2760
+ ( "src/main.rs" , & main_rs_contents) ,
2761
+ ( "src/main.rs.bak" , & main_rs_contents) ,
2762
+ ] ,
2763
+ ) ;
2764
+ }
0 commit comments