tests: set $HOME in a few tests to avoid depending in developer's ~/.gitignore

I just changed my `~/.gitignore` and some tests started failing
because the working copy respects the user's `~/.gitignore`. We should
probably not depend on `$HOME` in the library crate. For now, this
patch just makes sure we set it to an arbitrary directory in the tests
where it matters.
This commit is contained in:
Martin von Zweigbergk 2021-03-16 17:09:33 -07:00
parent 67e11e0fc3
commit 69de4698ac
5 changed files with 16 additions and 2 deletions

View file

@ -28,6 +28,14 @@ use crate::store_wrapper::StoreWrapper;
use crate::tree::Tree; use crate::tree::Tree;
use crate::tree_builder::TreeBuilder; use crate::tree_builder::TreeBuilder;
pub fn new_user_home() -> TempDir {
// Set $HOME to some arbitrary place so libgit2 doesn't use ~/.gitignore
// of the person running the tests.
let home_dir = tempfile::tempdir().unwrap();
std::env::set_var("HOME", home_dir.path());
home_dir
}
pub fn user_settings() -> UserSettings { pub fn user_settings() -> UserSettings {
let mut config = config::Config::new(); let mut config = config::Config::new();
config.set("user.name", "Test User").unwrap(); config.set("user.name", "Test User").unwrap();

View file

@ -288,6 +288,9 @@ impl TreeState {
pub fn write_tree(&mut self) -> &TreeId { pub fn write_tree(&mut self) -> &TreeId {
// We create a temporary git repo with the working copy shared with ours only // We create a temporary git repo with the working copy shared with ours only
// so we can use libgit2's .gitignore check. // so we can use libgit2's .gitignore check.
// TODO: We should probably have the caller pass in the home directory to the
// library crate instead of depending on $HOME directly here (as we do because
// git2::Repository::status_should_ignore() reads the .gitignore there).
// TODO: Do this more cleanly, perhaps by reading .gitignore files ourselves. // TODO: Do this more cleanly, perhaps by reading .gitignore files ourselves.
let git_repo_dir = tempfile::tempdir().unwrap(); let git_repo_dir = tempfile::tempdir().unwrap();
let mut git_repo_options = RepositoryInitOptions::new(); let mut git_repo_options = RepositoryInitOptions::new();

View file

@ -244,7 +244,7 @@ fn test_checkout_file_transitions(use_git: bool) {
fn test_commit_racy_timestamps(use_git: bool) { fn test_commit_racy_timestamps(use_git: bool) {
// Tests that file modifications are detected even if they happen the same // Tests that file modifications are detected even if they happen the same
// millisecond as the updated working copy state. // millisecond as the updated working copy state.
let _home_dir = testutils::new_user_home();
let settings = testutils::user_settings(); let settings = testutils::user_settings();
let (_temp_dir, mut repo) = testutils::init_repo(&settings, use_git); let (_temp_dir, mut repo) = testutils::init_repo(&settings, use_git);
@ -274,6 +274,7 @@ fn test_commit_racy_timestamps(use_git: bool) {
fn test_gitignores(use_git: bool) { fn test_gitignores(use_git: bool) {
// Tests that .gitignore files are respected. // Tests that .gitignore files are respected.
let _home_dir = testutils::new_user_home();
let settings = testutils::user_settings(); let settings = testutils::user_settings();
let (_temp_dir, mut repo) = testutils::init_repo(&settings, use_git); let (_temp_dir, mut repo) = testutils::init_repo(&settings, use_git);

View file

@ -72,6 +72,7 @@ fn test_concurrent_checkout(use_git: bool) {
fn test_concurrent_commit(use_git: bool) { fn test_concurrent_commit(use_git: bool) {
// Test that concurrent working copy commits result in a chain of successors // Test that concurrent working copy commits result in a chain of successors
// instead of divergence. // instead of divergence.
let _home_dir = testutils::new_user_home();
let settings = testutils::user_settings(); let settings = testutils::user_settings();
let (_temp_dir, mut repo1) = testutils::init_repo(&settings, use_git); let (_temp_dir, mut repo1) = testutils::init_repo(&settings, use_git);

View file

@ -15,7 +15,7 @@
use std::io::Cursor; use std::io::Cursor;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use jujube_lib::testutils::user_settings; use jujube_lib::testutils::{new_user_home, user_settings};
use crate::commands; use crate::commands;
use crate::ui::Ui; use crate::ui::Ui;
@ -34,6 +34,7 @@ impl CommandRunner {
} }
pub fn run(self, mut args: Vec<&str>) -> CommandOutput { pub fn run(self, mut args: Vec<&str>) -> CommandOutput {
let _home_dir = new_user_home();
let mut stdout_buf = self.stdout_buf; let mut stdout_buf = self.stdout_buf;
let stdout = Box::new(Cursor::new(&mut stdout_buf)); let stdout = Box::new(Cursor::new(&mut stdout_buf));
let ui = Ui::new(self.cwd, stdout, false, user_settings()); let ui = Ui::new(self.cwd, stdout, false, user_settings());