diff --git a/testing/fixtures.py b/testing/fixtures.py index 287eb309..91c095a8 100644 --- a/testing/fixtures.py +++ b/testing/fixtures.py @@ -18,6 +18,7 @@ from pre_commit.clientlib import CONFIG_SCHEMA from pre_commit.clientlib import load_manifest from pre_commit.util import cmd_output from testing.util import get_resource_path +from testing.util import git_commit def copy_tree_to_path(src_dir, dest_dir): @@ -48,7 +49,7 @@ def make_repo(tempdir_factory, repo_source): path = git_dir(tempdir_factory) copy_tree_to_path(get_resource_path(repo_source), path) cmd_output('git', 'add', '.', cwd=path) - cmd_output('git', 'commit', '--no-gpg-sign', '-m', 'Add hooks', cwd=path) + git_commit(msg=make_repo.__name__, cwd=path) return path @@ -63,11 +64,7 @@ def modify_manifest(path): yield manifest with io.open(manifest_path, 'w') as manifest_file: manifest_file.write(ordered_dump(manifest, **C.YAML_DUMP_KWARGS)) - cmd_output( - 'git', 'commit', '--no-gpg-sign', '-am', - 'update {}'.format(C.MANIFEST_FILE), - cwd=path, - ) + git_commit(msg=modify_manifest.__name__, cwd=path) @contextlib.contextmanager @@ -82,9 +79,7 @@ def modify_config(path='.', commit=True): with io.open(config_path, 'w', encoding='UTF-8') as config_file: config_file.write(ordered_dump(config, **C.YAML_DUMP_KWARGS)) if commit: - cmd_output( - 'git', 'commit', '--no-gpg-sign', '-am', 'update config', cwd=path, - ) + git_commit(msg=modify_config.__name__, cwd=path) def config_with_local_hooks(): @@ -140,19 +135,13 @@ def write_config(directory, config, config_file=C.CONFIG_FILE): def add_config_to_repo(git_path, config, config_file=C.CONFIG_FILE): write_config(git_path, config, config_file=config_file) cmd_output('git', 'add', config_file, cwd=git_path) - cmd_output( - 'git', 'commit', '--no-gpg-sign', '-m', 'Add hooks config', - cwd=git_path, - ) + git_commit(msg=add_config_to_repo.__name__, cwd=git_path) return git_path def remove_config_from_repo(git_path, config_file=C.CONFIG_FILE): cmd_output('git', 'rm', config_file, cwd=git_path) - cmd_output( - 'git', 'commit', '--no-gpg-sign', '-m', 'Remove hooks config', - cwd=git_path, - ) + git_commit(msg=remove_config_from_repo.__name__, cwd=git_path) return git_path diff --git a/testing/util.py b/testing/util.py index ed38affe..f0406089 100644 --- a/testing/util.py +++ b/testing/util.py @@ -133,3 +133,13 @@ def cwd(path): yield finally: os.chdir(original_cwd) + + +def git_commit(*args, **kwargs): + fn = kwargs.pop('fn', cmd_output) + msg = kwargs.pop('msg', 'commit!') + + cmd = ('git', 'commit', '--allow-empty', '--no-gpg-sign', '-a') + args + if msg is not None: # allow skipping `-m` with `msg=None` + cmd += ('-m', msg) + return fn(*cmd, **kwargs) diff --git a/tests/commands/autoupdate_test.py b/tests/commands/autoupdate_test.py index 34c7292b..e4d3cc88 100644 --- a/tests/commands/autoupdate_test.py +++ b/tests/commands/autoupdate_test.py @@ -21,6 +21,7 @@ from testing.fixtures import make_config_from_repo from testing.fixtures import make_repo from testing.fixtures import write_config from testing.util import get_resource_path +from testing.util import git_commit @pytest.fixture @@ -59,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) - cmd_output('git', 'commit', '-m', '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) - cmd_output('git', 'commit', '-m', 'move hooks file', cwd=path) + git_commit(cwd=path) update_rev = git.head_rev(path) config['rev'] = rev @@ -83,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 - cmd_output('git', 'commit', '--allow-empty', '-m', 'foo', cwd=path) + git_commit(cwd=path) head_rev = git.head_rev(path) yield auto_namedtuple( @@ -239,7 +239,7 @@ def test_autoupdate_tagged_repo(tagged_repo, in_tmpdir, store): @pytest.fixture def tagged_repo_with_more_commits(tagged_repo): - cmd_output('git', 'commit', '--allow-empty', '-mfoo', cwd=tagged_repo.path) + git_commit(cwd=tagged_repo.path) yield tagged_repo @@ -262,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', ':/') - cmd_output('git', '-C', out_of_date_repo.path, 'commit', '-m', 'rm') + 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 @@ -281,7 +281,7 @@ def hook_disappearing_repo(tempdir_factory): os.path.join(path, C.MANIFEST_FILE), ) cmd_output('git', 'add', '.', cwd=path) - cmd_output('git', 'commit', '-m', 'Remove foo', cwd=path) + git_commit(cwd=path) yield auto_namedtuple(path=path, original_rev=original_rev) diff --git a/tests/commands/install_uninstall_test.py b/tests/commands/install_uninstall_test.py index ce74a2ea..2faa1917 100644 --- a/tests/commands/install_uninstall_test.py +++ b/tests/commands/install_uninstall_test.py @@ -28,6 +28,7 @@ from testing.fixtures import make_consuming_repo from testing.fixtures import remove_config_from_repo 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 xfailif_no_symlink @@ -105,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', + return git_commit( + fn=cmd_output_mocked_pre_commit_home, # git commit puts pre-commit to stderr stderr=subprocess.STDOUT, retcode=None, @@ -131,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$', ) @@ -151,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') - cmd_output('git', 'commit', '-m', 'move pre-commit config') + git_commit(cwd=path) assert install('custom-config.yaml', store) == 0 ret, output = _get_commit_output(tempdir_factory) @@ -163,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) - cmd_output('git', 'commit', '-m', 'foo', cwd=parent_path) + git_commit(cwd=parent_path) sub_pth = os.path.join(parent_path, 'sub') with cwd(sub_pth): @@ -193,7 +193,7 @@ def test_commit_am(tempdir_factory, store): # Make an unstaged change open('unstaged', 'w').close() cmd_output('git', 'add', '.') - cmd_output('git', 'commit', '-m', 'foo') + git_commit(cwd=path) with io.open('unstaged', 'w') as foo_file: foo_file.write('Oh hai') @@ -208,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') - cmd_output('git', 'commit', '--allow-empty', '-n', '-m', 'branch2') + 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', + git_commit( + '--no-edit', + msg=None, + fn=cmd_output_mocked_pre_commit_home, tempdir_factory=tempdir_factory, ) @@ -246,8 +248,7 @@ def test_environment_not_sourced(tempdir_factory, store): # Use a specific homedir to ignore --user installs homedir = tempdir_factory.get() - ret, stdout, stderr = cmd_output( - 'git', 'commit', '--allow-empty', '-m', 'foo', + ret, stdout, stderr = git_commit( env={ 'HOME': homedir, 'PATH': _path_without_us(), @@ -290,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$', ) @@ -433,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$', ) @@ -545,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 @@ -624,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:"...') @@ -646,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' diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py index 33920e5e..28b6ab37 100644 --- a/tests/commands/run_test.py +++ b/tests/commands/run_test.py @@ -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!', + _, 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!', + git_commit( + fn=cmd_output_mocked_pre_commit_home, # git commit puts pre-commit to stderr stderr=subprocess.STDOUT, tempdir_factory=tempdir_factory, diff --git a/tests/conftest.py b/tests/conftest.py index 49fbf3fc..f72af094 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -19,6 +19,7 @@ from testing.fixtures import git_dir from testing.fixtures import make_consuming_repo from testing.fixtures import write_config from testing.util import cwd +from testing.util import git_commit @pytest.fixture(autouse=True) @@ -79,7 +80,7 @@ def _make_conflict(): with io.open('foo_only_file', 'w') as foo_only_file: foo_only_file.write('foo') cmd_output('git', 'add', 'foo_only_file') - cmd_output('git', 'commit', '-m', 'conflict_file') + git_commit(msg=_make_conflict.__name__) cmd_output('git', 'checkout', 'origin/master', '-b', 'bar') with io.open('conflict_file', 'w') as conflict_file: conflict_file.write('harp\nddrp\n') @@ -87,7 +88,7 @@ def _make_conflict(): with io.open('bar_only_file', 'w') as bar_only_file: bar_only_file.write('bar') cmd_output('git', 'add', 'bar_only_file') - cmd_output('git', 'commit', '-m', 'conflict_file') + git_commit(msg=_make_conflict.__name__) cmd_output('git', 'merge', 'foo', retcode=None) @@ -96,7 +97,7 @@ def in_merge_conflict(tempdir_factory): path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') open(os.path.join(path, 'dummy'), 'a').close() cmd_output('git', 'add', 'dummy', cwd=path) - cmd_output('git', 'commit', '-m', 'Add config.', cwd=path) + git_commit(msg=in_merge_conflict.__name__, cwd=path) conflict_path = tempdir_factory.get() cmd_output('git', 'clone', path, conflict_path) @@ -109,7 +110,7 @@ def in_merge_conflict(tempdir_factory): def in_conflicting_submodule(tempdir_factory): git_dir_1 = git_dir(tempdir_factory) git_dir_2 = git_dir(tempdir_factory) - cmd_output('git', 'commit', '--allow-empty', '-minit!', cwd=git_dir_2) + git_commit(msg=in_conflicting_submodule.__name__, cwd=git_dir_2) cmd_output('git', 'submodule', 'add', git_dir_2, 'sub', cwd=git_dir_1) with cwd(os.path.join(git_dir_1, 'sub')): _make_conflict() @@ -135,7 +136,7 @@ def commit_msg_repo(tempdir_factory): write_config(path, config) with cwd(path): cmd_output('git', 'add', '.') - cmd_output('git', 'commit', '-m', 'add hooks') + git_commit(msg=commit_msg_repo.__name__) yield path diff --git a/tests/git_test.py b/tests/git_test.py index 2a9bda4a..cb8a2bf1 100644 --- a/tests/git_test.py +++ b/tests/git_test.py @@ -9,6 +9,7 @@ import pytest from pre_commit import git from pre_commit.error_handler import FatalError from pre_commit.util import cmd_output +from testing.util import git_commit def test_get_root_at_root(in_git_dir): @@ -30,7 +31,7 @@ def test_get_root_not_git_dir(in_tmpdir): def test_get_staged_files_deleted(in_git_dir): in_git_dir.join('test').ensure() cmd_output('git', 'add', 'test') - cmd_output('git', 'commit', '-m', 'foo', '--allow-empty') + git_commit() cmd_output('git', 'rm', '--cached', 'test') assert git.get_staged_files() == [] @@ -104,11 +105,11 @@ def test_parse_merge_msg_for_conflicts(input, expected_output): def test_get_changed_files(in_git_dir): - cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit') + git_commit() in_git_dir.join('a.txt').ensure() in_git_dir.join('b.txt').ensure() cmd_output('git', 'add', '.') - cmd_output('git', 'commit', '-m', 'add some files') + git_commit() files = git.get_changed_files('HEAD', 'HEAD^') assert files == ['a.txt', 'b.txt'] @@ -132,10 +133,10 @@ def test_zsplit(s, expected): @pytest.fixture def non_ascii_repo(in_git_dir): - cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit') + git_commit() in_git_dir.join('интервью').ensure() cmd_output('git', 'add', '.') - cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit') + git_commit() yield in_git_dir diff --git a/tests/make_archives_test.py b/tests/make_archives_test.py index 7f198322..52c9c9b6 100644 --- a/tests/make_archives_test.py +++ b/tests/make_archives_test.py @@ -1,48 +1,45 @@ from __future__ import absolute_import from __future__ import unicode_literals -import os.path import tarfile from pre_commit import git from pre_commit import make_archives from pre_commit.util import cmd_output -from testing.fixtures import git_dir +from testing.util import git_commit -def test_make_archive(tempdir_factory): - output_dir = tempdir_factory.get() - git_path = git_dir(tempdir_factory) +def test_make_archive(in_git_dir, tmpdir): + output_dir = tmpdir.join('output').ensure_dir() # Add a files to the git directory - open(os.path.join(git_path, 'foo'), 'a').close() - cmd_output('git', 'add', '.', cwd=git_path) - cmd_output('git', 'commit', '-m', 'foo', cwd=git_path) + in_git_dir.join('foo').ensure() + cmd_output('git', 'add', '.') + git_commit() # We'll use this rev - head_rev = git.head_rev(git_path) + head_rev = git.head_rev('.') # And check that this file doesn't exist - open(os.path.join(git_path, 'bar'), 'a').close() - cmd_output('git', 'add', '.', cwd=git_path) - cmd_output('git', 'commit', '-m', 'bar', cwd=git_path) + in_git_dir.join('bar').ensure() + cmd_output('git', 'add', '.') + git_commit() # Do the thing archive_path = make_archives.make_archive( - 'foo', git_path, head_rev, output_dir, + 'foo', in_git_dir.strpath, head_rev, output_dir.strpath, ) - assert archive_path == os.path.join(output_dir, 'foo.tar.gz') - assert os.path.exists(archive_path) + expected = output_dir.join('foo.tar.gz') + assert archive_path == expected.strpath + assert expected.exists() - extract_dir = tempdir_factory.get() - - # Extract the tar + extract_dir = tmpdir.join('extract').ensure_dir() with tarfile.open(archive_path) as tf: - tf.extractall(extract_dir) + tf.extractall(extract_dir.strpath) # Verify the contents of the tar - assert os.path.exists(os.path.join(extract_dir, 'foo')) - assert os.path.exists(os.path.join(extract_dir, 'foo', 'foo')) - assert not os.path.exists(os.path.join(extract_dir, 'foo', '.git')) - assert not os.path.exists(os.path.join(extract_dir, 'foo', 'bar')) + assert extract_dir.join('foo').isdir() + assert extract_dir.join('foo/foo').exists() + assert not extract_dir.join('foo/.git').exists() + assert not extract_dir.join('foo/bar').exists() def test_main(tmpdir): diff --git a/tests/staged_files_only_test.py b/tests/staged_files_only_test.py index 9f226a41..619d739b 100644 --- a/tests/staged_files_only_test.py +++ b/tests/staged_files_only_test.py @@ -15,6 +15,7 @@ from testing.auto_namedtuple import auto_namedtuple from testing.fixtures import git_dir from testing.util import cwd from testing.util import get_resource_path +from testing.util import git_commit FOO_CONTENTS = '\n'.join(('1', '2', '3', '4', '5', '6', '7', '8', '')) @@ -186,9 +187,9 @@ def test_img_conflict(img_staged, patch_dir): def submodule_with_commits(tempdir_factory): path = git_dir(tempdir_factory) with cwd(path): - cmd_output('git', 'commit', '--allow-empty', '-m', 'foo') + git_commit() rev1 = cmd_output('git', 'rev-parse', 'HEAD')[1].strip() - cmd_output('git', 'commit', '--allow-empty', '-m', 'bar') + git_commit() rev2 = cmd_output('git', 'rev-parse', 'HEAD')[1].strip() yield auto_namedtuple(path=path, rev1=rev1, rev2=rev2) @@ -331,7 +332,7 @@ def test_autocrlf_commited_crlf(in_git_dir, patch_dir): cmd_output('git', 'config', '--local', 'core.autocrlf', 'false') _write(b'1\r\n2\r\n') cmd_output('git', 'add', 'foo') - cmd_output('git', 'commit', '-m', 'Check in crlf') + git_commit() cmd_output('git', 'config', '--local', 'core.autocrlf', 'true') _write(b'1\r\n2\r\n\r\n\r\n\r\n') diff --git a/tests/store_test.py b/tests/store_test.py index bed0e901..8ef10a93 100644 --- a/tests/store_test.py +++ b/tests/store_test.py @@ -12,10 +12,10 @@ import six from pre_commit import git from pre_commit.store import _get_default_directory from pre_commit.store import Store -from pre_commit.util import cmd_output from pre_commit.util import rmtree from testing.fixtures import git_dir from testing.util import cwd +from testing.util import git_commit def test_our_session_fixture_works(): @@ -90,9 +90,9 @@ def test_does_not_recreate_if_directory_already_exists(store): def test_clone(store, tempdir_factory, log_info_mock): path = git_dir(tempdir_factory) with cwd(path): - cmd_output('git', 'commit', '--allow-empty', '-m', 'foo') + git_commit() rev = git.head_rev(path) - cmd_output('git', 'commit', '--allow-empty', '-m', 'bar') + git_commit() ret = store.clone(path, rev) # Should have printed some stuff