@@ -782,70 +782,89 @@ mod tests {
782782 // Otherwise please just leave a comment in your PR as to why the hash value is
783783 // changing and why the old value can't be easily preserved.
784784 //
785- // The hash value depends on endianness and bit-width, so we only run this test on
786- // little-endian 64-bit CPUs (such as x86-64 and ARM64) where it matches the
787- // well-known value.
785+ // The hash value should be stable across platforms, and doesn't depend on
786+ // endianness and bit-width. One caveat is that absolute paths on Windows
787+ // are inherently different than on Unix-like platforms. Unless we omit or
788+ // strip the prefix components (e.g. `C:`), there is not way to have a true
789+ // cross-platform stable hash for absolute paths.
788790 #[ test]
789- #[ cfg( all( target_endian = "little" , target_pointer_width = "64" ) ) ]
790- fn test_cratesio_hash ( ) {
791- let gctx = GlobalContext :: default ( ) . unwrap ( ) ;
792- let crates_io = SourceId :: crates_io ( & gctx) . unwrap ( ) ;
793- assert_eq ! ( crate :: util:: hex:: short_hash( & crates_io) , "1ecc6299db9ec823" ) ;
794- }
795-
796- // See the comment in `test_cratesio_hash`.
797- //
798- // Only test on non-Windows as paths on Windows will get different hashes.
799- #[ test]
800- #[ cfg( all( target_endian = "little" , target_pointer_width = "64" , not( windows) ) ) ]
801791 fn test_stable_hash ( ) {
802792 use std:: hash:: Hasher ;
803793 use std:: path:: Path ;
804794
795+ use crate :: util:: StableHasher ;
796+
797+ #[ cfg( not( windows) ) ]
798+ let ws_root = Path :: new ( "/tmp/ws" ) ;
799+ #[ cfg( windows) ]
800+ let ws_root = Path :: new ( r"C:\\tmp\ws" ) ;
801+
805802 let gen_hash = |source_id : SourceId | {
806- let mut hasher = std :: collections :: hash_map :: DefaultHasher :: new ( ) ;
807- source_id. stable_hash ( Path :: new ( "/tmp/ws" ) , & mut hasher) ;
808- hasher . finish ( )
803+ let mut hasher = StableHasher :: new ( ) ;
804+ source_id. stable_hash ( ws_root , & mut hasher) ;
805+ Hasher :: finish ( & hasher )
809806 } ;
810807
808+ let source_id = SourceId :: crates_io ( & GlobalContext :: default ( ) . unwrap ( ) ) . unwrap ( ) ;
809+ assert_eq ! ( gen_hash( source_id) , 7062945687441624357 ) ;
810+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "25cdd57fae9f0462" ) ;
811+
811812 let url = "https://my-crates.io" . into_url ( ) . unwrap ( ) ;
812813 let source_id = SourceId :: for_registry ( & url) . unwrap ( ) ;
813- assert_eq ! ( gen_hash( source_id) , 18108075011063494626 ) ;
814- assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "fb60813d6cb8df79 " ) ;
814+ assert_eq ! ( gen_hash( source_id) , 8310250053664888498 ) ;
815+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "b2d65deb64f05373 " ) ;
815816
816817 let url = "https://your-crates.io" . into_url ( ) . unwrap ( ) ;
817818 let source_id = SourceId :: for_alt_registry ( & url, "alt" ) . unwrap ( ) ;
818- assert_eq ! ( gen_hash( source_id) , 12862859764592646184 ) ;
819- assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "09c10fd0cbd74bce " ) ;
819+ assert_eq ! ( gen_hash( source_id) , 14149534903000258933 ) ;
820+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "755952de063f5dc4 " ) ;
820821
821822 let url = "sparse+https://my-crates.io" . into_url ( ) . unwrap ( ) ;
822823 let source_id = SourceId :: for_registry ( & url) . unwrap ( ) ;
823- assert_eq ! ( gen_hash( source_id) , 8763561830438022424 ) ;
824- assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "d1ea0d96f6f759b5 " ) ;
824+ assert_eq ! ( gen_hash( source_id) , 16249512552851930162 ) ;
825+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "327cfdbd92dd81e1 " ) ;
825826
826827 let url = "sparse+https://your-crates.io" . into_url ( ) . unwrap ( ) ;
827828 let source_id = SourceId :: for_alt_registry ( & url, "alt" ) . unwrap ( ) ;
828- assert_eq ! ( gen_hash( source_id) , 5159702466575482972 ) ;
829- assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "135d23074253cb78 " ) ;
829+ assert_eq ! ( gen_hash( source_id) , 6156697384053352292 ) ;
830+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "64a713b6a6fb7055 " ) ;
830831
831832 let url = "file:///tmp/ws/crate" . into_url ( ) . unwrap ( ) ;
832833 let source_id = SourceId :: for_git ( & url, GitReference :: DefaultBranch ) . unwrap ( ) ;
833- assert_eq ! ( gen_hash( source_id) , 15332537265078583985 ) ;
834- assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "73a808694abda756" ) ;
835-
836- let path = Path :: new ( "/tmp/ws/crate" ) ;
834+ assert_eq ! ( gen_hash( source_id) , 473480029881867801 ) ;
835+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "199e591d94239206" ) ;
837836
837+ let path = & ws_root. join ( "crate" ) ;
838838 let source_id = SourceId :: for_local_registry ( path) . unwrap ( ) ;
839- assert_eq ! ( gen_hash( source_id) , 18446533307730842837 ) ;
840- assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "52a84cc73f6fd48b" ) ;
839+ #[ cfg( not( windows) ) ]
840+ {
841+ assert_eq ! ( gen_hash( source_id) , 11515846423845066584 ) ;
842+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "58d73c154f81d09f" ) ;
843+ }
844+ #[ cfg( windows) ]
845+ {
846+ assert_eq ! ( gen_hash( source_id) , 6146331155906064276 ) ;
847+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "946fb2239f274c55" ) ;
848+ }
841849
842850 let source_id = SourceId :: for_path ( path) . unwrap ( ) ;
843- assert_eq ! ( gen_hash( source_id) , 8764714075439899829 ) ;
844- assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "e1ddd48578620fc1" ) ;
851+ assert_eq ! ( gen_hash( source_id) , 215644081443634269 ) ;
852+ #[ cfg( not( windows) ) ]
853+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "64bace89c92b101f" ) ;
854+ #[ cfg( windows) ]
855+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "01e1e6c391813fb6" ) ;
845856
846857 let source_id = SourceId :: for_directory ( path) . unwrap ( ) ;
847- assert_eq ! ( gen_hash( source_id) , 17459999773908528552 ) ;
848- assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "6568fe2c2fab5bfe" ) ;
858+ #[ cfg( not( windows) ) ]
859+ {
860+ assert_eq ! ( gen_hash( source_id) , 6127590343904940368 ) ;
861+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "505191d1f3920955" ) ;
862+ }
863+ #[ cfg( windows) ]
864+ {
865+ assert_eq ! ( gen_hash( source_id) , 10423446877655960172 ) ;
866+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "6c8ad69db585a790" ) ;
867+ }
849868 }
850869
851870 #[ test]
0 commit comments