From 69de4698ac9e71ff8d28945624436387f99203ff Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Tue, 16 Mar 2021 17:09:33 -0700 Subject: [PATCH] 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. --- lib/src/testutils.rs | 8 ++++++++ lib/src/working_copy.rs | 3 +++ lib/tests/test_working_copy.rs | 3 ++- lib/tests/test_working_copy_concurrent.rs | 1 + src/testutils.rs | 3 ++- 5 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/src/testutils.rs b/lib/src/testutils.rs index 0ddc1a4a9..063f1dade 100644 --- a/lib/src/testutils.rs +++ b/lib/src/testutils.rs @@ -28,6 +28,14 @@ use crate::store_wrapper::StoreWrapper; use crate::tree::Tree; 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 { let mut config = config::Config::new(); config.set("user.name", "Test User").unwrap(); diff --git a/lib/src/working_copy.rs b/lib/src/working_copy.rs index a3aee0381..704bf07c0 100644 --- a/lib/src/working_copy.rs +++ b/lib/src/working_copy.rs @@ -288,6 +288,9 @@ impl TreeState { pub fn write_tree(&mut self) -> &TreeId { // We create a temporary git repo with the working copy shared with ours only // 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. let git_repo_dir = tempfile::tempdir().unwrap(); let mut git_repo_options = RepositoryInitOptions::new(); diff --git a/lib/tests/test_working_copy.rs b/lib/tests/test_working_copy.rs index 1bfa527e2..f5ba1a4b5 100644 --- a/lib/tests/test_working_copy.rs +++ b/lib/tests/test_working_copy.rs @@ -244,7 +244,7 @@ fn test_checkout_file_transitions(use_git: bool) { fn test_commit_racy_timestamps(use_git: bool) { // Tests that file modifications are detected even if they happen the same // millisecond as the updated working copy state. - + let _home_dir = testutils::new_user_home(); let settings = testutils::user_settings(); 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) { // Tests that .gitignore files are respected. + let _home_dir = testutils::new_user_home(); let settings = testutils::user_settings(); let (_temp_dir, mut repo) = testutils::init_repo(&settings, use_git); diff --git a/lib/tests/test_working_copy_concurrent.rs b/lib/tests/test_working_copy_concurrent.rs index 55ff47adf..38913afe2 100644 --- a/lib/tests/test_working_copy_concurrent.rs +++ b/lib/tests/test_working_copy_concurrent.rs @@ -72,6 +72,7 @@ fn test_concurrent_checkout(use_git: bool) { fn test_concurrent_commit(use_git: bool) { // Test that concurrent working copy commits result in a chain of successors // instead of divergence. + let _home_dir = testutils::new_user_home(); let settings = testutils::user_settings(); let (_temp_dir, mut repo1) = testutils::init_repo(&settings, use_git); diff --git a/src/testutils.rs b/src/testutils.rs index 0d2d4ebd6..db6a381be 100644 --- a/src/testutils.rs +++ b/src/testutils.rs @@ -15,7 +15,7 @@ use std::io::Cursor; 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::ui::Ui; @@ -34,6 +34,7 @@ impl CommandRunner { } pub fn run(self, mut args: Vec<&str>) -> CommandOutput { + let _home_dir = new_user_home(); let mut stdout_buf = self.stdout_buf; let stdout = Box::new(Cursor::new(&mut stdout_buf)); let ui = Ui::new(self.cwd, stdout, false, user_settings());