@@ -782,70 +782,99 @@ 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 is inherently
787
+ // different on Windows than on Unix-like platforms. Unless we omit or strip
788
+ // the prefix components (e.g. `C:`), there is not way to have a
789
+ // cross-platform stable hash for absolute paths.
788
790
#[ test]
789
- #[ cfg( all( target_endian = "little" , target_pointer_width = "64" ) ) ]
790
791
fn test_cratesio_hash ( ) {
791
792
let gctx = GlobalContext :: default ( ) . unwrap ( ) ;
792
793
let crates_io = SourceId :: crates_io ( & gctx) . unwrap ( ) ;
793
- assert_eq ! ( crate :: util:: hex:: short_hash( & crates_io) , "1ecc6299db9ec823 " ) ;
794
+ assert_eq ! ( crate :: util:: hex:: short_hash( & crates_io) , "83d63c3e13aca8cc " ) ;
794
795
}
795
796
796
797
// See the comment in `test_cratesio_hash`.
797
798
//
798
799
// Only test on non-Windows as paths on Windows will get different hashes.
799
800
#[ test]
800
- #[ cfg( all( target_endian = "little" , target_pointer_width = "64" , not( windows) ) ) ]
801
801
fn test_stable_hash ( ) {
802
802
use std:: hash:: Hasher ;
803
803
use std:: path:: Path ;
804
804
805
+ use crate :: util:: StableHasher ;
806
+
807
+ #[ cfg( not( windows) ) ]
808
+ let ws_root = Path :: new ( "/tmp/ws" ) ;
809
+ #[ cfg( windows) ]
810
+ let ws_root = Path :: new ( r"C:\\tmp\ws" ) ;
811
+
805
812
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 ( )
813
+ let mut hasher = StableHasher :: new ( ) ;
814
+ source_id. stable_hash ( ws_root , & mut hasher) ;
815
+ Hasher :: finish ( & hasher )
809
816
} ;
810
817
818
+ let source_id = SourceId :: crates_io ( & GlobalContext :: default ( ) . unwrap ( ) ) . unwrap ( ) ;
819
+ assert_eq ! ( gen_hash( source_id) , 7062945687441624357 ) ;
820
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "25cdd57fae9f0462" ) ;
821
+
811
822
let url = "https://my-crates.io" . into_url ( ) . unwrap ( ) ;
812
823
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 " ) ;
824
+ assert_eq ! ( gen_hash( source_id) , 8310250053664888498 ) ;
825
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "b2d65deb64f05373 " ) ;
815
826
816
827
let url = "https://your-crates.io" . into_url ( ) . unwrap ( ) ;
817
828
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 " ) ;
829
+ assert_eq ! ( gen_hash( source_id) , 14149534903000258933 ) ;
830
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "755952de063f5dc4 " ) ;
820
831
821
832
let url = "sparse+https://my-crates.io" . into_url ( ) . unwrap ( ) ;
822
833
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 " ) ;
834
+ assert_eq ! ( gen_hash( source_id) , 16249512552851930162 ) ;
835
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "327cfdbd92dd81e1 " ) ;
825
836
826
837
let url = "sparse+https://your-crates.io" . into_url ( ) . unwrap ( ) ;
827
838
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 " ) ;
839
+ assert_eq ! ( gen_hash( source_id) , 6156697384053352292 ) ;
840
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "64a713b6a6fb7055 " ) ;
830
841
831
842
let url = "file:///tmp/ws/crate" . into_url ( ) . unwrap ( ) ;
832
843
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" ) ;
844
+ assert_eq ! ( gen_hash( source_id) , 473480029881867801 ) ;
845
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "199e591d94239206" ) ;
837
846
847
+ let path = & ws_root. join ( "crate" ) ;
838
848
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" ) ;
849
+ #[ cfg( not( windows) ) ]
850
+ {
851
+ assert_eq ! ( gen_hash( source_id) , 11515846423845066584 ) ;
852
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "58d73c154f81d09f" ) ;
853
+ }
854
+ #[ cfg( windows) ]
855
+ {
856
+ assert_eq ! ( gen_hash( source_id) , 6146331155906064276 ) ;
857
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "946fb2239f274c55" ) ;
858
+ }
841
859
842
860
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" ) ;
861
+ assert_eq ! ( gen_hash( source_id) , 215644081443634269 ) ;
862
+ #[ cfg( not( windows) ) ]
863
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "64bace89c92b101f" ) ;
864
+ #[ cfg( windows) ]
865
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "01e1e6c391813fb6" ) ;
845
866
846
867
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" ) ;
868
+ #[ cfg( not( windows) ) ]
869
+ {
870
+ assert_eq ! ( gen_hash( source_id) , 6127590343904940368 ) ;
871
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "505191d1f3920955" ) ;
872
+ }
873
+ #[ cfg( windows) ]
874
+ {
875
+ assert_eq ! ( gen_hash( source_id) , 10423446877655960172 ) ;
876
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "6c8ad69db585a790" ) ;
877
+ }
849
878
}
850
879
851
880
#[ test]
0 commit comments