From efce1e54e648d74eb0e06803bb40258d35cb6ea8 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 23 Oct 2022 10:16:14 -0700 Subject: [PATCH] tests: extract setup of libgit2 config to lib crate We have some problems with non-hermetic tests in the lib crate, so we'll want to reuse the setup code there. --- Cargo.lock | 2 +- Cargo.toml | 1 - lib/Cargo.toml | 1 + lib/src/testutils.rs | 23 +++++++++++++++++++++++ tests/common/mod.rs | 21 +-------------------- 5 files changed, 26 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3e0415520..eed959258 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -684,7 +684,6 @@ dependencies = [ "insta", "itertools", "jujutsu-lib", - "lazy_static", "maplit", "pest", "pest_derive", @@ -713,6 +712,7 @@ dependencies = [ "hex", "insta", "itertools", + "lazy_static", "maplit", "num_cpus", "once_cell", diff --git a/Cargo.toml b/Cargo.toml index 2a62d11db..5dfa946bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,7 +60,6 @@ assert_cmd = "2.0.5" criterion = "0.4.0" criterion_bencher_compat = "0.4.0" insta = "1.21.0" -lazy_static = "1.4.0" regex = "1.6.0" predicates = "2.1.1" test-case = "2.2.2" diff --git a/lib/Cargo.toml b/lib/Cargo.toml index f9b2b6596..a7d9a6933 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -27,6 +27,7 @@ config = { version = "0.13.2", default-features = false, features = ["toml"] } git2 = "0.15.0" hex = "0.4.3" itertools = "0.10.5" +lazy_static = "1.4.0" maplit = "1.0.2" once_cell = "1.15.0" pest = "2.4.0" diff --git a/lib/src/testutils.rs b/lib/src/testutils.rs index f62a3eaf9..8961af068 100644 --- a/lib/src/testutils.rs +++ b/lib/src/testutils.rs @@ -19,6 +19,7 @@ use std::path::{Path, PathBuf}; use std::sync::Arc; use itertools::Itertools; +use lazy_static::lazy_static; use tempfile::TempDir; use crate::backend::{FileId, TreeId, TreeValue}; @@ -35,6 +36,28 @@ use crate::tree::Tree; use crate::tree_builder::TreeBuilder; use crate::workspace::Workspace; +lazy_static! { + // libgit2 respects init.defaultBranch (and possibly other config + // variables) in the user's config files. Disable access to them to make + // our tests hermetic. + // + // set_search_path is unsafe because it cannot guarantee thread safety (as + // its documentation states). For the same reason, we wrap these invocations + // in lazy_static!. + static ref CONFIGURE_GIT2: () = { + unsafe { + git2::opts::set_search_path(git2::ConfigLevel::System, "").unwrap(); + git2::opts::set_search_path(git2::ConfigLevel::Global, "").unwrap(); + git2::opts::set_search_path(git2::ConfigLevel::XDG, "").unwrap(); + git2::opts::set_search_path(git2::ConfigLevel::ProgramData, "").unwrap(); + } + }; +} + +pub fn hermetic_libgit2() { + lazy_static::initialize(&CONFIGURE_GIT2); +} + pub fn new_temp_dir() -> TempDir { tempfile::Builder::new() .prefix("jj-test-") diff --git a/tests/common/mod.rs b/tests/common/mod.rs index c1f4ed1ca..9ab4a1fdd 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -17,7 +17,6 @@ use std::collections::HashMap; use std::path::{Path, PathBuf}; use jujutsu_lib::testutils; -use lazy_static::lazy_static; use tempfile::TempDir; pub struct TestEnvironment { @@ -30,27 +29,9 @@ pub struct TestEnvironment { command_number: RefCell, } -lazy_static! { - // libgit2 respects init.defaultBranch (and possibly other config - // variables) in the user's config files. Disable access to them to make - // our tests hermetic. - // - // set_search_path is unsafe because it cannot guarantee thread safety (as - // its documentation states). For the same reason, we wrap these invocations - // in lazy_static!. - static ref CONFIGURE_GIT2: () = { - unsafe { - git2::opts::set_search_path(git2::ConfigLevel::System, "").unwrap(); - git2::opts::set_search_path(git2::ConfigLevel::Global, "").unwrap(); - git2::opts::set_search_path(git2::ConfigLevel::XDG, "").unwrap(); - git2::opts::set_search_path(git2::ConfigLevel::ProgramData, "").unwrap(); - } - }; -} - impl Default for TestEnvironment { fn default() -> Self { - lazy_static::initialize(&CONFIGURE_GIT2); + testutils::hermetic_libgit2(); let tmp_dir = testutils::new_temp_dir(); let env_root = tmp_dir.path().canonicalize().unwrap();