Skip to content

Commit

Permalink
add new tests for filesizes
Browse files Browse the repository at this point in the history
  • Loading branch information
antonok-edm committed Oct 29, 2022
1 parent 9333b8f commit 2450035
Showing 1 changed file with 293 additions and 0 deletions.
293 changes: 293 additions & 0 deletions tests/testsuite/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>();
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::<String>();
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),
],
);
}

0 comments on commit 2450035

Please sign in to comment.