]> git.proxmox.com Git - cargo.git/commitdiff
Fix `cargo run` for renamed bin targets
authorAlex Crichton <alex@alexcrichton.com>
Fri, 22 Aug 2014 16:53:56 +0000 (09:53 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 27 Aug 2014 00:57:01 +0000 (17:57 -0700)
src/cargo/ops/cargo_run.rs
tests/test_cargo_run.rs

index 67a1f7c2fb0b296bd1b22baa8056519425a89ed2..ea8af82865846c03835e1257151d50de00887281 100644 (file)
@@ -1,23 +1,30 @@
 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,
index adc4f0c6ebb810cfb85e8996ab35effcb164d8e8..fae6001e9f3579c0dd6dfe80025c624569dcd0dc 100644 (file)
@@ -79,8 +79,26 @@ test!(no_main_file {
 
     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 {
@@ -113,3 +131,21 @@ 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));
+})