From c96476be7f10616768584a95d06cd1bddfe6d404 Mon Sep 17 00:00:00 2001 From: Athos Ribeiro Date: Tue, 20 Oct 2020 14:51:31 +0200 Subject: Get system user id in a lazy manner Calling getpass.getuser may lead to breakage in environments where there is no entries in the /etc/passwd file for the current user. Setting the environment variables for the git user configurations should prevents GitPython from using values from /etc/passwd. However, doing so will not prevent reading /etc/passwd and looking for an entry with the current user UID. This patch changes the behavior described above so GitPython will perform a lazy evaluation of /etc/passwd, only doing so when the environment variables for the git user configuration are not available. Signed-off-by: Athos Ribeiro --- test/test_util.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/test_util.py b/test/test_util.py index df4e5474..2f946891 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -4,10 +4,11 @@ # This module is part of GitPython and is released under # the BSD License: http://www.opensource.org/licenses/bsd-license.php +import os import pickle import tempfile import time -from unittest import skipIf +from unittest import mock, skipIf from datetime import datetime import ddt @@ -215,6 +216,28 @@ class TestUtils(TestBase): self.assertIsInstance(Actor.author(cr), Actor) # END assure config reader is handled + @mock.patch("getpass.getuser") + def test_actor_get_uid_laziness_not_called(self, mock_get_uid): + env = { + "GIT_AUTHOR_NAME": "John Doe", + "GIT_AUTHOR_EMAIL": "jdoe@example.com", + "GIT_COMMITTER_NAME": "Jane Doe", + "GIT_COMMITTER_EMAIL": "jane@example.com", + } + os.environ.update(env) + for cr in (None, self.rorepo.config_reader()): + Actor.committer(cr) + Actor.author(cr) + self.assertFalse(mock_get_uid.called) + + @mock.patch("getpass.getuser") + def test_actor_get_uid_laziness_called(self, mock_get_uid): + for cr in (None, self.rorepo.config_reader()): + Actor.committer(cr) + Actor.author(cr) + self.assertTrue(mock_get_uid.called) + self.assertEqual(mock_get_uid.call_count, 4) + def test_actor_from_string(self): self.assertEqual(Actor._from_string("name"), Actor("name", None)) self.assertEqual(Actor._from_string("name <>"), Actor("name", "")) -- cgit v1.2.1