Skip to content

Commit

Permalink
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
9 changes: 6 additions & 3 deletions tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3171,9 +3171,12 @@ fn switch_features_rerun() {
)
.build();

p.cargo("run -v --features=foo").with_stdout("foo\n").run();
p.cargo("run -v").with_stdout("bar\n").run();
p.cargo("run -v --features=foo").with_stdout("foo\n").run();
p.cargo("build -v --features=foo").run();
p.rename_run("foo", "with_foo").with_stdout("foo\n").run();
p.cargo("build -v").run();
p.rename_run("foo", "without_foo").with_stdout("bar\n").run();
p.cargo("build -v --features=foo").run();
p.rename_run("foo", "with_foo2").with_stdout("foo\n").run();
}

#[test]
Expand Down
19 changes: 4 additions & 15 deletions tests/testsuite/freshness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,17 +447,6 @@ fn changing_bin_features_caches_targets() {
)
.build();

// Windows has a problem with replacing a binary that was just executed.
// Unlinking it will succeed, but then attempting to immediately replace
// it will sometimes fail with "Already Exists".
// See https://github.com/rust-lang/cargo/issues/5481
let foo_proc = |name: &str| {
let src = p.bin("foo");
let dst = p.bin(name);
fs::rename(&src, &dst).expect("Failed to rename foo");
p.process(dst)
};

p.cargo("build")
.with_stderr(
"\
Expand All @@ -466,7 +455,7 @@ fn changing_bin_features_caches_targets() {
",
)
.run();
foo_proc("off1").with_stdout("feature off").run();
p.rename_run("foo", "off1").with_stdout("feature off").run();

p.cargo("build --features foo")
.with_stderr(
Expand All @@ -476,7 +465,7 @@ fn changing_bin_features_caches_targets() {
",
)
.run();
foo_proc("on1").with_stdout("feature on").run();
p.rename_run("foo", "on1").with_stdout("feature on").run();

/* Targets should be cached from the first build */

Expand All @@ -487,7 +476,7 @@ fn changing_bin_features_caches_targets() {
",
)
.run();
foo_proc("off2").with_stdout("feature off").run();
p.rename_run("foo", "off2").with_stdout("feature off").run();

p.cargo("build --features foo")
.with_stderr(
Expand All @@ -496,7 +485,7 @@ fn changing_bin_features_caches_targets() {
",
)
.run();
foo_proc("on2").with_stdout("feature on").run();
p.rename_run("foo", "on2").with_stdout("feature on").run();
}

#[test]
Expand Down
21 changes: 21 additions & 0 deletions tests/testsuite/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,27 @@ impl Project {
execs
}

/// Safely run a process after `cargo build`.
///
/// Windows has a problem where a process cannot be reliably
/// be replaced, removed, or renamed immediately after executing it.
/// The action may fail (with errors like Access is denied), or
/// it may succeed, but future attempts to use the same filename
/// will fail with "Already Exists".
///
/// If you have a test that needs to do `cargo run` multiple
/// times, you should instead use `cargo build` and use this
/// method to run the executable. Each time you call this,
/// use a new name for `dst`.
/// See https://github.com/rust-lang/cargo/issues/5481
pub fn rename_run(&self, src: &str, dst: &str) -> Execs {
let src = self.bin(src);
let dst = self.bin(dst);
fs::rename(&src, &dst)
.unwrap_or_else(|e| panic!("Failed to rename `{:?}` to `{:?}`: {}", src, dst, e));
self.process(dst)
}

/// Returns the contents of `Cargo.lock`.
pub fn read_lockfile(&self) -> String {
self.read_file("Cargo.lock")
Expand Down

0 comments on commit 286896f

Please sign in to comment.