Test out-dir edge cases
authorAleksey Kladov <aleksey.kladov@gmail.com>
Tue, 3 Apr 2018 13:06:56 +0000 (16:06 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Tue, 3 Apr 2018 13:08:09 +0000 (16:08 +0300)
src/cargo/ops/cargo_rustc/mod.rs
tests/testsuite/out_dir.rs

index fe6a5b5f991971ad749e0f6c1f9795528ce82156..52e6cccaf28753fd196aa7e38e60d101d84aea43 100644 (file)
@@ -605,9 +605,8 @@ fn link_targets<'a, 'cfg>(
             destinations.push(dst.display().to_string());
             hardlink_or_copy(&src, &dst)?;
             if let Some(ref path) = export_dir {
-                //TODO: check if dir
                 if !path.exists() {
-                    fs::create_dir(path)?;
+                    fs::create_dir_all(path)?;
                 }
 
                 hardlink_or_copy(&src, &path.join(dst.file_name().unwrap()))?;
index 231f50122313900e7fc63f820db4efcb726cce36..9a241ae264c099496c188ad367553fbeea0018b7 100644 (file)
@@ -1,8 +1,12 @@
-use cargotest::ChannelChanger;
-use cargotest::support::{execs, project};
-use hamcrest::assert_that;
 use std::path::Path;
-use std::fs;
+use std::fs::{self, File};
+use std::env;
+use std::io::Write;
+
+use hamcrest::assert_that;
+
+use cargotest::{process, ChannelChanger};
+use cargotest::support::{execs, project};
 
 #[test]
 fn binary_with_debug() {
@@ -195,6 +199,74 @@ fn include_only_the_binary_from_the_current_package() {
     );
 }
 
+#[test]
+fn out_dir_is_a_file() {
+    let p = project("foo")
+        .file(
+            "Cargo.toml",
+            r#"
+            [project]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+        "#,
+        )
+        .file("src/main.rs", r#"fn main() { println!("Hello, World!") }"#)
+        .build();
+    File::create(p.root().join("out")).unwrap();
+
+    assert_that(
+        p.cargo("build -Z out-dir --out-dir out")
+            .masquerade_as_nightly_cargo(),
+        execs()
+            .with_status(101)
+            .with_stderr_contains("[ERROR] failed to link or copy [..]"),
+    );
+}
+
+#[test]
+fn replaces_artifacts() {
+    let p = project("foo")
+        .file(
+            "Cargo.toml",
+            r#"
+            [project]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+        "#,
+        )
+        .file("src/main.rs", r#"fn main() { println!("foo") }"#)
+        .build();
+
+    assert_that(
+        p.cargo("build -Z out-dir --out-dir out")
+            .masquerade_as_nightly_cargo(),
+        execs().with_status(0),
+    );
+    assert_that(
+        process(&p.root()
+            .join(&format!("out/foo{}", env::consts::EXE_SUFFIX))),
+        execs().with_stdout("foo"),
+    );
+
+    fs::File::create(p.root().join("src/main.rs"))
+        .unwrap()
+        .write_all(br#"fn main() { println!("bar") }"#)
+        .unwrap();
+
+    assert_that(
+        p.cargo("build -Z out-dir --out-dir out")
+            .masquerade_as_nightly_cargo(),
+        execs().with_status(0),
+    );
+    assert_that(
+        process(&p.root()
+            .join(&format!("out/foo{}", env::consts::EXE_SUFFIX))),
+        execs().with_stdout("bar"),
+    );
+}
+
 fn check_dir_contents(
     out_dir: &Path,
     expected_linux: &[&str],