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

Skip to content

Commit 7afb294

Browse files
authored
Merge pull request #896 from pre-commit/cleanup
misc cleanup
2 parents 8c550d0 + d46bbc4 commit 7afb294

11 files changed

Lines changed: 99 additions & 158 deletions

File tree

pre_commit/file_lock.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ def _locked(fileno, blocked_cb):
4141
# "Regions should be locked only briefly and should be unlocked
4242
# before closing a file or exiting the program."
4343
msvcrt.locking(fileno, msvcrt.LK_UNLCK, _region)
44-
except ImportError: # pragma: no cover (posix)
44+
except ImportError: # pragma: windows no cover
4545
import fcntl
4646

4747
@contextlib.contextmanager
4848
def _locked(fileno, blocked_cb):
4949
try:
5050
fcntl.flock(fileno, fcntl.LOCK_EX | fcntl.LOCK_NB)
51-
except IOError:
51+
except IOError: # pragma: no cover (tests are single-threaded)
5252
blocked_cb()
5353
fcntl.flock(fileno, fcntl.LOCK_EX)
5454
try:

pre_commit/languages/node.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def get_env_patch(venv):
2828
install_prefix = r'{}\bin'.format(win_venv.strip())
2929
elif sys.platform == 'win32': # pragma: no cover
3030
install_prefix = bin_dir(venv)
31-
else:
31+
else: # pragma: windows no cover
3232
install_prefix = venv
3333
return (
3434
('NODE_VIRTUAL_ENV', venv),

tests/commands/install_uninstall_test.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_install_refuses_core_hookspath(in_git_dir, store):
8484
assert install(C.CONFIG_FILE, store)
8585

8686

87-
@xfailif_no_symlink # pragma: no cover (non-windows)
87+
@xfailif_no_symlink # pragma: windows no cover
8888
def test_install_hooks_dead_symlink(in_git_dir, store):
8989
hook = in_git_dir.join('.git/hooks').ensure_dir().join('pre-commit')
9090
os.symlink('/fake/baz', hook.strpath)
@@ -421,17 +421,14 @@ def test_replace_old_commit_script(tempdir_factory, store):
421421
assert NORMAL_PRE_COMMIT_RUN.match(output)
422422

423423

424-
def test_uninstall_doesnt_remove_not_our_hooks(tempdir_factory):
425-
path = git_dir(tempdir_factory)
426-
with cwd(path):
427-
mkdirp(os.path.join(path, '.git/hooks'))
428-
with io.open(os.path.join(path, '.git/hooks/pre-commit'), 'w') as f:
429-
f.write('#!/usr/bin/env bash\necho 1\n')
430-
make_executable(f.name)
424+
def test_uninstall_doesnt_remove_not_our_hooks(in_git_dir):
425+
pre_commit = in_git_dir.join('.git/hooks').ensure_dir().join('pre-commit')
426+
pre_commit.write('#!/usr/bin/env bash\necho 1\n')
427+
make_executable(pre_commit.strpath)
431428

432-
assert uninstall() == 0
429+
assert uninstall() == 0
433430

434-
assert os.path.exists(os.path.join(path, '.git/hooks/pre-commit'))
431+
assert pre_commit.exists()
435432

436433

437434
PRE_INSTALLED = re.compile(

tests/commands/run_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,8 +781,8 @@ def test_include_exclude_base_case(some_filenames):
781781
]
782782

783783

784-
@xfailif_no_symlink
785-
def test_matches_broken_symlink(tmpdir): # pragma: no cover (non-windows)
784+
@xfailif_no_symlink # pragma: windows no cover
785+
def test_matches_broken_symlink(tmpdir):
786786
with tmpdir.as_cwd():
787787
os.symlink('does-not-exist', 'link')
788788
ret = _filter_by_include_exclude({'link'}, '', '^$')

tests/conftest.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ def in_tmpdir(tempdir_factory):
6565

6666
@pytest.fixture
6767
def in_git_dir(tmpdir):
68-
with tmpdir.as_cwd():
68+
repo = tmpdir.join('repo').ensure_dir()
69+
with repo.as_cwd():
6970
cmd_output('git', 'init')
70-
yield tmpdir
71+
yield repo
7172

7273

7374
def _make_conflict():

tests/git_test.py

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,34 @@
99
from pre_commit import git
1010
from pre_commit.error_handler import FatalError
1111
from pre_commit.util import cmd_output
12-
from testing.fixtures import git_dir
13-
from testing.util import cwd
1412

1513

16-
def test_get_root_at_root(tempdir_factory):
17-
path = git_dir(tempdir_factory)
18-
with cwd(path):
19-
assert os.path.normcase(git.get_root()) == os.path.normcase(path)
14+
def test_get_root_at_root(in_git_dir):
15+
expected = os.path.normcase(in_git_dir.strpath)
16+
assert os.path.normcase(git.get_root()) == expected
2017

2118

22-
def test_get_root_deeper(tempdir_factory):
23-
path = git_dir(tempdir_factory)
19+
def test_get_root_deeper(in_git_dir):
20+
expected = os.path.normcase(in_git_dir.strpath)
21+
with in_git_dir.join('foo').ensure_dir().as_cwd():
22+
assert os.path.normcase(git.get_root()) == expected
2423

25-
foo_path = os.path.join(path, 'foo')
26-
os.mkdir(foo_path)
27-
with cwd(foo_path):
28-
assert os.path.normcase(git.get_root()) == os.path.normcase(path)
2924

25+
def test_get_root_not_git_dir(in_tmpdir):
26+
with pytest.raises(FatalError):
27+
git.get_root()
3028

31-
def test_get_root_not_git_dir(tempdir_factory):
32-
with cwd(tempdir_factory.get()):
33-
with pytest.raises(FatalError):
34-
git.get_root()
3529

30+
def test_get_staged_files_deleted(in_git_dir):
31+
in_git_dir.join('test').ensure()
32+
cmd_output('git', 'add', 'test')
33+
cmd_output('git', 'commit', '-m', 'foo', '--allow-empty')
34+
cmd_output('git', 'rm', '--cached', 'test')
35+
assert git.get_staged_files() == []
3636

37-
def test_get_staged_files_deleted(tempdir_factory):
38-
path = git_dir(tempdir_factory)
39-
with cwd(path):
40-
open('test', 'a').close()
41-
cmd_output('git', 'add', 'test')
42-
cmd_output('git', 'commit', '-m', 'foo', '--allow-empty')
43-
cmd_output('git', 'rm', '--cached', 'test')
44-
assert git.get_staged_files() == []
4537

46-
47-
def test_is_not_in_merge_conflict(tempdir_factory):
48-
path = git_dir(tempdir_factory)
49-
with cwd(path):
50-
assert git.is_in_merge_conflict() is False
38+
def test_is_not_in_merge_conflict(in_git_dir):
39+
assert git.is_in_merge_conflict() is False
5140

5241

5342
def test_is_in_merge_conflict(in_merge_conflict):
@@ -114,11 +103,10 @@ def test_parse_merge_msg_for_conflicts(input, expected_output):
114103
assert ret == expected_output
115104

116105

117-
def test_get_changed_files(in_tmpdir):
118-
cmd_output('git', 'init', '.')
106+
def test_get_changed_files(in_git_dir):
119107
cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit')
120-
open('a.txt', 'a').close()
121-
open('b.txt', 'a').close()
108+
in_git_dir.join('a.txt').ensure()
109+
in_git_dir.join('b.txt').ensure()
122110
cmd_output('git', 'add', '.')
123111
cmd_output('git', 'commit', '-m', 'add some files')
124112
files = git.get_changed_files('HEAD', 'HEAD^')
@@ -143,15 +131,12 @@ def test_zsplit(s, expected):
143131

144132

145133
@pytest.fixture
146-
def non_ascii_repo(tmpdir):
147-
repo = tmpdir.join('repo').ensure_dir()
148-
with repo.as_cwd():
149-
cmd_output('git', 'init', '.')
150-
cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit')
151-
repo.join('интервью').ensure()
152-
cmd_output('git', 'add', '.')
153-
cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit')
154-
yield repo
134+
def non_ascii_repo(in_git_dir):
135+
cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit')
136+
in_git_dir.join('интервью').ensure()
137+
cmd_output('git', 'add', '.')
138+
cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit')
139+
yield in_git_dir
155140

156141

157142
def test_all_files_non_ascii(non_ascii_repo):

tests/languages/python_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def test_norm_version_expanduser():
1111
if os.name == 'nt': # pragma: no cover (nt)
1212
path = r'~\python343'
1313
expected_path = r'{}\python343'.format(home)
14-
else: # pragma: no cover (non-nt)
14+
else: # pragma: windows no cover
1515
path = '~/.pyenv/versions/3.4.3/bin/python'
1616
expected_path = home + '/.pyenv/versions/3.4.3/bin/python'
1717
result = python.norm_version(path)

tests/meta_hooks/check_hooks_apply_test.py

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
from pre_commit.meta_hooks import check_hooks_apply
22
from testing.fixtures import add_config_to_repo
3-
from testing.fixtures import git_dir
4-
from testing.util import cwd
53

64

7-
def test_hook_excludes_everything(capsys, tempdir_factory, mock_store_dir):
5+
def test_hook_excludes_everything(capsys, in_git_dir, mock_store_dir):
86
config = {
97
'repos': [
108
{
@@ -19,17 +17,15 @@ def test_hook_excludes_everything(capsys, tempdir_factory, mock_store_dir):
1917
],
2018
}
2119

22-
repo = git_dir(tempdir_factory)
23-
add_config_to_repo(repo, config)
20+
add_config_to_repo(in_git_dir.strpath, config)
2421

25-
with cwd(repo):
26-
assert check_hooks_apply.main(()) == 1
22+
assert check_hooks_apply.main(()) == 1
2723

2824
out, _ = capsys.readouterr()
2925
assert 'check-useless-excludes does not apply to this repository' in out
3026

3127

32-
def test_hook_includes_nothing(capsys, tempdir_factory, mock_store_dir):
28+
def test_hook_includes_nothing(capsys, in_git_dir, mock_store_dir):
3329
config = {
3430
'repos': [
3531
{
@@ -44,17 +40,15 @@ def test_hook_includes_nothing(capsys, tempdir_factory, mock_store_dir):
4440
],
4541
}
4642

47-
repo = git_dir(tempdir_factory)
48-
add_config_to_repo(repo, config)
43+
add_config_to_repo(in_git_dir.strpath, config)
4944

50-
with cwd(repo):
51-
assert check_hooks_apply.main(()) == 1
45+
assert check_hooks_apply.main(()) == 1
5246

5347
out, _ = capsys.readouterr()
5448
assert 'check-useless-excludes does not apply to this repository' in out
5549

5650

57-
def test_hook_types_not_matched(capsys, tempdir_factory, mock_store_dir):
51+
def test_hook_types_not_matched(capsys, in_git_dir, mock_store_dir):
5852
config = {
5953
'repos': [
6054
{
@@ -69,19 +63,15 @@ def test_hook_types_not_matched(capsys, tempdir_factory, mock_store_dir):
6963
],
7064
}
7165

72-
repo = git_dir(tempdir_factory)
73-
add_config_to_repo(repo, config)
66+
add_config_to_repo(in_git_dir.strpath, config)
7467

75-
with cwd(repo):
76-
assert check_hooks_apply.main(()) == 1
68+
assert check_hooks_apply.main(()) == 1
7769

7870
out, _ = capsys.readouterr()
7971
assert 'check-useless-excludes does not apply to this repository' in out
8072

8173

82-
def test_hook_types_excludes_everything(
83-
capsys, tempdir_factory, mock_store_dir,
84-
):
74+
def test_hook_types_excludes_everything(capsys, in_git_dir, mock_store_dir):
8575
config = {
8676
'repos': [
8777
{
@@ -96,17 +86,15 @@ def test_hook_types_excludes_everything(
9686
],
9787
}
9888

99-
repo = git_dir(tempdir_factory)
100-
add_config_to_repo(repo, config)
89+
add_config_to_repo(in_git_dir.strpath, config)
10190

102-
with cwd(repo):
103-
assert check_hooks_apply.main(()) == 1
91+
assert check_hooks_apply.main(()) == 1
10492

10593
out, _ = capsys.readouterr()
10694
assert 'check-useless-excludes does not apply to this repository' in out
10795

10896

109-
def test_valid_exceptions(capsys, tempdir_factory, mock_store_dir):
97+
def test_valid_exceptions(capsys, in_git_dir, mock_store_dir):
11098
config = {
11199
'repos': [
112100
{
@@ -142,11 +130,9 @@ def test_valid_exceptions(capsys, tempdir_factory, mock_store_dir):
142130
],
143131
}
144132

145-
repo = git_dir(tempdir_factory)
146-
add_config_to_repo(repo, config)
133+
add_config_to_repo(in_git_dir.strpath, config)
147134

148-
with cwd(repo):
149-
assert check_hooks_apply.main(()) == 0
135+
assert check_hooks_apply.main(()) == 0
150136

151137
out, _ = capsys.readouterr()
152138
assert out == ''

0 commit comments

Comments
 (0)