Skip to content

Commit

Permalink
fix(cli/installer): Fix concurrent env handling in tests (denoland#5182)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn authored May 9, 2020
1 parent b8364a2 commit 7a635ed
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions cli/installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ deno {} "$@"
}

fn get_installer_root() -> Result<PathBuf, Error> {
if let Ok(env_dir) = env::var("DENO_INSTALL_ROOT").map(PathBuf::from) {
return env_dir.canonicalize();
if let Ok(env_dir) = env::var("DENO_INSTALL_ROOT") {
if !env_dir.is_empty() {
return PathBuf::from(env_dir).canonicalize();
}
}
// In Windows's Powershell $HOME environmental variable maybe null
// if so use $USERPROFILE instead.
Expand Down Expand Up @@ -248,8 +250,13 @@ fn is_in_path(dir: &PathBuf) -> bool {
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Mutex;
use tempfile::TempDir;

lazy_static! {
pub static ref ENV_LOCK: Mutex<()> = Mutex::new(());
}

#[test]
fn test_is_remote_url() {
assert!(is_remote_url("https://deno.land/std/http/file_server.ts"));
Expand Down Expand Up @@ -317,6 +324,7 @@ mod tests {

#[test]
fn install_basic() {
let _guard = ENV_LOCK.lock().ok();
let temp_dir = TempDir::new().expect("tempdir fail");
let temp_dir_str = temp_dir.path().to_string_lossy().to_string();
// NOTE: this test overrides environmental variables
Expand All @@ -325,8 +333,10 @@ mod tests {
// It means that other test can override env vars when this test is running.
let original_home = env::var_os("HOME");
let original_user_profile = env::var_os("HOME");
let original_install_root = env::var_os("DENO_INSTALL_ROOT");
env::set_var("HOME", &temp_dir_str);
env::set_var("USERPROFILE", &temp_dir_str);
env::set_var("DENO_INSTALL_ROOT", "");

install(
Flags::default(),
Expand Down Expand Up @@ -357,6 +367,9 @@ mod tests {
if let Some(user_profile) = original_user_profile {
env::set_var("USERPROFILE", user_profile);
}
if let Some(install_root) = original_install_root {
env::set_var("DENO_INSTALL_ROOT", install_root);
}
}

#[test]
Expand Down Expand Up @@ -475,9 +488,11 @@ mod tests {

#[test]
fn install_custom_dir_env_var() {
let _guard = ENV_LOCK.lock().ok();
let temp_dir = TempDir::new().expect("tempdir fail");
let bin_dir = temp_dir.path().join("bin");
std::fs::create_dir(&bin_dir).unwrap();
let original_install_root = env::var_os("DENO_INSTALL_ROOT");
env::set_var("DENO_INSTALL_ROOT", temp_dir.path().to_path_buf());

install(
Expand All @@ -499,6 +514,9 @@ mod tests {
let content = fs::read_to_string(file_path).unwrap();
assert!(content
.contains(r#""run" "https://localhost:4545/cli/tests/echo_server.ts""#));
if let Some(install_root) = original_install_root {
env::set_var("DENO_INSTALL_ROOT", install_root);
}
}

#[test]
Expand Down

0 comments on commit 7a635ed

Please sign in to comment.