From c04208b0e020c283a1d839c1f01844b6dbbec0b0 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 7 Sep 2018 09:59:27 -0700 Subject: [PATCH] Handle renamed dependencies coming from Cargo This commit is intended to be coupled with https://github.com/rust-lang/cargo/pull/5993 where Cargo will start sending the registry more information about locally renamed crates to persist into the index. --- src/git.rs | 2 + src/models/dependency.rs | 13 ++++++- src/tests/http-data/krate_new_renamed_crate | 1 + src/tests/krate.rs | 43 +++++++++++++++++++++ src/views/krate_publish.rs | 1 + 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/tests/http-data/krate_new_renamed_crate diff --git a/src/git.rs b/src/git.rs index b516999037..994dacdfef 100644 --- a/src/git.rs +++ b/src/git.rs @@ -34,6 +34,8 @@ pub struct Dependency { pub default_features: bool, pub target: Option, pub kind: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub package: Option, } fn index_file(base: &Path, name: &str) -> PathBuf { diff --git a/src/models/dependency.rs b/src/models/dependency.rs index f01a6a6258..05defc00ae 100644 --- a/src/models/dependency.rs +++ b/src/models/dependency.rs @@ -96,15 +96,26 @@ pub fn add_dependencies( )); } + // If this dependency has an explicit name in `Cargo.toml` that + // means that the `name` we have listed is actually the package name + // that we're depending on. The `name` listed in the index is the + // Cargo.toml-written-name which is what cargo uses for + // `--extern foo=...` + let (name, package) = match &dep.explicit_name_in_toml { + Some(explicit) => (explicit.to_string(), Some(dep.name.to_string())), + None => (dep.name.to_string(), None), + }; + Ok(( git::Dependency { - name: dep.name.to_string(), + name, req: dep.version_req.to_string(), features: dep.features.iter().map(|s| s.to_string()).collect(), optional: dep.optional, default_features: dep.default_features, target: dep.target.clone(), kind: dep.kind.or(Some(DependencyKind::Normal)), + package, }, ( version_id.eq(target_version_id), diff --git a/src/tests/http-data/krate_new_renamed_crate b/src/tests/http-data/krate_new_renamed_crate new file mode 100644 index 0000000000..7addd47799 --- /dev/null +++ b/src/tests/http-data/krate_new_renamed_crate @@ -0,0 +1 @@ +[{"request":{"uri":"http://alexcrichton-test.s3.amazonaws.com/crates/new-krate/new-krate-1.0.0.crate","method":"PUT","headers":[["date","Fri, 15 Sep 2017 07:53:06 -0700"],["content-length","35"],["content-type","application/x-tar"],["proxy-connection","Keep-Alive"],["accept","*/*"],["host","alexcrichton-test.s3.amazonaws.com"],["authorization","AWS AKIAICL5IWUZYWWKA7JA:uDc39eNdF6CcwB+q+JwKsoDLQc4="]],"body":[31,139,8,0,0,0,0,0,0,255,237,192,1,1,0,0,0,130,32,255,175,110,72,80,192,171,1,46,175,181,239,0,4,0,0]},"response":{"status":200,"headers":[["content-length","0"],["Server","AmazonS3"],["date","Fri, 15 Sep 2017 14:53:07 GMT"],["ETag","\"f9016ad360cebb4fe2e6e96e5949f022\""],["x-amz-id-2","JdIvnNTw53aqXjBIqBLNuN4kxf/w1XWX+xuIiGBDYy7yzOSDuAMtBSrTW4ZWetcCIdqCUHuQ51A="],["x-amz-request-id","26589A5E52F8395C"]],"body":[]}}] diff --git a/src/tests/krate.rs b/src/tests/krate.rs index 4dad98c280..4360b8d02d 100644 --- a/src/tests/krate.rs +++ b/src/tests/krate.rs @@ -724,6 +724,7 @@ fn new_krate_with_dependency() { version_req: u::CrateVersionReq(semver::VersionReq::parse(">= 0").unwrap()), target: None, kind: None, + explicit_name_in_toml: None, }; let mut req = new_req_full(Arc::clone(&app), krate("new_dep"), "1.0.0", vec![dep]); { @@ -750,6 +751,45 @@ fn new_krate_with_dependency() { assert_eq!(p.deps[0].name, "foo_dep"); } +#[test] +fn new_renamed_crate() { + let (_b, app, middle) = app(); + let dep = u::CrateDependency { + name: u::CrateName("package-name".to_string()), + optional: false, + default_features: true, + features: Vec::new(), + version_req: u::CrateVersionReq(semver::VersionReq::parse(">= 0").unwrap()), + target: None, + kind: None, + explicit_name_in_toml: Some(u::CrateName("my-name".to_string())), + }; + let mut req = new_req_full(Arc::clone(&app), krate("new-krate"), "1.0.0", vec![dep]); + { + let conn = app.diesel_database.get().unwrap(); + let user = new_user("user").create_or_update(&conn).unwrap(); + sign_in_as(&mut req, &user); + CrateBuilder::new("package-name", user.id).expect_build(&conn); + } + + let mut response = ok_resp!(middle.call(&mut req)); + ::json::(&mut response); + + let path = ::git::checkout().join("ne/w-/new-krate"); + assert!(path.exists()); + let mut contents = String::new(); + File::open(&path) + .unwrap() + .read_to_string(&mut contents) + .unwrap(); + let p: git::Crate = serde_json::from_str(&contents).unwrap(); + assert_eq!(p.name, "new-krate"); + assert_eq!(p.vers, "1.0.0"); + assert_eq!(p.deps.len(), 1); + assert_eq!(p.deps[0].name, "my-name"); + assert_eq!(p.deps[0].package, Some("package-name".to_string())); +} + #[test] fn new_krate_non_canon_crate_name_dependencies() { let (_b, app, middle) = app(); @@ -761,6 +801,7 @@ fn new_krate_non_canon_crate_name_dependencies() { version_req: u::CrateVersionReq(semver::VersionReq::parse(">= 0").unwrap()), target: None, kind: None, + explicit_name_in_toml: None, }]; let mut req = new_req_full(Arc::clone(&app), krate("new_dep"), "1.0.0", deps); { @@ -785,6 +826,7 @@ fn new_krate_with_wildcard_dependency() { version_req: u::CrateVersionReq(semver::VersionReq::parse("*").unwrap()), target: None, kind: None, + explicit_name_in_toml: None, }; let mut req = new_req_full(Arc::clone(&app), krate("new_wild"), "1.0.0", vec![dep]); { @@ -1113,6 +1155,7 @@ fn new_krate_dependency_missing() { version_req: u::CrateVersionReq(semver::VersionReq::parse(">= 0.0.0").unwrap()), target: None, kind: None, + explicit_name_in_toml: None, }; let mut req = new_req_full(Arc::clone(&app), krate("foo_missing"), "1.0.0", vec![dep]); sign_in(&mut req, &app); diff --git a/src/views/krate_publish.rs b/src/views/krate_publish.rs index ecd732446f..d604da01e0 100644 --- a/src/views/krate_publish.rs +++ b/src/views/krate_publish.rs @@ -63,6 +63,7 @@ pub struct CrateDependency { pub version_req: CrateVersionReq, pub target: Option, pub kind: Option, + pub explicit_name_in_toml: Option, } impl<'de> Deserialize<'de> for CrateName {