Skip to content

Commit

Permalink
Add an integration test for missing user.name and user.email
Browse files Browse the repository at this point in the history
We test the behavior when these variables are completely missing by
setting the GIT_CONFIG environment variable. This requires adding a
mechanism to pass through the _env variable to the `sh` library. We
keep the existing process's environment to allow things like
virtualenvs to work.
  • Loading branch information
glasserc authored and jorisroovers committed Sep 25, 2020
1 parent 6ebeb8e commit abe45b1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions qa/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ def create_file(parent_dir):
io.open(os.path.join(parent_dir, test_filename), 'a', encoding=DEFAULT_ENCODING).close()
return test_filename

def create_tmp_git_config(self, contents):
""" Creates an environment with the GIT_CONFIG variable set to a file with the given contents. """
tmp_config = self.create_tmpfile(contents)
env = os.environ.copy()
env["GIT_CONFIG"] = tmp_config
return env

def create_simple_commit(self, message, out=None, ok_code=None, env=None, git_repo=None, tty_in=False):
""" Creates a simple commit with an empty test file.
:param message: Commit message for the commit. """
Expand Down
2 changes: 2 additions & 0 deletions qa/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def _exec(*args, **kwargs):
popen_kwargs = {'stdout': pipe, 'stderr': pipe, 'shell': kwargs.get('_tty_out', False)}
if '_cwd' in kwargs:
popen_kwargs['cwd'] = kwargs['_cwd']
if '_env' in kwargs:
popen_kwargs['env'] = kwargs['_env']

try:
p = subprocess.Popen(args, **popen_kwargs)
Expand Down
22 changes: 22 additions & 0 deletions qa/test_gitlint.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,28 @@ def test_msg_filename_no_tty(self):

self.assertEqualStdout(output, self.get_expected("test_gitlint/test_msg_filename_no_tty_1"))

def test_no_git_name_set(self):
""" Ensure we print out a helpful message if user.name is not set """
tmp_commit_msg_file = self.create_tmpfile(u"WIP: msg-fïlename NO name test.")
# Name is checked before email so this isn't strictly
# necessary but seems good for consistency.
env = self.create_tmp_git_config(u"[user]\n email = test-emåil@foo.com\n")
output = gitlint("--staged", "--msg-filename", tmp_commit_msg_file,
_ok_code=[self.GIT_CONTEXT_ERROR_CODE],
_env=env)
expected = u"Missing git configuration: please set user.name\n"
self.assertEqualStdout(output, expected)

def test_no_git_email_set(self):
""" Ensure we print out a helpful message if user.email is not set """
tmp_commit_msg_file = self.create_tmpfile(u"WIP: msg-fïlename NO email test.")
env = self.create_tmp_git_config(u"[user]\n name = test åuthor\n")
output = gitlint("--staged", "--msg-filename", tmp_commit_msg_file,
_ok_code=[self.GIT_CONTEXT_ERROR_CODE],
_env=env)
expected = u"Missing git configuration: please set user.email\n"
self.assertEqualStdout(output, expected)

def test_git_errors(self):
# Repo has no commits: caused by `git log`
empty_git_repo = self.create_tmp_git_repo()
Expand Down

0 comments on commit abe45b1

Please sign in to comment.