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

Commit 79d659e

Browse files
committed
Don't stomp each other's env vars in tests
1 parent edcf7ed commit 79d659e

File tree

6 files changed

+23
-7
lines changed

6 files changed

+23
-7
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ build = "build.rs"
1212
[dependencies]
1313
cargo = { git = "https://github.com/rust-lang/cargo" }
1414
env_logger = "0.4"
15+
jsonrpc-core = "7.0.1"
1516
languageserver-types = "0.12"
17+
lazy_static = "0.2"
1618
log = "0.3"
1719
racer = "2.0.6"
1820
rls-analysis = "0.4.5"
@@ -26,4 +28,3 @@ serde_derive = "1.0"
2628
toml = "0.4"
2729
url = "1.1.0"
2830
url_serde = "0.2.0"
29-
jsonrpc-core = "7.0.1"

src/build/mod.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ use std::ffi::OsString;
1919
use std::io::{self, Write};
2020
use std::mem;
2121
use std::path::{Path, PathBuf};
22-
use std::sync::{Arc, Mutex};
22+
use std::sync::{Arc, Mutex, MutexGuard};
2323
use std::sync::atomic::{AtomicBool, Ordering};
2424
use std::thread;
2525
use std::time::Duration;
2626

27+
2728
mod cargo;
2829
mod rustc;
2930

@@ -409,15 +410,24 @@ impl Write for BufWriter {
409410
}
410411
}
411412

413+
414+
// Ensures we don't race on the env vars. This is only likely in tests, where we
415+
// have multiple copies of the RLS running in the same process.
416+
lazy_static! {
417+
static ref ENV_LOCK: Mutex<()> = Mutex::new(());
418+
}
419+
412420
// An RAII helper to set and reset the current working directory and env vars.
413-
struct Environment {
421+
struct Environment<'a> {
414422
old_vars: HashMap<String, Option<OsString>>,
423+
_guard: MutexGuard<'a, ()>,
415424
}
416425

417-
impl Environment {
418-
fn push(envs: &HashMap<String, Option<OsString>>) -> Environment {
426+
impl<'a> Environment<'a> {
427+
fn push(envs: &HashMap<String, Option<OsString>>) -> Environment<'a> {
419428
let mut result = Environment {
420429
old_vars: HashMap::new(),
430+
_guard: ENV_LOCK.lock().unwrap(),
421431
};
422432

423433
for (k, v) in envs {
@@ -435,7 +445,7 @@ impl Environment {
435445
}
436446
}
437447

438-
impl Drop for Environment {
448+
impl<'a> Drop for Environment<'a> {
439449
fn drop(&mut self) {
440450
for (k, v) in &self.old_vars {
441451
match *v {

src/build/rustc.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use std::io;
3636
use std::path::{Path, PathBuf};
3737
use std::sync::{Arc, Mutex};
3838

39+
3940
// Runs a single instance of rustc. Runs in-process.
4041
pub fn rustc(vfs: &Vfs, args: &[String], envs: &HashMap<String, Option<OsString>>, build_dir: &Path, rls_config: Arc<Mutex<Config>>) -> BuildResult {
4142
trace!("rustc - args: `{:?}`, envs: {:?}, build dir: {:?}", args, envs, build_dir);
@@ -47,6 +48,7 @@ pub fn rustc(vfs: &Vfs, args: &[String], envs: &HashMap<String, Option<OsString>
4748
if rls_config.lock().unwrap().clear_env_rust_log {
4849
local_envs.insert(String::from("RUST_LOG"), None);
4950
}
51+
5052
let _restore_env = Environment::push(&local_envs);
5153
let buf = Arc::new(Mutex::new(vec![]));
5254
let err_buf = buf.clone();

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ extern crate cargo;
1818
extern crate env_logger;
1919
extern crate languageserver_types as ls_types;
2020
#[macro_use]
21+
extern crate lazy_static;
22+
#[macro_use]
2123
extern crate log;
2224
extern crate racer;
2325
extern crate rls_analysis as analysis;

src/test/harness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use serde_json;
2525
use server::{self as ls_server, ServerMessage};
2626
use vfs;
2727

28-
const TEST_TIMEOUT_IN_SEC: u64 = 60;
28+
const TEST_TIMEOUT_IN_SEC: u64 = 240;
2929

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

0 commit comments

Comments
 (0)