Skip to content

Commit f76a679

Browse files
authored
Replace deprecated cargo_bin with cargo_bin! macro (#579)
The `assert_cmd::cargo::cargo_bin()` function relies on Cargo's internal directory structure to locate test binaries. This breaks when `CARGO_BUILD_BUILD_DIR` is set, and will fail with upcoming Cargo changes (rust-lang/cargo#16147, rust-lang/cargo#15010).
1 parent 3dd6989 commit f76a679

File tree

3 files changed

+49
-42
lines changed

3 files changed

+49
-42
lines changed

tests/integration_util/mod.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2024 Martin Pool
2+
3+
//! Utilities specific to integration tests that need access to the binary under test.
4+
5+
use std::env;
6+
7+
/// Create a Command to run cargo-mutants for integration tests.
8+
pub fn run() -> assert_cmd::Command {
9+
let mut cmd = assert_cmd::Command::new(assert_cmd::cargo_bin!("cargo-mutants"));
10+
// Strip any options configured in the environment running these tests,
11+
// so that they don't cause unexpected behavior in the code under test.
12+
//
13+
// For example, without this,
14+
// `env CARGO_MUTANTS_JOBS=4 cargo mutants`
15+
//
16+
// would end up with tests running 4 jobs by default, which would cause
17+
// the tests to fail.
18+
//
19+
// Even more generally than that example, we want the tests to be as hermetic
20+
// as reasonably possible.
21+
//
22+
// Also strip GITHUB_ACTION to avoid automatically emitting github annotations,
23+
// so that tests are more hermetic and reproducible between local and CI.
24+
env::vars()
25+
.map(|(k, _v)| k)
26+
.filter(|k| {
27+
k.starts_with("CARGO_MUTANTS_")
28+
|| k == "CLICOLOR_FORCE"
29+
|| k == "NOCOLOR"
30+
|| k == "CARGO_TERM_COLOR"
31+
|| k == "GITHUB_ACTION"
32+
})
33+
.for_each(|k| {
34+
cmd.env_remove(k);
35+
});
36+
cmd
37+
}
38+
39+
/// Returns the path to the cargo-mutants binary under test.
40+
pub fn main_binary() -> &'static std::path::Path {
41+
assert_cmd::cargo_bin!("cargo-mutants")
42+
}

tests/main.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ use serde_json::json;
2121
use similar::TextDiff;
2222
use tempfile::{tempdir, NamedTempFile, TempDir};
2323

24+
mod integration_util;
2425
mod util;
26+
use integration_util::run;
2527
use util::{
26-
assert_bytes_eq_json, copy_of_testdata, copy_testdata_to, outcome_json_counts, run,
27-
CommandInstaExt, OUTER_TIMEOUT,
28+
assert_bytes_eq_json, copy_of_testdata, copy_testdata_to, outcome_json_counts, CommandInstaExt,
29+
OUTER_TIMEOUT,
2830
};
2931

3032
#[test]
@@ -1413,7 +1415,7 @@ fn interrupt_caught_and_kills_children() {
14131415
use nix::sys::signal::{kill, SIGTERM};
14141416
use nix::unistd::Pid;
14151417

1416-
use crate::util::MAIN_BINARY;
1418+
use crate::integration_util::main_binary;
14171419

14181420
let tmp_src_dir = copy_of_testdata("well_tested");
14191421
// We can't use `assert_cmd` `timeout` here because that sends the child a `SIGKILL`,
@@ -1426,7 +1428,7 @@ fn interrupt_caught_and_kills_children() {
14261428
// Skip baseline because firstly it should already pass but more importantly
14271429
// #333 exhibited only during non-baseline scenarios.
14281430
let args = [
1429-
MAIN_BINARY.to_str().unwrap(),
1431+
main_binary().to_str().unwrap(),
14301432
"mutants",
14311433
"--timeout=300",
14321434
"--baseline=skip",

tests/util/mod.rs

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88
//! cargo-mutants (as `use crate::test_util`).
99
1010
use std::borrow::Borrow;
11-
use std::env;
1211
use std::fs::{read_dir, read_to_string, rename};
13-
use std::path::{Path, PathBuf};
12+
use std::path::Path;
1413
use std::time::Duration;
1514

1615
use itertools::Itertools;
17-
use lazy_static::lazy_static;
1816
use tempfile::TempDir;
1917

2018
/// A timeout for a `cargo mutants` invocation from the test suite. Needs to be
@@ -23,41 +21,6 @@ use tempfile::TempDir;
2321
/// forever.
2422
pub const OUTER_TIMEOUT: Duration = Duration::from_secs(60);
2523

26-
lazy_static! {
27-
pub static ref MAIN_BINARY: PathBuf = assert_cmd::cargo::cargo_bin("cargo-mutants");
28-
}
29-
30-
pub fn run() -> assert_cmd::Command {
31-
let mut cmd = assert_cmd::Command::new(MAIN_BINARY.as_os_str());
32-
// Strip any options configured in the environment running these tests,
33-
// so that they don't cause unexpected behavior in the code under test.
34-
//
35-
// For example, without this,
36-
// `env CARGO_MUTANTS_JOBS=4 cargo mutants`
37-
//
38-
// would end up with tests running 4 jobs by default, which would cause
39-
// the tests to fail.
40-
//
41-
// Even more generally than that example, we want the tests to be as hermetic
42-
// as reasonably possible.
43-
//
44-
// Also strip GITHUB_ACTION to avoid automatically emitting github annotations,
45-
// so that tests are more hermetic and reproducible between local and CI.
46-
env::vars()
47-
.map(|(k, _v)| k)
48-
.filter(|k| {
49-
k.starts_with("CARGO_MUTANTS_")
50-
|| k == "CLICOLOR_FORCE"
51-
|| k == "NOCOLOR"
52-
|| k == "CARGO_TERM_COLOR"
53-
|| k == "GITHUB_ACTION"
54-
})
55-
.for_each(|k| {
56-
cmd.env_remove(k);
57-
});
58-
cmd
59-
}
60-
6124
pub trait CommandInstaExt {
6225
fn assert_insta(&mut self, snapshot_name: &str);
6326
}

0 commit comments

Comments
 (0)