mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Clean up directories on failure. Closes #58.
This commit is contained in:
parent
443b62d56a
commit
bcb00726a1
8 changed files with 103 additions and 38 deletions
|
|
@ -13,12 +13,17 @@ from testing.util import get_resource_path
|
|||
|
||||
|
||||
@pytest.yield_fixture
|
||||
def empty_git_dir(tmpdir):
|
||||
def in_tmpdir(tmpdir):
|
||||
with local.cwd(tmpdir.strpath):
|
||||
local['git']['init']()
|
||||
yield tmpdir.strpath
|
||||
|
||||
|
||||
@pytest.yield_fixture
|
||||
def empty_git_dir(in_tmpdir):
|
||||
local['git']['init']()
|
||||
yield in_tmpdir
|
||||
|
||||
|
||||
def add_and_commit():
|
||||
local['git']['add', '.']()
|
||||
local['git']['commit', '-m', 'random commit {0}'.format(time.time())]()
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ import os
|
|||
import mock
|
||||
import pytest
|
||||
import subprocess
|
||||
from plumbum import local
|
||||
|
||||
from pre_commit.prefixed_command_runner import _replace_cmd
|
||||
from pre_commit.prefixed_command_runner import CalledProcessError
|
||||
|
|
@ -110,23 +109,20 @@ def test_from_command_runner_preserves_popen(popen_mock, makedirs_mock):
|
|||
makedirs_mock.assert_called_once_with('foo/bar/')
|
||||
|
||||
|
||||
def test_create_path_if_not_exists(tmpdir):
|
||||
with local.cwd(tmpdir.strpath):
|
||||
instance = PrefixedCommandRunner('foo')
|
||||
assert not os.path.exists('foo')
|
||||
instance._create_path_if_not_exists()
|
||||
assert os.path.exists('foo')
|
||||
def test_create_path_if_not_exists(in_tmpdir):
|
||||
instance = PrefixedCommandRunner('foo')
|
||||
assert not os.path.exists('foo')
|
||||
instance._create_path_if_not_exists()
|
||||
assert os.path.exists('foo')
|
||||
|
||||
|
||||
def test_exists_does_not_exist(tmpdir):
|
||||
with local.cwd(tmpdir.strpath):
|
||||
assert not PrefixedCommandRunner('.').exists('foo')
|
||||
def test_exists_does_not_exist(in_tmpdir):
|
||||
assert not PrefixedCommandRunner('.').exists('foo')
|
||||
|
||||
|
||||
def test_exists_does_exist(tmpdir):
|
||||
with local.cwd(tmpdir.strpath):
|
||||
os.mkdir('foo')
|
||||
assert PrefixedCommandRunner('.').exists('foo')
|
||||
def test_exists_does_exist(in_tmpdir):
|
||||
os.mkdir('foo')
|
||||
assert PrefixedCommandRunner('.').exists('foo')
|
||||
|
||||
|
||||
def test_raises_on_error(popen_mock, makedirs_mock):
|
||||
|
|
|
|||
|
|
@ -1,11 +1,14 @@
|
|||
|
||||
import mock
|
||||
import pytest
|
||||
import os
|
||||
import os.path
|
||||
import random
|
||||
import sys
|
||||
from plumbum import local
|
||||
|
||||
from pre_commit.util import cached_property
|
||||
from pre_commit.util import clean_path_on_failure
|
||||
from pre_commit.util import entry
|
||||
from pre_commit.util import memoize_by_cwd
|
||||
|
||||
|
|
@ -84,3 +87,35 @@ def test_no_arguments_passed_uses_argv(entry_func):
|
|||
with mock.patch.object(sys, 'argv', argv):
|
||||
ret = entry_func()
|
||||
assert ret == argv[1:]
|
||||
|
||||
|
||||
def test_clean_on_failure_noop(in_tmpdir):
|
||||
with clean_path_on_failure('foo'): pass
|
||||
|
||||
|
||||
def test_clean_path_on_failure_does_nothing_when_not_raising(in_tmpdir):
|
||||
with clean_path_on_failure('foo'):
|
||||
os.mkdir('foo')
|
||||
assert os.path.exists('foo')
|
||||
|
||||
|
||||
def test_clean_path_on_failure_cleans_for_normal_exception(in_tmpdir):
|
||||
class MyException(Exception): pass
|
||||
|
||||
with pytest.raises(MyException):
|
||||
with clean_path_on_failure('foo'):
|
||||
os.mkdir('foo')
|
||||
raise MyException
|
||||
|
||||
assert not os.path.exists('foo')
|
||||
|
||||
|
||||
def test_clean_path_on_failure_cleans_for_system_exit(in_tmpdir):
|
||||
class MySystemExit(SystemExit): pass
|
||||
|
||||
with pytest.raises(MySystemExit):
|
||||
with clean_path_on_failure('foo'):
|
||||
os.mkdir('foo')
|
||||
raise MySystemExit
|
||||
|
||||
assert not os.path.exists('foo')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue