Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 160a11a

Browse files
committed
Improve git_commit helper
1 parent 28c97a9 commit 160a11a

10 files changed

Lines changed: 75 additions & 84 deletions

testing/fixtures.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def make_repo(tempdir_factory, repo_source):
4949
path = git_dir(tempdir_factory)
5050
copy_tree_to_path(get_resource_path(repo_source), path)
5151
cmd_output('git', 'add', '.', cwd=path)
52-
git_commit('Add hooks', cwd=path)
52+
git_commit(msg=make_repo.__name__, cwd=path)
5353
return path
5454

5555

@@ -64,7 +64,7 @@ def modify_manifest(path):
6464
yield manifest
6565
with io.open(manifest_path, 'w') as manifest_file:
6666
manifest_file.write(ordered_dump(manifest, **C.YAML_DUMP_KWARGS))
67-
git_commit('update {}'.format(C.MANIFEST_FILE), cwd=path)
67+
git_commit(msg=modify_manifest.__name__, cwd=path)
6868

6969

7070
@contextlib.contextmanager
@@ -79,7 +79,7 @@ def modify_config(path='.', commit=True):
7979
with io.open(config_path, 'w', encoding='UTF-8') as config_file:
8080
config_file.write(ordered_dump(config, **C.YAML_DUMP_KWARGS))
8181
if commit:
82-
git_commit('update config', cwd=path)
82+
git_commit(msg=modify_config.__name__, cwd=path)
8383

8484

8585
def config_with_local_hooks():
@@ -135,13 +135,13 @@ def write_config(directory, config, config_file=C.CONFIG_FILE):
135135
def add_config_to_repo(git_path, config, config_file=C.CONFIG_FILE):
136136
write_config(git_path, config, config_file=config_file)
137137
cmd_output('git', 'add', config_file, cwd=git_path)
138-
git_commit('Add hooks config', cwd=git_path)
138+
git_commit(msg=add_config_to_repo.__name__, cwd=git_path)
139139
return git_path
140140

141141

142142
def remove_config_from_repo(git_path, config_file=C.CONFIG_FILE):
143143
cmd_output('git', 'rm', config_file, cwd=git_path)
144-
git_commit('Remove hooks config', cwd=git_path)
144+
git_commit(msg=remove_config_from_repo.__name__, cwd=git_path)
145145
return git_path
146146

147147

testing/util.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,11 @@ def cwd(path):
135135
os.chdir(original_cwd)
136136

137137

138-
def git_commit(msg, *_args, **kwargs):
139-
args = ['git']
140-
config = kwargs.pop('config', None)
141-
if config is not None:
142-
args.extend(['-C', config])
143-
args.append('commit')
144-
if msg is not None:
145-
args.extend(['-m', msg])
146-
if '--allow-empty' not in _args:
147-
args.append('--allow-empty')
148-
if '--no-gpg-sign' not in _args:
149-
args.append('--no-gpg-sign')
150-
return cmd_output(*(tuple(args) + tuple(_args)), **kwargs)
138+
def git_commit(*args, **kwargs):
139+
fn = kwargs.pop('fn', cmd_output)
140+
msg = kwargs.pop('msg', 'commit!')
141+
142+
cmd = ('git', 'commit', '--allow-empty', '--no-gpg-sign', '-a') + args
143+
if msg is not None: # allow skipping `-m` with `msg=None`
144+
cmd += ('-m', msg)
145+
return fn(*cmd, **kwargs)

tests/commands/autoupdate_test.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ def test_autoupdate_old_revision_broken(tempdir_factory, in_tmpdir, store):
6060
config = make_config_from_repo(path, check=False)
6161

6262
cmd_output('git', 'mv', C.MANIFEST_FILE, 'nope.yaml', cwd=path)
63-
git_commit('simulate old repo', cwd=path)
63+
git_commit(cwd=path)
6464
# Assume this is the revision the user's old repository was at
6565
rev = git.head_rev(path)
6666
cmd_output('git', 'mv', 'nope.yaml', C.MANIFEST_FILE, cwd=path)
67-
git_commit('move hooks file', cwd=path)
67+
git_commit(cwd=path)
6868
update_rev = git.head_rev(path)
6969

7070
config['rev'] = rev
@@ -84,8 +84,7 @@ def out_of_date_repo(tempdir_factory):
8484
path = make_repo(tempdir_factory, 'python_hooks_repo')
8585
original_rev = git.head_rev(path)
8686

87-
# Make a commit
88-
git_commit('foo', cwd=path)
87+
git_commit(cwd=path)
8988
head_rev = git.head_rev(path)
9089

9190
yield auto_namedtuple(
@@ -240,7 +239,7 @@ def test_autoupdate_tagged_repo(tagged_repo, in_tmpdir, store):
240239

241240
@pytest.fixture
242241
def tagged_repo_with_more_commits(tagged_repo):
243-
git_commit('foo', cwd=tagged_repo.path)
242+
git_commit(cwd=tagged_repo.path)
244243
yield tagged_repo
245244

246245

@@ -263,8 +262,8 @@ def test_autoupdate_latest_no_config(out_of_date_repo, in_tmpdir, store):
263262
)
264263
write_config('.', config)
265264

266-
cmd_output('git', '-C', out_of_date_repo.path, 'rm', '-r', ':/')
267-
git_commit('rm', config=out_of_date_repo.path)
265+
cmd_output('git', 'rm', '-r', ':/', cwd=out_of_date_repo.path)
266+
git_commit(cwd=out_of_date_repo.path)
268267

269268
ret = autoupdate(C.CONFIG_FILE, store, tags_only=False)
270269
assert ret == 1
@@ -282,7 +281,7 @@ def hook_disappearing_repo(tempdir_factory):
282281
os.path.join(path, C.MANIFEST_FILE),
283282
)
284283
cmd_output('git', 'add', '.', cwd=path)
285-
git_commit('Remove foo', cwd=path)
284+
git_commit(cwd=path)
286285

287286
yield auto_namedtuple(path=path, original_rev=original_rev)
288287

tests/commands/install_uninstall_test.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,10 @@ def test_uninstall(in_git_dir, store):
106106

107107

108108
def _get_commit_output(tempdir_factory, touch_file='foo', **kwargs):
109-
commit_msg = kwargs.pop('commit_msg', 'Commit!')
110109
open(touch_file, 'a').close()
111110
cmd_output('git', 'add', touch_file)
112-
return cmd_output_mocked_pre_commit_home(
113-
'git', 'commit', '-am', commit_msg, '--allow-empty', '--no-gpg-sign',
111+
return git_commit(
112+
fn=cmd_output_mocked_pre_commit_home,
114113
# git commit puts pre-commit to stderr
115114
stderr=subprocess.STDOUT,
116115
retcode=None,
@@ -132,7 +131,7 @@ def _get_commit_output(tempdir_factory, touch_file='foo', **kwargs):
132131
NORMAL_PRE_COMMIT_RUN = re.compile(
133132
r'^\[INFO\] Initializing environment for .+\.\r?\n'
134133
r'Bash hook\.+Passed\r?\n'
135-
r'\[master [a-f0-9]{7}\] Commit!\r?\n' +
134+
r'\[master [a-f0-9]{7}\] commit!\r?\n' +
136135
FILES_CHANGED +
137136
r' create mode 100644 foo\r?\n$',
138137
)
@@ -152,7 +151,7 @@ def test_install_pre_commit_and_run_custom_path(tempdir_factory, store):
152151
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
153152
with cwd(path):
154153
cmd_output('git', 'mv', C.CONFIG_FILE, 'custom-config.yaml')
155-
git_commit('move pre-commit config')
154+
git_commit(cwd=path)
156155
assert install('custom-config.yaml', store) == 0
157156

158157
ret, output = _get_commit_output(tempdir_factory)
@@ -164,7 +163,7 @@ def test_install_in_submodule_and_run(tempdir_factory, store):
164163
src_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
165164
parent_path = git_dir(tempdir_factory)
166165
cmd_output('git', 'submodule', 'add', src_path, 'sub', cwd=parent_path)
167-
git_commit('foo', cwd=parent_path)
166+
git_commit(cwd=parent_path)
168167

169168
sub_pth = os.path.join(parent_path, 'sub')
170169
with cwd(sub_pth):
@@ -194,7 +193,7 @@ def test_commit_am(tempdir_factory, store):
194193
# Make an unstaged change
195194
open('unstaged', 'w').close()
196195
cmd_output('git', 'add', '.')
197-
git_commit('foo')
196+
git_commit(cwd=path)
198197
with io.open('unstaged', 'w') as foo_file:
199198
foo_file.write('Oh hai')
200199

@@ -209,12 +208,14 @@ def test_unicode_merge_commit_message(tempdir_factory, store):
209208
with cwd(path):
210209
assert install(C.CONFIG_FILE, store) == 0
211210
cmd_output('git', 'checkout', 'master', '-b', 'foo')
212-
git_commit('branch2', '-n')
211+
git_commit('-n', cwd=path)
213212
cmd_output('git', 'checkout', 'master')
214213
cmd_output('git', 'merge', 'foo', '--no-ff', '--no-commit', '-m', '☃')
215214
# Used to crash
216-
cmd_output_mocked_pre_commit_home(
217-
'git', 'commit', '--no-edit', '--no-gpg-sign',
215+
git_commit(
216+
'--no-edit',
217+
msg=None,
218+
fn=cmd_output_mocked_pre_commit_home,
218219
tempdir_factory=tempdir_factory,
219220
)
220221

@@ -248,7 +249,6 @@ def test_environment_not_sourced(tempdir_factory, store):
248249
# Use a specific homedir to ignore --user installs
249250
homedir = tempdir_factory.get()
250251
ret, stdout, stderr = git_commit(
251-
'foo',
252252
env={
253253
'HOME': homedir,
254254
'PATH': _path_without_us(),
@@ -291,7 +291,7 @@ def test_failing_hooks_returns_nonzero(tempdir_factory, store):
291291

292292
EXISTING_COMMIT_RUN = re.compile(
293293
r'^legacy hook\r?\n'
294-
r'\[master [a-f0-9]{7}\] Commit!\r?\n' +
294+
r'\[master [a-f0-9]{7}\] commit!\r?\n' +
295295
FILES_CHANGED +
296296
r' create mode 100644 baz\r?\n$',
297297
)
@@ -434,7 +434,7 @@ def test_uninstall_doesnt_remove_not_our_hooks(in_git_dir):
434434

435435
PRE_INSTALLED = re.compile(
436436
r'Bash hook\.+Passed\r?\n'
437-
r'\[master [a-f0-9]{7}\] Commit!\r?\n' +
437+
r'\[master [a-f0-9]{7}\] commit!\r?\n' +
438438
FILES_CHANGED +
439439
r' create mode 100644 foo\r?\n$',
440440
)
@@ -546,7 +546,7 @@ def test_pre_push_force_push_without_fetch(tempdir_factory, store):
546546

547547
with cwd(path2):
548548
install(C.CONFIG_FILE, store, hook_type='pre-push')
549-
assert _get_commit_output(tempdir_factory, commit_msg='force!')[0] == 0
549+
assert _get_commit_output(tempdir_factory, msg='force!')[0] == 0
550550

551551
retc, output = _get_push_output(tempdir_factory, opts=('--force',))
552552
assert retc == 0
@@ -625,7 +625,7 @@ def test_commit_msg_integration_passing(
625625
):
626626
install(C.CONFIG_FILE, store, hook_type='commit-msg')
627627
msg = 'Hi\nSigned off by: me, lol'
628-
retc, out = _get_commit_output(tempdir_factory, commit_msg=msg)
628+
retc, out = _get_commit_output(tempdir_factory, msg=msg)
629629
assert retc == 0
630630
first_line = out.splitlines()[0]
631631
assert first_line.startswith('Must have "Signed off by:"...')
@@ -647,7 +647,7 @@ def test_commit_msg_legacy(commit_msg_repo, tempdir_factory, store):
647647
install(C.CONFIG_FILE, store, hook_type='commit-msg')
648648

649649
msg = 'Hi\nSigned off by: asottile'
650-
retc, out = _get_commit_output(tempdir_factory, commit_msg=msg)
650+
retc, out = _get_commit_output(tempdir_factory, msg=msg)
651651
assert retc == 0
652652
first_line, second_line = out.splitlines()[:2]
653653
assert first_line == 'legacy'

tests/commands/run_test.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from testing.fixtures import read_config
2525
from testing.util import cmd_output_mocked_pre_commit_home
2626
from testing.util import cwd
27+
from testing.util import git_commit
2728
from testing.util import run_opts
2829
from testing.util import xfailif_no_symlink
2930

@@ -478,8 +479,8 @@ def test_stdout_write_bug_py26(repo_with_failing_hook, store, tempdir_factory):
478479
install(C.CONFIG_FILE, store)
479480

480481
# Have to use subprocess because pytest monkeypatches sys.stdout
481-
_, stdout, _ = cmd_output_mocked_pre_commit_home(
482-
'git', 'commit', '-m', 'Commit!', '--no-gpg-sign',
482+
_, stdout, _ = git_commit(
483+
fn=cmd_output_mocked_pre_commit_home,
483484
# git commit puts pre-commit to stderr
484485
stderr=subprocess.STDOUT,
485486
retcode=None,
@@ -507,8 +508,8 @@ def test_lots_of_files(store, tempdir_factory):
507508
cmd_output('git', 'add', '.')
508509
install(C.CONFIG_FILE, store)
509510

510-
cmd_output_mocked_pre_commit_home(
511-
'git', 'commit', '-m', 'Commit!', '--no-gpg-sign',
511+
git_commit(
512+
fn=cmd_output_mocked_pre_commit_home,
512513
# git commit puts pre-commit to stderr
513514
stderr=subprocess.STDOUT,
514515
tempdir_factory=tempdir_factory,

tests/conftest.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ def _make_conflict():
8080
with io.open('foo_only_file', 'w') as foo_only_file:
8181
foo_only_file.write('foo')
8282
cmd_output('git', 'add', 'foo_only_file')
83-
git_commit('conflict_file')
83+
git_commit(msg=_make_conflict.__name__)
8484
cmd_output('git', 'checkout', 'origin/master', '-b', 'bar')
8585
with io.open('conflict_file', 'w') as conflict_file:
8686
conflict_file.write('harp\nddrp\n')
8787
cmd_output('git', 'add', 'conflict_file')
8888
with io.open('bar_only_file', 'w') as bar_only_file:
8989
bar_only_file.write('bar')
9090
cmd_output('git', 'add', 'bar_only_file')
91-
git_commit('conflict_file')
91+
git_commit(msg=_make_conflict.__name__)
9292
cmd_output('git', 'merge', 'foo', retcode=None)
9393

9494

@@ -97,7 +97,7 @@ def in_merge_conflict(tempdir_factory):
9797
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
9898
open(os.path.join(path, 'dummy'), 'a').close()
9999
cmd_output('git', 'add', 'dummy', cwd=path)
100-
git_commit('Add config.', cwd=path)
100+
git_commit(msg=in_merge_conflict.__name__, cwd=path)
101101

102102
conflict_path = tempdir_factory.get()
103103
cmd_output('git', 'clone', path, conflict_path)
@@ -110,7 +110,7 @@ def in_merge_conflict(tempdir_factory):
110110
def in_conflicting_submodule(tempdir_factory):
111111
git_dir_1 = git_dir(tempdir_factory)
112112
git_dir_2 = git_dir(tempdir_factory)
113-
git_commit('init!', cwd=git_dir_2)
113+
git_commit(msg=in_conflicting_submodule.__name__, cwd=git_dir_2)
114114
cmd_output('git', 'submodule', 'add', git_dir_2, 'sub', cwd=git_dir_1)
115115
with cwd(os.path.join(git_dir_1, 'sub')):
116116
_make_conflict()
@@ -136,7 +136,7 @@ def commit_msg_repo(tempdir_factory):
136136
write_config(path, config)
137137
with cwd(path):
138138
cmd_output('git', 'add', '.')
139-
git_commit('add hooks')
139+
git_commit(msg=commit_msg_repo.__name__)
140140
yield path
141141

142142

tests/git_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_get_root_not_git_dir(in_tmpdir):
3131
def test_get_staged_files_deleted(in_git_dir):
3232
in_git_dir.join('test').ensure()
3333
cmd_output('git', 'add', 'test')
34-
cmd_output('git', 'commit', '-m', 'foo', '--allow-empty')
34+
git_commit()
3535
cmd_output('git', 'rm', '--cached', 'test')
3636
assert git.get_staged_files() == []
3737

@@ -105,11 +105,11 @@ def test_parse_merge_msg_for_conflicts(input, expected_output):
105105

106106

107107
def test_get_changed_files(in_git_dir):
108-
git_commit('initial commit')
108+
git_commit()
109109
in_git_dir.join('a.txt').ensure()
110110
in_git_dir.join('b.txt').ensure()
111111
cmd_output('git', 'add', '.')
112-
git_commit('add some files')
112+
git_commit()
113113
files = git.get_changed_files('HEAD', 'HEAD^')
114114
assert files == ['a.txt', 'b.txt']
115115

@@ -133,10 +133,10 @@ def test_zsplit(s, expected):
133133

134134
@pytest.fixture
135135
def non_ascii_repo(in_git_dir):
136-
git_commit('initial commit')
136+
git_commit()
137137
in_git_dir.join('интервью').ensure()
138138
cmd_output('git', 'add', '.')
139-
git_commit('initial commit')
139+
git_commit()
140140
yield in_git_dir
141141

142142

0 commit comments

Comments
 (0)