use std::os;
use ops;
-use util::{CargoResult, human, process, ProcessError};
+use util::{CargoResult, human, process, ProcessError, Require};
use core::source::Source;
use sources::PathSource;
pub fn run(manifest_path: &Path,
options: &mut ops::CompileOptions,
args: &[String]) -> CargoResult<Option<ProcessError>> {
- if !manifest_path.dir_path().join("src").join("main.rs").exists() {
- return Err(human("`src/main.rs` must be present for `cargo run`"))
- }
-
let mut src = try!(PathSource::for_path(&manifest_path.dir_path()));
try!(src.update());
let root = try!(src.get_root_package());
+ let mut bins = root.get_manifest().get_targets().iter().filter(|a| {
+ a.is_bin() && a.get_profile().is_compile()
+ });
+ let bin = try!(bins.next().require(|| {
+ human("a bin target must be available for `cargo run`")
+ }));
+ match bins.next() {
+ Some(..) => return Err(human("`cargo run` requires that a project only \
+ have one executable")),
+ None => {}
+ }
let compile = try!(ops::compile(manifest_path, options));
- let exe = manifest_path.dir_path().join("target").join(root.get_name());
+ let exe = manifest_path.dir_path().join("target").join(bin.get_name());
let exe = match exe.path_relative_from(&os::getcwd()) {
Some(path) => path,
None => exe,
assert_that(p.cargo_process("cargo-run"),
execs().with_status(101)
- .with_stderr("`src/main.rs` must be present for \
- `cargo run`\n"));
+ .with_stderr("a bin target must be available \
+ for `cargo run`\n"));
+})
+
+test!(too_many_bins {
+ let p = project("foo")
+ .file("Cargo.toml", r#"
+ [project]
+ name = "foo"
+ version = "0.0.1"
+ authors = []
+ "#)
+ .file("src/lib.rs", "")
+ .file("src/bin/a.rs", "")
+ .file("src/bin/b.rs", "");
+
+ assert_that(p.cargo_process("cargo-run"),
+ execs().with_status(101)
+ .with_stderr("`cargo run` requires that a project only \
+ have one executable\n"));
})
test!(run_dylib_dep {
assert_that(p.cargo_process("cargo-run").arg("hello").arg("world"),
execs().with_status(0));
})
+
+test!(run_bin_different_name {
+ let p = project("foo")
+ .file("Cargo.toml", r#"
+ [project]
+ name = "foo"
+ version = "0.0.1"
+ authors = []
+
+ [[bin]]
+ name = "bar"
+ "#)
+ .file("src/bar.rs", r#"
+ fn main() { }
+ "#);
+
+ assert_that(p.cargo_process("cargo-run"), execs().with_status(0));
+})