From 6c77c32d06368fd549a1a7914d31a8d284a377f1 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Tue, 14 Nov 2023 13:05:47 -0800 Subject: [PATCH] aider: Refactored `test_check_gitignore` to use a temporary git config file and set the `GIT_CONFIG_GLOBAL` environment variable during the test execution. # Aider chat conversation: USER: in `test_check_gitignore` can we have gitpython not use the global gitignore? ASSISTANT: To ensure that GitPython does not use the global gitignore, we can set the `GIT_CONFIG_GLOBAL` environment variable to point to a non-existent file or an empty temporary file during the test execution. This will effectively ignore the global gitignore settings. We will modify the `test_check_gitignore` method in the `tests/test_main.py` file to set the `GIT_CONFIG_GLOBAL` environment variable before the test runs and reset it after the test is completed. Here are the *SEARCH/REPLACE* blocks: tests/test_main.py ```python <<<<<<< SEARCH def test_check_gitignore(self): make_repo() io = InputOutput(pretty=False, yes=True) cwd = Path.cwd() gitignore = cwd / ".gitignore" self.assertFalse(gitignore.exists()) check_gitignore(cwd, io) self.assertTrue(gitignore.exists()) self.assertEqual(".aider*", gitignore.read_text().splitlines()[0]) gitignore.write_text("one\ntwo\n") check_gitignore(cwd, io) self.assertEqual("one\ntwo\n.aider*\n", gitignore.read_text()) ======= def test_check_gitignore(self): with tempfile.NamedTemporaryFile() as temp_gitconfig: os.environ['GIT_CONFIG_GLOBAL'] = temp_gitconfig.name make_repo() io = InputOutput(pretty=False, yes=True) cwd = Path.cwd() gitignore = cwd / ".gitignore" self.assertFalse(gitignore.exists()) check_gitignore(cwd, io) self.assertTrue(gitignore.exists()) self.assertEqual(".aider*", gitignore.read_text().splitlines()[0]) gitignore.write_text("one\ntwo\n") check_gitignore(cwd, io) self.assertEqual("one\ntwo\n.aider*\n", gitignore.read_text()) del os.environ['GIT_CONFIG_GLOBAL'] >>>>>>> REPLACE ``` This change will create a temporary file to act as a dummy global git configuration file and set the `GIT_CONFIG_GLOBAL` environment variable to its path. After the test, it will delete the environment variable to avoid affecting other tests or operations. --- tests/test_main.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index 214679843..baffef430 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -113,20 +113,23 @@ class TestMain(TestCase): self.assertEqual(".aider*", gitignore.read_text().splitlines()[0]) def test_check_gitignore(self): - make_repo() - io = InputOutput(pretty=False, yes=True) - cwd = Path.cwd() - gitignore = cwd / ".gitignore" + with tempfile.NamedTemporaryFile() as temp_gitconfig: + os.environ['GIT_CONFIG_GLOBAL'] = temp_gitconfig.name + make_repo() + io = InputOutput(pretty=False, yes=True) + cwd = Path.cwd() + gitignore = cwd / ".gitignore" - self.assertFalse(gitignore.exists()) - check_gitignore(cwd, io) - self.assertTrue(gitignore.exists()) + self.assertFalse(gitignore.exists()) + check_gitignore(cwd, io) + self.assertTrue(gitignore.exists()) - self.assertEqual(".aider*", gitignore.read_text().splitlines()[0]) + self.assertEqual(".aider*", gitignore.read_text().splitlines()[0]) - gitignore.write_text("one\ntwo\n") - check_gitignore(cwd, io) - self.assertEqual("one\ntwo\n.aider*\n", gitignore.read_text()) + gitignore.write_text("one\ntwo\n") + check_gitignore(cwd, io) + self.assertEqual("one\ntwo\n.aider*\n", gitignore.read_text()) + del os.environ['GIT_CONFIG_GLOBAL'] def test_main_git_ignore(self): cwd = Path().cwd()