Merge pull request #896 from pre-commit/cleanup

misc cleanup
This commit is contained in:
Anthony Sottile 2018-12-27 19:26:16 -08:00 committed by GitHub
commit 7afb2944b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 99 additions and 158 deletions

View file

@ -41,14 +41,14 @@ try: # pragma: no cover (windows)
# "Regions should be locked only briefly and should be unlocked # "Regions should be locked only briefly and should be unlocked
# before closing a file or exiting the program." # before closing a file or exiting the program."
msvcrt.locking(fileno, msvcrt.LK_UNLCK, _region) msvcrt.locking(fileno, msvcrt.LK_UNLCK, _region)
except ImportError: # pragma: no cover (posix) except ImportError: # pragma: windows no cover
import fcntl import fcntl
@contextlib.contextmanager @contextlib.contextmanager
def _locked(fileno, blocked_cb): def _locked(fileno, blocked_cb):
try: try:
fcntl.flock(fileno, fcntl.LOCK_EX | fcntl.LOCK_NB) fcntl.flock(fileno, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError: except IOError: # pragma: no cover (tests are single-threaded)
blocked_cb() blocked_cb()
fcntl.flock(fileno, fcntl.LOCK_EX) fcntl.flock(fileno, fcntl.LOCK_EX)
try: try:

View file

@ -28,7 +28,7 @@ def get_env_patch(venv):
install_prefix = r'{}\bin'.format(win_venv.strip()) install_prefix = r'{}\bin'.format(win_venv.strip())
elif sys.platform == 'win32': # pragma: no cover elif sys.platform == 'win32': # pragma: no cover
install_prefix = bin_dir(venv) install_prefix = bin_dir(venv)
else: else: # pragma: windows no cover
install_prefix = venv install_prefix = venv
return ( return (
('NODE_VIRTUAL_ENV', venv), ('NODE_VIRTUAL_ENV', venv),

View file

@ -84,7 +84,7 @@ def test_install_refuses_core_hookspath(in_git_dir, store):
assert install(C.CONFIG_FILE, store) assert install(C.CONFIG_FILE, store)
@xfailif_no_symlink # pragma: no cover (non-windows) @xfailif_no_symlink # pragma: windows no cover
def test_install_hooks_dead_symlink(in_git_dir, store): def test_install_hooks_dead_symlink(in_git_dir, store):
hook = in_git_dir.join('.git/hooks').ensure_dir().join('pre-commit') hook = in_git_dir.join('.git/hooks').ensure_dir().join('pre-commit')
os.symlink('/fake/baz', hook.strpath) os.symlink('/fake/baz', hook.strpath)
@ -421,17 +421,14 @@ def test_replace_old_commit_script(tempdir_factory, store):
assert NORMAL_PRE_COMMIT_RUN.match(output) assert NORMAL_PRE_COMMIT_RUN.match(output)
def test_uninstall_doesnt_remove_not_our_hooks(tempdir_factory): def test_uninstall_doesnt_remove_not_our_hooks(in_git_dir):
path = git_dir(tempdir_factory) pre_commit = in_git_dir.join('.git/hooks').ensure_dir().join('pre-commit')
with cwd(path): pre_commit.write('#!/usr/bin/env bash\necho 1\n')
mkdirp(os.path.join(path, '.git/hooks')) make_executable(pre_commit.strpath)
with io.open(os.path.join(path, '.git/hooks/pre-commit'), 'w') as f:
f.write('#!/usr/bin/env bash\necho 1\n')
make_executable(f.name)
assert uninstall() == 0 assert uninstall() == 0
assert os.path.exists(os.path.join(path, '.git/hooks/pre-commit')) assert pre_commit.exists()
PRE_INSTALLED = re.compile( PRE_INSTALLED = re.compile(

View file

@ -781,8 +781,8 @@ def test_include_exclude_base_case(some_filenames):
] ]
@xfailif_no_symlink @xfailif_no_symlink # pragma: windows no cover
def test_matches_broken_symlink(tmpdir): # pragma: no cover (non-windows) def test_matches_broken_symlink(tmpdir):
with tmpdir.as_cwd(): with tmpdir.as_cwd():
os.symlink('does-not-exist', 'link') os.symlink('does-not-exist', 'link')
ret = _filter_by_include_exclude({'link'}, '', '^$') ret = _filter_by_include_exclude({'link'}, '', '^$')

View file

@ -65,9 +65,10 @@ def in_tmpdir(tempdir_factory):
@pytest.fixture @pytest.fixture
def in_git_dir(tmpdir): def in_git_dir(tmpdir):
with tmpdir.as_cwd(): repo = tmpdir.join('repo').ensure_dir()
with repo.as_cwd():
cmd_output('git', 'init') cmd_output('git', 'init')
yield tmpdir yield repo
def _make_conflict(): def _make_conflict():

View file

@ -9,45 +9,34 @@ import pytest
from pre_commit import git from pre_commit import git
from pre_commit.error_handler import FatalError from pre_commit.error_handler import FatalError
from pre_commit.util import cmd_output from pre_commit.util import cmd_output
from testing.fixtures import git_dir
from testing.util import cwd
def test_get_root_at_root(tempdir_factory): def test_get_root_at_root(in_git_dir):
path = git_dir(tempdir_factory) expected = os.path.normcase(in_git_dir.strpath)
with cwd(path): assert os.path.normcase(git.get_root()) == expected
assert os.path.normcase(git.get_root()) == os.path.normcase(path)
def test_get_root_deeper(tempdir_factory): def test_get_root_deeper(in_git_dir):
path = git_dir(tempdir_factory) expected = os.path.normcase(in_git_dir.strpath)
with in_git_dir.join('foo').ensure_dir().as_cwd():
foo_path = os.path.join(path, 'foo') assert os.path.normcase(git.get_root()) == expected
os.mkdir(foo_path)
with cwd(foo_path):
assert os.path.normcase(git.get_root()) == os.path.normcase(path)
def test_get_root_not_git_dir(tempdir_factory): def test_get_root_not_git_dir(in_tmpdir):
with cwd(tempdir_factory.get()): with pytest.raises(FatalError):
with pytest.raises(FatalError): git.get_root()
git.get_root()
def test_get_staged_files_deleted(tempdir_factory): def test_get_staged_files_deleted(in_git_dir):
path = git_dir(tempdir_factory) in_git_dir.join('test').ensure()
with cwd(path): cmd_output('git', 'add', 'test')
open('test', 'a').close() cmd_output('git', 'commit', '-m', 'foo', '--allow-empty')
cmd_output('git', 'add', 'test') cmd_output('git', 'rm', '--cached', 'test')
cmd_output('git', 'commit', '-m', 'foo', '--allow-empty') assert git.get_staged_files() == []
cmd_output('git', 'rm', '--cached', 'test')
assert git.get_staged_files() == []
def test_is_not_in_merge_conflict(tempdir_factory): def test_is_not_in_merge_conflict(in_git_dir):
path = git_dir(tempdir_factory) assert git.is_in_merge_conflict() is False
with cwd(path):
assert git.is_in_merge_conflict() is False
def test_is_in_merge_conflict(in_merge_conflict): def test_is_in_merge_conflict(in_merge_conflict):
@ -114,11 +103,10 @@ def test_parse_merge_msg_for_conflicts(input, expected_output):
assert ret == expected_output assert ret == expected_output
def test_get_changed_files(in_tmpdir): def test_get_changed_files(in_git_dir):
cmd_output('git', 'init', '.')
cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit') cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit')
open('a.txt', 'a').close() in_git_dir.join('a.txt').ensure()
open('b.txt', 'a').close() in_git_dir.join('b.txt').ensure()
cmd_output('git', 'add', '.') cmd_output('git', 'add', '.')
cmd_output('git', 'commit', '-m', 'add some files') cmd_output('git', 'commit', '-m', 'add some files')
files = git.get_changed_files('HEAD', 'HEAD^') files = git.get_changed_files('HEAD', 'HEAD^')
@ -143,15 +131,12 @@ def test_zsplit(s, expected):
@pytest.fixture @pytest.fixture
def non_ascii_repo(tmpdir): def non_ascii_repo(in_git_dir):
repo = tmpdir.join('repo').ensure_dir() cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit')
with repo.as_cwd(): in_git_dir.join('интервью').ensure()
cmd_output('git', 'init', '.') cmd_output('git', 'add', '.')
cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit') cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit')
repo.join('интервью').ensure() yield in_git_dir
cmd_output('git', 'add', '.')
cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit')
yield repo
def test_all_files_non_ascii(non_ascii_repo): def test_all_files_non_ascii(non_ascii_repo):

View file

@ -11,7 +11,7 @@ def test_norm_version_expanduser():
if os.name == 'nt': # pragma: no cover (nt) if os.name == 'nt': # pragma: no cover (nt)
path = r'~\python343' path = r'~\python343'
expected_path = r'{}\python343'.format(home) expected_path = r'{}\python343'.format(home)
else: # pragma: no cover (non-nt) else: # pragma: windows no cover
path = '~/.pyenv/versions/3.4.3/bin/python' path = '~/.pyenv/versions/3.4.3/bin/python'
expected_path = home + '/.pyenv/versions/3.4.3/bin/python' expected_path = home + '/.pyenv/versions/3.4.3/bin/python'
result = python.norm_version(path) result = python.norm_version(path)

View file

@ -1,10 +1,8 @@
from pre_commit.meta_hooks import check_hooks_apply from pre_commit.meta_hooks import check_hooks_apply
from testing.fixtures import add_config_to_repo from testing.fixtures import add_config_to_repo
from testing.fixtures import git_dir
from testing.util import cwd
def test_hook_excludes_everything(capsys, tempdir_factory, mock_store_dir): def test_hook_excludes_everything(capsys, in_git_dir, mock_store_dir):
config = { config = {
'repos': [ 'repos': [
{ {
@ -19,17 +17,15 @@ def test_hook_excludes_everything(capsys, tempdir_factory, mock_store_dir):
], ],
} }
repo = git_dir(tempdir_factory) add_config_to_repo(in_git_dir.strpath, config)
add_config_to_repo(repo, config)
with cwd(repo): assert check_hooks_apply.main(()) == 1
assert check_hooks_apply.main(()) == 1
out, _ = capsys.readouterr() out, _ = capsys.readouterr()
assert 'check-useless-excludes does not apply to this repository' in out assert 'check-useless-excludes does not apply to this repository' in out
def test_hook_includes_nothing(capsys, tempdir_factory, mock_store_dir): def test_hook_includes_nothing(capsys, in_git_dir, mock_store_dir):
config = { config = {
'repos': [ 'repos': [
{ {
@ -44,17 +40,15 @@ def test_hook_includes_nothing(capsys, tempdir_factory, mock_store_dir):
], ],
} }
repo = git_dir(tempdir_factory) add_config_to_repo(in_git_dir.strpath, config)
add_config_to_repo(repo, config)
with cwd(repo): assert check_hooks_apply.main(()) == 1
assert check_hooks_apply.main(()) == 1
out, _ = capsys.readouterr() out, _ = capsys.readouterr()
assert 'check-useless-excludes does not apply to this repository' in out assert 'check-useless-excludes does not apply to this repository' in out
def test_hook_types_not_matched(capsys, tempdir_factory, mock_store_dir): def test_hook_types_not_matched(capsys, in_git_dir, mock_store_dir):
config = { config = {
'repos': [ 'repos': [
{ {
@ -69,19 +63,15 @@ def test_hook_types_not_matched(capsys, tempdir_factory, mock_store_dir):
], ],
} }
repo = git_dir(tempdir_factory) add_config_to_repo(in_git_dir.strpath, config)
add_config_to_repo(repo, config)
with cwd(repo): assert check_hooks_apply.main(()) == 1
assert check_hooks_apply.main(()) == 1
out, _ = capsys.readouterr() out, _ = capsys.readouterr()
assert 'check-useless-excludes does not apply to this repository' in out assert 'check-useless-excludes does not apply to this repository' in out
def test_hook_types_excludes_everything( def test_hook_types_excludes_everything(capsys, in_git_dir, mock_store_dir):
capsys, tempdir_factory, mock_store_dir,
):
config = { config = {
'repos': [ 'repos': [
{ {
@ -96,17 +86,15 @@ def test_hook_types_excludes_everything(
], ],
} }
repo = git_dir(tempdir_factory) add_config_to_repo(in_git_dir.strpath, config)
add_config_to_repo(repo, config)
with cwd(repo): assert check_hooks_apply.main(()) == 1
assert check_hooks_apply.main(()) == 1
out, _ = capsys.readouterr() out, _ = capsys.readouterr()
assert 'check-useless-excludes does not apply to this repository' in out assert 'check-useless-excludes does not apply to this repository' in out
def test_valid_exceptions(capsys, tempdir_factory, mock_store_dir): def test_valid_exceptions(capsys, in_git_dir, mock_store_dir):
config = { config = {
'repos': [ 'repos': [
{ {
@ -142,11 +130,9 @@ def test_valid_exceptions(capsys, tempdir_factory, mock_store_dir):
], ],
} }
repo = git_dir(tempdir_factory) add_config_to_repo(in_git_dir.strpath, config)
add_config_to_repo(repo, config)
with cwd(repo): assert check_hooks_apply.main(()) == 0
assert check_hooks_apply.main(()) == 0
out, _ = capsys.readouterr() out, _ = capsys.readouterr()
assert out == '' assert out == ''

View file

@ -1,10 +1,8 @@
from pre_commit.meta_hooks import check_useless_excludes from pre_commit.meta_hooks import check_useless_excludes
from testing.fixtures import add_config_to_repo from testing.fixtures import add_config_to_repo
from testing.fixtures import git_dir
from testing.util import cwd
def test_useless_exclude_global(capsys, tempdir_factory): def test_useless_exclude_global(capsys, in_git_dir):
config = { config = {
'exclude': 'foo', 'exclude': 'foo',
'repos': [ 'repos': [
@ -15,18 +13,16 @@ def test_useless_exclude_global(capsys, tempdir_factory):
], ],
} }
repo = git_dir(tempdir_factory) add_config_to_repo(in_git_dir.strpath, config)
add_config_to_repo(repo, config)
with cwd(repo): assert check_useless_excludes.main(()) == 1
assert check_useless_excludes.main(()) == 1
out, _ = capsys.readouterr() out, _ = capsys.readouterr()
out = out.strip() out = out.strip()
assert "The global exclude pattern 'foo' does not match any files" == out assert "The global exclude pattern 'foo' does not match any files" == out
def test_useless_exclude_for_hook(capsys, tempdir_factory): def test_useless_exclude_for_hook(capsys, in_git_dir):
config = { config = {
'repos': [ 'repos': [
{ {
@ -36,11 +32,9 @@ def test_useless_exclude_for_hook(capsys, tempdir_factory):
], ],
} }
repo = git_dir(tempdir_factory) add_config_to_repo(in_git_dir.strpath, config)
add_config_to_repo(repo, config)
with cwd(repo): assert check_useless_excludes.main(()) == 1
assert check_useless_excludes.main(()) == 1
out, _ = capsys.readouterr() out, _ = capsys.readouterr()
out = out.strip() out = out.strip()
@ -51,7 +45,7 @@ def test_useless_exclude_for_hook(capsys, tempdir_factory):
assert expected == out assert expected == out
def test_useless_exclude_with_types_filter(capsys, tempdir_factory): def test_useless_exclude_with_types_filter(capsys, in_git_dir):
config = { config = {
'repos': [ 'repos': [
{ {
@ -67,11 +61,9 @@ def test_useless_exclude_with_types_filter(capsys, tempdir_factory):
], ],
} }
repo = git_dir(tempdir_factory) add_config_to_repo(in_git_dir.strpath, config)
add_config_to_repo(repo, config)
with cwd(repo): assert check_useless_excludes.main(()) == 1
assert check_useless_excludes.main(()) == 1
out, _ = capsys.readouterr() out, _ = capsys.readouterr()
out = out.strip() out = out.strip()
@ -82,7 +74,7 @@ def test_useless_exclude_with_types_filter(capsys, tempdir_factory):
assert expected == out assert expected == out
def test_no_excludes(capsys, tempdir_factory): def test_no_excludes(capsys, in_git_dir):
config = { config = {
'repos': [ 'repos': [
{ {
@ -92,17 +84,15 @@ def test_no_excludes(capsys, tempdir_factory):
], ],
} }
repo = git_dir(tempdir_factory) add_config_to_repo(in_git_dir.strpath, config)
add_config_to_repo(repo, config)
with cwd(repo): assert check_useless_excludes.main(()) == 0
assert check_useless_excludes.main(()) == 0
out, _ = capsys.readouterr() out, _ = capsys.readouterr()
assert out == '' assert out == ''
def test_valid_exclude(capsys, tempdir_factory): def test_valid_exclude(capsys, in_git_dir):
config = { config = {
'repos': [ 'repos': [
{ {
@ -117,11 +107,9 @@ def test_valid_exclude(capsys, tempdir_factory):
], ],
} }
repo = git_dir(tempdir_factory) add_config_to_repo(in_git_dir.strpath, config)
add_config_to_repo(repo, config)
with cwd(repo): assert check_useless_excludes.main(()) == 0
assert check_useless_excludes.main(()) == 0
out, _ = capsys.readouterr() out, _ = capsys.readouterr()
assert out == '' assert out == ''

View file

@ -2,7 +2,6 @@ from __future__ import absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
import collections import collections
import io
import os.path import os.path
import re import re
import shutil import shutil
@ -24,7 +23,6 @@ from pre_commit.languages import rust
from pre_commit.repository import Repository from pre_commit.repository import Repository
from pre_commit.util import cmd_output from pre_commit.util import cmd_output
from testing.fixtures import config_with_local_hooks from testing.fixtures import config_with_local_hooks
from testing.fixtures import git_dir
from testing.fixtures import make_config_from_repo from testing.fixtures import make_config_from_repo
from testing.fixtures import make_repo from testing.fixtures import make_repo
from testing.fixtures import modify_manifest from testing.fixtures import modify_manifest
@ -96,17 +94,14 @@ def test_python_hook_args_with_spaces(tempdir_factory, store):
) )
def test_python_hook_weird_setup_cfg(tempdir_factory, store): def test_python_hook_weird_setup_cfg(in_git_dir, tempdir_factory, store):
path = git_dir(tempdir_factory) in_git_dir.join('setup.cfg').write('[install]\ninstall_scripts=/usr/sbin')
with cwd(path):
with io.open('setup.cfg', 'w') as setup_cfg:
setup_cfg.write('[install]\ninstall_scripts=/usr/sbin\n')
_test_hook_repo( _test_hook_repo(
tempdir_factory, store, 'python_hooks_repo', tempdir_factory, store, 'python_hooks_repo',
'foo', [os.devnull], 'foo', [os.devnull],
b"['" + five.to_bytes(os.devnull) + b"']\nHello World\n", b"['" + five.to_bytes(os.devnull) + b"']\nHello World\n",
) )
@xfailif_no_venv @xfailif_no_venv
@ -444,14 +439,12 @@ def _norm_pwd(path):
)[1].strip() )[1].strip()
def test_cwd_of_hook(tempdir_factory, store): def test_cwd_of_hook(in_git_dir, tempdir_factory, store):
# Note: this doubles as a test for `system` hooks # Note: this doubles as a test for `system` hooks
path = git_dir(tempdir_factory) _test_hook_repo(
with cwd(path): tempdir_factory, store, 'prints_cwd_repo',
_test_hook_repo( 'prints_cwd', ['-L'], _norm_pwd(in_git_dir.strpath) + b'\n',
tempdir_factory, store, 'prints_cwd_repo', )
'prints_cwd', ['-L'], _norm_pwd(path) + b'\n',
)
def test_lots_of_files(tempdir_factory, store): def test_lots_of_files(tempdir_factory, store):
@ -502,10 +495,8 @@ def test_additional_dependencies_roll_forward(tempdir_factory, store):
assert 'mccabe' not in cmd_output('pip', 'freeze', '-l')[1] assert 'mccabe' not in cmd_output('pip', 'freeze', '-l')[1]
@xfailif_windows_no_ruby @xfailif_windows_no_ruby # pragma: windows no cover
def test_additional_ruby_dependencies_installed( def test_additional_ruby_dependencies_installed(tempdir_factory, store):
tempdir_factory, store,
): # pragma: no cover (non-windows)
path = make_repo(tempdir_factory, 'ruby_hooks_repo') path = make_repo(tempdir_factory, 'ruby_hooks_repo')
config = make_config_from_repo(path) config = make_config_from_repo(path)
config['hooks'][0]['additional_dependencies'] = ['thread_safe', 'tins'] config['hooks'][0]['additional_dependencies'] = ['thread_safe', 'tins']
@ -518,10 +509,8 @@ def test_additional_ruby_dependencies_installed(
assert 'tins' in output assert 'tins' in output
@xfailif_broken_deep_listdir @xfailif_broken_deep_listdir # pragma: windows no cover
def test_additional_node_dependencies_installed( def test_additional_node_dependencies_installed(tempdir_factory, store):
tempdir_factory, store,
): # pragma: no cover (non-windows)
path = make_repo(tempdir_factory, 'node_hooks_repo') path = make_repo(tempdir_factory, 'node_hooks_repo')
config = make_config_from_repo(path) config = make_config_from_repo(path)
# Careful to choose a small package that's not depped by npm # Careful to choose a small package that's not depped by npm

View file

@ -31,14 +31,11 @@ def get_short_git_status():
@pytest.fixture @pytest.fixture
def foo_staged(tempdir_factory): def foo_staged(in_git_dir):
path = git_dir(tempdir_factory) foo = in_git_dir.join('foo')
with cwd(path): foo.write(FOO_CONTENTS)
with io.open('foo', 'w') as foo_file: cmd_output('git', 'add', 'foo')
foo_file.write(FOO_CONTENTS) yield auto_namedtuple(path=in_git_dir.strpath, foo_filename=foo.strpath)
cmd_output('git', 'add', 'foo')
foo_filename = os.path.join(path, 'foo')
yield auto_namedtuple(path=path, foo_filename=foo_filename)
def _test_foo_state( def _test_foo_state(
@ -134,13 +131,11 @@ def test_foo_both_modify_conflicting(foo_staged, patch_dir):
@pytest.fixture @pytest.fixture
def img_staged(tempdir_factory): def img_staged(in_git_dir):
path = git_dir(tempdir_factory) img = in_git_dir.join('img.jpg')
with cwd(path): shutil.copy(get_resource_path('img1.jpg'), img.strpath)
img_filename = os.path.join(path, 'img.jpg') cmd_output('git', 'add', 'img.jpg')
shutil.copy(get_resource_path('img1.jpg'), img_filename) yield auto_namedtuple(path=in_git_dir.strpath, img_filename=img.strpath)
cmd_output('git', 'add', 'img.jpg')
yield auto_namedtuple(path=path, img_filename=img_filename)
def _test_img_state(path, expected_file='img1.jpg', status='A'): def _test_img_state(path, expected_file='img1.jpg', status='A'):