Skip to content

tests/builders/publish: Inline files_with_io() and simplify files() fn #6892

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 4 commits into from
Jul 27, 2023
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
27 changes: 5 additions & 22 deletions src/tests/builders/publish.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crates_io::views::krate_publish as u;
use std::{collections::BTreeMap, io::Read};
use std::collections::BTreeMap;

use crates_io_tarball::TarballBuilder;
use flate2::{write::GzEncoder, Compression};
Expand Down Expand Up @@ -51,31 +51,14 @@ impl PublishBuilder {
}

/// Set the files in the crate's tarball.
pub fn files(self, files: &[(&str, &[u8])]) -> Self {
let mut slices = files.iter().map(|p| p.1).collect::<Vec<_>>();
let mut files = files
.iter()
.zip(&mut slices)
.map(|(&(name, _), data)| {
let len = data.len() as u64;
(name, data as &mut dyn Read, len)
})
.collect::<Vec<_>>();

self.files_with_io(&mut files)
}

/// Set the tarball from a Read trait object
pub fn files_with_io(mut self, files: &mut [(&str, &mut dyn Read, u64)]) -> Self {
pub fn files(mut self, files: &[(&str, &[u8])]) -> Self {
let mut tarball = Vec::new();
{
let mut ar = tar::Builder::new(GzEncoder::new(&mut tarball, Compression::default()));
for &mut (name, ref mut data, size) in files {
for (name, data) in files {
let mut header = tar::Header::new_gnu();
assert_ok!(header.set_path(name));
header.set_size(size);
header.set_cksum();
assert_ok!(ar.append(&header, data));
header.set_size(data.len() as u64);
assert_ok!(ar.append_data(&mut header, name, *data));
}
assert_ok!(ar.finish());
}
Expand Down
5 changes: 3 additions & 2 deletions src/tests/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,12 @@ fn new_krate_gzip_bomb() {
let (app, _, _, token) = TestApp::full().with_token();

let len = 512 * 1024;
let mut body = io::repeat(0).take(len);
let mut body = Vec::new();
io::repeat(0).take(len).read_to_end(&mut body).unwrap();

let crate_to_publish = PublishBuilder::new("foo")
.version("1.1.0")
.files_with_io(&mut [("foo-1.1.0/a", &mut body, len)]);
.files(&[("foo-1.1.0/a", &body)]);

let response = token.publish_crate(crate_to_publish);
assert_eq!(response.status(), StatusCode::OK);
Expand Down