Skip to content

Commit 0bb8a8f

Browse files
committed
Move test to install_uninstall test so environment variables apply
1 parent 57cc814 commit 0bb8a8f

File tree

6 files changed

+46
-47
lines changed

6 files changed

+46
-47
lines changed

pre_commit/commands/run.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,9 @@ def run(
312312
environ['PRE_COMMIT_ORIGIN'] = args.origin
313313
environ['PRE_COMMIT_SOURCE'] = args.source
314314

315-
if args.push_remote_name and args.push_remote_url:
316-
environ['PRE_COMMIT_REMOTE_NAME'] = args.push_remote_name
317-
environ['PRE_COMMIT_REMOTE_URL'] = args.push_remote_url
315+
if args.remote_name and args.remote_url:
316+
environ['PRE_COMMIT_REMOTE_NAME'] = args.remote_name
317+
environ['PRE_COMMIT_REMOTE_URL'] = args.remote_url
318318

319319
with contextlib.ExitStack() as exit_stack:
320320
if stash:

pre_commit/main.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,9 @@ def _add_run_options(parser: argparse.ArgumentParser) -> None:
102102
help='Filename to check when running during `commit-msg`',
103103
)
104104
parser.add_argument(
105-
'--push-remote-name', help='Remote name used by `git push`.',
106-
)
107-
parser.add_argument(
108-
'--push-remote-url', help='Remote url used by `git push`.',
105+
'--remote-name', help='Remote name used by `git push`.',
109106
)
107+
parser.add_argument('--remote-url', help='Remote url used by `git push`.')
110108
parser.add_argument(
111109
'--hook-stage', choices=C.STAGES, default='commit',
112110
help='The stage during which the hook is fired. One of %(choices)s',

pre_commit/resources/hook-tmpl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,9 @@ def _pre_push(stdin: bytes) -> Tuple[str, ...]:
151151
opts = ('--origin', local_sha, '--source', source)
152152

153153
if opts:
154-
remote_opts = (
155-
'--push-remote-name', remote_name, '--push-remote-url', remote_url,
154+
return (
155+
*opts, '--remote-name', remote_name, '--remote-url', remote_url,
156156
)
157-
return opts + remote_opts
158157
else:
159158
# An attempt to push an empty changeset
160159
raise EarlyExit()

testing/util.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ def run_opts(
6767
hook=None,
6868
origin='',
6969
source='',
70-
push_remote_name='',
71-
push_remote_url='',
70+
remote_name='',
71+
remote_url='',
7272
hook_stage='commit',
7373
show_diff_on_failure=False,
7474
commit_msg_filename='',
@@ -83,8 +83,8 @@ def run_opts(
8383
hook=hook,
8484
origin=origin,
8585
source=source,
86-
push_remote_name=push_remote_name,
87-
push_remote_url=push_remote_url,
86+
remote_name=remote_name,
87+
remote_url=remote_url,
8888
hook_stage=hook_stage,
8989
show_diff_on_failure=show_diff_on_failure,
9090
commit_msg_filename=commit_msg_filename,

tests/commands/install_uninstall_test.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pre_commit.util import cmd_output
1616
from pre_commit.util import make_executable
1717
from pre_commit.util import resource_text
18+
from testing.fixtures import add_config_to_repo
1819
from testing.fixtures import git_dir
1920
from testing.fixtures import make_consuming_repo
2021
from testing.fixtures import remove_config_from_repo
@@ -512,9 +513,9 @@ def test_installed_from_venv(tempdir_factory, store):
512513
assert NORMAL_PRE_COMMIT_RUN.match(output)
513514

514515

515-
def _get_push_output(tempdir_factory, opts=()):
516+
def _get_push_output(tempdir_factory, remote='origin', opts=()):
516517
return cmd_output_mocked_pre_commit_home(
517-
'git', 'push', 'origin', 'HEAD:new_branch', *opts,
518+
'git', 'push', remote, 'HEAD:new_branch', *opts,
518519
tempdir_factory=tempdir_factory,
519520
retcode=None,
520521
)[:2]
@@ -589,6 +590,33 @@ def test_pre_push_new_upstream(tempdir_factory, store):
589590
assert 'Passed' in output
590591

591592

593+
def test_pre_push_environment_variables(tempdir_factory, store):
594+
config = {
595+
'repo': 'local',
596+
'hooks': [
597+
{
598+
'id': 'print-remote-info',
599+
'name': 'print remote info',
600+
'entry': 'bash -c "echo remote: $PRE_COMMIT_REMOTE_NAME"',
601+
'language': 'system',
602+
'verbose': True,
603+
},
604+
],
605+
}
606+
607+
upstream = git_dir(tempdir_factory)
608+
clone = tempdir_factory.get()
609+
cmd_output('git', 'clone', upstream, clone)
610+
add_config_to_repo(clone, config)
611+
with cwd(clone):
612+
install(C.CONFIG_FILE, store, hook_types=['pre-push'])
613+
614+
cmd_output('git', 'remote', 'rename', 'origin', 'origin2')
615+
retc, output = _get_push_output(tempdir_factory, remote='origin2')
616+
assert retc == 0
617+
assert '\nremote: origin2\n' in output
618+
619+
592620
def test_pre_push_integration_empty_push(tempdir_factory, store):
593621
upstream = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
594622
path = tempdir_factory.get()

tests/commands/run_test.py

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,11 @@ def test_origin_source_error_msg_error(
456456
assert b'Specify both --origin and --source.' in printed
457457

458458

459-
def test_origin_source_both_ok(cap_out, store, repo_with_passing_hook):
460-
args = run_opts(origin='master', source='master')
459+
def test_all_push_options_ok(cap_out, store, repo_with_passing_hook):
460+
args = run_opts(
461+
origin='master', source='master',
462+
remote_name='origin', remote_url='https://example.com/repo',
463+
)
461464
ret, printed = _do_run(cap_out, store, repo_with_passing_hook, args)
462465
assert ret == 0
463466
assert b'Specify both --origin and --source.' not in printed
@@ -687,35 +690,6 @@ def _run_for_stage(stage):
687690
assert _run_for_stage('commit-msg').startswith(b'hook 5...')
688691

689692

690-
def test_push_remote_environment(cap_out, store, repo_with_passing_hook):
691-
config = {
692-
'repo': 'local',
693-
'hooks': [
694-
{
695-
'id': 'print-push-remote',
696-
'name': 'Print push remote name',
697-
'entry': 'entry: bash -c \'echo "$PRE_COMMIT_REMOTE_NAME"\'',
698-
'language': 'system',
699-
'verbose': bool(1),
700-
},
701-
],
702-
}
703-
add_config_to_repo(repo_with_passing_hook, config)
704-
705-
_test_run(
706-
cap_out,
707-
store,
708-
repo_with_passing_hook,
709-
opts={
710-
'push_remote_name': 'origin',
711-
'push_remote_url': 'https://github.com/pre-commit/pre-commit',
712-
},
713-
expected_outputs=[b'Print push remote name', b'Passed'],
714-
expected_ret=0,
715-
stage=['push'],
716-
)
717-
718-
719693
def test_commit_msg_hook(cap_out, store, commit_msg_repo):
720694
filename = '.git/COMMIT_EDITMSG'
721695
with open(filename, 'w') as f:

0 commit comments

Comments
 (0)