Clean up process DSL in tests
authorYehuda Katz <wycats@gmail.com>
Fri, 9 May 2014 00:50:28 +0000 (17:50 -0700)
committerYehuda Katz <wycats@gmail.com>
Fri, 9 May 2014 00:50:58 +0000 (17:50 -0700)
tests/support.rs
tests/test_cargo_compile.rs

index 72ae985d0a1e784f370d71beba5b79fc50b21e19..cae03d45b73480c75036e5a64b4b28b2a62b1dfa 100644 (file)
@@ -66,9 +66,11 @@ impl ProjectBuilder {
     }
 
     pub fn cargo_process(&self, program: &str) -> ProcessBuilder {
-      process(program)
-        .cwd(self.root())
-        .extra_path(cargo_dir())
+        self.build();
+
+        process(program)
+            .cwd(self.root())
+            .extra_path(cargo_dir())
     }
 
     pub fn file<B: BytesContainer>(mut self, path: B, body: &str) -> ProjectBuilder {
@@ -77,7 +79,7 @@ impl ProjectBuilder {
     }
 
     // TODO: return something different than a ProjectBuilder
-    pub fn build(self) -> ProjectBuilder {
+    pub fn build<'a>(&'a self) -> &'a ProjectBuilder {
         match self.build_with_result() {
             Err(e) => fail!(e),
             _ => return self
@@ -208,7 +210,7 @@ impl ham::Matcher<ProcessBuilder> for Execs {
 
     match res {
       Ok(out) => self.match_output(&out),
-      Err(_) => Err("could not exec process".to_owned())
+      Err(_) => Err(format!("could not exec process {}", process))
     }
   }
 }
index 93fa0ff0b6aeecd4da02fc106bb9d4d69eea9ced..f3e88d94e0fd94910b7d8b6ba9ae9ff952bd9ad0 100644 (file)
@@ -1,6 +1,7 @@
 use support::{ResultTest,project,execs};
 use hamcrest::{assert_that,existing_file};
 use cargo;
+use cargo::util::process;
 
 fn setup() {
 }
@@ -18,25 +19,32 @@ test!(cargo_compile {
 
             name = "foo"
         "#)
-        .file("src/foo.rs", r#"
-            fn main() {
-                println!("i am foo");
-            }
-        "#)
-        .build();
-
-    p.cargo_process("cargo-compile")
-      .args([])
-      .exec_with_output()
-      .assert();
+        .file("src/foo.rs", main_file(r#""i am foo""#, []));
 
+    assert_that(p.cargo_process("cargo-compile"), execs());
     assert_that(&p.root().join("target/foo"), existing_file());
 
+    let target = p.root().join("target");
+
     assert_that(
-      cargo::util::process("foo").extra_path(p.root().join("target")),
+      process("foo").extra_path(target),
       execs().with_stdout("i am foo\n"));
 })
 
+fn main_file(println: &str, deps: &[&str]) -> ~str {
+    let mut buf = StrBuf::new();
+
+    for dep in deps.iter() {
+        buf.push_str(format!("extern crate {};\n", dep));
+    }
+
+    buf.push_str("fn main() { println!(");
+    buf.push_str(println);
+    buf.push_str("); }\n");
+
+    buf.to_owned()
+}
+
 test!(cargo_compile_with_nested_deps {
     let mut p = project("foo");
     let bar = p.root().join("bar");
@@ -61,13 +69,7 @@ test!(cargo_compile_with_nested_deps {
 
             name = "foo"
         "#)
-        .file("src/foo.rs", r#"
-            extern crate bar;
-
-            fn main() {
-                println!("{}", bar::gimme());
-            }
-        "#)
+        .file("src/foo.rs", main_file(r#""{}", bar::gimme()"#, ["bar"]))
         .file("bar/Cargo.toml", r#"
             [project]
 
@@ -105,8 +107,7 @@ test!(cargo_compile_with_nested_deps {
             pub fn gimme() -> ~str {
                 "test passed".to_owned()
             }
-        "#)
-        .build();
+        "#);
 
     p.cargo_process("cargo-compile")
         .exec_with_output()