@@ -786,70 +786,93 @@ 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
- use std :: hash :: Hasher ;
806
+ use crate :: util :: StableHasher ;
807
807
use std:: path:: Path ;
808
808
809
+ #[ cfg( not( windows) ) ]
810
+ let ws_root = Path :: new ( "/tmp/ws" ) ;
811
+ #[ cfg( windows) ]
812
+ let ws_root = Path :: new ( r"C:\\tmp\ws" ) ;
813
+
809
814
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) ;
815
+ let mut hasher = StableHasher :: new ( ) ;
816
+ source_id. stable_hash ( ws_root , & mut hasher) ;
812
817
hasher. finish ( )
813
818
} ;
814
819
815
820
let url = "https://my-crates.io" . into_url ( ) . unwrap ( ) ;
816
821
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 " ) ;
822
+ assert_eq ! ( gen_hash( source_id) , 2056262832525457700 ) ;
823
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "24b984d12650891c " ) ;
819
824
820
825
let url = "https://your-crates.io" . into_url ( ) . unwrap ( ) ;
821
826
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 " ) ;
827
+ assert_eq ! ( gen_hash( source_id) , 7851411715584162426 ) ;
828
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "7afabb545bd1f56c " ) ;
824
829
825
830
let url = "sparse+https://my-crates.io" . into_url ( ) . unwrap ( ) ;
826
831
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 " ) ;
832
+ assert_eq ! ( gen_hash( source_id) , 15233380663065439616 ) ;
833
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "80ed51ce00d767d3 " ) ;
829
834
830
835
let url = "sparse+https://your-crates.io" . into_url ( ) . unwrap ( ) ;
831
836
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 " ) ;
837
+ assert_eq ! ( gen_hash( source_id) , 12749290624384351691 ) ;
838
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "cbbda5344694eeb0 " ) ;
834
839
835
840
let url = "file:///tmp/ws/crate" . into_url ( ) . unwrap ( ) ;
836
841
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" ) ;
842
+ assert_eq ! ( gen_hash( source_id) , 3109465066469481245 ) ;
843
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "1d5b66d8000a272b" ) ;
841
844
845
+ let path = & ws_root. join ( "crate" ) ;
842
846
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" ) ;
847
+ #[ cfg( not( windows) ) ]
848
+ {
849
+ assert_eq ! ( gen_hash( source_id) , 17171351456028149232 ) ;
850
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "f0c5f1e92be54cee" ) ;
851
+ }
852
+ #[ cfg( windows) ]
853
+ {
854
+ assert_eq ! ( gen_hash( source_id) , 10712195329887934127 ) ;
855
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "af96919ae55ca994" ) ;
856
+ }
845
857
846
858
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" ) ;
859
+ assert_eq ! ( gen_hash( source_id) , 13241112980875747369 ) ;
860
+ #[ cfg( not( windows) ) ]
861
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "e5ba2edec163e65a" ) ;
862
+ #[ cfg( windows) ]
863
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "429dd6f2283a9b5c" ) ;
849
864
850
865
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" ) ;
866
+ #[ cfg( not( windows) ) ]
867
+ {
868
+ assert_eq ! ( gen_hash( source_id) , 12461124588148212881 ) ;
869
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "91c47582caceeeac" ) ;
870
+ }
871
+ #[ cfg( windows) ]
872
+ {
873
+ assert_eq ! ( gen_hash( source_id) , 17000469607053345884 ) ;
874
+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "5c443d0709cdedeb" ) ;
875
+ }
853
876
}
854
877
855
878
#[ test]
0 commit comments