Improve git_commit helper

This commit is contained in:
Anthony Sottile 2018-12-28 15:13:06 -08:00
parent 28c97a95cd
commit 160a11a0a7
10 changed files with 75 additions and 84 deletions

View file

@ -60,11 +60,11 @@ def test_autoupdate_old_revision_broken(tempdir_factory, in_tmpdir, store):
config = make_config_from_repo(path, check=False)
cmd_output('git', 'mv', C.MANIFEST_FILE, 'nope.yaml', cwd=path)
git_commit('simulate old repo', cwd=path)
git_commit(cwd=path)
# Assume this is the revision the user's old repository was at
rev = git.head_rev(path)
cmd_output('git', 'mv', 'nope.yaml', C.MANIFEST_FILE, cwd=path)
git_commit('move hooks file', cwd=path)
git_commit(cwd=path)
update_rev = git.head_rev(path)
config['rev'] = rev
@ -84,8 +84,7 @@ def out_of_date_repo(tempdir_factory):
path = make_repo(tempdir_factory, 'python_hooks_repo')
original_rev = git.head_rev(path)
# Make a commit
git_commit('foo', cwd=path)
git_commit(cwd=path)
head_rev = git.head_rev(path)
yield auto_namedtuple(
@ -240,7 +239,7 @@ def test_autoupdate_tagged_repo(tagged_repo, in_tmpdir, store):
@pytest.fixture
def tagged_repo_with_more_commits(tagged_repo):
git_commit('foo', cwd=tagged_repo.path)
git_commit(cwd=tagged_repo.path)
yield tagged_repo
@ -263,8 +262,8 @@ def test_autoupdate_latest_no_config(out_of_date_repo, in_tmpdir, store):
)
write_config('.', config)
cmd_output('git', '-C', out_of_date_repo.path, 'rm', '-r', ':/')
git_commit('rm', config=out_of_date_repo.path)
cmd_output('git', 'rm', '-r', ':/', cwd=out_of_date_repo.path)
git_commit(cwd=out_of_date_repo.path)
ret = autoupdate(C.CONFIG_FILE, store, tags_only=False)
assert ret == 1
@ -282,7 +281,7 @@ def hook_disappearing_repo(tempdir_factory):
os.path.join(path, C.MANIFEST_FILE),
)
cmd_output('git', 'add', '.', cwd=path)
git_commit('Remove foo', cwd=path)
git_commit(cwd=path)
yield auto_namedtuple(path=path, original_rev=original_rev)

View file

@ -106,11 +106,10 @@ def test_uninstall(in_git_dir, store):
def _get_commit_output(tempdir_factory, touch_file='foo', **kwargs):
commit_msg = kwargs.pop('commit_msg', 'Commit!')
open(touch_file, 'a').close()
cmd_output('git', 'add', touch_file)
return cmd_output_mocked_pre_commit_home(
'git', 'commit', '-am', commit_msg, '--allow-empty', '--no-gpg-sign',
return git_commit(
fn=cmd_output_mocked_pre_commit_home,
# git commit puts pre-commit to stderr
stderr=subprocess.STDOUT,
retcode=None,
@ -132,7 +131,7 @@ FILES_CHANGED = (
NORMAL_PRE_COMMIT_RUN = re.compile(
r'^\[INFO\] Initializing environment for .+\.\r?\n'
r'Bash hook\.+Passed\r?\n'
r'\[master [a-f0-9]{7}\] Commit!\r?\n' +
r'\[master [a-f0-9]{7}\] commit!\r?\n' +
FILES_CHANGED +
r' create mode 100644 foo\r?\n$',
)
@ -152,7 +151,7 @@ def test_install_pre_commit_and_run_custom_path(tempdir_factory, store):
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
with cwd(path):
cmd_output('git', 'mv', C.CONFIG_FILE, 'custom-config.yaml')
git_commit('move pre-commit config')
git_commit(cwd=path)
assert install('custom-config.yaml', store) == 0
ret, output = _get_commit_output(tempdir_factory)
@ -164,7 +163,7 @@ def test_install_in_submodule_and_run(tempdir_factory, store):
src_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
parent_path = git_dir(tempdir_factory)
cmd_output('git', 'submodule', 'add', src_path, 'sub', cwd=parent_path)
git_commit('foo', cwd=parent_path)
git_commit(cwd=parent_path)
sub_pth = os.path.join(parent_path, 'sub')
with cwd(sub_pth):
@ -194,7 +193,7 @@ def test_commit_am(tempdir_factory, store):
# Make an unstaged change
open('unstaged', 'w').close()
cmd_output('git', 'add', '.')
git_commit('foo')
git_commit(cwd=path)
with io.open('unstaged', 'w') as foo_file:
foo_file.write('Oh hai')
@ -209,12 +208,14 @@ def test_unicode_merge_commit_message(tempdir_factory, store):
with cwd(path):
assert install(C.CONFIG_FILE, store) == 0
cmd_output('git', 'checkout', 'master', '-b', 'foo')
git_commit('branch2', '-n')
git_commit('-n', cwd=path)
cmd_output('git', 'checkout', 'master')
cmd_output('git', 'merge', 'foo', '--no-ff', '--no-commit', '-m', '')
# Used to crash
cmd_output_mocked_pre_commit_home(
'git', 'commit', '--no-edit', '--no-gpg-sign',
git_commit(
'--no-edit',
msg=None,
fn=cmd_output_mocked_pre_commit_home,
tempdir_factory=tempdir_factory,
)
@ -248,7 +249,6 @@ def test_environment_not_sourced(tempdir_factory, store):
# Use a specific homedir to ignore --user installs
homedir = tempdir_factory.get()
ret, stdout, stderr = git_commit(
'foo',
env={
'HOME': homedir,
'PATH': _path_without_us(),
@ -291,7 +291,7 @@ def test_failing_hooks_returns_nonzero(tempdir_factory, store):
EXISTING_COMMIT_RUN = re.compile(
r'^legacy hook\r?\n'
r'\[master [a-f0-9]{7}\] Commit!\r?\n' +
r'\[master [a-f0-9]{7}\] commit!\r?\n' +
FILES_CHANGED +
r' create mode 100644 baz\r?\n$',
)
@ -434,7 +434,7 @@ def test_uninstall_doesnt_remove_not_our_hooks(in_git_dir):
PRE_INSTALLED = re.compile(
r'Bash hook\.+Passed\r?\n'
r'\[master [a-f0-9]{7}\] Commit!\r?\n' +
r'\[master [a-f0-9]{7}\] commit!\r?\n' +
FILES_CHANGED +
r' create mode 100644 foo\r?\n$',
)
@ -546,7 +546,7 @@ def test_pre_push_force_push_without_fetch(tempdir_factory, store):
with cwd(path2):
install(C.CONFIG_FILE, store, hook_type='pre-push')
assert _get_commit_output(tempdir_factory, commit_msg='force!')[0] == 0
assert _get_commit_output(tempdir_factory, msg='force!')[0] == 0
retc, output = _get_push_output(tempdir_factory, opts=('--force',))
assert retc == 0
@ -625,7 +625,7 @@ def test_commit_msg_integration_passing(
):
install(C.CONFIG_FILE, store, hook_type='commit-msg')
msg = 'Hi\nSigned off by: me, lol'
retc, out = _get_commit_output(tempdir_factory, commit_msg=msg)
retc, out = _get_commit_output(tempdir_factory, msg=msg)
assert retc == 0
first_line = out.splitlines()[0]
assert first_line.startswith('Must have "Signed off by:"...')
@ -647,7 +647,7 @@ def test_commit_msg_legacy(commit_msg_repo, tempdir_factory, store):
install(C.CONFIG_FILE, store, hook_type='commit-msg')
msg = 'Hi\nSigned off by: asottile'
retc, out = _get_commit_output(tempdir_factory, commit_msg=msg)
retc, out = _get_commit_output(tempdir_factory, msg=msg)
assert retc == 0
first_line, second_line = out.splitlines()[:2]
assert first_line == 'legacy'

View file

@ -24,6 +24,7 @@ from testing.fixtures import modify_config
from testing.fixtures import read_config
from testing.util import cmd_output_mocked_pre_commit_home
from testing.util import cwd
from testing.util import git_commit
from testing.util import run_opts
from testing.util import xfailif_no_symlink
@ -478,8 +479,8 @@ def test_stdout_write_bug_py26(repo_with_failing_hook, store, tempdir_factory):
install(C.CONFIG_FILE, store)
# Have to use subprocess because pytest monkeypatches sys.stdout
_, stdout, _ = cmd_output_mocked_pre_commit_home(
'git', 'commit', '-m', 'Commit!', '--no-gpg-sign',
_, stdout, _ = git_commit(
fn=cmd_output_mocked_pre_commit_home,
# git commit puts pre-commit to stderr
stderr=subprocess.STDOUT,
retcode=None,
@ -507,8 +508,8 @@ def test_lots_of_files(store, tempdir_factory):
cmd_output('git', 'add', '.')
install(C.CONFIG_FILE, store)
cmd_output_mocked_pre_commit_home(
'git', 'commit', '-m', 'Commit!', '--no-gpg-sign',
git_commit(
fn=cmd_output_mocked_pre_commit_home,
# git commit puts pre-commit to stderr
stderr=subprocess.STDOUT,
tempdir_factory=tempdir_factory,