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