Skip to content

Commit b7976d1

Browse files
committed
Test out-dir edge cases
1 parent 028a7b6 commit b7976d1

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

src/cargo/ops/cargo_rustc/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,8 @@ fn link_targets<'a, 'cfg>(
605605
destinations.push(dst.display().to_string());
606606
hardlink_or_copy(&src, &dst)?;
607607
if let Some(ref path) = export_dir {
608-
//TODO: check if dir
609608
if !path.exists() {
610-
fs::create_dir(path)?;
609+
fs::create_dir_all(path)?;
611610
}
612611

613612
hardlink_or_copy(&src, &path.join(dst.file_name().unwrap()))?;

tests/testsuite/out_dir.rs

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
use cargotest::ChannelChanger;
2-
use cargotest::support::{execs, project};
3-
use hamcrest::assert_that;
41
use std::path::Path;
5-
use std::fs;
2+
use std::fs::{self, File};
3+
use std::env;
4+
use std::io::Write;
5+
6+
use hamcrest::assert_that;
7+
8+
use cargotest::{process, ChannelChanger};
9+
use cargotest::support::{execs, project};
610

711
#[test]
812
fn binary_with_debug() {
@@ -195,6 +199,74 @@ fn include_only_the_binary_from_the_current_package() {
195199
);
196200
}
197201

202+
#[test]
203+
fn out_dir_is_a_file() {
204+
let p = project("foo")
205+
.file(
206+
"Cargo.toml",
207+
r#"
208+
[project]
209+
name = "foo"
210+
version = "0.0.1"
211+
authors = []
212+
"#,
213+
)
214+
.file("src/main.rs", r#"fn main() { println!("Hello, World!") }"#)
215+
.build();
216+
File::create(p.root().join("out")).unwrap();
217+
218+
assert_that(
219+
p.cargo("build -Z out-dir --out-dir out")
220+
.masquerade_as_nightly_cargo(),
221+
execs()
222+
.with_status(101)
223+
.with_stderr_contains("[ERROR] failed to link or copy [..]"),
224+
);
225+
}
226+
227+
#[test]
228+
fn replaces_artifacts() {
229+
let p = project("foo")
230+
.file(
231+
"Cargo.toml",
232+
r#"
233+
[project]
234+
name = "foo"
235+
version = "0.0.1"
236+
authors = []
237+
"#,
238+
)
239+
.file("src/main.rs", r#"fn main() { println!("foo") }"#)
240+
.build();
241+
242+
assert_that(
243+
p.cargo("build -Z out-dir --out-dir out")
244+
.masquerade_as_nightly_cargo(),
245+
execs().with_status(0),
246+
);
247+
assert_that(
248+
process(&p.root()
249+
.join(&format!("out/foo{}", env::consts::EXE_SUFFIX))),
250+
execs().with_stdout("foo"),
251+
);
252+
253+
fs::File::create(p.root().join("src/main.rs"))
254+
.unwrap()
255+
.write_all(br#"fn main() { println!("bar") }"#)
256+
.unwrap();
257+
258+
assert_that(
259+
p.cargo("build -Z out-dir --out-dir out")
260+
.masquerade_as_nightly_cargo(),
261+
execs().with_status(0),
262+
);
263+
assert_that(
264+
process(&p.root()
265+
.join(&format!("out/foo{}", env::consts::EXE_SUFFIX))),
266+
execs().with_stdout("bar"),
267+
);
268+
}
269+
198270
fn check_dir_contents(
199271
out_dir: &Path,
200272
expected_linux: &[&str],

0 commit comments

Comments
 (0)