Skip to content

Commit dbf009c

Browse files
committed
Support bytes content in _validate_upload
1 parent b407c97 commit dbf009c

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

crates/cargo-test-support/src/publish.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,15 @@ use crate::registry::{self, alt_api_path, FeatureMap};
33
use flate2::read::GzDecoder;
44
use std::collections::{HashMap, HashSet};
55
use std::fs;
6-
use std::fs::File;
7-
use std::io::{self, prelude::*, SeekFrom};
6+
use std::io::prelude::*;
87
use std::path::{Path, PathBuf};
98
use tar::Archive;
109

11-
fn read_le_u32<R>(mut reader: R) -> io::Result<u32>
12-
where
13-
R: Read,
14-
{
15-
let mut buf = [0; 4];
16-
reader.read_exact(&mut buf)?;
17-
Ok(u32::from_le_bytes(buf))
18-
}
19-
2010
/// Checks the result of a crate publish.
2111
pub fn validate_upload(expected_json: &str, expected_crate_name: &str, expected_files: &[&str]) {
2212
let new_path = registry::api_path().join("api/v1/crates/new");
2313
_validate_upload(
24-
&new_path,
14+
&fs::read(&new_path).unwrap(),
2515
expected_json,
2616
expected_crate_name,
2717
expected_files,
@@ -38,7 +28,7 @@ pub fn validate_upload_with_contents(
3828
) {
3929
let new_path = registry::api_path().join("api/v1/crates/new");
4030
_validate_upload(
41-
&new_path,
31+
&fs::read(&new_path).unwrap(),
4232
expected_json,
4333
expected_crate_name,
4434
expected_files,
@@ -54,7 +44,7 @@ pub fn validate_alt_upload(
5444
) {
5545
let new_path = alt_api_path().join("api/v1/crates/new");
5646
_validate_upload(
57-
&new_path,
47+
&fs::read(&new_path).unwrap(),
5848
expected_json,
5949
expected_crate_name,
6050
expected_files,
@@ -63,17 +53,16 @@ pub fn validate_alt_upload(
6353
}
6454

6555
fn _validate_upload(
66-
new_path: &Path,
56+
content: &[u8],
6757
expected_json: &str,
6858
expected_crate_name: &str,
6959
expected_files: &[&str],
7060
expected_contents: &[(&str, &str)],
7161
) {
72-
let mut f = File::open(new_path).unwrap();
7362
// 32-bit little-endian integer of length of JSON data.
74-
let json_sz = read_le_u32(&mut f).expect("read json length");
75-
let mut json_bytes = vec![0; json_sz as usize];
76-
f.read_exact(&mut json_bytes).expect("read JSON data");
63+
let (len, remaining) = content.split_at(4);
64+
let json_len = u32::from_le_bytes(len.try_into().unwrap());
65+
let (json_bytes, remaining) = remaining.split_at(json_len as usize);
7766
let actual_json = serde_json::from_slice(&json_bytes).expect("uploaded JSON should be valid");
7867
let expected_json = serde_json::from_str(expected_json).expect("expected JSON does not parse");
7968

@@ -82,12 +71,12 @@ fn _validate_upload(
8271
}
8372

8473
// 32-bit little-endian integer of length of crate file.
85-
let crate_sz = read_le_u32(&mut f).expect("read crate length");
86-
let mut krate_bytes = vec![0; crate_sz as usize];
87-
f.read_exact(&mut krate_bytes).expect("read crate data");
74+
let (len, remaining) = remaining.split_at(4);
75+
let file_len = u32::from_le_bytes(len.try_into().unwrap());
76+
let (krate_bytes, _remaining) = remaining.split_at(file_len as usize);
77+
8878
// Check at end.
89-
let current = f.seek(SeekFrom::Current(0)).unwrap();
90-
assert_eq!(f.seek(SeekFrom::End(0)).unwrap(), current);
79+
assert_eq!(_remaining.len(), 0);
9180

9281
// Verify the tarball.
9382
validate_crate_contents(

0 commit comments

Comments
 (0)