Skip to content

Commit

Permalink
Handle renamed dependencies coming from Cargo
Browse files Browse the repository at this point in the history
This commit is intended to be coupled with
rust-lang/cargo#5993 where Cargo will start sending the
registry more information about locally renamed crates to persist into the
index.
  • Loading branch information
alexcrichton committed Sep 14, 2018
1 parent f58ca05 commit c04208b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub struct Dependency {
pub default_features: bool,
pub target: Option<String>,
pub kind: Option<DependencyKind>,
#[serde(skip_serializing_if = "Option::is_none")]
pub package: Option<String>,
}

fn index_file(base: &Path, name: &str) -> PathBuf {
Expand Down
13 changes: 12 additions & 1 deletion src/models/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions src/tests/http-data/krate_new_renamed_crate
Original file line number Diff line number Diff line change
@@ -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":[]}}]
43 changes: 43 additions & 0 deletions src/tests/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
{
Expand All @@ -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::<GoodCrate>(&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();
Expand All @@ -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);
{
Expand All @@ -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]);
{
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/views/krate_publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub struct CrateDependency {
pub version_req: CrateVersionReq,
pub target: Option<String>,
pub kind: Option<DependencyKind>,
pub explicit_name_in_toml: Option<CrateName>,
}

impl<'de> Deserialize<'de> for CrateName {
Expand Down

0 comments on commit c04208b

Please sign in to comment.