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

Skip to content

Commit 5763813

Browse files
committed
Make a helper for running pre-commit as a subprocess under test
1 parent e245110 commit 5763813

4 files changed

Lines changed: 33 additions & 42 deletions

File tree

testing/util.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ def is_valid_according_to_schema(obj, schema):
4949
return False
5050

5151

52+
def cmd_output_mocked_pre_commit_home(*args, **kwargs):
53+
# keyword-only argument
54+
tempdir_factory = kwargs.pop('tempdir_factory')
55+
pre_commit_home = kwargs.pop('pre_commit_home', tempdir_factory.get())
56+
# Don't want to write to the home directory
57+
env = dict(kwargs.pop('env', os.environ), PRE_COMMIT_HOME=pre_commit_home)
58+
return cmd_output(*args, env=env, **kwargs)
59+
60+
5261
skipif_slowtests_false = pytest.mark.skipif(
5362
os.environ.get('slowtests') == 'false',
5463
reason='slowtests=false',

tests/commands/install_uninstall_test.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from pre_commit.util import resource_filename
2626
from testing.fixtures import git_dir
2727
from testing.fixtures import make_consuming_repo
28+
from testing.util import cmd_output_mocked_pre_commit_home
2829
from testing.util import xfailif_no_symlink
2930

3031

@@ -121,23 +122,16 @@ def test_uninstall(tempdir_factory):
121122
assert not os.path.exists(runner.pre_commit_path)
122123

123124

124-
def _get_commit_output(
125-
tempdir_factory,
126-
touch_file='foo',
127-
home=None,
128-
env_base=os.environ,
129-
):
125+
def _get_commit_output(tempdir_factory, touch_file='foo', **kwargs):
130126
cmd_output('touch', touch_file)
131127
cmd_output('git', 'add', touch_file)
132-
# Don't want to write to home directory
133-
home = home or tempdir_factory.get()
134-
env = dict(env_base, PRE_COMMIT_HOME=home)
135-
return cmd_output(
128+
return cmd_output_mocked_pre_commit_home(
136129
'git', 'commit', '-am', 'Commit!', '--allow-empty',
137130
# git commit puts pre-commit to stderr
138131
stderr=subprocess.STDOUT,
139-
env=env,
140132
retcode=None,
133+
tempdir_factory=tempdir_factory,
134+
**kwargs
141135
)[:2]
142136

143137

@@ -458,7 +452,7 @@ def test_installs_hooks_with_hooks_True(
458452
with cwd(path):
459453
install(Runner(path), hooks=True)
460454
ret, output = _get_commit_output(
461-
tempdir_factory, home=mock_out_store_directory,
455+
tempdir_factory, pre_commit_home=mock_out_store_directory,
462456
)
463457

464458
assert ret == 0
@@ -473,7 +467,7 @@ def test_installed_from_venv(tempdir_factory):
473467
# Should still pick up the python from when we installed
474468
ret, output = _get_commit_output(
475469
tempdir_factory,
476-
env_base={
470+
env={
477471
'HOME': os.path.expanduser('~'),
478472
'PATH': _path_without_us(),
479473
'TERM': os.environ.get('TERM', ''),
@@ -486,14 +480,11 @@ def test_installed_from_venv(tempdir_factory):
486480

487481

488482
def _get_push_output(tempdir_factory):
489-
# Don't want to write to home directory
490-
home = tempdir_factory.get()
491-
env = dict(os.environ, PRE_COMMIT_HOME=home)
492-
return cmd_output(
483+
return cmd_output_mocked_pre_commit_home(
493484
'git', 'push', 'origin', 'HEAD:new_branch',
494-
# git commit puts pre-commit to stderr
485+
# git push puts pre-commit to stderr
495486
stderr=subprocess.STDOUT,
496-
env=env,
487+
tempdir_factory=tempdir_factory,
497488
retcode=None,
498489
)[:2]
499490

tests/commands/run_test.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from testing.fixtures import add_config_to_repo
2727
from testing.fixtures import make_consuming_repo
2828
from testing.fixtures import modify_config
29+
from testing.util import cmd_output_mocked_pre_commit_home
2930

3031

3132
@pytest.yield_fixture
@@ -336,11 +337,9 @@ def test_non_ascii_hook_id(
336337
):
337338
with cwd(repo_with_passing_hook):
338339
install(Runner(repo_with_passing_hook))
339-
# Don't want to write to home directory
340-
env = dict(os.environ, PRE_COMMIT_HOME=tempdir_factory.get())
341-
_, stdout, _ = cmd_output(
340+
_, stdout, _ = cmd_output_mocked_pre_commit_home(
342341
sys.executable, '-m', 'pre_commit.main', 'run', '☃',
343-
env=env, retcode=None,
342+
retcode=None, tempdir_factory=tempdir_factory,
344343
)
345344
assert 'UnicodeDecodeError' not in stdout
346345
# Doesn't actually happen, but a reasonable assertion
@@ -357,15 +356,13 @@ def test_stdout_write_bug_py26(
357356

358357
install(Runner(repo_with_failing_hook))
359358

360-
# Don't want to write to home directory
361-
env = dict(os.environ, PRE_COMMIT_HOME=tempdir_factory.get())
362359
# Have to use subprocess because pytest monkeypatches sys.stdout
363-
_, stdout, _ = cmd_output(
360+
_, stdout, _ = cmd_output_mocked_pre_commit_home(
364361
'git', 'commit', '-m', 'Commit!',
365362
# git commit puts pre-commit to stderr
366363
stderr=subprocess.STDOUT,
367-
env=env,
368364
retcode=None,
365+
tempdir_factory=tempdir_factory,
369366
)
370367
assert 'UnicodeEncodeError' not in stdout
371368
# Doesn't actually happen, but a reasonable assertion
@@ -377,15 +374,13 @@ def test_hook_install_failure(mock_out_store_directory, tempdir_factory):
377374
with cwd(git_path):
378375
install(Runner(git_path))
379376

380-
# Don't want to write to home directory
381-
env = dict(os.environ, PRE_COMMIT_HOME=tempdir_factory.get())
382-
_, stdout, _ = cmd_output(
377+
_, stdout, _ = cmd_output_mocked_pre_commit_home(
383378
'git', 'commit', '-m', 'Commit!',
384379
# git commit puts pre-commit to stderr
385380
stderr=subprocess.STDOUT,
386-
env=env,
387381
retcode=None,
388382
encoding=None,
383+
tempdir_factory=tempdir_factory,
389384
)
390385
assert b'UnicodeDecodeError' not in stdout
391386
# Doesn't actually happen, but a reasonable assertion
@@ -424,13 +419,11 @@ def test_lots_of_files(mock_out_store_directory, tempdir_factory):
424419
cmd_output('bash', '-c', 'git add .')
425420
install(Runner(git_path))
426421

427-
# Don't want to write to home directory
428-
env = dict(os.environ, PRE_COMMIT_HOME=tempdir_factory.get())
429-
cmd_output(
422+
cmd_output_mocked_pre_commit_home(
430423
'git', 'commit', '-m', 'Commit!',
431424
# git commit puts pre-commit to stderr
432425
stderr=subprocess.STDOUT,
433-
env=env,
426+
tempdir_factory=tempdir_factory,
434427
)
435428

436429

@@ -609,13 +602,11 @@ def test_files_running_subdir(
609602
cmd_output('git', 'add', 'subdir/foo.py')
610603

611604
with cwd('subdir'):
612-
# Don't want to write to home directory
613-
env = dict(os.environ, PRE_COMMIT_HOME=tempdir_factory.get())
614605
# Use subprocess to demonstrate behaviour in main
615-
_, stdout, _ = cmd_output(
606+
_, stdout, _ = cmd_output_mocked_pre_commit_home(
616607
sys.executable, '-m', 'pre_commit.main', 'run', '-v',
617608
# Files relative to where we are (#339)
618609
'--files', 'foo.py',
619-
env=env,
610+
tempdir_factory=tempdir_factory,
620611
)
621612
assert 'subdir/foo.py'.replace('/', os.sep) in stdout

tests/error_handler_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pre_commit import error_handler
1414
from pre_commit import five
1515
from pre_commit.errors import FatalError
16-
from pre_commit.util import cmd_output
16+
from testing.util import cmd_output_mocked_pre_commit_home
1717

1818

1919
@pytest.yield_fixture
@@ -107,14 +107,14 @@ def test_error_handler_non_ascii_exception(mock_out_store_directory):
107107

108108

109109
def test_error_handler_no_tty(tempdir_factory):
110-
output = cmd_output(
110+
output = cmd_output_mocked_pre_commit_home(
111111
sys.executable, '-c',
112112
'from __future__ import unicode_literals\n'
113113
'from pre_commit.error_handler import error_handler\n'
114114
'with error_handler():\n'
115115
' raise ValueError("\\u2603")\n',
116-
env=dict(os.environ, PRE_COMMIT_HOME=tempdir_factory.get()),
117116
retcode=1,
117+
tempdir_factory=tempdir_factory,
118118
)
119119
assert output[1].replace('\r', '') == (
120120
'An unexpected error has occurred: ValueError: ☃\n'

0 commit comments

Comments
 (0)