@@ -681,31 +681,6 @@ impl Ord for SourceKind {
681681 }
682682}
683683
684- // This is a test that the hash of the `SourceId` for crates.io is a well-known
685- // value.
686- //
687- // Note that the hash value matches what the crates.io source id has hashed
688- // since long before Rust 1.30. We strive to keep this value the same across
689- // versions of Cargo because changing it means that users will need to
690- // redownload the index and all crates they use when using a new Cargo version.
691- //
692- // This isn't to say that this hash can *never* change, only that when changing
693- // this it should be explicitly done. If this hash changes accidentally and
694- // you're able to restore the hash to its original value, please do so!
695- // Otherwise please just leave a comment in your PR as to why the hash value is
696- // changing and why the old value can't be easily preserved.
697- //
698- // The hash value depends on endianness and bit-width, so we only run this test on
699- // little-endian 64-bit CPUs (such as x86-64 and ARM64) where it matches the
700- // well-known value.
701- #[ test]
702- #[ cfg( all( target_endian = "little" , target_pointer_width = "64" ) ) ]
703- fn test_cratesio_hash ( ) {
704- let config = Config :: default ( ) . unwrap ( ) ;
705- let crates_io = SourceId :: crates_io ( & config) . unwrap ( ) ;
706- assert_eq ! ( crate :: util:: hex:: short_hash( & crates_io) , "1ecc6299db9ec823" ) ;
707- }
708-
709684/// A `Display`able view into a `SourceId` that will write it as a url
710685pub struct SourceIdAsUrl < ' a > {
711686 inner : & ' a SourceIdInner ,
@@ -792,7 +767,7 @@ impl<'a> fmt::Display for PrettyRef<'a> {
792767#[ cfg( test) ]
793768mod tests {
794769 use super :: { GitReference , SourceId , SourceKind } ;
795- use crate :: util:: IntoUrl ;
770+ use crate :: util:: { Config , IntoUrl } ;
796771
797772 #[ test]
798773 fn github_sources_equal ( ) {
@@ -809,4 +784,84 @@ mod tests {
809784 let s3 = SourceId :: new ( foo, loc, None ) . unwrap ( ) ;
810785 assert_ne ! ( s1, s3) ;
811786 }
787+
788+ // This is a test that the hash of the `SourceId` for crates.io is a well-known
789+ // value.
790+ //
791+ // Note that the hash value matches what the crates.io source id has hashed
792+ // since long before Rust 1.30. We strive to keep this value the same across
793+ // versions of Cargo because changing it means that users will need to
794+ // redownload the index and all crates they use when using a new Cargo version.
795+ //
796+ // This isn't to say that this hash can *never* change, only that when changing
797+ // this it should be explicitly done. If this hash changes accidentally and
798+ // you're able to restore the hash to its original value, please do so!
799+ // Otherwise please just leave a comment in your PR as to why the hash value is
800+ // changing and why the old value can't be easily preserved.
801+ //
802+ // The hash value depends on endianness and bit-width, so we only run this test on
803+ // little-endian 64-bit CPUs (such as x86-64 and ARM64) where it matches the
804+ // well-known value.
805+ #[ test]
806+ #[ cfg( all( target_endian = "little" , target_pointer_width = "64" ) ) ]
807+ fn test_cratesio_hash ( ) {
808+ let config = Config :: default ( ) . unwrap ( ) ;
809+ let crates_io = SourceId :: crates_io ( & config) . unwrap ( ) ;
810+ assert_eq ! ( crate :: util:: hex:: short_hash( & crates_io) , "1ecc6299db9ec823" ) ;
811+ }
812+
813+ // See the comment in `test_cratesio_hash`.
814+ //
815+ // Only test on non-Windows as paths on Windows will get different hashes.
816+ #[ test]
817+ #[ cfg( all( target_endian = "little" , target_pointer_width = "64" , not( windows) ) ) ]
818+ fn test_stable_hash ( ) {
819+ use std:: hash:: Hasher ;
820+ use std:: path:: Path ;
821+
822+ let gen_hash = |source_id : SourceId | {
823+ let mut hasher = std:: collections:: hash_map:: DefaultHasher :: new ( ) ;
824+ source_id. stable_hash ( Path :: new ( "/tmp/ws" ) , & mut hasher) ;
825+ hasher. finish ( )
826+ } ;
827+
828+ let url = "https://my-crates.io" . into_url ( ) . unwrap ( ) ;
829+ let source_id = SourceId :: for_registry ( & url) . unwrap ( ) ;
830+ assert_eq ! ( gen_hash( source_id) , 18108075011063494626 ) ;
831+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "fb60813d6cb8df79" ) ;
832+
833+ let url = "https://your-crates.io" . into_url ( ) . unwrap ( ) ;
834+ let source_id = SourceId :: for_alt_registry ( & url, "alt" ) . unwrap ( ) ;
835+ assert_eq ! ( gen_hash( source_id) , 12862859764592646184 ) ;
836+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "09c10fd0cbd74bce" ) ;
837+
838+ let url = "sparse+https://my-crates.io" . into_url ( ) . unwrap ( ) ;
839+ let source_id = SourceId :: for_registry ( & url) . unwrap ( ) ;
840+ assert_eq ! ( gen_hash( source_id) , 8763561830438022424 ) ;
841+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "d1ea0d96f6f759b5" ) ;
842+
843+ let url = "sparse+https://your-crates.io" . into_url ( ) . unwrap ( ) ;
844+ let source_id = SourceId :: for_alt_registry ( & url, "alt" ) . unwrap ( ) ;
845+ assert_eq ! ( gen_hash( source_id) , 5159702466575482972 ) ;
846+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "135d23074253cb78" ) ;
847+
848+ let url = "file:///tmp/ws/crate" . into_url ( ) . unwrap ( ) ;
849+ let source_id = SourceId :: for_git ( & url, GitReference :: DefaultBranch ) . unwrap ( ) ;
850+ assert_eq ! ( gen_hash( source_id) , 15332537265078583985 ) ;
851+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "73a808694abda756" ) ;
852+
853+ let path = Path :: new ( "/tmp/ws/crate" ) ;
854+
855+ let source_id = SourceId :: for_local_registry ( path) . unwrap ( ) ;
856+ assert_eq ! ( gen_hash( source_id) , 18446533307730842837 ) ;
857+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "52a84cc73f6fd48b" ) ;
858+
859+ let source_id = SourceId :: for_path ( path) . unwrap ( ) ;
860+ assert_eq ! ( gen_hash( source_id) , 8764714075439899829 ) ;
861+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "e1ddd48578620fc1" ) ;
862+
863+ let source_id = SourceId :: for_directory ( path) . unwrap ( ) ;
864+ assert_eq ! ( gen_hash( source_id) , 17459999773908528552 ) ;
865+ assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "6568fe2c2fab5bfe" ) ;
866+ }
812867}
0 commit comments