Skip to content

Commit 1fcf5ee

Browse files
committed
test(depinfo): basic serialization round-trip test
1 parent 06e0ef4 commit 1fcf5ee

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub enum DepInfoPathType {
8484
/// | len of key | key bytes | value exists? | len of value | value bytes |
8585
/// +------------+-----------+---------------+--------------+-------------+
8686
/// ```
87-
#[derive(Default)]
87+
#[derive(Default, Debug, PartialEq, Eq)]
8888
pub struct EncodedDepInfo {
8989
pub files: Vec<(DepInfoPathType, PathBuf, Option<(u64, String)>)>,
9090
pub env: Vec<(String, Option<String>)>,
@@ -614,3 +614,57 @@ pub enum InvalidChecksum {
614614
#[error("expected a string with format \"algorithm=hex_checksum\"")]
615615
InvalidFormat,
616616
}
617+
618+
#[cfg(test)]
619+
mod encoded_dep_info {
620+
use super::*;
621+
622+
#[track_caller]
623+
fn gen_test(checksum: bool) {
624+
let checksum = checksum.then_some((768, "c01efc669f09508b55eced32d3c88702578a7c3e".into()));
625+
let lib_rs = (
626+
DepInfoPathType::TargetRootRelative,
627+
PathBuf::from("src/lib.rs"),
628+
checksum.clone(),
629+
);
630+
631+
let depinfo = EncodedDepInfo {
632+
files: vec![lib_rs.clone()],
633+
env: Vec::new(),
634+
};
635+
let data = depinfo.serialize().unwrap();
636+
assert_eq!(EncodedDepInfo::parse(&data).unwrap(), depinfo);
637+
638+
let mod_rs = (
639+
DepInfoPathType::TargetRootRelative,
640+
PathBuf::from("src/mod.rs"),
641+
checksum.clone(),
642+
);
643+
let depinfo = EncodedDepInfo {
644+
files: vec![lib_rs.clone(), mod_rs.clone()],
645+
env: Vec::new(),
646+
};
647+
let data = depinfo.serialize().unwrap();
648+
assert_eq!(EncodedDepInfo::parse(&data).unwrap(), depinfo);
649+
650+
let depinfo = EncodedDepInfo {
651+
files: vec![lib_rs, mod_rs],
652+
env: vec![
653+
("Gimli".into(), Some("Legolas".into())),
654+
("Beren".into(), Some("Lúthien".into())),
655+
],
656+
};
657+
let data = depinfo.serialize().unwrap();
658+
assert_eq!(EncodedDepInfo::parse(&data).unwrap(), depinfo);
659+
}
660+
661+
#[test]
662+
fn round_trip() {
663+
gen_test(false);
664+
}
665+
666+
#[test]
667+
fn round_trip_with_checksums() {
668+
gen_test(true);
669+
}
670+
}

0 commit comments

Comments
 (0)