|
3 | 3 |
|
4 | 4 | import io |
5 | 5 | import os.path |
| 6 | +import shutil |
6 | 7 |
|
7 | 8 | import mock |
8 | 9 | import pytest |
9 | 10 |
|
10 | 11 | from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA |
11 | 12 | from pre_commit.clientlib.validate_config import validate_config_extra |
12 | 13 | from pre_commit.jsonschema_extensions import apply_defaults |
| 14 | +from pre_commit.languages.python import PythonEnv |
13 | 15 | from pre_commit.repository import Repository |
14 | 16 | from pre_commit.util import cmd_output |
15 | 17 | from pre_commit.util import cwd |
@@ -266,6 +268,35 @@ def test_reinstall(tmpdir_factory, store): |
266 | 268 | repo.require_installed() |
267 | 269 |
|
268 | 270 |
|
| 271 | +def test_control_c_control_c_on_install(tmpdir_factory, store): |
| 272 | + """Regression test for #186.""" |
| 273 | + path = make_repo(tmpdir_factory, 'python_hooks_repo') |
| 274 | + config = make_config_from_repo(path) |
| 275 | + repo = Repository.create(config, store) |
| 276 | + hook = repo.hooks[0][1] |
| 277 | + |
| 278 | + class MyKeyboardInterrupt(KeyboardInterrupt): |
| 279 | + pass |
| 280 | + |
| 281 | + # To simulate a killed install, we'll make PythonEnv.run raise ^C |
| 282 | + # and then to simulate a second ^C during cleanup, we'll make shutil.rmtree |
| 283 | + # raise as well. |
| 284 | + with pytest.raises(MyKeyboardInterrupt): |
| 285 | + with mock.patch.object( |
| 286 | + PythonEnv, 'run', side_effect=MyKeyboardInterrupt, |
| 287 | + ): |
| 288 | + with mock.patch.object(shutil, 'rmtree', MyKeyboardInterrupt): |
| 289 | + repo.run_hook(hook, []) |
| 290 | + |
| 291 | + # Should have made an environment, however this environment is broken! |
| 292 | + assert os.path.exists(repo.cmd_runner.path('py_env')) |
| 293 | + |
| 294 | + # However, it should be perfectly runnable (reinstall after botched |
| 295 | + # install) |
| 296 | + retv, stdout, stderr = repo.run_hook(hook, []) |
| 297 | + assert retv == 0 |
| 298 | + |
| 299 | + |
269 | 300 | @pytest.mark.integration |
270 | 301 | def test_really_long_file_paths(tmpdir_factory, store): |
271 | 302 | base_path = tmpdir_factory.get() |
|
0 commit comments