Merge pull request #343 from pre-commit/testing_helpers

Make a helper for running pre-commit as a subprocess under test
This commit is contained in:
Anthony Sottile
2016-02-07 13:54:38 -08:00
4 changed files with 33 additions and 42 deletions

View File

@@ -49,6 +49,15 @@ def is_valid_according_to_schema(obj, schema):
return False
def cmd_output_mocked_pre_commit_home(*args, **kwargs):
# keyword-only argument
tempdir_factory = kwargs.pop('tempdir_factory')
pre_commit_home = kwargs.pop('pre_commit_home', tempdir_factory.get())
# Don't want to write to the home directory
env = dict(kwargs.pop('env', os.environ), PRE_COMMIT_HOME=pre_commit_home)
return cmd_output(*args, env=env, **kwargs)
skipif_slowtests_false = pytest.mark.skipif(
os.environ.get('slowtests') == 'false',
reason='slowtests=false',

View File

@@ -25,6 +25,7 @@ from pre_commit.util import mkdirp
from pre_commit.util import resource_filename
from testing.fixtures import git_dir
from testing.fixtures import make_consuming_repo
from testing.util import cmd_output_mocked_pre_commit_home
from testing.util import xfailif_no_symlink
@@ -121,23 +122,16 @@ def test_uninstall(tempdir_factory):
assert not os.path.exists(runner.pre_commit_path)
def _get_commit_output(
tempdir_factory,
touch_file='foo',
home=None,
env_base=os.environ,
):
def _get_commit_output(tempdir_factory, touch_file='foo', **kwargs):
cmd_output('touch', touch_file)
cmd_output('git', 'add', touch_file)
# Don't want to write to home directory
home = home or tempdir_factory.get()
env = dict(env_base, PRE_COMMIT_HOME=home)
return cmd_output(
return cmd_output_mocked_pre_commit_home(
'git', 'commit', '-am', 'Commit!', '--allow-empty',
# git commit puts pre-commit to stderr
stderr=subprocess.STDOUT,
env=env,
retcode=None,
tempdir_factory=tempdir_factory,
**kwargs
)[:2]
@@ -458,7 +452,7 @@ def test_installs_hooks_with_hooks_True(
with cwd(path):
install(Runner(path), hooks=True)
ret, output = _get_commit_output(
tempdir_factory, home=mock_out_store_directory,
tempdir_factory, pre_commit_home=mock_out_store_directory,
)
assert ret == 0
@@ -473,7 +467,7 @@ def test_installed_from_venv(tempdir_factory):
# Should still pick up the python from when we installed
ret, output = _get_commit_output(
tempdir_factory,
env_base={
env={
'HOME': os.path.expanduser('~'),
'PATH': _path_without_us(),
'TERM': os.environ.get('TERM', ''),
@@ -486,14 +480,11 @@ def test_installed_from_venv(tempdir_factory):
def _get_push_output(tempdir_factory):
# Don't want to write to home directory
home = tempdir_factory.get()
env = dict(os.environ, PRE_COMMIT_HOME=home)
return cmd_output(
return cmd_output_mocked_pre_commit_home(
'git', 'push', 'origin', 'HEAD:new_branch',
# git commit puts pre-commit to stderr
# git push puts pre-commit to stderr
stderr=subprocess.STDOUT,
env=env,
tempdir_factory=tempdir_factory,
retcode=None,
)[:2]

View File

@@ -26,6 +26,7 @@ from testing.auto_namedtuple import auto_namedtuple
from testing.fixtures import add_config_to_repo
from testing.fixtures import make_consuming_repo
from testing.fixtures import modify_config
from testing.util import cmd_output_mocked_pre_commit_home
@pytest.yield_fixture
@@ -336,11 +337,9 @@ def test_non_ascii_hook_id(
):
with cwd(repo_with_passing_hook):
install(Runner(repo_with_passing_hook))
# Don't want to write to home directory
env = dict(os.environ, PRE_COMMIT_HOME=tempdir_factory.get())
_, stdout, _ = cmd_output(
_, stdout, _ = cmd_output_mocked_pre_commit_home(
sys.executable, '-m', 'pre_commit.main', 'run', '',
env=env, retcode=None,
retcode=None, tempdir_factory=tempdir_factory,
)
assert 'UnicodeDecodeError' not in stdout
# Doesn't actually happen, but a reasonable assertion
@@ -357,15 +356,13 @@ def test_stdout_write_bug_py26(
install(Runner(repo_with_failing_hook))
# Don't want to write to home directory
env = dict(os.environ, PRE_COMMIT_HOME=tempdir_factory.get())
# Have to use subprocess because pytest monkeypatches sys.stdout
_, stdout, _ = cmd_output(
_, stdout, _ = cmd_output_mocked_pre_commit_home(
'git', 'commit', '-m', 'Commit!',
# git commit puts pre-commit to stderr
stderr=subprocess.STDOUT,
env=env,
retcode=None,
tempdir_factory=tempdir_factory,
)
assert 'UnicodeEncodeError' not in stdout
# Doesn't actually happen, but a reasonable assertion
@@ -377,15 +374,13 @@ def test_hook_install_failure(mock_out_store_directory, tempdir_factory):
with cwd(git_path):
install(Runner(git_path))
# Don't want to write to home directory
env = dict(os.environ, PRE_COMMIT_HOME=tempdir_factory.get())
_, stdout, _ = cmd_output(
_, stdout, _ = cmd_output_mocked_pre_commit_home(
'git', 'commit', '-m', 'Commit!',
# git commit puts pre-commit to stderr
stderr=subprocess.STDOUT,
env=env,
retcode=None,
encoding=None,
tempdir_factory=tempdir_factory,
)
assert b'UnicodeDecodeError' not in stdout
# Doesn't actually happen, but a reasonable assertion
@@ -424,13 +419,11 @@ def test_lots_of_files(mock_out_store_directory, tempdir_factory):
cmd_output('bash', '-c', 'git add .')
install(Runner(git_path))
# Don't want to write to home directory
env = dict(os.environ, PRE_COMMIT_HOME=tempdir_factory.get())
cmd_output(
cmd_output_mocked_pre_commit_home(
'git', 'commit', '-m', 'Commit!',
# git commit puts pre-commit to stderr
stderr=subprocess.STDOUT,
env=env,
tempdir_factory=tempdir_factory,
)
@@ -609,13 +602,11 @@ def test_files_running_subdir(
cmd_output('git', 'add', 'subdir/foo.py')
with cwd('subdir'):
# Don't want to write to home directory
env = dict(os.environ, PRE_COMMIT_HOME=tempdir_factory.get())
# Use subprocess to demonstrate behaviour in main
_, stdout, _ = cmd_output(
_, stdout, _ = cmd_output_mocked_pre_commit_home(
sys.executable, '-m', 'pre_commit.main', 'run', '-v',
# Files relative to where we are (#339)
'--files', 'foo.py',
env=env,
tempdir_factory=tempdir_factory,
)
assert 'subdir/foo.py'.replace('/', os.sep) in stdout

View File

@@ -13,7 +13,7 @@ import pytest
from pre_commit import error_handler
from pre_commit import five
from pre_commit.errors import FatalError
from pre_commit.util import cmd_output
from testing.util import cmd_output_mocked_pre_commit_home
@pytest.yield_fixture
@@ -107,14 +107,14 @@ def test_error_handler_non_ascii_exception(mock_out_store_directory):
def test_error_handler_no_tty(tempdir_factory):
output = cmd_output(
output = cmd_output_mocked_pre_commit_home(
sys.executable, '-c',
'from __future__ import unicode_literals\n'
'from pre_commit.error_handler import error_handler\n'
'with error_handler():\n'
' raise ValueError("\\u2603")\n',
env=dict(os.environ, PRE_COMMIT_HOME=tempdir_factory.get()),
retcode=1,
tempdir_factory=tempdir_factory,
)
assert output[1].replace('\r', '') == (
'An unexpected error has occurred: ValueError: ☃\n'