From fdd103b31b4d27f84a4d07d1b6994352d451a2d2 Mon Sep 17 00:00:00 2001 From: Mark Thomas Date: Tue, 26 Mar 2019 07:30:25 -0700 Subject: [PATCH] configparser: avoid environment race in configparser tests Summary: The configparser tests `hg::tests::test_basic_hgplain` and `hg::tests::test_hgplainexcept` set different values for `HGPLAIN` and `HGPLAINEXCEPT`. Since the tests run in parallel and use the same environment, one of the tests may fail if they run at the same time. For these tests, create a mutex for the environment and lock it for the duration of the test, ensuring these tests do not interfere with each other. Reviewed By: jsgf Differential Revision: D14615394 fbshipit-source-id: 9f123668d93223655514db2ae34b05354a6b578c --- lib/configparser/Cargo.toml | 1 + lib/configparser/src/hg.rs | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/configparser/Cargo.toml b/lib/configparser/Cargo.toml index 0245c40556..a2edac7b04 100644 --- a/lib/configparser/Cargo.toml +++ b/lib/configparser/Cargo.toml @@ -14,6 +14,7 @@ pest_derive = "2.1.0" shellexpand = "1.0.0" [dev-dependencies] +lazy_static = "1.3.0" minibench = { path = "../minibench" } tempdir = "0.3.7" diff --git a/lib/configparser/src/hg.rs b/lib/configparser/src/hg.rs index 4928b1bf62..e7d2e0068e 100644 --- a/lib/configparser/src/hg.rs +++ b/lib/configparser/src/hg.rs @@ -514,8 +514,18 @@ mod tests { use crate::config::tests::write_file; + use lazy_static::lazy_static; + use std::sync::Mutex; + + lazy_static! { + /// Lock for the environment. This should be acquired by tests that rely on particular + /// environment variable values that might be overwritten by other tests. + static ref ENV_LOCK: Mutex<()> = Mutex::new(()); + } + #[test] fn test_basic_hgplain() { + let _guard = ENV_LOCK.lock().unwrap(); env::set_var(HGPLAIN, "1"); env::remove_var(HGPLAINEXCEPT); @@ -540,6 +550,7 @@ mod tests { #[test] fn test_hgplainexcept() { + let _guard = ENV_LOCK.lock().unwrap(); env::remove_var(HGPLAIN); env::set_var(HGPLAINEXCEPT, "alias,revsetalias");