Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Don't stomp each other's env vars in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed Jul 26, 2017
1 parent edcf7ed commit 79d659e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ build = "build.rs"
[dependencies]
cargo = { git = "https://github.com/rust-lang/cargo" }
env_logger = "0.4"
jsonrpc-core = "7.0.1"
languageserver-types = "0.12"
lazy_static = "0.2"
log = "0.3"
racer = "2.0.6"
rls-analysis = "0.4.5"
Expand All @@ -26,4 +28,3 @@ serde_derive = "1.0"
toml = "0.4"
url = "1.1.0"
url_serde = "0.2.0"
jsonrpc-core = "7.0.1"
20 changes: 15 additions & 5 deletions src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ use std::ffi::OsString;
use std::io::{self, Write};
use std::mem;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::sync::{Arc, Mutex, MutexGuard};
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use std::time::Duration;


mod cargo;
mod rustc;

Expand Down Expand Up @@ -409,15 +410,24 @@ impl Write for BufWriter {
}
}


// Ensures we don't race on the env vars. This is only likely in tests, where we
// have multiple copies of the RLS running in the same process.
lazy_static! {
static ref ENV_LOCK: Mutex<()> = Mutex::new(());
}

// An RAII helper to set and reset the current working directory and env vars.
struct Environment {
struct Environment<'a> {
old_vars: HashMap<String, Option<OsString>>,
_guard: MutexGuard<'a, ()>,
}

impl Environment {
fn push(envs: &HashMap<String, Option<OsString>>) -> Environment {
impl<'a> Environment<'a> {
fn push(envs: &HashMap<String, Option<OsString>>) -> Environment<'a> {
let mut result = Environment {
old_vars: HashMap::new(),
_guard: ENV_LOCK.lock().unwrap(),
};

for (k, v) in envs {
Expand All @@ -435,7 +445,7 @@ impl Environment {
}
}

impl Drop for Environment {
impl<'a> Drop for Environment<'a> {
fn drop(&mut self) {
for (k, v) in &self.old_vars {
match *v {
Expand Down
2 changes: 2 additions & 0 deletions src/build/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use std::io;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};


// Runs a single instance of rustc. Runs in-process.
pub fn rustc(vfs: &Vfs, args: &[String], envs: &HashMap<String, Option<OsString>>, build_dir: &Path, rls_config: Arc<Mutex<Config>>) -> BuildResult {
trace!("rustc - args: `{:?}`, envs: {:?}, build dir: {:?}", args, envs, build_dir);
Expand All @@ -47,6 +48,7 @@ pub fn rustc(vfs: &Vfs, args: &[String], envs: &HashMap<String, Option<OsString>
if rls_config.lock().unwrap().clear_env_rust_log {
local_envs.insert(String::from("RUST_LOG"), None);
}

let _restore_env = Environment::push(&local_envs);
let buf = Arc::new(Mutex::new(vec![]));
let err_buf = buf.clone();
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ extern crate cargo;
extern crate env_logger;
extern crate languageserver_types as ls_types;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
extern crate racer;
extern crate rls_analysis as analysis;
Expand Down
2 changes: 1 addition & 1 deletion src/test/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use serde_json;
use server::{self as ls_server, ServerMessage};
use vfs;

const TEST_TIMEOUT_IN_SEC: u64 = 60;
const TEST_TIMEOUT_IN_SEC: u64 = 240;

// Initialise and run the internals of an LS protocol RLS server.
pub fn mock_server(messages: Vec<ServerMessage>) -> (ls_server::LsService<RecordOutput>, LsResultList)
Expand Down

0 comments on commit 79d659e

Please sign in to comment.