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

Skip to content

Commit d2b92e8

Browse files
authored
Merge pull request #894 from s0undt3ch/hotfix/no-gpg-sign
Don't fail if GPG signing is configured by default. All references.
2 parents 7afb294 + 160a11a commit d2b92e8

10 files changed

Lines changed: 86 additions & 85 deletions

testing/fixtures.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from pre_commit.clientlib import load_manifest
1919
from pre_commit.util import cmd_output
2020
from testing.util import get_resource_path
21+
from testing.util import git_commit
2122

2223

2324
def copy_tree_to_path(src_dir, dest_dir):
@@ -48,7 +49,7 @@ def make_repo(tempdir_factory, repo_source):
4849
path = git_dir(tempdir_factory)
4950
copy_tree_to_path(get_resource_path(repo_source), path)
5051
cmd_output('git', 'add', '.', cwd=path)
51-
cmd_output('git', 'commit', '--no-gpg-sign', '-m', 'Add hooks', cwd=path)
52+
git_commit(msg=make_repo.__name__, cwd=path)
5253
return path
5354

5455

@@ -63,11 +64,7 @@ def modify_manifest(path):
6364
yield manifest
6465
with io.open(manifest_path, 'w') as manifest_file:
6566
manifest_file.write(ordered_dump(manifest, **C.YAML_DUMP_KWARGS))
66-
cmd_output(
67-
'git', 'commit', '--no-gpg-sign', '-am',
68-
'update {}'.format(C.MANIFEST_FILE),
69-
cwd=path,
70-
)
67+
git_commit(msg=modify_manifest.__name__, cwd=path)
7168

7269

7370
@contextlib.contextmanager
@@ -82,9 +79,7 @@ def modify_config(path='.', commit=True):
8279
with io.open(config_path, 'w', encoding='UTF-8') as config_file:
8380
config_file.write(ordered_dump(config, **C.YAML_DUMP_KWARGS))
8481
if commit:
85-
cmd_output(
86-
'git', 'commit', '--no-gpg-sign', '-am', 'update config', cwd=path,
87-
)
82+
git_commit(msg=modify_config.__name__, cwd=path)
8883

8984

9085
def config_with_local_hooks():
@@ -140,19 +135,13 @@ def write_config(directory, config, config_file=C.CONFIG_FILE):
140135
def add_config_to_repo(git_path, config, config_file=C.CONFIG_FILE):
141136
write_config(git_path, config, config_file=config_file)
142137
cmd_output('git', 'add', config_file, cwd=git_path)
143-
cmd_output(
144-
'git', 'commit', '--no-gpg-sign', '-m', 'Add hooks config',
145-
cwd=git_path,
146-
)
138+
git_commit(msg=add_config_to_repo.__name__, cwd=git_path)
147139
return git_path
148140

149141

150142
def remove_config_from_repo(git_path, config_file=C.CONFIG_FILE):
151143
cmd_output('git', 'rm', config_file, cwd=git_path)
152-
cmd_output(
153-
'git', 'commit', '--no-gpg-sign', '-m', 'Remove hooks config',
154-
cwd=git_path,
155-
)
144+
git_commit(msg=remove_config_from_repo.__name__, cwd=git_path)
156145
return git_path
157146

158147

testing/util.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,13 @@ def cwd(path):
133133
yield
134134
finally:
135135
os.chdir(original_cwd)
136+
137+
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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from testing.fixtures import make_repo
2222
from testing.fixtures import write_config
2323
from testing.util import get_resource_path
24+
from testing.util import git_commit
2425

2526

2627
@pytest.fixture
@@ -59,11 +60,11 @@ def test_autoupdate_old_revision_broken(tempdir_factory, in_tmpdir, store):
5960
config = make_config_from_repo(path, check=False)
6061

6162
cmd_output('git', 'mv', C.MANIFEST_FILE, 'nope.yaml', cwd=path)
62-
cmd_output('git', 'commit', '-m', 'simulate old repo', cwd=path)
63+
git_commit(cwd=path)
6364
# Assume this is the revision the user's old repository was at
6465
rev = git.head_rev(path)
6566
cmd_output('git', 'mv', 'nope.yaml', C.MANIFEST_FILE, cwd=path)
66-
cmd_output('git', 'commit', '-m', 'move hooks file', cwd=path)
67+
git_commit(cwd=path)
6768
update_rev = git.head_rev(path)
6869

6970
config['rev'] = rev
@@ -83,8 +84,7 @@ def out_of_date_repo(tempdir_factory):
8384
path = make_repo(tempdir_factory, 'python_hooks_repo')
8485
original_rev = git.head_rev(path)
8586

86-
# Make a commit
87-
cmd_output('git', 'commit', '--allow-empty', '-m', 'foo', cwd=path)
87+
git_commit(cwd=path)
8888
head_rev = git.head_rev(path)
8989

9090
yield auto_namedtuple(
@@ -239,7 +239,7 @@ def test_autoupdate_tagged_repo(tagged_repo, in_tmpdir, store):
239239

240240
@pytest.fixture
241241
def tagged_repo_with_more_commits(tagged_repo):
242-
cmd_output('git', 'commit', '--allow-empty', '-mfoo', cwd=tagged_repo.path)
242+
git_commit(cwd=tagged_repo.path)
243243
yield tagged_repo
244244

245245

@@ -262,8 +262,8 @@ def test_autoupdate_latest_no_config(out_of_date_repo, in_tmpdir, store):
262262
)
263263
write_config('.', config)
264264

265-
cmd_output('git', '-C', out_of_date_repo.path, 'rm', '-r', ':/')
266-
cmd_output('git', '-C', out_of_date_repo.path, 'commit', '-m', 'rm')
265+
cmd_output('git', 'rm', '-r', ':/', cwd=out_of_date_repo.path)
266+
git_commit(cwd=out_of_date_repo.path)
267267

268268
ret = autoupdate(C.CONFIG_FILE, store, tags_only=False)
269269
assert ret == 1
@@ -281,7 +281,7 @@ def hook_disappearing_repo(tempdir_factory):
281281
os.path.join(path, C.MANIFEST_FILE),
282282
)
283283
cmd_output('git', 'add', '.', cwd=path)
284-
cmd_output('git', 'commit', '-m', 'Remove foo', cwd=path)
284+
git_commit(cwd=path)
285285

286286
yield auto_namedtuple(path=path, original_rev=original_rev)
287287

tests/commands/install_uninstall_test.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from testing.fixtures import remove_config_from_repo
2929
from testing.util import cmd_output_mocked_pre_commit_home
3030
from testing.util import cwd
31+
from testing.util import git_commit
3132
from testing.util import xfailif_no_symlink
3233

3334

@@ -105,11 +106,10 @@ def test_uninstall(in_git_dir, store):
105106

106107

107108
def _get_commit_output(tempdir_factory, touch_file='foo', **kwargs):
108-
commit_msg = kwargs.pop('commit_msg', 'Commit!')
109109
open(touch_file, 'a').close()
110110
cmd_output('git', 'add', touch_file)
111-
return cmd_output_mocked_pre_commit_home(
112-
'git', 'commit', '-am', commit_msg, '--allow-empty',
111+
return git_commit(
112+
fn=cmd_output_mocked_pre_commit_home,
113113
# git commit puts pre-commit to stderr
114114
stderr=subprocess.STDOUT,
115115
retcode=None,
@@ -131,7 +131,7 @@ def _get_commit_output(tempdir_factory, touch_file='foo', **kwargs):
131131
NORMAL_PRE_COMMIT_RUN = re.compile(
132132
r'^\[INFO\] Initializing environment for .+\.\r?\n'
133133
r'Bash hook\.+Passed\r?\n'
134-
r'\[master [a-f0-9]{7}\] Commit!\r?\n' +
134+
r'\[master [a-f0-9]{7}\] commit!\r?\n' +
135135
FILES_CHANGED +
136136
r' create mode 100644 foo\r?\n$',
137137
)
@@ -151,7 +151,7 @@ def test_install_pre_commit_and_run_custom_path(tempdir_factory, store):
151151
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
152152
with cwd(path):
153153
cmd_output('git', 'mv', C.CONFIG_FILE, 'custom-config.yaml')
154-
cmd_output('git', 'commit', '-m', 'move pre-commit config')
154+
git_commit(cwd=path)
155155
assert install('custom-config.yaml', store) == 0
156156

157157
ret, output = _get_commit_output(tempdir_factory)
@@ -163,7 +163,7 @@ def test_install_in_submodule_and_run(tempdir_factory, store):
163163
src_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
164164
parent_path = git_dir(tempdir_factory)
165165
cmd_output('git', 'submodule', 'add', src_path, 'sub', cwd=parent_path)
166-
cmd_output('git', 'commit', '-m', 'foo', cwd=parent_path)
166+
git_commit(cwd=parent_path)
167167

168168
sub_pth = os.path.join(parent_path, 'sub')
169169
with cwd(sub_pth):
@@ -193,7 +193,7 @@ def test_commit_am(tempdir_factory, store):
193193
# Make an unstaged change
194194
open('unstaged', 'w').close()
195195
cmd_output('git', 'add', '.')
196-
cmd_output('git', 'commit', '-m', 'foo')
196+
git_commit(cwd=path)
197197
with io.open('unstaged', 'w') as foo_file:
198198
foo_file.write('Oh hai')
199199

@@ -208,12 +208,14 @@ def test_unicode_merge_commit_message(tempdir_factory, store):
208208
with cwd(path):
209209
assert install(C.CONFIG_FILE, store) == 0
210210
cmd_output('git', 'checkout', 'master', '-b', 'foo')
211-
cmd_output('git', 'commit', '--allow-empty', '-n', '-m', 'branch2')
211+
git_commit('-n', cwd=path)
212212
cmd_output('git', 'checkout', 'master')
213213
cmd_output('git', 'merge', 'foo', '--no-ff', '--no-commit', '-m', '☃')
214214
# Used to crash
215-
cmd_output_mocked_pre_commit_home(
216-
'git', 'commit', '--no-edit',
215+
git_commit(
216+
'--no-edit',
217+
msg=None,
218+
fn=cmd_output_mocked_pre_commit_home,
217219
tempdir_factory=tempdir_factory,
218220
)
219221

@@ -246,8 +248,7 @@ def test_environment_not_sourced(tempdir_factory, store):
246248

247249
# Use a specific homedir to ignore --user installs
248250
homedir = tempdir_factory.get()
249-
ret, stdout, stderr = cmd_output(
250-
'git', 'commit', '--allow-empty', '-m', 'foo',
251+
ret, stdout, stderr = git_commit(
251252
env={
252253
'HOME': homedir,
253254
'PATH': _path_without_us(),
@@ -290,7 +291,7 @@ def test_failing_hooks_returns_nonzero(tempdir_factory, store):
290291

291292
EXISTING_COMMIT_RUN = re.compile(
292293
r'^legacy hook\r?\n'
293-
r'\[master [a-f0-9]{7}\] Commit!\r?\n' +
294+
r'\[master [a-f0-9]{7}\] commit!\r?\n' +
294295
FILES_CHANGED +
295296
r' create mode 100644 baz\r?\n$',
296297
)
@@ -433,7 +434,7 @@ def test_uninstall_doesnt_remove_not_our_hooks(in_git_dir):
433434

434435
PRE_INSTALLED = re.compile(
435436
r'Bash hook\.+Passed\r?\n'
436-
r'\[master [a-f0-9]{7}\] Commit!\r?\n' +
437+
r'\[master [a-f0-9]{7}\] commit!\r?\n' +
437438
FILES_CHANGED +
438439
r' create mode 100644 foo\r?\n$',
439440
)
@@ -545,7 +546,7 @@ def test_pre_push_force_push_without_fetch(tempdir_factory, store):
545546

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

550551
retc, output = _get_push_output(tempdir_factory, opts=('--force',))
551552
assert retc == 0
@@ -624,7 +625,7 @@ def test_commit_msg_integration_passing(
624625
):
625626
install(C.CONFIG_FILE, store, hook_type='commit-msg')
626627
msg = 'Hi\nSigned off by: me, lol'
627-
retc, out = _get_commit_output(tempdir_factory, commit_msg=msg)
628+
retc, out = _get_commit_output(tempdir_factory, msg=msg)
628629
assert retc == 0
629630
first_line = out.splitlines()[0]
630631
assert first_line.startswith('Must have "Signed off by:"...')
@@ -646,7 +647,7 @@ def test_commit_msg_legacy(commit_msg_repo, tempdir_factory, store):
646647
install(C.CONFIG_FILE, store, hook_type='commit-msg')
647648

648649
msg = 'Hi\nSigned off by: asottile'
649-
retc, out = _get_commit_output(tempdir_factory, commit_msg=msg)
650+
retc, out = _get_commit_output(tempdir_factory, msg=msg)
650651
assert retc == 0
651652
first_line, second_line = out.splitlines()[:2]
652653
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!',
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!',
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: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from testing.fixtures import make_consuming_repo
2020
from testing.fixtures import write_config
2121
from testing.util import cwd
22+
from testing.util import git_commit
2223

2324

2425
@pytest.fixture(autouse=True)
@@ -79,15 +80,15 @@ def _make_conflict():
7980
with io.open('foo_only_file', 'w') as foo_only_file:
8081
foo_only_file.write('foo')
8182
cmd_output('git', 'add', 'foo_only_file')
82-
cmd_output('git', 'commit', '-m', 'conflict_file')
83+
git_commit(msg=_make_conflict.__name__)
8384
cmd_output('git', 'checkout', 'origin/master', '-b', 'bar')
8485
with io.open('conflict_file', 'w') as conflict_file:
8586
conflict_file.write('harp\nddrp\n')
8687
cmd_output('git', 'add', 'conflict_file')
8788
with io.open('bar_only_file', 'w') as bar_only_file:
8889
bar_only_file.write('bar')
8990
cmd_output('git', 'add', 'bar_only_file')
90-
cmd_output('git', 'commit', '-m', 'conflict_file')
91+
git_commit(msg=_make_conflict.__name__)
9192
cmd_output('git', 'merge', 'foo', retcode=None)
9293

9394

@@ -96,7 +97,7 @@ def in_merge_conflict(tempdir_factory):
9697
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
9798
open(os.path.join(path, 'dummy'), 'a').close()
9899
cmd_output('git', 'add', 'dummy', cwd=path)
99-
cmd_output('git', 'commit', '-m', 'Add config.', cwd=path)
100+
git_commit(msg=in_merge_conflict.__name__, cwd=path)
100101

101102
conflict_path = tempdir_factory.get()
102103
cmd_output('git', 'clone', path, conflict_path)
@@ -109,7 +110,7 @@ def in_merge_conflict(tempdir_factory):
109110
def in_conflicting_submodule(tempdir_factory):
110111
git_dir_1 = git_dir(tempdir_factory)
111112
git_dir_2 = git_dir(tempdir_factory)
112-
cmd_output('git', 'commit', '--allow-empty', '-minit!', cwd=git_dir_2)
113+
git_commit(msg=in_conflicting_submodule.__name__, cwd=git_dir_2)
113114
cmd_output('git', 'submodule', 'add', git_dir_2, 'sub', cwd=git_dir_1)
114115
with cwd(os.path.join(git_dir_1, 'sub')):
115116
_make_conflict()
@@ -135,7 +136,7 @@ def commit_msg_repo(tempdir_factory):
135136
write_config(path, config)
136137
with cwd(path):
137138
cmd_output('git', 'add', '.')
138-
cmd_output('git', 'commit', '-m', 'add hooks')
139+
git_commit(msg=commit_msg_repo.__name__)
139140
yield path
140141

141142

tests/git_test.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pre_commit import git
1010
from pre_commit.error_handler import FatalError
1111
from pre_commit.util import cmd_output
12+
from testing.util import git_commit
1213

1314

1415
def test_get_root_at_root(in_git_dir):
@@ -30,7 +31,7 @@ def test_get_root_not_git_dir(in_tmpdir):
3031
def test_get_staged_files_deleted(in_git_dir):
3132
in_git_dir.join('test').ensure()
3233
cmd_output('git', 'add', 'test')
33-
cmd_output('git', 'commit', '-m', 'foo', '--allow-empty')
34+
git_commit()
3435
cmd_output('git', 'rm', '--cached', 'test')
3536
assert git.get_staged_files() == []
3637

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

105106

106107
def test_get_changed_files(in_git_dir):
107-
cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit')
108+
git_commit()
108109
in_git_dir.join('a.txt').ensure()
109110
in_git_dir.join('b.txt').ensure()
110111
cmd_output('git', 'add', '.')
111-
cmd_output('git', 'commit', '-m', 'add some files')
112+
git_commit()
112113
files = git.get_changed_files('HEAD', 'HEAD^')
113114
assert files == ['a.txt', 'b.txt']
114115

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

133134
@pytest.fixture
134135
def non_ascii_repo(in_git_dir):
135-
cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit')
136+
git_commit()
136137
in_git_dir.join('интервью').ensure()
137138
cmd_output('git', 'add', '.')
138-
cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit')
139+
git_commit()
139140
yield in_git_dir
140141

141142

0 commit comments

Comments
 (0)