Skip to content

Commit ef1faa5

Browse files
committed
auto merge of #939 : alexcrichton/cargo/issue-38, r=brson
This ensures that the registry understands whether dependencies are build dependencies or dev dependencies. Closes rust-lang/crates.io#38
2 parents 66bfc8c + 3b5994e commit ef1faa5

File tree

6 files changed

+67
-19
lines changed

6 files changed

+67
-19
lines changed

src/cargo/core/dependency.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ impl Dependency {
9090
&self.source_id
9191
}
9292

93+
pub fn get_kind(&self) -> Kind { self.kind }
94+
9395
pub fn kind(mut self, kind: Kind) -> Dependency {
9496
self.kind = kind;
9597
self

src/cargo/ops/registry.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use registry::{Registry, NewCrate, NewCrateDependency};
99

1010
use core::source::Source;
1111
use core::{Package, MultiShell, SourceId};
12+
use core::dependency::Kind;
1213
use core::manifest::ManifestMetadata;
1314
use ops;
1415
use sources::{PathSource, RegistrySource};
@@ -76,6 +77,11 @@ fn transmit(pkg: &Package, tarball: &Path, registry: &mut Registry)
7677
features: dep.get_features().to_vec(),
7778
version_req: dep.get_version_req().to_string(),
7879
target: dep.get_only_for_platform().map(|s| s.to_string()),
80+
kind: match dep.get_kind() {
81+
Kind::Normal => "normal",
82+
Kind::Build => "build",
83+
Kind::Development => "dev",
84+
}.to_string(),
7985
}
8086
}).collect::<Vec<NewCrateDependency>>();
8187
let manifest = pkg.get_manifest();

src/cargo/sources/registry.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ use tar::Archive;
171171
use url::Url;
172172

173173
use core::{Source, SourceId, PackageId, Package, Summary, Registry};
174-
use core::Dependency;
174+
use core::dependency::{Dependency, Kind};
175175
use sources::{PathSource, git};
176176
use util::{CargoResult, Config, internal, ChainError, ToUrl, human};
177177
use util::{hex, Require, Sha256};
@@ -222,6 +222,7 @@ struct RegistryDependency {
222222
optional: bool,
223223
default_features: bool,
224224
target: Option<String>,
225+
kind: Option<String>,
225226
}
226227

227228
impl<'a, 'b> RegistrySource<'a, 'b> {
@@ -412,16 +413,22 @@ impl<'a, 'b> RegistrySource<'a, 'b> {
412413
fn parse_registry_dependency(&self, dep: RegistryDependency)
413414
-> CargoResult<Dependency> {
414415
let RegistryDependency {
415-
name, req, features, optional, default_features, target
416+
name, req, features, optional, default_features, target, kind
416417
} = dep;
417418

418419
let dep = try!(Dependency::parse(name.as_slice(), Some(req.as_slice()),
419420
&self.source_id));
421+
let kind = match kind.as_ref().map(|s| s.as_slice()).unwrap_or("") {
422+
"dev" => Kind::Development,
423+
"build" => Kind::Build,
424+
_ => Kind::Normal,
425+
};
420426

421427
Ok(dep.optional(optional)
422428
.default_features(default_features)
423429
.features(features)
424-
.only_for_platform(target))
430+
.only_for_platform(target)
431+
.kind(kind))
425432
}
426433

427434
/// Actually perform network operations to update the registry

src/registry/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ pub struct NewCrateDependency {
6666
pub features: Vec<String>,
6767
pub version_req: String,
6868
pub target: Option<String>,
69+
pub kind: String,
6970
}
7071

7172
#[deriving(Decodable)]

tests/support/registry.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,22 @@ pub fn init() {
3434
.build();
3535
}
3636

37-
pub fn mock_archive(name: &str, version: &str, deps: &[(&str, &str)]) {
37+
pub fn mock_archive(name: &str, version: &str, deps: &[(&str, &str, &str)]) {
3838
let mut manifest = format!(r#"
3939
[package]
4040
name = "{}"
4141
version = "{}"
4242
authors = []
4343
"#, name, version);
44-
for &(dep, req) in deps.iter() {
44+
for &(dep, req, kind) in deps.iter() {
4545
manifest.push_str(format!(r#"
46-
[dependencies.{}]
46+
[{}dependencies.{}]
4747
version = "{}"
48-
"#, dep, req).as_slice());
48+
"#, match kind {
49+
"build" => "build-",
50+
"dev" => "dev-",
51+
_ => ""
52+
}, dep, req).as_slice());
4953
}
5054
let p = project(name)
5155
.file("Cargo.toml", manifest.as_slice())
@@ -67,11 +71,11 @@ pub fn mock_archive_dst(name: &str, version: &str) -> Path {
6771
dl_path().join(name).join(version).join("download")
6872
}
6973

70-
pub fn mock_pkg(name: &str, version: &str, deps: &[(&str, &str)]) {
74+
pub fn mock_pkg(name: &str, version: &str, deps: &[(&str, &str, &str)]) {
7175
mock_pkg_yank(name, version, deps, false)
7276
}
7377

74-
pub fn mock_pkg_yank(name: &str, version: &str, deps: &[(&str, &str)],
78+
pub fn mock_pkg_yank(name: &str, version: &str, deps: &[(&str, &str, &str)],
7579
yanked: bool) {
7680
mock_archive(name, version, deps);
7781
let c = File::open(&mock_archive_dst(name, version)).read_to_end().unwrap();
@@ -107,22 +111,23 @@ pub fn publish(file: &str, line: &str) {
107111
&[&parent]).unwrap();
108112
}
109113

110-
pub fn pkg(name: &str, vers: &str, deps: &[(&str, &str)], cksum: &str,
114+
pub fn pkg(name: &str, vers: &str, deps: &[(&str, &str, &str)], cksum: &str,
111115
yanked: bool) -> String {
112-
let deps = deps.iter().map(|&(a, b)| dep(a, b)).collect::<Vec<String>>();
116+
let deps = deps.iter().map(|&(a, b, c)| dep(a, b, c)).collect::<Vec<String>>();
113117
format!("{{\"name\":\"{}\",\"vers\":\"{}\",\
114118
\"deps\":{},\"cksum\":\"{}\",\"features\":{{}},\
115119
\"yanked\":{}}}",
116120
name, vers, deps, cksum, yanked)
117121
}
118122

119-
pub fn dep(name: &str, req: &str) -> String {
123+
pub fn dep(name: &str, req: &str, kind: &str) -> String {
120124
format!("{{\"name\":\"{}\",\
121125
\"req\":\"{}\",\
122126
\"features\":[],\
123127
\"default_features\":false,\
124128
\"target\":null,\
125-
\"optional\":false}}", name, req)
129+
\"optional\":false,\
130+
\"kind\":\"{}\"}}", name, req, kind)
126131
}
127132

128133
pub fn cksum(s: &[u8]) -> String {

tests/test_cargo_registry.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ test!(deps {
6565
.file("src/main.rs", "fn main() {}");
6666

6767
r::mock_pkg("baz", "0.0.1", &[]);
68-
r::mock_pkg("bar", "0.0.1", &[("baz", "*")]);
68+
r::mock_pkg("bar", "0.0.1", &[("baz", "*", "normal")]);
6969

7070
assert_that(p.cargo_process("build"),
7171
execs().with_status(0).with_stdout(format!("\
@@ -269,7 +269,7 @@ test!(lockfile_locks_transitively {
269269
p.build();
270270

271271
r::mock_pkg("baz", "0.0.1", &[]);
272-
r::mock_pkg("bar", "0.0.1", &[("baz", "*")]);
272+
r::mock_pkg("bar", "0.0.1", &[("baz", "*", "normal")]);
273273

274274
assert_that(p.process(cargo_dir().join("cargo")).arg("build"),
275275
execs().with_status(0).with_stdout(format!("\
@@ -284,7 +284,7 @@ test!(lockfile_locks_transitively {
284284

285285
p.root().move_into_the_past().unwrap();
286286
r::mock_pkg("baz", "0.0.2", &[]);
287-
r::mock_pkg("bar", "0.0.2", &[("baz", "*")]);
287+
r::mock_pkg("bar", "0.0.2", &[("baz", "*", "normal")]);
288288

289289
assert_that(p.process(cargo_dir().join("cargo")).arg("build"),
290290
execs().with_status(0).with_stdout(""));
@@ -306,8 +306,8 @@ test!(yanks_are_not_used {
306306

307307
r::mock_pkg("baz", "0.0.1", &[]);
308308
r::mock_pkg_yank("baz", "0.0.2", &[], true);
309-
r::mock_pkg("bar", "0.0.1", &[("baz", "*")]);
310-
r::mock_pkg_yank("bar", "0.0.2", &[("baz", "*")], true);
309+
r::mock_pkg("bar", "0.0.1", &[("baz", "*", "normal")]);
310+
r::mock_pkg_yank("bar", "0.0.2", &[("baz", "*", "normal")], true);
311311

312312
assert_that(p.process(cargo_dir().join("cargo")).arg("build"),
313313
execs().with_status(0).with_stdout(format!("\
@@ -337,7 +337,7 @@ test!(relying_on_a_yank_is_bad {
337337

338338
r::mock_pkg("baz", "0.0.1", &[]);
339339
r::mock_pkg_yank("baz", "0.0.2", &[], true);
340-
r::mock_pkg("bar", "0.0.1", &[("baz", "=0.0.2")]);
340+
r::mock_pkg("bar", "0.0.1", &[("baz", "=0.0.2", "normal")]);
341341

342342
assert_that(p.process(cargo_dir().join("cargo")).arg("build"),
343343
execs().with_status(101).with_stderr("\
@@ -442,3 +442,30 @@ test!(update_lockfile {
442442
", downloading = DOWNLOADING, compiling = COMPILING,
443443
dir = p.url()).as_slice()));
444444
})
445+
446+
test!(dev_dependency_not_used {
447+
let p = project("foo")
448+
.file("Cargo.toml", r#"
449+
[project]
450+
name = "foo"
451+
version = "0.0.1"
452+
authors = []
453+
454+
[dependencies]
455+
bar = "*"
456+
"#)
457+
.file("src/main.rs", "fn main() {}");
458+
p.build();
459+
460+
r::mock_pkg("baz", "0.0.1", &[]);
461+
r::mock_pkg("bar", "0.0.1", &[("baz", "*", "dev")]);
462+
463+
assert_that(p.process(cargo_dir().join("cargo")).arg("build"),
464+
execs().with_status(0).with_stdout(format!("\
465+
{updating} registry `[..]`
466+
{downloading} [..] v0.0.1 (registry file://[..])
467+
{compiling} bar v0.0.1 (registry file://[..])
468+
{compiling} foo v0.0.1 ({dir})
469+
", updating = UPDATING, downloading = DOWNLOADING, compiling = COMPILING,
470+
dir = p.url()).as_slice()));
471+
})

0 commit comments

Comments
 (0)