From 81f0f63c6a672121d6a21f691ff22a8fcf350892 Mon Sep 17 00:00:00 2001 From: Anton Lazarev Date: Wed, 19 Oct 2022 08:41:50 -0700 Subject: [PATCH 1/4] report crate size on package and publish --- src/cargo/ops/cargo_package.rs | 33 ++++++++++++++++++++++---- src/cargo/sources/git/utils.rs | 11 +++------ src/cargo/util/mod.rs | 43 ++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 12 deletions(-) diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index d3fd85af377..ae5c7558fc4 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -14,7 +14,7 @@ use crate::core::{Package, PackageId, PackageSet, Resolve, SourceId}; use crate::sources::PathSource; use crate::util::errors::CargoResult; use crate::util::toml::TomlManifest; -use crate::util::{self, restricted_names, Config, FileLock}; +use crate::util::{self, human_readable_bytes, restricted_names, Config, FileLock}; use crate::{drop_println, ops}; use anyhow::Context as _; use cargo_util::paths; @@ -110,6 +110,8 @@ pub fn package_one( let ar_files = build_ar_list(ws, pkg, src_files, vcs_info)?; + let filecount = ar_files.len(); + if opts.list { for ar_file in ar_files { drop_println!(config, "{}", ar_file.rel_str); @@ -138,7 +140,7 @@ pub fn package_one( .shell() .status("Packaging", pkg.package_id().to_string())?; dst.file().set_len(0)?; - tar(ws, pkg, ar_files, dst.file(), &filename) + let uncompressed_size = tar(ws, pkg, ar_files, dst.file(), &filename) .with_context(|| "failed to prepare local package for uploading")?; if opts.verify { dst.seek(SeekFrom::Start(0))?; @@ -151,6 +153,22 @@ pub fn package_one( fs::rename(&src_path, &dst_path) .with_context(|| "failed to move temporary tarball into final location")?; + let dst_metadata = dst + .file() + .metadata() + .with_context(|| format!("could not learn metadata for: `{}`", dst_path.display()))?; + let compressed_size = dst_metadata.len(); + + let uncompressed = human_readable_bytes(uncompressed_size); + let compressed = human_readable_bytes(compressed_size); + + let message = format!( + "{} files, {:.1}{} ({:.1}{} compressed)", + filecount, uncompressed.0, uncompressed.1, compressed.0, compressed.1, + ); + // It doesn't really matter if this fails. + drop(config.shell().status("Packaged", message)); + return Ok(Some(dst)); } @@ -567,13 +585,16 @@ fn check_repo_state( } } +/// Compresses and packages a list of [`ArchiveFile`]s and writes into the given file. +/// +/// Returns the uncompressed size of the contents of the new archive file. fn tar( ws: &Workspace<'_>, pkg: &Package, ar_files: Vec, dst: &File, filename: &str, -) -> CargoResult<()> { +) -> CargoResult { // Prepare the encoder and its header. let filename = Path::new(filename); let encoder = GzBuilder::new() @@ -586,6 +607,8 @@ fn tar( let base_name = format!("{}-{}", pkg.name(), pkg.version()); let base_path = Path::new(&base_name); + + let mut uncompressed_size = 0; for ar_file in ar_files { let ArchiveFile { rel_path, @@ -611,6 +634,7 @@ fn tar( .with_context(|| { format!("could not archive source file `{}`", disk_path.display()) })?; + uncompressed_size += metadata.len() as u64; } FileContents::Generated(generated_kind) => { let contents = match generated_kind { @@ -626,13 +650,14 @@ fn tar( header.set_cksum(); ar.append_data(&mut header, &ar_path, contents.as_bytes()) .with_context(|| format!("could not archive source file `{}`", rel_str))?; + uncompressed_size += contents.len() as u64; } } } let encoder = ar.into_inner()?; encoder.finish()?; - Ok(()) + Ok(uncompressed_size) } /// Generate warnings when packaging Cargo.lock, and the resolve have changed. diff --git a/src/cargo/sources/git/utils.rs b/src/cargo/sources/git/utils.rs index 5238fd3c3d6..f5144adb4f4 100644 --- a/src/cargo/sources/git/utils.rs +++ b/src/cargo/sources/git/utils.rs @@ -3,7 +3,7 @@ use crate::core::GitReference; use crate::util::errors::CargoResult; -use crate::util::{network, Config, IntoUrl, MetricsCounter, Progress}; +use crate::util::{human_readable_bytes, network, Config, IntoUrl, MetricsCounter, Progress}; use anyhow::{anyhow, Context as _}; use cargo_util::{paths, ProcessBuilder}; use curl::easy::List; @@ -755,13 +755,8 @@ pub fn with_fetch_options( counter.add(stats.received_bytes(), now); last_update = now; } - fn format_bytes(bytes: f32) -> (&'static str, f32) { - static UNITS: [&str; 5] = ["", "Ki", "Mi", "Gi", "Ti"]; - let i = (bytes.log2() / 10.0).min(4.0) as usize; - (UNITS[i], bytes / 1024_f32.powi(i as i32)) - } - let (unit, rate) = format_bytes(counter.rate()); - format!(", {:.2}{}B/s", rate, unit) + let (rate, unit) = human_readable_bytes(counter.rate() as u64); + format!(", {:.2}{}/s", rate, unit) }; progress .tick(stats.indexed_objects(), stats.total_objects(), &msg) diff --git a/src/cargo/util/mod.rs b/src/cargo/util/mod.rs index 9881332ecaf..8fb24368856 100644 --- a/src/cargo/util/mod.rs +++ b/src/cargo/util/mod.rs @@ -73,6 +73,15 @@ pub fn elapsed(duration: Duration) -> String { } } +/// Formats a number of bytes into a human readable SI-prefixed size. +/// Returns a tuple of `(quantity, units)`. +pub fn human_readable_bytes(bytes: u64) -> (f32, &'static str) { + static UNITS: [&str; 7] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"]; + let bytes = bytes as f32; + let i = ((bytes.log2() / 10.0) as usize).min(UNITS.len() - 1); + (bytes / 1024_f32.powi(i as i32), UNITS[i]) +} + pub fn iter_join_onto(mut w: W, iter: I, delim: &str) -> fmt::Result where W: fmt::Write, @@ -122,3 +131,37 @@ pub fn truncate_with_ellipsis(s: &str, max_width: usize) -> String { } prefix } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_human_readable_bytes() { + assert_eq!(human_readable_bytes(0), (0., "B")); + assert_eq!(human_readable_bytes(8), (8., "B")); + assert_eq!(human_readable_bytes(1000), (1000., "B")); + assert_eq!(human_readable_bytes(1024), (1., "KiB")); + assert_eq!(human_readable_bytes(1024 * 420 + 512), (420.5, "KiB")); + assert_eq!(human_readable_bytes(1024 * 1024), (1., "MiB")); + assert_eq!( + human_readable_bytes(1024 * 1024 + 1024 * 256), + (1.25, "MiB") + ); + assert_eq!(human_readable_bytes(1024 * 1024 * 1024), (1., "GiB")); + assert_eq!( + human_readable_bytes((1024. * 1024. * 1024. * 3.1415) as u64), + (3.1415, "GiB") + ); + assert_eq!(human_readable_bytes(1024 * 1024 * 1024 * 1024), (1., "TiB")); + assert_eq!( + human_readable_bytes(1024 * 1024 * 1024 * 1024 * 1024), + (1., "PiB") + ); + assert_eq!( + human_readable_bytes(1024 * 1024 * 1024 * 1024 * 1024 * 1024), + (1., "EiB") + ); + assert_eq!(human_readable_bytes(u64::MAX), (16., "EiB")); + } +} From d70a4ee93c60a0605450ca8afa410c17e413ef01 Mon Sep 17 00:00:00 2001 From: Anton Lazarev Date: Wed, 19 Oct 2022 11:33:17 -0700 Subject: [PATCH 2/4] update stderr in tests for unrelated functionality --- crates/cargo-test-support/src/compare.rs | 1 + tests/testsuite/artifact_dep.rs | 1 + tests/testsuite/credential_process.rs | 2 ++ tests/testsuite/features_namespaced.rs | 2 ++ tests/testsuite/registry.rs | 1 + tests/testsuite/source_replacement.rs | 1 + tests/testsuite/weak_dep_features.rs | 1 + 7 files changed, 9 insertions(+) diff --git a/crates/cargo-test-support/src/compare.rs b/crates/cargo-test-support/src/compare.rs index 9d33904d430..a9feb5f6fea 100644 --- a/crates/cargo-test-support/src/compare.rs +++ b/crates/cargo-test-support/src/compare.rs @@ -174,6 +174,7 @@ fn substitute_macros(input: &str) -> String { ("[REMOVING]", " Removing"), ("[DOCTEST]", " Doc-tests"), ("[PACKAGING]", " Packaging"), + ("[PACKAGED]", " Packaged"), ("[DOWNLOADING]", " Downloading"), ("[DOWNLOADED]", " Downloaded"), ("[UPLOADING]", " Uploading"), diff --git a/tests/testsuite/artifact_dep.rs b/tests/testsuite/artifact_dep.rs index 0b58842399e..1ff8dafdc8a 100644 --- a/tests/testsuite/artifact_dep.rs +++ b/tests/testsuite/artifact_dep.rs @@ -1919,6 +1919,7 @@ fn publish_artifact_dep() { "\ [UPDATING] [..] [PACKAGING] foo v0.1.0 [..] +[PACKAGED] [..] [UPLOADING] foo v0.1.0 [..] [UPDATING] [..] ", diff --git a/tests/testsuite/credential_process.rs b/tests/testsuite/credential_process.rs index 09e8a32996f..0739e8e2bff 100644 --- a/tests/testsuite/credential_process.rs +++ b/tests/testsuite/credential_process.rs @@ -134,6 +134,7 @@ Only one of these values may be set, remove one or the other to proceed. "\ [UPDATING] [..] [PACKAGING] foo v0.1.0 [..] +[PACKAGED] [..] [UPLOADING] foo v0.1.0 [..] [UPDATING] [..] ", @@ -208,6 +209,7 @@ fn publish() { "\ [UPDATING] [..] [PACKAGING] foo v0.1.0 [..] +[PACKAGED] [..] [UPLOADING] foo v0.1.0 [..] [UPDATING] [..] ", diff --git a/tests/testsuite/features_namespaced.rs b/tests/testsuite/features_namespaced.rs index d5cdca8ddea..72ad6c5cb20 100644 --- a/tests/testsuite/features_namespaced.rs +++ b/tests/testsuite/features_namespaced.rs @@ -902,6 +902,7 @@ fn publish_no_implicit() { "\ [UPDATING] [..] [PACKAGING] foo v0.1.0 [..] +[PACKAGED] [..] [UPLOADING] foo v0.1.0 [..] [UPDATING] [..] ", @@ -1029,6 +1030,7 @@ fn publish() { [VERIFYING] foo v0.1.0 [..] [COMPILING] foo v0.1.0 [..] [FINISHED] [..] +[PACKAGED] [..] [UPLOADING] foo v0.1.0 [..] [UPDATING] [..] ", diff --git a/tests/testsuite/registry.rs b/tests/testsuite/registry.rs index 60c52fd5462..5311c0b8cd7 100644 --- a/tests/testsuite/registry.rs +++ b/tests/testsuite/registry.rs @@ -500,6 +500,7 @@ Caused by: [COMPILING] notyet v0.0.1 [COMPILING] foo v0.0.1 ([CWD][..]) [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s +[PACKAGED] [..] ", ) .run(); diff --git a/tests/testsuite/source_replacement.rs b/tests/testsuite/source_replacement.rs index 9b87bf34c00..ef7cc555c04 100644 --- a/tests/testsuite/source_replacement.rs +++ b/tests/testsuite/source_replacement.rs @@ -218,6 +218,7 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for [COMPILING] bar v1.0.0 [COMPILING] foo v0.0.1 ([..]foo-0.0.1) [FINISHED] dev [..] +[PACKAGED] [..] [UPLOADING] foo v0.0.1 ([..]) [UPDATING] crates.io index ", diff --git a/tests/testsuite/weak_dep_features.rs b/tests/testsuite/weak_dep_features.rs index 30f5de86081..4e3cd8dcb63 100644 --- a/tests/testsuite/weak_dep_features.rs +++ b/tests/testsuite/weak_dep_features.rs @@ -568,6 +568,7 @@ fn publish() { [VERIFYING] foo v0.1.0 [..] [COMPILING] foo v0.1.0 [..] [FINISHED] [..] +[PACKAGED] [..] [UPLOADING] foo v0.1.0 [..] [UPDATING] [..] ", From 9333b8f5ba9f3aef909e7d4c807bdacddbcca1c1 Mon Sep 17 00:00:00 2001 From: Anton Lazarev Date: Thu, 20 Oct 2022 20:05:34 -0700 Subject: [PATCH 3/4] update package, publish, and publish_lockfile tests --- tests/testsuite/cross_publish.rs | 2 ++ tests/testsuite/package.rs | 18 +++++++++++++++++ tests/testsuite/publish.rs | 31 +++++++++++++++++++++++++++++ tests/testsuite/publish_lockfile.rs | 14 ++++++++++++- 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/tests/testsuite/cross_publish.rs b/tests/testsuite/cross_publish.rs index 57bb29c1041..5b1416ef072 100644 --- a/tests/testsuite/cross_publish.rs +++ b/tests/testsuite/cross_publish.rs @@ -46,6 +46,7 @@ fn simple_cross_package() { [VERIFYING] foo v0.0.0 ([CWD]) [COMPILING] foo v0.0.0 ([CWD]/target/package/foo-0.0.0) [FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] 4 files, [..] ([..] compressed) ", ) .run(); @@ -109,6 +110,7 @@ fn publish_with_target() { [VERIFYING] foo v0.0.0 ([CWD]) [COMPILING] foo v0.0.0 ([CWD]/target/package/foo-0.0.0) [FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] [..] [UPLOADING] foo v0.0.0 ([CWD]) [UPDATING] crates.io index ", diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 5ebde46358e..3e7b5b7153f 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -39,6 +39,7 @@ See [..] [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 ([CWD][..]) [FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] 4 files, [..] ([..] compressed) ", ) .run(); @@ -77,6 +78,7 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 ([CWD][..]) [FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] [..] files, [..] ([..] compressed) ", ) .run(); @@ -103,6 +105,7 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 ([CWD][..]) [FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] [..] files, [..] ([..] compressed) ", ) .run(); @@ -129,6 +132,7 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 ([CWD][..]) [FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] [..] files, [..] ([..] compressed) ", ) .run(); @@ -158,6 +162,7 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for [ARCHIVING] Cargo.toml [ARCHIVING] Cargo.toml.orig [ARCHIVING] src/main.rs +[PACKAGED] 5 files, [..] ([..] compressed) ", ) .run(); @@ -198,6 +203,7 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for [ARCHIVING] Cargo.toml [ARCHIVING] Cargo.toml.orig [ARCHIVING] src/lib.rs +[PACKAGED] 4 files, [..] ([..] compressed) ", ) .run(); @@ -239,6 +245,7 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 ([CWD][..]) [FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] [..] files, [..] ([..] compressed) ", ) .run(); @@ -488,6 +495,7 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for [ARCHIVING] some_dir/file_deep_4 [ARCHIVING] some_dir/file_deep_5 [ARCHIVING] src/main.rs +[PACKAGED] 15 files, [..] ([..] compressed) ", ) .run(); @@ -555,6 +563,7 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for [ARCHIVING] Cargo.toml.orig [ARCHIVING] foo.txt [ARCHIVING] src/main.rs +[PACKAGED] 7 files, [..] ([..] compressed) ", ) .run(); @@ -708,6 +717,7 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 ([CWD][..]) [FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] 4 files, [..] ([..] compressed) ", ) .run(); @@ -775,6 +785,7 @@ See [..] [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 ([CWD][..]) [FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] 5 files, [..] ([..] compressed) ", ) .run(); @@ -1788,6 +1799,7 @@ fn invalid_license_file_path() { Please update the license-file setting in the manifest at `[..]/foo/Cargo.toml` This may become a hard error in the future. [PACKAGING] foo v1.0.0 ([..]/foo) +[PACKAGED] [..] files, [..] ([..] compressed) ", ) .run(); @@ -1835,6 +1847,7 @@ subdir/LICENSE [ARCHIVING] Cargo.toml.orig [ARCHIVING] src/lib.rs [ARCHIVING] subdir/LICENSE +[PACKAGED] 5 files, [..] ([..] compressed) ", ) .run(); @@ -1891,6 +1904,7 @@ src/lib.rs [VERIFYING] foo v1.0.0 [..] [COMPILING] foo v1.0.0 [..] [FINISHED] [..] +[PACKAGED] 4 files, [..] ([..] compressed) ", ) .run(); @@ -1949,6 +1963,7 @@ src/lib.rs [VERIFYING] foo v1.0.0 [..] [COMPILING] foo v1.0.0 [..] [FINISHED] [..] +[PACKAGED] 4 files, [..] ([..] compressed) ", ) .run(); @@ -1994,6 +2009,7 @@ fn package_restricted_windows() { [PACKAGING] foo [..] [VERIFYING] foo [..] [COMPILING] foo [..] +[PACKAGED] [..] files, [..] ([..] compressed) [FINISHED] [..] ", ) @@ -2367,12 +2383,14 @@ See [..] [VERIFYING] bar v0.0.1 ([CWD]/bar) [COMPILING] bar v0.0.1 ([CWD][..]) [FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] [..] files, [..] ([..] compressed) [WARNING] manifest has no documentation, [..] See [..] [PACKAGING] foo v0.0.1 ([CWD]) [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 ([CWD][..]) [FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] [..] files, [..] ([..] compressed) ", ) .run(); diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index 70177383a05..8d4768a2eed 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -122,6 +122,7 @@ fn simple() { [WARNING] manifest has no documentation, [..] See [..] [PACKAGING] foo v0.0.1 ([CWD]) +[PACKAGED] [..] files, [..] ([..] compressed) [UPLOADING] foo v0.0.1 ([CWD]) [UPDATING] [..] ", @@ -176,6 +177,7 @@ fn old_token_location() { [WARNING] manifest has no documentation, [..] See [..] [PACKAGING] foo v0.0.1 ([CWD]) +[PACKAGED] [..] files, [..] ([..] compressed) [UPLOADING] foo v0.0.1 ([CWD]) [UPDATING] [..] ", @@ -217,6 +219,7 @@ fn simple_with_index() { [..] [..] [..] +[..] [UPLOADING] foo v0.0.1 ([CWD]) [UPDATING] [..] ", @@ -414,6 +417,7 @@ fn publish_clean() { [VERIFYING] foo v0.0.1 ([CWD]) [..] [..] +[..] [UPLOADING] foo v0.0.1 ([CWD]) [UPDATING] [..] ", @@ -459,6 +463,7 @@ fn publish_in_sub_repo() { [VERIFYING] foo v0.0.1 ([CWD]) [..] [..] +[..] [UPLOADING] foo v0.0.1 ([CWD]) [UPDATING] [..] ", @@ -504,6 +509,7 @@ fn publish_when_ignored() { [VERIFYING] foo v0.0.1 ([CWD]) [..] [..] +[..] [UPLOADING] foo v0.0.1 ([CWD]) [UPDATING] [..] ", @@ -548,6 +554,7 @@ fn ignore_when_crate_ignored() { [VERIFYING] foo v0.0.1 ([CWD]) [..] [..] +[..] [UPLOADING] foo v0.0.1 ([CWD]) [UPDATING] [..] ", @@ -622,6 +629,7 @@ See [..] [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 [..] [FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] [..] files, [..] ([..] compressed) [UPLOADING] foo v0.0.1 ([CWD]) [WARNING] aborting upload due to dry run ", @@ -739,6 +747,7 @@ fn publish_allowed_registry() { [VERIFYING] foo v0.0.1 ([CWD]) [..] [..] +[..] [UPLOADING] foo v0.0.1 ([CWD]) [UPDATING] `alternative` index ", @@ -803,6 +812,7 @@ fn publish_implicitly_to_only_allowed_registry() { [VERIFYING] foo v0.0.1 ([CWD]) [..] [..] +[..] [UPLOADING] foo v0.0.1 ([CWD]) [UPDATING] `alternative` index ", @@ -928,6 +938,7 @@ The registry `alternative` is not listed in the `package.publish` value in Cargo [VERIFYING] foo v0.0.1 ([CWD]) [..] [..] +[..] [UPLOADING] foo v0.0.1 ([CWD]) [UPDATING] crates.io index ", @@ -975,6 +986,7 @@ fn publish_with_select_features() { [VERIFYING] foo v0.0.1 ([CWD]) [..] [..] +[..] [UPLOADING] foo v0.0.1 ([CWD]) [UPDATING] crates.io index ", @@ -1022,6 +1034,7 @@ fn publish_with_all_features() { [VERIFYING] foo v0.0.1 ([CWD]) [..] [..] +[..] [UPLOADING] foo v0.0.1 ([CWD]) [UPDATING] crates.io index ", @@ -1132,6 +1145,7 @@ fn publish_with_patch() { [..] [..] [..] +[..] [UPLOADING] foo v0.0.1 ([CWD]) [UPDATING] crates.io index ", @@ -1219,6 +1233,7 @@ fn publish_checks_for_token_before_verify() { [VERIFYING] foo v0.0.1 ([CWD]) [..] [..] +[..] [UPLOADING] foo v0.0.1 [..] [WARNING] aborting upload due to dry run ", @@ -1338,6 +1353,7 @@ fn publish_git_with_version() { [..] [..] [..] +[..] [UPLOADING] foo v0.1.0 ([CWD]) [UPDATING] crates.io index ", @@ -1466,6 +1482,7 @@ fn publish_dev_dep_no_version() { "\ [UPDATING] [..] [PACKAGING] foo v0.1.0 [..] +[PACKAGED] [..] files, [..] ([..] compressed) [UPLOADING] foo v0.1.0 [..] [UPDATING] crates.io index ", @@ -1550,6 +1567,7 @@ fn credentials_ambiguous_filename() { [..] [..] [..] +[..] [UPLOADING] foo v0.0.1 [..] [UPDATING] crates.io index ", @@ -1653,6 +1671,7 @@ fn publish_with_missing_readme() { "\ [UPDATING] [..] [PACKAGING] foo v0.1.0 [..] +[PACKAGED] [..] files, [..] ([..] compressed) [UPLOADING] foo v0.1.0 [..] [ERROR] failed to read `readme` file for package `foo v0.1.0 ([ROOT]/foo)` @@ -1704,6 +1723,7 @@ fn api_error_json() { "\ [UPDATING] [..] [PACKAGING] foo v0.0.1 [..] +[PACKAGED] [..] files, [..] ([..] compressed) [UPLOADING] foo v0.0.1 [..] [ERROR] failed to publish to registry at http://127.0.0.1:[..]/ @@ -1751,6 +1771,7 @@ fn api_error_200() { "\ [UPDATING] [..] [PACKAGING] foo v0.0.1 [..] +[PACKAGED] [..] files, [..] ([..] compressed) [UPLOADING] foo v0.0.1 [..] [ERROR] failed to publish to registry at http://127.0.0.1:[..]/ @@ -1798,6 +1819,7 @@ fn api_error_code() { "\ [UPDATING] [..] [PACKAGING] foo v0.0.1 [..] +[PACKAGED] [..] files, [..] ([..] compressed) [UPLOADING] foo v0.0.1 [..] [ERROR] failed to publish to registry at http://127.0.0.1:[..]/ @@ -1853,6 +1875,7 @@ fn api_curl_error() { "\ [UPDATING] [..] [PACKAGING] foo v0.0.1 [..] +[PACKAGED] [..] files, [..] ([..] compressed) [UPLOADING] foo v0.0.1 [..] [ERROR] failed to publish to registry at http://127.0.0.1:[..]/ @@ -1900,6 +1923,7 @@ fn api_other_error() { "\ [UPDATING] [..] [PACKAGING] foo v0.0.1 [..] +[PACKAGED] [..] files, [..] ([..] compressed) [UPLOADING] foo v0.0.1 [..] [ERROR] failed to publish to registry at http://127.0.0.1:[..]/ @@ -1959,6 +1983,7 @@ fn in_package_workspace() { [WARNING] manifest has no documentation, homepage or repository. See [..] [PACKAGING] li v0.0.1 ([CWD]/li) +[PACKAGED] [..] files, [..] ([..] compressed) [UPLOADING] li v0.0.1 ([CWD]/li) [UPDATING] crates.io index ", @@ -2065,6 +2090,7 @@ fn in_package_workspace_with_members_with_features_old() { [WARNING] manifest has no documentation, homepage or repository. See [..] [PACKAGING] li v0.0.1 ([CWD]/li) +[PACKAGED] [..] files, [..] ([..] compressed) [UPLOADING] li v0.0.1 ([CWD]/li) [UPDATING] crates.io index ", @@ -2156,6 +2182,7 @@ fn in_virtual_workspace_with_p() { [WARNING] manifest has no documentation, homepage or repository. See [..] [PACKAGING] li v0.0.1 ([CWD]/li) +[PACKAGED] [..] files, [..] ([..] compressed) [UPLOADING] li v0.0.1 ([CWD]/li) [UPDATING] crates.io index ", @@ -2342,6 +2369,7 @@ fn http_api_not_noop() { [VERIFYING] foo v0.0.1 ([CWD]) [..] [..] +[..] [UPLOADING] foo v0.0.1 ([CWD]) [UPDATING] [..] ", @@ -2423,6 +2451,7 @@ fn wait_for_publish() { [WARNING] manifest has no documentation, [..] See [..] [PACKAGING] delay v0.0.1 ([CWD]) +[PACKAGED] [..] files, [..] ([..] compressed) [UPLOADING] delay v0.0.1 ([CWD]) [UPDATING] crates.io index [WAITING] on `delay` to propagate to crates.io index (ctrl-c to wait asynchronously) @@ -2513,6 +2542,7 @@ fn wait_for_publish_underscore() { [WARNING] manifest has no documentation, [..] See [..] [PACKAGING] delay_with_underscore v0.0.1 ([CWD]) +[PACKAGED] [..] files, [..] ([..] compressed) [UPLOADING] delay_with_underscore v0.0.1 ([CWD]) [UPDATING] crates.io index [WAITING] on `delay_with_underscore` to propagate to crates.io index (ctrl-c to wait asynchronously) @@ -2583,6 +2613,7 @@ fn skip_wait_for_publish() { [WARNING] manifest has no documentation, [..] See [..] [PACKAGING] foo v0.0.1 ([CWD]) +[PACKAGED] [..] files, [..] ([..] compressed) [UPLOADING] foo v0.0.1 ([CWD]) ", ) diff --git a/tests/testsuite/publish_lockfile.rs b/tests/testsuite/publish_lockfile.rs index 56ece3e645f..96d24d40f50 100644 --- a/tests/testsuite/publish_lockfile.rs +++ b/tests/testsuite/publish_lockfile.rs @@ -77,6 +77,7 @@ fn package_lockfile() { [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 ([CWD][..]) [FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] [..] files, [..] ([..] compressed) ", ) .run(); @@ -135,6 +136,7 @@ src/main.rs [COMPILING] foo v0.0.1 ([..]) [RUNNING] `rustc --crate-name foo src/main.rs [..] [FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] 5 files, [..] ([..] compressed) ", ) .run(); @@ -232,6 +234,7 @@ fn note_resolve_changes() { [UPDATING] `[..]` index [NOTE] package `multi v0.1.0` added to the packaged Cargo.lock file, was originally sourced from `[..]/foo/multi` [NOTE] package `patched v1.0.0` added to the packaged Cargo.lock file, was originally sourced from `[..]/foo/patched` +[PACKAGED] [..] files, [..] ([..] compressed) ", ) .run(); @@ -251,7 +254,12 @@ fn outdated_lock_version_change_does_not_warn() { p.change_file("Cargo.toml", &pl_manifest("foo", "0.2.0", "")); p.cargo("package --no-verify") - .with_stderr("[PACKAGING] foo v0.2.0 ([..])") + .with_stderr( + "\ +[PACKAGING] foo v0.2.0 ([..]) +[PACKAGED] [..] files, [..] ([..] compressed) +", + ) .run(); } @@ -301,6 +309,7 @@ fn no_warn_workspace_extras() { "\ [PACKAGING] a v0.1.0 ([..]) [UPDATING] `[..]` index +[PACKAGED] [..] files, [..] ([..] compressed) ", ) .run(); @@ -334,6 +343,7 @@ fn warn_package_with_yanked() { [UPDATING] `[..]` index [WARNING] package `bar v0.1.0` in Cargo.lock is yanked in registry \ `crates-io`, consider updating to a version that is not yanked +[PACKAGED] [..] files, [..] ([..] compressed) ", ) .run(); @@ -450,6 +460,7 @@ src/main.rs [COMPILING] foo v0.0.1 ([..]) [RUNNING] `rustc --crate-name foo src/main.rs [..] [FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] 5 files, [..] ([..] compressed) ", ) .run(); @@ -476,6 +487,7 @@ fn ignore_lockfile_inner() { [ARCHIVING] Cargo.toml [ARCHIVING] Cargo.toml.orig [ARCHIVING] src/main.rs +[PACKAGED] 6 files, [..] ([..] compressed) ", ) .run(); From 24500354bfaac1343e01803f3e66390d5c3c61c4 Mon Sep 17 00:00:00 2001 From: Anton Lazarev Date: Thu, 20 Oct 2022 22:52:15 -0700 Subject: [PATCH 4/4] add new tests for filesizes --- tests/testsuite/package.rs | 293 +++++++++++++++++++++++++++++++++++++ 1 file changed, 293 insertions(+) diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 3e7b5b7153f..d2e4c9ab72e 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -2469,3 +2469,296 @@ version = "0.1.0" &[("Cargo.toml", &rewritten_toml)], ); } + +fn verify_packaged_status_line( + output: std::process::Output, + num_files: usize, + uncompressed_size: u64, + compressed_size: u64, +) { + use cargo::util::human_readable_bytes; + + let stderr = String::from_utf8(output.stderr).unwrap(); + let mut packaged_lines = stderr + .lines() + .filter(|line| line.trim().starts_with("Packaged")); + let packaged_line = packaged_lines + .next() + .expect("`Packaged` status line should appear in stderr"); + assert!( + packaged_lines.next().is_none(), + "Only one `Packaged` status line should appear in stderr" + ); + let size_info = packaged_line.trim().trim_start_matches("Packaged").trim(); + let uncompressed = human_readable_bytes(uncompressed_size); + let compressed = human_readable_bytes(compressed_size); + let expected = format!( + "{} files, {:.1}{} ({:.1}{} compressed)", + num_files, uncompressed.0, uncompressed.1, compressed.0, compressed.1 + ); + assert_eq!(size_info, expected); +} + +#[cargo_test] +fn basic_filesizes() { + let cargo_toml_orig_contents = r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + exclude = ["*.txt"] + license = "MIT" + description = "foo" + "#; + let main_rs_contents = r#"fn main() { println!("🦀"); }"#; + let cargo_toml_contents = format!( + r#"{} +[package] +name = "foo" +version = "0.0.1" +authors = [] +exclude = ["*.txt"] +description = "foo" +license = "MIT" +"#, + cargo::core::package::MANIFEST_PREAMBLE + ); + let cargo_lock_contents = r#"# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "foo" +version = "0.0.1" +"#; + let p = project() + .file("Cargo.toml", cargo_toml_orig_contents) + .file("src/main.rs", main_rs_contents) + .file("src/bar.txt", "Ignored text file contents") // should be ignored when packaging + .build(); + + let uncompressed_size = (cargo_toml_orig_contents.len() + + main_rs_contents.len() + + cargo_toml_contents.len() + + cargo_lock_contents.len()) as u64; + let output = p.cargo("package").exec_with_output().unwrap(); + + assert!(p.root().join("target/package/foo-0.0.1.crate").is_file()); + p.cargo("package -l") + .with_stdout( + "\ +Cargo.lock +Cargo.toml +Cargo.toml.orig +src/main.rs +", + ) + .run(); + p.cargo("package").with_stdout("").run(); + + let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); + let compressed_size = f.metadata().unwrap().len(); + verify_packaged_status_line(output, 4, uncompressed_size, compressed_size); + validate_crate_contents( + f, + "foo-0.0.1.crate", + &["Cargo.lock", "Cargo.toml", "Cargo.toml.orig", "src/main.rs"], + &[ + ("Cargo.lock", cargo_lock_contents), + ("Cargo.toml", &cargo_toml_contents), + ("Cargo.toml.orig", cargo_toml_orig_contents), + ("src/main.rs", main_rs_contents), + ], + ); +} + +#[cargo_test] +fn larger_filesizes() { + let cargo_toml_orig_contents = r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + license = "MIT" + description = "foo" + "#; + let lots_of_crabs = std::iter::repeat("🦀").take(1337).collect::(); + let main_rs_contents = format!(r#"fn main() {{ println!("{}"); }}"#, lots_of_crabs); + let bar_txt_contents = "This file is relatively uncompressible, to increase the compressed + package size beyond 1KiB. + Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt + ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation + ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in + reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur + sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est + laborum."; + let cargo_toml_contents = format!( + r#"{} +[package] +name = "foo" +version = "0.0.1" +authors = [] +description = "foo" +license = "MIT" +"#, + cargo::core::package::MANIFEST_PREAMBLE + ); + let cargo_lock_contents = r#"# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "foo" +version = "0.0.1" +"#; + let p = project() + .file("Cargo.toml", cargo_toml_orig_contents) + .file("src/main.rs", &main_rs_contents) + .file("src/bar.txt", bar_txt_contents) + .build(); + + let uncompressed_size = (cargo_toml_orig_contents.len() + + main_rs_contents.len() + + cargo_toml_contents.len() + + cargo_lock_contents.len() + + bar_txt_contents.len()) as u64; + + let output = p.cargo("package").exec_with_output().unwrap(); + assert!(p.root().join("target/package/foo-0.0.1.crate").is_file()); + p.cargo("package -l") + .with_stdout( + "\ +Cargo.lock +Cargo.toml +Cargo.toml.orig +src/bar.txt +src/main.rs +", + ) + .run(); + p.cargo("package").with_stdout("").run(); + + let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); + let compressed_size = f.metadata().unwrap().len(); + verify_packaged_status_line(output, 5, uncompressed_size, compressed_size); + validate_crate_contents( + f, + "foo-0.0.1.crate", + &[ + "Cargo.lock", + "Cargo.toml", + "Cargo.toml.orig", + "src/bar.txt", + "src/main.rs", + ], + &[ + ("Cargo.lock", cargo_lock_contents), + ("Cargo.toml", &cargo_toml_contents), + ("Cargo.toml.orig", cargo_toml_orig_contents), + ("src/bar.txt", bar_txt_contents), + ("src/main.rs", &main_rs_contents), + ], + ); +} + +#[cargo_test] +fn symlink_filesizes() { + if !symlink_supported() { + return; + } + + let cargo_toml_orig_contents = r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + license = "MIT" + description = "foo" + "#; + let lots_of_crabs = std::iter::repeat("🦀").take(1337).collect::(); + let main_rs_contents = format!(r#"fn main() {{ println!("{}"); }}"#, lots_of_crabs); + let bar_txt_contents = "This file is relatively uncompressible, to increase the compressed + package size beyond 1KiB. + Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt + ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation + ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in + reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur + sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est + laborum."; + let cargo_toml_contents = format!( + r#"{} +[package] +name = "foo" +version = "0.0.1" +authors = [] +description = "foo" +license = "MIT" +"#, + cargo::core::package::MANIFEST_PREAMBLE + ); + let cargo_lock_contents = r#"# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "foo" +version = "0.0.1" +"#; + + let p = project() + .file("Cargo.toml", cargo_toml_orig_contents) + .file("src/main.rs", &main_rs_contents) + .file("bla/bar.txt", bar_txt_contents) + .symlink("src/main.rs", "src/main.rs.bak") + .symlink_dir("bla", "foo") + .build(); + + let uncompressed_size = (cargo_toml_orig_contents.len() + + main_rs_contents.len() * 2 + + cargo_toml_contents.len() + + cargo_lock_contents.len() + + bar_txt_contents.len() * 2) as u64; + + let output = p.cargo("package").exec_with_output().unwrap(); + assert!(p.root().join("target/package/foo-0.0.1.crate").is_file()); + p.cargo("package -l") + .with_stdout( + "\ +Cargo.lock +Cargo.toml +Cargo.toml.orig +bla/bar.txt +foo/bar.txt +src/main.rs +src/main.rs.bak +", + ) + .run(); + p.cargo("package").with_stdout("").run(); + + let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); + let compressed_size = f.metadata().unwrap().len(); + verify_packaged_status_line(output, 7, uncompressed_size, compressed_size); + validate_crate_contents( + f, + "foo-0.0.1.crate", + &[ + "Cargo.lock", + "Cargo.toml", + "Cargo.toml.orig", + "bla/bar.txt", + "foo/bar.txt", + "src/main.rs", + "src/main.rs.bak", + ], + &[ + ("Cargo.lock", cargo_lock_contents), + ("Cargo.toml", &cargo_toml_contents), + ("Cargo.toml.orig", cargo_toml_orig_contents), + ("bla/bar.txt", bar_txt_contents), + ("foo/bar.txt", bar_txt_contents), + ("src/main.rs", &main_rs_contents), + ("src/main.rs.bak", &main_rs_contents), + ], + ); +}