def test_control_c_control_c_on_install(tempdir_factory, store): """Regression test for #186.""" path = make_repo(tempdir_factory, 'python_hooks_repo') config = make_config_from_repo(path) repo = Repository.create(config, store) hook = repo.hooks[0][1] class MyKeyboardInterrupt(KeyboardInterrupt): pass # To simulate a killed install, we'll make PythonEnv.run raise ^C # and then to simulate a second ^C during cleanup, we'll make shutil.rmtree # raise as well. with pytest.raises(MyKeyboardInterrupt): with mock.patch.object( helpers, 'run_setup_cmd', side_effect=MyKeyboardInterrupt, ): with mock.patch.object( shutil, 'rmtree', side_effect=MyKeyboardInterrupt, ): repo.run_hook(hook, []) # Should have made an environment, however this environment is broken! envdir = 'py_env-{}'.format(python.get_default_version()) assert repo._cmd_runner.exists(envdir) # However, it should be perfectly runnable (reinstall after botched # install) retv, stdout, stderr = repo.run_hook(hook, []) assert retv == 0
def test_control_c_control_c_on_install(tempdir_factory, store): """Regression test for #186.""" path = make_repo(tempdir_factory, 'python_hooks_repo') config = make_config_from_repo(path) repo = Repository.create(config, store) hook = repo.hooks[0][1] class MyKeyboardInterrupt(KeyboardInterrupt): pass # To simulate a killed install, we'll make PythonEnv.run raise ^C # and then to simulate a second ^C during cleanup, we'll make shutil.rmtree # raise as well. with pytest.raises(MyKeyboardInterrupt): with mock.patch.object( helpers, 'run_setup_cmd', side_effect=MyKeyboardInterrupt, ): with mock.patch.object( shutil, 'rmtree', side_effect=MyKeyboardInterrupt, ): repo.run_hook(hook, []) # Should have made an environment, however this environment is broken! envdir = 'py_env-{}'.format(python.get_default_version()) assert repo._cmd_runner.exists(envdir) # However, it should be perfectly runnable (reinstall after botched # install) retv, stdout, stderr = repo.run_hook(hook, []) assert retv == 0
def test_additional_dependencies(tempdir_factory, store): path = make_repo(tempdir_factory, 'python_hooks_repo') config = make_config_from_repo(path) config['hooks'][0]['additional_dependencies'] = ['pep8'] repo = Repository.create(config, store) venv, = repo._venvs assert venv == (mock.ANY, 'python', python.get_default_version(), ['pep8'])
def test_additional_dependencies(tempdir_factory, store): path = make_repo(tempdir_factory, 'python_hooks_repo') config = make_config_from_repo(path) config['hooks'][0]['additional_dependencies'] = ['pep8'] repo = Repository.create(config, store) venv, = repo._venvs assert venv == (mock.ANY, 'python', python.get_default_version(), ['pep8'])
def test_invalidated_virtualenv(tempdir_factory, store): # A cached virtualenv may become invalidated if the system python upgrades # This should not cause every hook in that virtualenv to fail. path = make_repo(tempdir_factory, 'python_hooks_repo') config = make_config_from_repo(path) repo = Repository.create(config, store) # Simulate breaking of the virtualenv repo.require_installed() version = python.get_default_version() libdir = repo._cmd_runner.path('py_env-{}'.format(version), 'lib', version) paths = [ os.path.join(libdir, p) for p in ('site.py', 'site.pyc', '__pycache__') ] cmd_output('rm', '-rf', *paths) # pre-commit should rebuild the virtualenv and it should be runnable repo = Repository.create(config, store) hook = repo.hooks[0][1] retv, stdout, stderr = repo.run_hook(hook, []) assert retv == 0
def test_invalidated_virtualenv(tempdir_factory, store): # A cached virtualenv may become invalidated if the system python upgrades # This should not cause every hook in that virtualenv to fail. path = make_repo(tempdir_factory, 'python_hooks_repo') config = make_config_from_repo(path) repo = Repository.create(config, store) # Simulate breaking of the virtualenv repo.require_installed() version = python.get_default_version() libdir = repo._cmd_runner.path('py_env-{}'.format(version), 'lib', version) paths = [ os.path.join(libdir, p) for p in ('site.py', 'site.pyc', '__pycache__') ] cmd_output('rm', '-rf', *paths) # pre-commit should rebuild the virtualenv and it should be runnable repo = Repository.create(config, store) hook = repo.hooks[0][1] retv, stdout, stderr = repo.run_hook(hook, []) assert retv == 0
def test_venvs(tempdir_factory, store): path = make_repo(tempdir_factory, 'python_hooks_repo') config = make_config_from_repo(path) repo = Repository.create(config, store) venv, = repo._venvs assert venv == (mock.ANY, 'python', python.get_default_version(), [])
def get_default_version(): # pragma: no cover (version specific) if sys.version_info < (3,): return 'python3' else: return python.get_default_version()
def test_venvs(tempdir_factory, store): path = make_repo(tempdir_factory, 'python_hooks_repo') config = make_config_from_repo(path) repo = Repository.create(config, store) venv, = repo._venvs assert venv == (mock.ANY, 'python', python.get_default_version(), [])