Skip to content

crates_io_tarball: Use insta snapshots to simplify test code #8786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/crates_io_tarball/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ anyhow = "=1.0.86"
claims = "=0.7.1"
clap = { version = "=4.5.4", features = ["derive", "unicode", "wrap_help"] }
indicatif = { version = "=0.17.8", features = ["rayon"] }
insta = "=1.39.0"
rayon = "=1.10.0"
tracing-subscriber = { version = "=0.3.18", features = ["env-filter"] }
walkdir = "=2.5.0"
59 changes: 28 additions & 31 deletions crates/crates_io_tarball/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ pub fn process_tarball<R: Read>(
#[cfg(test)]
mod tests {
use super::process_tarball;
use crate::{TarballBuilder, TarballError};
use crate::TarballBuilder;
use cargo_manifest::{MaybeInherited, StringOrBool};
use std::path::PathBuf;
use insta::assert_snapshot;

#[test]
fn process_tarball_test() {
Expand All @@ -158,7 +158,8 @@ mod tests {
let tarball_info = assert_ok!(process_tarball("foo-0.0.1", &*tarball, limit));
assert_none!(tarball_info.vcs_info);

assert_err!(process_tarball("bar-0.0.1", &*tarball, limit));
let err = assert_err!(process_tarball("bar-0.0.1", &*tarball, limit));
assert_snapshot!(err, @"invalid path found: foo-0.0.1/Cargo.toml");
}

#[test]
Expand Down Expand Up @@ -292,26 +293,28 @@ mod tests {

#[test]
fn process_tarball_test_incorrect_manifest_casing() {
for file in ["CARGO.TOML", "Cargo.Toml"] {
let manifest = br#"
let manifest = br#"
[package]
name = "foo"
version = "0.0.1"
repository = "https://github.com/foo/bar"
"#;

let limit = 512 * 1024 * 1024;

let process = |file: &str| {
let tarball = TarballBuilder::new()
.add_file(&format!("foo-0.0.1/{file}"), manifest)
.build();

let limit = 512 * 1024 * 1024;
process_tarball("foo-0.0.1", &*tarball, limit)
};

let err = assert_err!(process_tarball("foo-0.0.1", &*tarball, limit));
if let TarballError::IncorrectlyCasedManifest(have) = err {
assert_eq!(have, PathBuf::from(file));
} else {
panic!("expected IncorrectlyCasedManifest, got {err:?} instead");
}
}
let err = assert_err!(process("CARGO.TOML"));
assert_snapshot!(err, @r###"Cargo.toml manifest is incorrectly cased: "CARGO.TOML""###);

let err = assert_err!(process("Cargo.Toml"));
assert_snapshot!(err, @r###"Cargo.toml manifest is incorrectly cased: "Cargo.Toml""###);
}

#[test]
Expand All @@ -323,32 +326,26 @@ mod tests {
repository = "https://github.com/foo/bar"
"#;

for files in [
vec!["cargo.toml", "Cargo.toml"],
vec!["Cargo.toml", "Cargo.Toml"],
vec!["Cargo.toml", "cargo.toml", "CARGO.TOML"],
] {
let limit = 512 * 1024 * 1024;

let process = |files: Vec<&str>| {
let tarball = files
.iter()
.fold(TarballBuilder::new(), |builder, file| {
builder.add_file(&format!("foo-0.0.1/{file}"), manifest)
})
.build();

let limit = 512 * 1024 * 1024;
process_tarball("foo-0.0.1", &*tarball, limit)
};

let err = assert_err!(process_tarball("foo-0.0.1", &*tarball, limit));
if let TarballError::TooManyManifests(have) = err {
let mut want: Vec<_> = files
.into_iter()
.map(|file| PathBuf::from("foo-0.0.1").join(file))
.collect();
want.sort();
let err = assert_err!(process(vec!["cargo.toml", "Cargo.toml"]));
assert_snapshot!(err, @r###"more than one Cargo.toml manifest in tarball: ["foo-0.0.1/Cargo.toml", "foo-0.0.1/cargo.toml"]"###);

assert_eq!(have, want);
} else {
panic!("expected TooManyManifests, got {err:?} instead");
}
}
let err = assert_err!(process(vec!["Cargo.toml", "Cargo.Toml"]));
assert_snapshot!(err, @r###"more than one Cargo.toml manifest in tarball: ["foo-0.0.1/Cargo.Toml", "foo-0.0.1/Cargo.toml"]"###);

let err = assert_err!(process(vec!["Cargo.toml", "cargo.toml", "CARGO.TOML"]));
assert_snapshot!(err, @r###"more than one Cargo.toml manifest in tarball: ["foo-0.0.1/CARGO.TOML", "foo-0.0.1/Cargo.toml", "foo-0.0.1/cargo.toml"]"###);
}
}