Skip to content

Commit 5455998

Browse files
committed
address review comments
1 parent f535b67 commit 5455998

File tree

4 files changed

+206
-119
lines changed

4 files changed

+206
-119
lines changed

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

+79
Original file line numberDiff line numberDiff line change
@@ -1582,3 +1582,82 @@ where
15821582
let thread = std::thread::spawn(|| f());
15831583
thread_wait_timeout(n, thread)
15841584
}
1585+
1586+
// Helper for testing dep-info files in the fingerprint dir.
1587+
#[track_caller]
1588+
pub fn assert_deps(project: &Project, fingerprint: &str, test_cb: impl Fn(&Path, &[(u8, &str)])) {
1589+
let mut files = project
1590+
.glob(fingerprint)
1591+
.map(|f| f.expect("unwrap glob result"))
1592+
// Filter out `.json` entries.
1593+
.filter(|f| f.extension().is_none());
1594+
let info_path = files
1595+
.next()
1596+
.unwrap_or_else(|| panic!("expected 1 dep-info file at {}, found 0", fingerprint));
1597+
assert!(files.next().is_none(), "expected only 1 dep-info file");
1598+
let dep_info = fs::read(&info_path).unwrap();
1599+
let dep_info = &mut &dep_info[..];
1600+
let deps = (0..read_usize(dep_info))
1601+
.map(|_| {
1602+
let ty = read_u8(dep_info);
1603+
let path = std::str::from_utf8(read_bytes(dep_info)).unwrap();
1604+
let checksum_present = read_bool(dep_info);
1605+
if checksum_present {
1606+
// Read out the checksum info without using it
1607+
let _file_len = read_u64(dep_info);
1608+
let _checksum = read_bytes(dep_info);
1609+
}
1610+
(ty, path)
1611+
})
1612+
.collect::<Vec<_>>();
1613+
test_cb(&info_path, &deps);
1614+
1615+
fn read_usize(bytes: &mut &[u8]) -> usize {
1616+
let ret = &bytes[..4];
1617+
*bytes = &bytes[4..];
1618+
1619+
u32::from_le_bytes(ret.try_into().unwrap()) as usize
1620+
}
1621+
1622+
fn read_u8(bytes: &mut &[u8]) -> u8 {
1623+
let ret = bytes[0];
1624+
*bytes = &bytes[1..];
1625+
ret
1626+
}
1627+
1628+
fn read_bool(bytes: &mut &[u8]) -> bool {
1629+
read_u8(bytes) != 0
1630+
}
1631+
1632+
fn read_u64(bytes: &mut &[u8]) -> u64 {
1633+
let ret = &bytes[..8];
1634+
*bytes = &bytes[8..];
1635+
1636+
u64::from_le_bytes(ret.try_into().unwrap())
1637+
}
1638+
1639+
fn read_bytes<'a>(bytes: &mut &'a [u8]) -> &'a [u8] {
1640+
let n = read_usize(bytes);
1641+
let ret = &bytes[..n];
1642+
*bytes = &bytes[n..];
1643+
ret
1644+
}
1645+
}
1646+
1647+
pub fn assert_deps_contains(project: &Project, fingerprint: &str, expected: &[(u8, &str)]) {
1648+
assert_deps(project, fingerprint, |info_path, entries| {
1649+
for (e_kind, e_path) in expected {
1650+
let pattern = glob::Pattern::new(e_path).unwrap();
1651+
let count = entries
1652+
.iter()
1653+
.filter(|(kind, path)| kind == e_kind && pattern.matches(path))
1654+
.count();
1655+
if count != 1 {
1656+
panic!(
1657+
"Expected 1 match of {} {} in {:?}, got {}:\n{:#?}",
1658+
e_kind, e_path, info_path, count, entries
1659+
);
1660+
}
1661+
}
1662+
})
1663+
}

src/cargo/core/compiler/fingerprint/mod.rs

+6-23
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,7 @@ where
20932093
/// Tells the associated path in [`EncodedDepInfo::files`] is relative to package root,
20942094
/// target root, or absolute.
20952095
#[derive(Debug, Eq, PartialEq, Hash, Copy, Clone)]
2096-
pub enum DepInfoPathType {
2096+
enum DepInfoPathType {
20972097
/// src/, e.g. src/lib.rs
20982098
PackageRootRelative,
20992099
/// target/debug/deps/lib...
@@ -2451,13 +2451,13 @@ impl ChecksumAlgo {
24512451
}
24522452

24532453
impl FromStr for ChecksumAlgo {
2454-
type Err = InvalidChecksumAlgo;
2454+
type Err = InvalidChecksum;
24552455

24562456
fn from_str(s: &str) -> Result<Self, Self::Err> {
24572457
match s {
24582458
"sha256" => Ok(Self::Sha256),
24592459
"blake3" => Ok(Self::Blake3),
2460-
_ => Err(InvalidChecksumAlgo {}),
2460+
_ => Err(InvalidChecksum::InvalidChecksumAlgo),
24612461
}
24622462
}
24632463
}
@@ -2471,17 +2471,6 @@ impl Display for ChecksumAlgo {
24712471
}
24722472
}
24732473

2474-
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
2475-
pub struct InvalidChecksumAlgo {}
2476-
2477-
impl Display for InvalidChecksumAlgo {
2478-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
2479-
write!(f, "expected `sha256`, or `blake3`")
2480-
}
2481-
}
2482-
2483-
impl std::error::Error for InvalidChecksumAlgo {}
2484-
24852474
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
24862475
pub struct Checksum {
24872476
algo: ChecksumAlgo,
@@ -2596,18 +2585,12 @@ impl Display for Checksum {
25962585
}
25972586
}
25982587

2599-
#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)]
2588+
#[derive(Debug, thiserror::Error)]
26002589
pub enum InvalidChecksum {
2601-
#[error("algorithm portion incorrect, {0}")]
2602-
InvalidChecksumAlgo(InvalidChecksumAlgo),
2590+
#[error("algorithm portion incorrect, expected `sha256`, or `blake3`")]
2591+
InvalidChecksumAlgo,
26032592
#[error("expected {} hexadecimal digits in checksum portion", .0.hash_len() * 2)]
26042593
InvalidChecksum(ChecksumAlgo),
26052594
#[error("expected a string with format \"algorithm=hex_checksum\"")]
26062595
InvalidFormat,
26072596
}
2608-
2609-
impl From<InvalidChecksumAlgo> for InvalidChecksum {
2610-
fn from(value: InvalidChecksumAlgo) -> Self {
2611-
InvalidChecksum::InvalidChecksumAlgo(value)
2612-
}
2613-
}

tests/testsuite/dep_info.rs

+2-84
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,17 @@
11
//! Tests for dep-info files. This includes the dep-info file Cargo creates in
22
//! the output directory, and the ones stored in the fingerprint.
33
4-
use std::fs;
54
use std::path::Path;
6-
use std::str;
75

86
use cargo_test_support::compare::assert_e2e;
97
use cargo_test_support::paths;
108
use cargo_test_support::prelude::*;
119
use cargo_test_support::registry::Package;
1210
use cargo_test_support::str;
13-
use cargo_test_support::{
14-
basic_bin_manifest, basic_manifest, main_file, project, rustc_host, Project,
15-
};
11+
use cargo_test_support::{assert_deps, assert_deps_contains};
12+
use cargo_test_support::{basic_bin_manifest, basic_manifest, main_file, project, rustc_host};
1613
use filetime::FileTime;
1714

18-
// Helper for testing dep-info files in the fingerprint dir.
19-
#[track_caller]
20-
fn assert_deps(project: &Project, fingerprint: &str, test_cb: impl Fn(&Path, &[(u8, &str)])) {
21-
let mut files = project
22-
.glob(fingerprint)
23-
.map(|f| f.expect("unwrap glob result"))
24-
// Filter out `.json` entries.
25-
.filter(|f| f.extension().is_none());
26-
let info_path = files
27-
.next()
28-
.unwrap_or_else(|| panic!("expected 1 dep-info file at {}, found 0", fingerprint));
29-
assert!(files.next().is_none(), "expected only 1 dep-info file");
30-
let dep_info = fs::read(&info_path).unwrap();
31-
let dep_info = &mut &dep_info[..];
32-
let deps = (0..read_usize(dep_info))
33-
.map(|_| {
34-
let ty = read_u8(dep_info);
35-
let path = str::from_utf8(read_bytes(dep_info)).unwrap();
36-
let checksum_present = read_bool(dep_info);
37-
if checksum_present {
38-
// Read out the checksum info without using it
39-
let _file_len = read_u64(dep_info);
40-
let _checksum = read_bytes(dep_info);
41-
}
42-
(ty, path)
43-
})
44-
.collect::<Vec<_>>();
45-
test_cb(&info_path, &deps);
46-
47-
fn read_usize(bytes: &mut &[u8]) -> usize {
48-
let ret = &bytes[..4];
49-
*bytes = &bytes[4..];
50-
51-
u32::from_le_bytes(ret.try_into().unwrap()) as usize
52-
}
53-
54-
fn read_u8(bytes: &mut &[u8]) -> u8 {
55-
let ret = bytes[0];
56-
*bytes = &bytes[1..];
57-
ret
58-
}
59-
60-
fn read_bool(bytes: &mut &[u8]) -> bool {
61-
read_u8(bytes) != 0
62-
}
63-
64-
fn read_u64(bytes: &mut &[u8]) -> u64 {
65-
let ret = &bytes[..8];
66-
*bytes = &bytes[8..];
67-
68-
u64::from_le_bytes(ret.try_into().unwrap())
69-
}
70-
71-
fn read_bytes<'a>(bytes: &mut &'a [u8]) -> &'a [u8] {
72-
let n = read_usize(bytes);
73-
let ret = &bytes[..n];
74-
*bytes = &bytes[n..];
75-
ret
76-
}
77-
}
78-
79-
fn assert_deps_contains(project: &Project, fingerprint: &str, expected: &[(u8, &str)]) {
80-
assert_deps(project, fingerprint, |info_path, entries| {
81-
for (e_kind, e_path) in expected {
82-
let pattern = glob::Pattern::new(e_path).unwrap();
83-
let count = entries
84-
.iter()
85-
.filter(|(kind, path)| kind == e_kind && pattern.matches(path))
86-
.count();
87-
if count != 1 {
88-
panic!(
89-
"Expected 1 match of {} {} in {:?}, got {}:\n{:#?}",
90-
e_kind, e_path, info_path, count, entries
91-
);
92-
}
93-
}
94-
})
95-
}
96-
9715
#[cargo_test]
9816
fn build_dep_info() {
9917
let p = project()

0 commit comments

Comments
 (0)