A passing test
authorCarl Lerche <me@carllerche.com>
Thu, 20 Mar 2014 00:57:12 +0000 (17:57 -0700)
committerCarl Lerche <me@carllerche.com>
Thu, 20 Mar 2014 00:57:12 +0000 (17:57 -0700)
Makefile
src/bin/cargo-compile.rs
src/bin/cargo-read-manifest.rs
src/bin/cargo-rustc.rs
src/bin/cargo-verify-project.rs
src/cargo/mod.rs
src/cargo/util/mod.rs
src/cargo/util/process_builder.rs
tests/support.rs
tests/test_cargo_compile.rs
tests/tests.rs

index 71c621d3e8f97ee1832383f2265efb02f63257fb..99b7559f842088eafec7d56f623dfcc2f74052c4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -52,7 +52,7 @@ tests/tests: $(BIN_TARGETS) $(HAMCREST) $(TEST_SRC)
        $(RUSTC) --test --crate-type=lib $(TEST_DEPS) -Ltarget --out-dir tests tests/tests.rs
 
 test-integration: tests/tests
-       tests/tests
+       CARGO_BIN_PATH=$(PWD)/target/ tests/tests
 
 test: test-integration
 
index 39e885b23549a3b2b15b2db793fbd22d84100383..34ca4ee196d5862ce7126381e802aadf6589f3e2 100644 (file)
@@ -1,4 +1,5 @@
 #[crate_id="cargo-compile"];
+#[allow(deprecated_owned_vector)];
 
 extern crate serialize;
 extern crate hammer;
index e7d9fc0d134ced94e2a1c0ff478e422e14105f7e..5e99cf56f73c2bfddbe8e9075e857cac6bfc8569 100644 (file)
@@ -1,4 +1,5 @@
 #[crate_id="cargo-read-manifest"];
+#[allow(deprecated_owned_vector)];
 
 extern crate cargo;
 extern crate hammer;
@@ -111,7 +112,7 @@ fn normalize(lib: &Option<~[SerializedLibTarget]>, bin: &Option<~[SerializedExec
             let b = b_ref.clone();
             let mut path = b.path.clone();
             if path.is_none() {
-                path = Some(format!("src/bin/{}.rs", b.name.clone()));
+                path = Some(format!("src/{}.rs", b.name.clone()));
             }
             ExecTarget{ path: path.unwrap(), name: b.name }
         });
index 4e7778655259771498db2135d463bc76e94800e1..4e55d37e9c71c14d43ab5d8177755e5306b4fe9d 100644 (file)
@@ -1,4 +1,5 @@
 #[crate_id="cargo-rustc"];
+#[allow(deprecated_owned_vector)];
 
 extern crate toml;
 extern crate serialize;
@@ -10,7 +11,7 @@ use std::io::process::{Process,ProcessConfig,InheritFd};
 use serialize::json;
 use serialize::Decodable;
 use std::path::Path;
-use cargo::{Manifest,CargoResult,ToCargoError};
+use cargo::{Manifest,CargoResult,CargoError,ToCargoError};
 
 /**
     cargo-rustc -- ...args
@@ -21,7 +22,7 @@ use cargo::{Manifest,CargoResult,ToCargoError};
 fn main() {
     match execute() {
         Err(e) => {
-            println!("{}", e.message);
+            write!(&mut std::io::stderr(), "{}", e.message);
             // TODO: Exit with error code
         },
         _ => return
@@ -36,16 +37,23 @@ fn execute() -> CargoResult<()> {
     let mut decoder = json::Decoder::new(json);
     let manifest: Manifest = Decodable::decode(&mut decoder);
 
-    let Manifest{ root, lib, .. } = manifest;
+    let Manifest{ root, lib, bin, .. } = manifest;
+
+    let (crate_type, out_dir) = if lib.len() > 0 {
+        ( ~"lib", lib[0].path )
+    } else if bin.len() > 0 {
+        ( ~"bin", bin[0].path )
+    } else {
+        return Err(CargoError::new(~"bad manifest, no lib or bin specified", 1));
+    };
 
     let root = Path::new(root);
-    let out_dir = lib[0].path;
     let target = join(&root, ~"target");
 
     let args = [
         join(&root, out_dir),
         ~"--out-dir", target,
-        ~"--crate-type", ~"lib"
+        ~"--crate-type", crate_type
     ];
 
     match io::fs::mkdir_recursive(&root.join("target"), io::UserRWX) {
@@ -75,3 +83,11 @@ fn execute() -> CargoResult<()> {
 fn join(path: &Path, part: ~str) -> ~str {
     format!("{}", path.join(part).display())
 }
+
+fn vec_idx<T>(v: ~[T], idx: uint) -> Option<T> {
+    if idx < v.len() {
+        Some(v[idx])
+    } else {
+        None
+    }
+}
index 7ee47ae88daea4d71c8dee001e85e52095ae70da..255a585e02672f26cad04a583ad805e22aa5f7b1 100644 (file)
@@ -1,4 +1,5 @@
 #[crate_id="cargo-verify-project"];
+#[allow(deprecated_owned_vector)];
 
 extern crate toml;
 extern crate getopts;
index 837b0ba9bc1a7223b5873e347169b772ab80c258..8ea9d950723f7d461fff70a333c92bdab292ff72 100644 (file)
@@ -1,6 +1,8 @@
 #[crate_id="cargo"];
 #[crate_type="rlib"];
 
+#[allow(deprecated_owned_vector)];
+
 extern crate serialize;
 use serialize::{Decoder};
 use std::fmt;
index 352b45227a39af8e9856e772d42993decc4b6a2a..27784a1ec02593856ff609d79bd4800b101b8291 100644 (file)
@@ -1,2 +1,2 @@
 pub use self::process_builder::process;
-mod process_builder;
+pub mod process_builder;
index e45063f04c881e07dd14b2f99cc0703886f5d027..821fa4789a266cc2158cc16b46879dd4e450116c 100644 (file)
@@ -1,26 +1,63 @@
 use std;
 use std::os;
-use std::io::process::{Process,ProcessConfig,InheritFd};
+use std::path::Path;
+use std::io::IoResult;
+use std::io::process::{Process,ProcessConfig,ProcessOutput};
+use ToCargoError;
+use CargoResult;
 
 pub struct ProcessBuilder {
   program: ~str,
   args: ~[~str],
-  path: ~[~str]
+  path: ~[~str],
+  cwd: Path
 }
 
+// TODO: Upstream a Windows/Posix branch to Rust proper
+static PATH_SEP : &'static str = ":";
+
 impl ProcessBuilder {
   pub fn args(mut self, arguments: &[~str]) -> ProcessBuilder {
     self.args = arguments.to_owned();
     self
   }
-}
 
-pub fn process(cmd: &str) -> ProcessBuilder {
-  ProcessBuilder { program: cmd.to_owned(), args: ~[], path: get_curr_path() }
+  pub fn extra_path(mut self, path: &str) -> ProcessBuilder {
+    self.path.push(path.to_owned());
+    self
+  }
+
+  pub fn cwd(mut self, path: Path) -> ProcessBuilder {
+    self.cwd = path;
+    self
+  }
+
+  pub fn exec_with_output(self) -> CargoResult<ProcessOutput> {
+    let mut config = ProcessConfig::new();
+
+    println!("cwd: {}", self.cwd.display());
+
+    config.program = self.program.as_slice();
+    config.args = self.args.as_slice();
+    config.cwd = Some(&self.cwd);
+
+    let os_path = try!(os::getenv("PATH").to_cargo_error(~"Could not find the PATH environment variable", 1));
+    let path = os_path + PATH_SEP + self.path.connect(PATH_SEP);
+
+    let path = [(~"PATH", path)];
+    config.env = Some(path.as_slice());
+
+    println!("{:?}", config);
+
+    Process::configure(config).map(|mut ok| ok.wait_with_output()).to_cargo_error(~"Could not spawn process", 1)
+  }
 }
 
-pub fn get_curr_path() -> ~[~str] {
-  os::getenv("PATH").map(|path| {
-    path.split(std::path::SEP).map(|seg| seg.to_owned()).collect()
-  }).unwrap_or(~[])
+pub fn process(cmd: &str) -> ProcessBuilder {
+  ProcessBuilder {
+    program: cmd.to_owned(),
+    args: ~[],
+    path: ~[],
+    cwd: os::getcwd()
+  }
 }
index 293a28d1e2f769cc507d239603cb4b4d42c65087..0c5242dffabaa9a54fcec5f63659e71d9182558a 100644 (file)
@@ -49,19 +49,24 @@ impl ProjectBuilder {
         }
     }
 
+    pub fn root(&self) -> Path {
+      self.root.clone()
+    }
+
     pub fn file(mut self, path: &str, body: &str) -> ProjectBuilder {
         self.files.push(FileBuilder::new(self.root.join(path), body));
         self
     }
 
-    pub fn build(self) {
+    // TODO: return something different than a ProjectBuilder
+    pub fn build(self) -> ProjectBuilder {
         match self.build_with_result() {
             Err(e) => fail!(e),
-            _ => return
+            _ => return self
         }
     }
 
-    pub fn build_with_result(self) -> Result<(), ~str> {
+    pub fn build_with_result(&self) -> Result<(), ~str> {
         // First, clean the directory if it already exists
         try!(self.rm_root());
 
index 466853675de6679b69f905fdb2d1ad12c9990a0b..48690aa3bb45cbeed2edeb6baf345608f9fad1ea 100644 (file)
@@ -1,3 +1,4 @@
+use std;
 use support::project;
 use cargo;
 
@@ -6,7 +7,7 @@ fn setup() {
 }
 
 test!(cargo_compile_with_explicit_manifest_path {
-    project("foo")
+    let p = project("foo")
         .file("Cargo.toml", r#"
             [project]
 
@@ -24,13 +25,29 @@ test!(cargo_compile_with_explicit_manifest_path {
             }"#)
         .build();
 
-     cargo::util::process("cargo-compile")
-       .args([]);
-     //   //.extra_path("target/")
-     //   //.cwd("/foo/bar")
-     //   //.exec_with_output()
+    let output = cargo::util::process("cargo-compile")
+      .args([~"--manifest-path", ~"Cargo.toml"])
+      .extra_path(target_path())
+      .cwd(p.root())
+      .exec_with_output();
+
+    match output {
+      Ok(out) => {
+        println!("out:\n{}\n", std::str::from_utf8(out.output));
+        println!("err:\n{}\n", std::str::from_utf8(out.error));
+      },
+      Err(e) => println!("err: {}", e)
+    }
+
+    assert!(p.root().join("target/foo").exists(), "the executable exists");
+
+    let o = cargo::util::process("foo")
+      .extra_path(format!("{}", p.root().join("target").display()))
+      .exec_with_output()
+      .unwrap();
+
+    assert_eq!(std::str::from_utf8(o.output).unwrap(), "i am foo\n");
 
-    fail!("not implemented");
     // 1) Setup project
     // 2) Run cargo-compile --manifest-path /tmp/bar/zomg
     // 3) assertThat(target/foo) exists assertThat("target/foo", isCompiledBin())
@@ -38,3 +55,9 @@ test!(cargo_compile_with_explicit_manifest_path {
 })
 
 // test!(compiling_project_with_invalid_manifest)
+
+fn target_path() -> ~str {
+  std::os::getenv("CARGO_BIN_PATH").unwrap_or_else(|| {
+    fail!("CARGO_BIN_PATH wasn't set. Cannot continue running test")
+  })
+}
index 6eea1cf8916118ec7702f17777921818c6cf22a5..9e51bc5596a23a19c7feabceb46afe2e3b6540d4 100644 (file)
@@ -1,4 +1,5 @@
 #[feature(macro_rules)];
+#[allow(deprecated_owned_vector)];
 
 extern crate cargo;