Skip to content

Commit

Permalink
fix tests when target dir is not CWD or available via CARGO_TARGET_DIR
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismooredev committed Dec 13, 2020
1 parent 1c986a6 commit 933ebfe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion tests/cargo-add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1534,7 +1534,7 @@ fn add_prints_message_for_features_deps() {
let (_tmpdir, manifest) = clone_out_test("tests/fixtures/add/Cargo.toml.sample");

assert_cli::Assert::command(&[
"target/debug/cargo-add",
utils::EXE_PATH_ADD,
"add",
"hello-world",
"--vers",
Expand Down
27 changes: 11 additions & 16 deletions tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ use std::ffi::{OsStr, OsString};
use std::io::prelude::*;
use std::{env, fs, path::Path, path::PathBuf, process};

pub const EXE_PATH_ADD: &'static str = env!("CARGO_BIN_EXE_cargo-add");
pub const EXE_PATH_RM: &'static str = env!("CARGO_BIN_EXE_cargo-rm");
pub const EXE_PATH_UPGRADE: &'static str = env!("CARGO_BIN_EXE_cargo-upgrade");

/// Helper function that copies the workspace test into a temporary directory.
pub fn copy_workspace_test() -> (tempfile::TempDir, String, Vec<String>) {
// Create a temporary directory and copy in the root manifest, the dummy rust file, and
Expand Down Expand Up @@ -190,20 +194,11 @@ pub fn get_toml(manifest_path: &str) -> toml_edit::Document {
}

pub fn get_command_path(s: impl AsRef<OsStr>) -> String {
let target_dir: PathBuf = match env::var_os("CARGO_TARGET_DIR") {
Some(dir) => dir.into(),
None => env::current_dir()
.expect("Failed to get current dir")
.join("target"),
};

let mut binary_name = OsString::from("cargo-");
binary_name.push(s.as_ref());

target_dir
.join("debug")
.join(binary_name)
.to_str()
.unwrap()
.to_string()
// would probably be better to replace all calls to get_command_path, but this reduces the diff
match s.as_ref().to_string_lossy().as_ref() {
"add" => EXE_PATH_ADD.into(),
"rm" => EXE_PATH_RM.into(),
"upgrade" => EXE_PATH_UPGRADE.into(),
_ => panic!("unexpected command {}", s.as_ref().to_string_lossy()),
}
}

0 comments on commit 933ebfe

Please sign in to comment.