@@ -782,70 +782,89 @@ mod tests {
782
782
// Otherwise please just leave a comment in your PR as to why the hash value is
783
783
// changing and why the old value can't be easily preserved.
784
784
//
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.
788
790
#[ 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) ) ) ]
801
791
fn test_stable_hash ( ) {
802
792
use std:: hash:: Hasher ;
803
793
use std:: path:: Path ;
804
794
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
+
805
802
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 )
809
806
} ;
810
807
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
+
811
812
let url = "https://my-crates.io" . into_url ( ) . unwrap ( ) ;
812
813
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 " ) ;
815
816
816
817
let url = "https://your-crates.io" . into_url ( ) . unwrap ( ) ;
817
818
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 " ) ;
820
821
821
822
let url = "sparse+https://my-crates.io" . into_url ( ) . unwrap ( ) ;
822
823
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 " ) ;
825
826
826
827
let url = "sparse+https://your-crates.io" . into_url ( ) . unwrap ( ) ;
827
828
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 " ) ;
830
831
831
832
let url = "file:///tmp/ws/crate" . into_url ( ) . unwrap ( ) ;
832
833
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" ) ;
837
836
837
+ let path = & ws_root. join ( "crate" ) ;
838
838
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
+ }
841
849
842
850
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" ) ;
845
856
846
857
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
+ }
849
868
}
850
869
851
870
#[ test]
0 commit comments