Skip to content

Commit

Permalink
test: Simplify get_exec_path using CARGO_BIN_EXE_toml
Browse files Browse the repository at this point in the history
This seems to be the way Cargo docs prescribe:
  https://doc.rust-lang.org/cargo/reference/cargo-targets.html#integration-tests

The code comes out much nicer... and it also behaves more robustly,
because Cargo doesn't set CARGO_TARGET_DIR; it reads it, and there
are several other ways to tell Cargo the same thing:
  https://doc.rust-lang.org/cargo/reference/config.html#buildtarget-dir

So this new version will work correctly with, e.g.,
`cargo test --target-dir=…`, while the old version would break.
  • Loading branch information
gnprice committed Dec 11, 2022
1 parent e4bc812 commit 3ee1219
Showing 1 changed file with 1 addition and 11 deletions.
12 changes: 1 addition & 11 deletions test/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::env;
use std::ffi::OsString;
use std::fs;
use std::path::PathBuf;
use std::process;
Expand Down Expand Up @@ -156,7 +155,7 @@ struct TestCaseState {

impl TestCaseState {
pub fn new() -> Self {
let cmd = process::Command::new(get_exec_path());
let cmd = process::Command::new(env!("CARGO_BIN_EXE_toml"));
let dir = tempfile::tempdir().expect("failed to create tempdir");
let filename = dir.path().join("test.toml");
TestCaseState { cmd, dir, filename }
Expand Down Expand Up @@ -209,15 +208,6 @@ impl TestCaseState {
}
}

fn get_exec_path() -> PathBuf {
// TODO is there no cleaner way to get this from Cargo?
// Also should it really be "debug"?
let target_dir: PathBuf = env::var_os("CARGO_TARGET_DIR")
.unwrap_or_else(|| OsString::from("target"))
.into();
target_dir.join("debug").join("toml")
}

/// Like `assert!(actual.contains(pattern))`, but with more informative output.
#[rustfmt::skip]
fn check_contains(pattern: &str, actual: &str) {
Expand Down

0 comments on commit 3ee1219

Please sign in to comment.