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.
This commit is contained in:
Martin von Zweigbergk 2022-10-23 10:16:14 -07:00 committed by Martin von Zweigbergk
parent c366795d80
commit efce1e54e6
5 changed files with 26 additions and 22 deletions

2
Cargo.lock generated
View file

@ -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",

View file

@ -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"

View file

@ -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"

View file

@ -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-")

View file

@ -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<i64>,
}
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();