roc/crates/cli_test_utils/src/helpers.rs
2025-03-24 16:43:18 +01:00

53 lines
1.5 KiB
Rust

extern crate bumpalo;
extern crate roc_collections;
extern crate roc_load;
extern crate roc_module;
extern crate tempfile;
use roc_command_utils::root_dir;
use std::env;
use std::path::PathBuf;
pub fn path_to_roc_binary() -> PathBuf {
path_to_binary(if cfg!(windows) { "roc.exe" } else { "roc" })
}
pub fn path_to_binary(binary_name: &str) -> PathBuf {
// Adapted from https://github.com/volta-cli/volta/blob/cefdf7436a15af3ce3a38b8fe53bb0cfdb37d3dd/tests/acceptance/support/sandbox.rs#L680
// by the Volta Contributors - license information can be found in
// the legal_details file in the root directory of this distribution.
//
// Thank you, Volta contributors!
let mut path = env::var_os("CARGO_BIN_PATH")
.map(PathBuf::from)
.or_else(|| {
env::current_exe().ok().map(|mut path| {
path.pop();
if path.ends_with("deps") {
path.pop();
}
path
})
})
.unwrap_or_else(|| panic!("CARGO_BIN_PATH wasn't set, and couldn't be inferred from context. Can't run CLI tests."));
path.push(binary_name);
path
}
pub fn dir_from_root(dir_name: &str) -> PathBuf {
let mut path = root_dir();
path.extend(dir_name.split('/')); // Make slashes cross-target
path
}
pub fn file_from_root(dir_name: &str, file_name: &str) -> PathBuf {
let mut path = dir_from_root(dir_name);
path.push(file_name);
path
}