Skip to content

Commit

Permalink
Support bytes content in _validate_upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Rustin170506 committed Feb 17, 2023
1 parent 1e39ba7 commit af6975a
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions crates/cargo-test-support/src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,15 @@ use crate::registry::{self, alt_api_path, FeatureMap};
use flate2::read::GzDecoder;
use std::collections::{HashMap, HashSet};
use std::fs;
use std::fs::File;
use std::io::{self, prelude::*, SeekFrom};
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use tar::Archive;

fn read_le_u32<R>(mut reader: R) -> io::Result<u32>
where
R: Read,
{
let mut buf = [0; 4];
reader.read_exact(&mut buf)?;
Ok(u32::from_le_bytes(buf))
}

/// Checks the result of a crate publish.
pub fn validate_upload(expected_json: &str, expected_crate_name: &str, expected_files: &[&str]) {
let new_path = registry::api_path().join("api/v1/crates/new");
_validate_upload(
&new_path,
&fs::read(&new_path).unwrap(),
expected_json,
expected_crate_name,
expected_files,
Expand All @@ -38,7 +28,7 @@ pub fn validate_upload_with_contents(
) {
let new_path = registry::api_path().join("api/v1/crates/new");
_validate_upload(
&new_path,
&fs::read(&new_path).unwrap(),
expected_json,
expected_crate_name,
expected_files,
Expand All @@ -54,7 +44,7 @@ pub fn validate_alt_upload(
) {
let new_path = alt_api_path().join("api/v1/crates/new");
_validate_upload(
&new_path,
&fs::read(&new_path).unwrap(),
expected_json,
expected_crate_name,
expected_files,
Expand All @@ -63,17 +53,16 @@ pub fn validate_alt_upload(
}

fn _validate_upload(
new_path: &Path,
content: &[u8],
expected_json: &str,
expected_crate_name: &str,
expected_files: &[&str],
expected_contents: &[(&str, &str)],
) {
let mut f = File::open(new_path).unwrap();
// 32-bit little-endian integer of length of JSON data.
let json_sz = read_le_u32(&mut f).expect("read json length");
let mut json_bytes = vec![0; json_sz as usize];
f.read_exact(&mut json_bytes).expect("read JSON data");
let (len, remaining) = content.split_at(4);
let json_len = u32::from_le_bytes(len.try_into().unwrap());
let (json_bytes, remaining) = remaining.split_at(json_len as usize);
let actual_json = serde_json::from_slice(&json_bytes).expect("uploaded JSON should be valid");
let expected_json = serde_json::from_str(expected_json).expect("expected JSON does not parse");

Expand All @@ -82,12 +71,12 @@ fn _validate_upload(
}

// 32-bit little-endian integer of length of crate file.
let crate_sz = read_le_u32(&mut f).expect("read crate length");
let mut krate_bytes = vec![0; crate_sz as usize];
f.read_exact(&mut krate_bytes).expect("read crate data");
let (len, remaining) = remaining.split_at(4);
let file_len = u32::from_le_bytes(len.try_into().unwrap());
let (krate_bytes, _remaining) = remaining.split_at(file_len as usize);

// Check at end.
let current = f.seek(SeekFrom::Current(0)).unwrap();
assert_eq!(f.seek(SeekFrom::End(0)).unwrap(), current);
assert_eq!(_remaining.len(), 0);

// Verify the tarball.
validate_crate_contents(
Expand Down

0 comments on commit af6975a

Please sign in to comment.