Fix (hopefully) bug where an empty patch was generated. Closes #76

This commit is contained in:
Anthony Sottile 2014-04-13 21:22:08 -07:00
parent 6d8621c09c
commit f94f56addf
2 changed files with 35 additions and 4 deletions

View file

@ -17,11 +17,11 @@ def staged_files_only(cmd_runner):
cmd_runner - PrefixedCommandRunner cmd_runner - PrefixedCommandRunner
""" """
# Determine if there are unstaged files # Determine if there are unstaged files
retcode, _, _ = cmd_runner.run( retcode, diff_stdout, _ = cmd_runner.run(
['git', 'diff-files', '--quiet'], ['git', 'diff', '--ignore-submodules', '--binary', '--exit-code'],
retcode=None, retcode=None,
) )
if retcode: if retcode and diff_stdout.strip():
patch_filename = cmd_runner.path('patch{0}'.format(int(time.time()))) patch_filename = cmd_runner.path('patch{0}'.format(int(time.time())))
logger.warning('Unstaged files detected.') logger.warning('Unstaged files detected.')
logger.info( logger.info(
@ -29,7 +29,7 @@ def staged_files_only(cmd_runner):
) )
# Save the current unstaged changes as a patch # Save the current unstaged changes as a patch
with open(patch_filename, 'w') as patch_file: with open(patch_filename, 'w') as patch_file:
cmd_runner.run(['git', 'diff', '--binary'], stdout=patch_file) patch_file.write(diff_stdout)
# Clear the working directory of unstaged changes # Clear the working directory of unstaged changes
cmd_runner.run(['git', 'checkout', '--', '.']) cmd_runner.run(['git', 'checkout', '--', '.'])

View file

@ -1,3 +1,5 @@
import logging
import mock
import os.path import os.path
import pytest import pytest
import shutil import shutil
@ -215,3 +217,32 @@ def test_sub_something_unstaged(sub_staged, cmd_runner):
_test_sub_state(sub_staged, 'sha2', 'AM') _test_sub_state(sub_staged, 'sha2', 'AM')
_test_sub_state(sub_staged, 'sha2', 'AM') _test_sub_state(sub_staged, 'sha2', 'AM')
@pytest.yield_fixture
def fake_logging_handler():
class FakeHandler(logging.Handler):
def __init__(self):
logging.Handler.__init__(self)
self.logs = []
def emit(self, record):
self.logs.append(record)
pre_commit_logger = logging.getLogger('pre_commit')
original_level = pre_commit_logger.getEffectiveLevel()
handler = FakeHandler()
pre_commit_logger.addHandler(handler)
pre_commit_logger.setLevel(logging.WARNING)
yield handler
pre_commit_logger.setLevel(original_level)
pre_commit_logger.removeHandler(handler)
def test_diff_returns_1_no_diff_though(fake_logging_handler, foo_staged):
cmd_runner = mock.Mock()
cmd_runner.run.return_value = (1, '', '')
cmd_runner.path.return_value = '.pre-commit-files_patch'
with staged_files_only(cmd_runner):
pass
assert not fake_logging_handler.logs