Skip to content

Commit 2aeb7a4

Browse files
committed
auto merge of #421 : alexcrichton/cargo/issue-418, r=wycats
2 parents df9470b + 0e91bed commit 2aeb7a4

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

src/cargo/ops/cargo_run.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
use std::os;
22

33
use ops;
4-
use util::{CargoResult, human, process, ProcessError};
4+
use util::{CargoResult, human, process, ProcessError, Require};
55
use core::source::Source;
66
use sources::PathSource;
77

88
pub fn run(manifest_path: &Path,
99
options: &mut ops::CompileOptions,
1010
args: &[String]) -> CargoResult<Option<ProcessError>> {
11-
if !manifest_path.dir_path().join("src").join("main.rs").exists() {
12-
return Err(human("`src/main.rs` must be present for `cargo run`"))
13-
}
14-
1511
let mut src = try!(PathSource::for_path(&manifest_path.dir_path()));
1612
try!(src.update());
1713
let root = try!(src.get_root_package());
14+
let mut bins = root.get_manifest().get_targets().iter().filter(|a| {
15+
a.is_bin() && a.get_profile().is_compile()
16+
});
17+
let bin = try!(bins.next().require(|| {
18+
human("a bin target must be available for `cargo run`")
19+
}));
20+
match bins.next() {
21+
Some(..) => return Err(human("`cargo run` requires that a project only \
22+
have one executable")),
23+
None => {}
24+
}
1825

1926
let compile = try!(ops::compile(manifest_path, options));
20-
let exe = manifest_path.dir_path().join("target").join(root.get_name());
27+
let exe = manifest_path.dir_path().join("target").join(bin.get_name());
2128
let exe = match exe.path_relative_from(&os::getcwd()) {
2229
Some(path) => path,
2330
None => exe,

tests/test_cargo_run.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,26 @@ test!(no_main_file {
7979

8080
assert_that(p.cargo_process("cargo-run"),
8181
execs().with_status(101)
82-
.with_stderr("`src/main.rs` must be present for \
83-
`cargo run`\n"));
82+
.with_stderr("a bin target must be available \
83+
for `cargo run`\n"));
84+
})
85+
86+
test!(too_many_bins {
87+
let p = project("foo")
88+
.file("Cargo.toml", r#"
89+
[project]
90+
name = "foo"
91+
version = "0.0.1"
92+
authors = []
93+
"#)
94+
.file("src/lib.rs", "")
95+
.file("src/bin/a.rs", "")
96+
.file("src/bin/b.rs", "");
97+
98+
assert_that(p.cargo_process("cargo-run"),
99+
execs().with_status(101)
100+
.with_stderr("`cargo run` requires that a project only \
101+
have one executable\n"));
84102
})
85103

86104
test!(run_dylib_dep {
@@ -113,3 +131,21 @@ test!(run_dylib_dep {
113131
assert_that(p.cargo_process("cargo-run").arg("hello").arg("world"),
114132
execs().with_status(0));
115133
})
134+
135+
test!(run_bin_different_name {
136+
let p = project("foo")
137+
.file("Cargo.toml", r#"
138+
[project]
139+
name = "foo"
140+
version = "0.0.1"
141+
authors = []
142+
143+
[[bin]]
144+
name = "bar"
145+
"#)
146+
.file("src/bar.rs", r#"
147+
fn main() { }
148+
"#);
149+
150+
assert_that(p.cargo_process("cargo-run"), execs().with_status(0));
151+
})

0 commit comments

Comments
 (0)