diff --git a/src/cargo/core/source_id.rs b/src/cargo/core/source_id.rs index ca022c936590..68f916b36b5a 100644 --- a/src/cargo/core/source_id.rs +++ b/src/cargo/core/source_id.rs @@ -787,15 +787,23 @@ mod tests { // changing and why the old value can't be easily preserved. // // The hash value should be stable across platforms, and doesn't depend on - // endianness and bit-width. + // endianness and bit-width. One caveat is that absolute paths is inherently + // different on Windows than on Unix-like platforms. Unless we omit or strip + // the prefix components (e.g. `C:`), there is not way to have a + // cross-platform stable hash for absolute paths. #[test] fn test_stable_hash() { use crate::util::StableHasher; use std::path::Path; + #[cfg(not(windows))] + let ws_root = Path::new("/tmp/ws"); + #[cfg(windows)] + let ws_root = Path::new(r"C:\tmp\ws"); + let gen_hash = |source_id: SourceId| { let mut hasher = StableHasher::new(); - source_id.stable_hash(Path::new("/tmp/ws"), &mut hasher); + source_id.stable_hash(ws_root, &mut hasher); hasher.finish() }; @@ -828,8 +836,7 @@ mod tests { assert_eq!(gen_hash(source_id), 3109465066469481245); assert_eq!(crate::util::hex::short_hash(&source_id), "1d5b66d8000a272b"); - let path = Path::new("/tmp/ws/crate"); - + let path = &ws_root.join("crate"); let source_id = SourceId::for_local_registry(path).unwrap(); assert_eq!(gen_hash(source_id), 17171351456028149232); assert_eq!(crate::util::hex::short_hash(&source_id), "f0c5f1e92be54cee");