mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-08 20:44:18 +00:00
Simplify the tests
This commit is contained in:
parent
b64436cdda
commit
a99475afa0
14 changed files with 144 additions and 201 deletions
|
|
@ -1,7 +1,6 @@
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import contextlib
|
|
||||||
import io
|
import io
|
||||||
import os.path
|
import os.path
|
||||||
|
|
||||||
|
|
@ -9,16 +8,6 @@ import os.path
|
||||||
TESTING_DIR = os.path.abspath(os.path.dirname(__file__))
|
TESTING_DIR = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
|
||||||
def cwd(path):
|
|
||||||
pwd = os.getcwd()
|
|
||||||
os.chdir(path)
|
|
||||||
try:
|
|
||||||
yield
|
|
||||||
finally:
|
|
||||||
os.chdir(pwd)
|
|
||||||
|
|
||||||
|
|
||||||
def get_resource_path(path):
|
def get_resource_path(path):
|
||||||
return os.path.join(TESTING_DIR, 'resources', path)
|
return os.path.join(TESTING_DIR, 'resources', path)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import io
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.autopep8_wrapper import main
|
from pre_commit_hooks.autopep8_wrapper import main
|
||||||
|
|
@ -16,20 +14,15 @@ from pre_commit_hooks.autopep8_wrapper import main
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
def test_main_failing(tmpdir, input_src, expected_ret, output_src):
|
def test_main_failing(tmpdir, input_src, expected_ret, output_src):
|
||||||
filename = tmpdir.join('test.py').strpath
|
path = tmpdir.join('test.py')
|
||||||
with io.open(filename, 'w') as file_obj:
|
path.write(input_src)
|
||||||
file_obj.write(input_src)
|
ret = main([path.strpath, '-i', '-v'])
|
||||||
ret = main([filename, '-i', '-v'])
|
|
||||||
assert ret == expected_ret
|
assert ret == expected_ret
|
||||||
assert io.open(filename).read() == output_src
|
assert path.read() == output_src
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures('in_tmpdir')
|
def test_respects_config_file(tmpdir):
|
||||||
def test_respects_config_file():
|
with tmpdir.as_cwd():
|
||||||
with io.open('setup.cfg', 'w') as setup_cfg:
|
tmpdir.join('setup.cfg').write('[pep8]\nignore=E221')
|
||||||
setup_cfg.write('[pep8]\nignore=E221')
|
tmpdir.join('test.py').write('print(1 + 2)\n')
|
||||||
|
|
||||||
with io.open('test.py', 'w') as test_py:
|
|
||||||
test_py.write('print(1 + 2)\n')
|
|
||||||
|
|
||||||
assert main(['test.py', '-i', '-v']) == 0
|
assert main(['test.py', '-i', '-v']) == 0
|
||||||
|
|
|
||||||
|
|
@ -8,18 +8,16 @@ import pytest
|
||||||
from pre_commit_hooks.check_added_large_files import find_large_added_files
|
from pre_commit_hooks.check_added_large_files import find_large_added_files
|
||||||
from pre_commit_hooks.check_added_large_files import main
|
from pre_commit_hooks.check_added_large_files import main
|
||||||
from pre_commit_hooks.util import cmd_output
|
from pre_commit_hooks.util import cmd_output
|
||||||
from testing.util import cwd
|
|
||||||
from testing.util import write_file
|
|
||||||
|
|
||||||
|
|
||||||
def test_nothing_added(temp_git_dir):
|
def test_nothing_added(temp_git_dir):
|
||||||
with cwd(temp_git_dir):
|
with temp_git_dir.as_cwd():
|
||||||
assert find_large_added_files(['f.py'], 0) == 0
|
assert find_large_added_files(['f.py'], 0) == 0
|
||||||
|
|
||||||
|
|
||||||
def test_adding_something(temp_git_dir):
|
def test_adding_something(temp_git_dir):
|
||||||
with cwd(temp_git_dir):
|
with temp_git_dir.as_cwd():
|
||||||
write_file('f.py', "print('hello world')")
|
temp_git_dir.join('f.py').write("print('hello world')")
|
||||||
cmd_output('git', 'add', 'f.py')
|
cmd_output('git', 'add', 'f.py')
|
||||||
|
|
||||||
# Should fail with max size of 0
|
# Should fail with max size of 0
|
||||||
|
|
@ -27,8 +25,8 @@ def test_adding_something(temp_git_dir):
|
||||||
|
|
||||||
|
|
||||||
def test_add_something_giant(temp_git_dir):
|
def test_add_something_giant(temp_git_dir):
|
||||||
with cwd(temp_git_dir):
|
with temp_git_dir.as_cwd():
|
||||||
write_file('f.py', 'a' * 10000)
|
temp_git_dir.join('f.py').write('a' * 10000)
|
||||||
|
|
||||||
# Should not fail when not added
|
# Should not fail when not added
|
||||||
assert find_large_added_files(['f.py'], 0) == 0
|
assert find_large_added_files(['f.py'], 0) == 0
|
||||||
|
|
@ -46,8 +44,8 @@ def test_add_something_giant(temp_git_dir):
|
||||||
|
|
||||||
|
|
||||||
def test_added_file_not_in_pre_commits_list(temp_git_dir):
|
def test_added_file_not_in_pre_commits_list(temp_git_dir):
|
||||||
with cwd(temp_git_dir):
|
with temp_git_dir.as_cwd():
|
||||||
write_file('f.py', "print('hello world')")
|
temp_git_dir.join('f.py').write("print('hello world')")
|
||||||
cmd_output('git', 'add', 'f.py')
|
cmd_output('git', 'add', 'f.py')
|
||||||
|
|
||||||
# Should pass even with a size of 0
|
# Should pass even with a size of 0
|
||||||
|
|
@ -55,10 +53,10 @@ def test_added_file_not_in_pre_commits_list(temp_git_dir):
|
||||||
|
|
||||||
|
|
||||||
def test_integration(temp_git_dir):
|
def test_integration(temp_git_dir):
|
||||||
with cwd(temp_git_dir):
|
with temp_git_dir.as_cwd():
|
||||||
assert main(argv=[]) == 0
|
assert main(argv=[]) == 0
|
||||||
|
|
||||||
write_file('f.py', 'a' * 10000)
|
temp_git_dir.join('f.py').write('a' * 10000)
|
||||||
cmd_output('git', 'add', 'f.py')
|
cmd_output('git', 'add', 'f.py')
|
||||||
|
|
||||||
# Should not fail with default
|
# Should not fail with default
|
||||||
|
|
@ -80,11 +78,11 @@ xfailif_no_gitlfs = pytest.mark.xfail(
|
||||||
|
|
||||||
@xfailif_no_gitlfs
|
@xfailif_no_gitlfs
|
||||||
def test_allows_gitlfs(temp_git_dir): # pragma: no cover
|
def test_allows_gitlfs(temp_git_dir): # pragma: no cover
|
||||||
with cwd(temp_git_dir):
|
with temp_git_dir.as_cwd():
|
||||||
# Work around https://github.com/github/git-lfs/issues/913
|
# Work around https://github.com/github/git-lfs/issues/913
|
||||||
cmd_output('git', 'commit', '--allow-empty', '-m', 'foo')
|
cmd_output('git', 'commit', '--allow-empty', '-m', 'foo')
|
||||||
cmd_output('git', 'lfs', 'install')
|
cmd_output('git', 'lfs', 'install')
|
||||||
write_file('f.py', 'a' * 10000)
|
temp_git_dir.join('f.py').write('a' * 10000)
|
||||||
cmd_output('git', 'lfs', 'track', 'f.py')
|
cmd_output('git', 'lfs', 'track', 'f.py')
|
||||||
cmd_output('git', 'add', '.')
|
cmd_output('git', 'add', '.')
|
||||||
# Should succeed
|
# Should succeed
|
||||||
|
|
@ -93,11 +91,11 @@ def test_allows_gitlfs(temp_git_dir): # pragma: no cover
|
||||||
|
|
||||||
@xfailif_no_gitlfs
|
@xfailif_no_gitlfs
|
||||||
def test_moves_with_gitlfs(temp_git_dir): # pragma: no cover
|
def test_moves_with_gitlfs(temp_git_dir): # pragma: no cover
|
||||||
with cwd(temp_git_dir):
|
with temp_git_dir.as_cwd():
|
||||||
cmd_output('git', 'lfs', 'install')
|
cmd_output('git', 'lfs', 'install')
|
||||||
cmd_output('git', 'lfs', 'track', 'a.bin', 'b.bin')
|
cmd_output('git', 'lfs', 'track', 'a.bin', 'b.bin')
|
||||||
# First add the file we're going to move
|
# First add the file we're going to move
|
||||||
write_file('a.bin', 'a' * 10000)
|
temp_git_dir.join('a.bin').write('a' * 10000)
|
||||||
cmd_output('git', 'add', '.')
|
cmd_output('git', 'add', '.')
|
||||||
cmd_output('git', 'commit', '-am', 'foo')
|
cmd_output('git', 'commit', '-am', 'foo')
|
||||||
# Now move it and make sure the hook still succeeds
|
# Now move it and make sure the hook still succeeds
|
||||||
|
|
|
||||||
|
|
@ -4,63 +4,61 @@ from __future__ import unicode_literals
|
||||||
from pre_commit_hooks.check_case_conflict import find_conflicting_filenames
|
from pre_commit_hooks.check_case_conflict import find_conflicting_filenames
|
||||||
from pre_commit_hooks.check_case_conflict import main
|
from pre_commit_hooks.check_case_conflict import main
|
||||||
from pre_commit_hooks.util import cmd_output
|
from pre_commit_hooks.util import cmd_output
|
||||||
from testing.util import cwd
|
|
||||||
from testing.util import write_file
|
|
||||||
|
|
||||||
|
|
||||||
def test_nothing_added(temp_git_dir):
|
def test_nothing_added(temp_git_dir):
|
||||||
with cwd(temp_git_dir):
|
with temp_git_dir.as_cwd():
|
||||||
assert find_conflicting_filenames(['f.py']) == 0
|
assert find_conflicting_filenames(['f.py']) == 0
|
||||||
|
|
||||||
|
|
||||||
def test_adding_something(temp_git_dir):
|
def test_adding_something(temp_git_dir):
|
||||||
with cwd(temp_git_dir):
|
with temp_git_dir.as_cwd():
|
||||||
write_file('f.py', "print('hello world')")
|
temp_git_dir.join('f.py').write("print('hello world')")
|
||||||
cmd_output('git', 'add', 'f.py')
|
cmd_output('git', 'add', 'f.py')
|
||||||
|
|
||||||
assert find_conflicting_filenames(['f.py']) == 0
|
assert find_conflicting_filenames(['f.py']) == 0
|
||||||
|
|
||||||
|
|
||||||
def test_adding_something_with_conflict(temp_git_dir):
|
def test_adding_something_with_conflict(temp_git_dir):
|
||||||
with cwd(temp_git_dir):
|
with temp_git_dir.as_cwd():
|
||||||
write_file('f.py', "print('hello world')")
|
temp_git_dir.join('f.py').write("print('hello world')")
|
||||||
cmd_output('git', 'add', 'f.py')
|
cmd_output('git', 'add', 'f.py')
|
||||||
write_file('F.py', "print('hello world')")
|
temp_git_dir.join('F.py').write("print('hello world')")
|
||||||
cmd_output('git', 'add', 'F.py')
|
cmd_output('git', 'add', 'F.py')
|
||||||
|
|
||||||
assert find_conflicting_filenames(['f.py', 'F.py']) == 1
|
assert find_conflicting_filenames(['f.py', 'F.py']) == 1
|
||||||
|
|
||||||
|
|
||||||
def test_added_file_not_in_pre_commits_list(temp_git_dir):
|
def test_added_file_not_in_pre_commits_list(temp_git_dir):
|
||||||
with cwd(temp_git_dir):
|
with temp_git_dir.as_cwd():
|
||||||
write_file('f.py', "print('hello world')")
|
temp_git_dir.join('f.py').write("print('hello world')")
|
||||||
cmd_output('git', 'add', 'f.py')
|
cmd_output('git', 'add', 'f.py')
|
||||||
|
|
||||||
assert find_conflicting_filenames(['g.py']) == 0
|
assert find_conflicting_filenames(['g.py']) == 0
|
||||||
|
|
||||||
|
|
||||||
def test_file_conflicts_with_committed_file(temp_git_dir):
|
def test_file_conflicts_with_committed_file(temp_git_dir):
|
||||||
with cwd(temp_git_dir):
|
with temp_git_dir.as_cwd():
|
||||||
write_file('f.py', "print('hello world')")
|
temp_git_dir.join('f.py').write("print('hello world')")
|
||||||
cmd_output('git', 'add', 'f.py')
|
cmd_output('git', 'add', 'f.py')
|
||||||
cmd_output('git', 'commit', '--no-verify', '-m', 'Add f.py')
|
cmd_output('git', 'commit', '--no-verify', '-m', 'Add f.py')
|
||||||
|
|
||||||
write_file('F.py', "print('hello world')")
|
temp_git_dir.join('F.py').write("print('hello world')")
|
||||||
cmd_output('git', 'add', 'F.py')
|
cmd_output('git', 'add', 'F.py')
|
||||||
|
|
||||||
assert find_conflicting_filenames(['F.py']) == 1
|
assert find_conflicting_filenames(['F.py']) == 1
|
||||||
|
|
||||||
|
|
||||||
def test_integration(temp_git_dir):
|
def test_integration(temp_git_dir):
|
||||||
with cwd(temp_git_dir):
|
with temp_git_dir.as_cwd():
|
||||||
assert main(argv=[]) == 0
|
assert main(argv=[]) == 0
|
||||||
|
|
||||||
write_file('f.py', "print('hello world')")
|
temp_git_dir.join('f.py').write("print('hello world')")
|
||||||
cmd_output('git', 'add', 'f.py')
|
cmd_output('git', 'add', 'f.py')
|
||||||
|
|
||||||
assert main(argv=['f.py']) == 0
|
assert main(argv=['f.py']) == 0
|
||||||
|
|
||||||
write_file('F.py', "print('hello world')")
|
temp_git_dir.join('F.py').write("print('hello world')")
|
||||||
cmd_output('git', 'add', 'F.py')
|
cmd_output('git', 'add', 'F.py')
|
||||||
|
|
||||||
assert main(argv=['F.py']) == 1
|
assert main(argv=['F.py']) == 1
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import io
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.check_docstring_first import check_docstring_first
|
from pre_commit_hooks.check_docstring_first import check_docstring_first
|
||||||
|
|
@ -59,9 +57,7 @@ def test_unit(capsys, contents, expected, expected_out):
|
||||||
|
|
||||||
@all_tests
|
@all_tests
|
||||||
def test_integration(tmpdir, capsys, contents, expected, expected_out):
|
def test_integration(tmpdir, capsys, contents, expected, expected_out):
|
||||||
tmpfilename = tmpdir.join('test.py').strpath
|
f = tmpdir.join('test.py')
|
||||||
with io.open(tmpfilename, 'w') as tmpfile:
|
f.write(contents)
|
||||||
tmpfile.write(contents)
|
assert main([f.strpath]) == expected
|
||||||
|
assert capsys.readouterr()[0] == expected_out.format(filename=f.strpath)
|
||||||
assert main([tmpfilename]) == expected
|
|
||||||
assert capsys.readouterr()[0] == expected_out.format(filename=tmpfilename)
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import io
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
|
|
@ -9,7 +8,6 @@ import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.check_merge_conflict import detect_merge_conflict
|
from pre_commit_hooks.check_merge_conflict import detect_merge_conflict
|
||||||
from pre_commit_hooks.util import cmd_output
|
from pre_commit_hooks.util import cmd_output
|
||||||
from testing.util import cwd
|
|
||||||
from testing.util import get_resource_path
|
from testing.util import get_resource_path
|
||||||
from testing.util import write_file
|
from testing.util import write_file
|
||||||
|
|
||||||
|
|
@ -18,28 +16,33 @@ from testing.util import write_file
|
||||||
|
|
||||||
|
|
||||||
@pytest.yield_fixture
|
@pytest.yield_fixture
|
||||||
def f1_is_a_conflict_file(in_tmpdir):
|
def f1_is_a_conflict_file(tmpdir):
|
||||||
# Make a merge conflict
|
# Make a merge conflict
|
||||||
cmd_output('git', 'init', 'repo1')
|
repo1 = tmpdir.join('repo1')
|
||||||
with cwd('repo1'):
|
repo1_f1 = repo1.join('f1')
|
||||||
io.open('f1', 'w').close()
|
repo2 = tmpdir.join('repo2')
|
||||||
cmd_output('git', 'add', 'f1')
|
repo2_f1 = repo2.join('f1')
|
||||||
cmd_output('git', 'commit', '-m' 'commit1')
|
|
||||||
|
|
||||||
cmd_output('git', 'clone', 'repo1', 'repo2')
|
cmd_output('git', 'init', repo1.strpath)
|
||||||
|
with repo1.as_cwd():
|
||||||
|
repo1_f1.ensure()
|
||||||
|
cmd_output('git', 'add', repo1_f1.strpath)
|
||||||
|
cmd_output('git', 'commit', '-m', 'commit1')
|
||||||
|
|
||||||
|
cmd_output('git', 'clone', repo1.strpath, repo2.strpath)
|
||||||
|
|
||||||
# Commit in master
|
# Commit in master
|
||||||
with cwd('repo1'):
|
with repo1.as_cwd():
|
||||||
write_file('f1', 'parent\n')
|
repo1_f1.write('parent\n')
|
||||||
cmd_output('git', 'commit', '-am', 'master commit2')
|
cmd_output('git', 'commit', '-am', 'master commit2')
|
||||||
|
|
||||||
# Commit in clone and pull
|
# Commit in clone and pull
|
||||||
with cwd('repo2'):
|
with repo2.as_cwd():
|
||||||
write_file('f1', 'child\n')
|
repo2_f1.write('child\n')
|
||||||
cmd_output('git', 'commit', '-am', 'clone commit2')
|
cmd_output('git', 'commit', '-am', 'clone commit2')
|
||||||
cmd_output('git', 'pull', retcode=None)
|
cmd_output('git', 'pull', retcode=None)
|
||||||
# We should end up in a merge conflict!
|
# We should end up in a merge conflict!
|
||||||
f1 = io.open('f1').read()
|
f1 = repo2_f1.read()
|
||||||
assert f1.startswith(
|
assert f1.startswith(
|
||||||
'<<<<<<< HEAD\n'
|
'<<<<<<< HEAD\n'
|
||||||
'child\n'
|
'child\n'
|
||||||
|
|
@ -60,30 +63,35 @@ def f1_is_a_conflict_file(in_tmpdir):
|
||||||
|
|
||||||
|
|
||||||
@pytest.yield_fixture
|
@pytest.yield_fixture
|
||||||
def repository_is_pending_merge(in_tmpdir):
|
def repository_is_pending_merge(tmpdir):
|
||||||
# Make a (non-conflicting) merge
|
# Make a (non-conflicting) merge
|
||||||
cmd_output('git', 'init', 'repo1')
|
repo1 = tmpdir.join('repo1')
|
||||||
with cwd('repo1'):
|
repo1_f1 = repo1.join('f1')
|
||||||
io.open('f1', 'w').close()
|
repo2 = tmpdir.join('repo2')
|
||||||
cmd_output('git', 'add', 'f1')
|
repo2_f1 = repo2.join('f1')
|
||||||
|
repo2_f2 = repo2.join('f2')
|
||||||
|
cmd_output('git', 'init', repo1.strpath)
|
||||||
|
with repo1.as_cwd():
|
||||||
|
repo1_f1.ensure()
|
||||||
|
cmd_output('git', 'add', repo1_f1.strpath)
|
||||||
cmd_output('git', 'commit', '-m' 'commit1')
|
cmd_output('git', 'commit', '-m' 'commit1')
|
||||||
|
|
||||||
cmd_output('git', 'clone', 'repo1', 'repo2')
|
cmd_output('git', 'clone', repo1.strpath, repo2.strpath)
|
||||||
|
|
||||||
# Commit in master
|
# Commit in master
|
||||||
with cwd('repo1'):
|
with repo1.as_cwd():
|
||||||
write_file('f1', 'parent\n')
|
repo1_f1.write('parent\n')
|
||||||
cmd_output('git', 'commit', '-am', 'master commit2')
|
cmd_output('git', 'commit', '-am', 'master commit2')
|
||||||
|
|
||||||
# Commit in clone and pull without committing
|
# Commit in clone and pull without committing
|
||||||
with cwd('repo2'):
|
with repo2.as_cwd():
|
||||||
write_file('f2', 'child\n')
|
repo2_f2.write('child\n')
|
||||||
cmd_output('git', 'add', 'f2')
|
cmd_output('git', 'add', repo2_f2.strpath)
|
||||||
cmd_output('git', 'commit', '-m', 'clone commit2')
|
cmd_output('git', 'commit', '-m', 'clone commit2')
|
||||||
cmd_output('git', 'pull', '--no-commit')
|
cmd_output('git', 'pull', '--no-commit')
|
||||||
# We should end up in a pending merge
|
# We should end up in a pending merge
|
||||||
assert io.open('f1').read().startswith('parent\n')
|
assert repo2_f1.read() == 'parent\n'
|
||||||
assert io.open('f2').read().startswith('child\n')
|
assert repo2_f2.read() == 'child\n'
|
||||||
assert os.path.exists(os.path.join('.git', 'MERGE_HEAD'))
|
assert os.path.exists(os.path.join('.git', 'MERGE_HEAD'))
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
@ -117,8 +125,6 @@ def test_ignores_binary_files():
|
||||||
assert detect_merge_conflict(['f1']) == 0
|
assert detect_merge_conflict(['f1']) == 0
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures('in_tmpdir')
|
def test_does_not_care_when_not_in_a_merge(tmpdir):
|
||||||
def test_does_not_care_when_not_in_a_merge():
|
tmpdir.join('README.md').write('problem\n=======\n')
|
||||||
with io.open('README.md', 'w') as readme_file:
|
|
||||||
readme_file.write('problem\n=======\n')
|
|
||||||
assert detect_merge_conflict(['README.md']) == 0
|
assert detect_merge_conflict(['README.md']) == 0
|
||||||
|
|
|
||||||
|
|
@ -5,17 +5,10 @@ from __future__ import unicode_literals
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.util import cmd_output
|
from pre_commit_hooks.util import cmd_output
|
||||||
from testing.util import cwd
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.yield_fixture
|
|
||||||
def in_tmpdir(tmpdir):
|
|
||||||
with cwd(tmpdir.strpath):
|
|
||||||
yield tmpdir
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.yield_fixture
|
@pytest.yield_fixture
|
||||||
def temp_git_dir(tmpdir):
|
def temp_git_dir(tmpdir):
|
||||||
git_dir = tmpdir.join('gits').strpath
|
git_dir = tmpdir.join('gits')
|
||||||
cmd_output('git', 'init', git_dir)
|
cmd_output('git', 'init', git_dir.strpath)
|
||||||
yield git_dir
|
yield git_dir
|
||||||
|
|
|
||||||
|
|
@ -16,9 +16,10 @@ TESTS = (
|
||||||
@pytest.mark.parametrize(('filename', 'expected_retval'), TESTS)
|
@pytest.mark.parametrize(('filename', 'expected_retval'), TESTS)
|
||||||
def test_detect_aws_credentials(filename, expected_retval):
|
def test_detect_aws_credentials(filename, expected_retval):
|
||||||
# with a valid credentials file
|
# with a valid credentials file
|
||||||
ret = main(
|
ret = main((
|
||||||
[get_resource_path(filename), "--credentials-file=testing/resources/sample_aws_credentials"]
|
get_resource_path(filename),
|
||||||
)
|
"--credentials-file=testing/resources/sample_aws_credentials",
|
||||||
|
))
|
||||||
assert ret == expected_retval
|
assert ret == expected_retval
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
import os.path
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.detect_private_key import detect_private_key
|
from pre_commit_hooks.detect_private_key import detect_private_key
|
||||||
|
|
@ -18,9 +16,6 @@ TESTS = (
|
||||||
|
|
||||||
@pytest.mark.parametrize(('input_s', 'expected_retval'), TESTS)
|
@pytest.mark.parametrize(('input_s', 'expected_retval'), TESTS)
|
||||||
def test_detect_private_key(input_s, expected_retval, tmpdir):
|
def test_detect_private_key(input_s, expected_retval, tmpdir):
|
||||||
path = os.path.join(tmpdir.strpath, 'file.txt')
|
path = tmpdir.join('file.txt')
|
||||||
|
path.write_binary(input_s)
|
||||||
with open(path, 'wb') as file_obj:
|
assert detect_private_key([path.strpath]) == expected_retval
|
||||||
file_obj.write(input_s)
|
|
||||||
|
|
||||||
assert detect_private_key([path]) == expected_retval
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import io
|
import io
|
||||||
import os.path
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
@ -21,8 +20,7 @@ TESTS = (
|
||||||
|
|
||||||
@pytest.mark.parametrize(('input_s', 'expected_retval', 'output'), TESTS)
|
@pytest.mark.parametrize(('input_s', 'expected_retval', 'output'), TESTS)
|
||||||
def test_fix_file(input_s, expected_retval, output):
|
def test_fix_file(input_s, expected_retval, output):
|
||||||
file_obj = io.BytesIO()
|
file_obj = io.BytesIO(input_s)
|
||||||
file_obj.write(input_s)
|
|
||||||
ret = fix_file(file_obj)
|
ret = fix_file(file_obj)
|
||||||
assert file_obj.getvalue() == output
|
assert file_obj.getvalue() == output
|
||||||
assert ret == expected_retval
|
assert ret == expected_retval
|
||||||
|
|
@ -30,13 +28,11 @@ def test_fix_file(input_s, expected_retval, output):
|
||||||
|
|
||||||
@pytest.mark.parametrize(('input_s', 'expected_retval', 'output'), TESTS)
|
@pytest.mark.parametrize(('input_s', 'expected_retval', 'output'), TESTS)
|
||||||
def test_integration(input_s, expected_retval, output, tmpdir):
|
def test_integration(input_s, expected_retval, output, tmpdir):
|
||||||
file_path = os.path.join(tmpdir.strpath, 'file.txt')
|
path = tmpdir.join('file.txt')
|
||||||
|
path.write_binary(input_s)
|
||||||
|
|
||||||
with open(file_path, 'wb') as file_obj:
|
ret = end_of_file_fixer([path.strpath])
|
||||||
file_obj.write(input_s)
|
file_output = path.read_binary()
|
||||||
|
|
||||||
ret = end_of_file_fixer([file_path])
|
|
||||||
file_output = open(file_path, 'rb').read()
|
|
||||||
|
|
||||||
assert file_output == output
|
assert file_output == output
|
||||||
assert ret == expected_retval
|
assert ret == expected_retval
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import io
|
import shutil
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
@ -28,8 +28,10 @@ def test_unsorted_pretty_format_json(filename, expected_retval):
|
||||||
|
|
||||||
def test_autofix_pretty_format_json(tmpdir):
|
def test_autofix_pretty_format_json(tmpdir):
|
||||||
srcfile = tmpdir.join('to_be_json_formatted.json')
|
srcfile = tmpdir.join('to_be_json_formatted.json')
|
||||||
with io.open(get_resource_path('not_pretty_formatted_json.json')) as f:
|
shutil.copyfile(
|
||||||
srcfile.write_text(f.read(), 'UTF-8')
|
get_resource_path('not_pretty_formatted_json.json'),
|
||||||
|
srcfile.strpath,
|
||||||
|
)
|
||||||
|
|
||||||
# now launch the autofix on that file
|
# now launch the autofix on that file
|
||||||
ret = pretty_format_json(['--autofix', srcfile.strpath])
|
ret = pretty_format_json(['--autofix', srcfile.strpath])
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
import os.path
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.requirements_txt_fixer import fix_requirements_txt
|
from pre_commit_hooks.requirements_txt_fixer import fix_requirements_txt
|
||||||
|
|
@ -22,13 +20,11 @@ TESTS = (
|
||||||
|
|
||||||
@pytest.mark.parametrize(('input_s', 'expected_retval', 'output'), TESTS)
|
@pytest.mark.parametrize(('input_s', 'expected_retval', 'output'), TESTS)
|
||||||
def test_integration(input_s, expected_retval, output, tmpdir):
|
def test_integration(input_s, expected_retval, output, tmpdir):
|
||||||
path = os.path.join(tmpdir.strpath, 'file.txt')
|
path = tmpdir.join('file.txt')
|
||||||
|
path.write_binary(input_s)
|
||||||
|
|
||||||
with open(path, 'wb') as file_obj:
|
assert fix_requirements_txt([path.strpath]) == expected_retval
|
||||||
file_obj.write(input_s)
|
assert path.read_binary() == output
|
||||||
|
|
||||||
assert fix_requirements_txt([path]) == expected_retval
|
|
||||||
assert open(path, 'rb').read() == output
|
|
||||||
|
|
||||||
|
|
||||||
def test_requirement_object():
|
def test_requirement_object():
|
||||||
|
|
|
||||||
|
|
@ -40,11 +40,8 @@ TESTS = (
|
||||||
|
|
||||||
@pytest.mark.parametrize(('input_s', 'output', 'expected_retval'), TESTS)
|
@pytest.mark.parametrize(('input_s', 'output', 'expected_retval'), TESTS)
|
||||||
def test_rewrite(input_s, output, expected_retval, tmpdir):
|
def test_rewrite(input_s, output, expected_retval, tmpdir):
|
||||||
tmpfile = tmpdir.join('file.txt')
|
path = tmpdir.join('file.txt')
|
||||||
|
path.write(input_s)
|
||||||
with open(tmpfile.strpath, 'w') as f:
|
retval = main([path.strpath])
|
||||||
f.write(input_s)
|
assert path.read() == output
|
||||||
|
|
||||||
retval = main([tmpfile.strpath])
|
|
||||||
assert tmpfile.read() == output
|
|
||||||
assert retval == expected_retval
|
assert retval == expected_retval
|
||||||
|
|
|
||||||
|
|
@ -1,31 +1,23 @@
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import sys
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.trailing_whitespace_fixer import fix_trailing_whitespace
|
from pre_commit_hooks.trailing_whitespace_fixer import fix_trailing_whitespace
|
||||||
from testing.util import cwd
|
|
||||||
|
|
||||||
|
|
||||||
def test_fixes_trailing_whitespace(tmpdir):
|
@pytest.mark.parametrize(
|
||||||
with cwd(tmpdir.strpath):
|
('input_s', 'expected'),
|
||||||
for filename, contents in (
|
(
|
||||||
('foo.py', 'foo \nbar \n'),
|
('foo \nbar \n', 'foo\nbar\n'),
|
||||||
('bar.py', 'bar\t\nbaz\t\n'),
|
('bar\t\nbaz\t\n', 'bar\nbaz\n'),
|
||||||
):
|
),
|
||||||
with open(filename, 'w') as file_obj:
|
)
|
||||||
file_obj.write(contents) # pragma: no branch (26 coverage bug)
|
def test_fixes_trailing_whitespace(input_s, expected, tmpdir):
|
||||||
|
path = tmpdir.join('file.txt')
|
||||||
ret = fix_trailing_whitespace(['foo.py', 'bar.py'])
|
path.write(input_s)
|
||||||
assert ret == 1
|
assert fix_trailing_whitespace((path.strpath,)) == 1
|
||||||
|
assert path.read() == expected
|
||||||
for filename, after_contents in (
|
|
||||||
('foo.py', 'foo\nbar\n'),
|
|
||||||
('bar.py', 'bar\nbaz\n'),
|
|
||||||
):
|
|
||||||
assert open(filename).read() == after_contents
|
|
||||||
|
|
||||||
|
|
||||||
# filename, expected input, expected output
|
# filename, expected input, expected output
|
||||||
|
|
@ -39,13 +31,11 @@ MD_TESTS_1 = (
|
||||||
|
|
||||||
@pytest.mark.parametrize(('filename', 'input_s', 'output'), MD_TESTS_1)
|
@pytest.mark.parametrize(('filename', 'input_s', 'output'), MD_TESTS_1)
|
||||||
def test_fixes_trailing_markdown_whitespace(filename, input_s, output, tmpdir):
|
def test_fixes_trailing_markdown_whitespace(filename, input_s, output, tmpdir):
|
||||||
with cwd(tmpdir.strpath):
|
path = tmpdir.join(filename)
|
||||||
with open(filename, 'w') as file_obj:
|
path.write(input_s)
|
||||||
file_obj.write(input_s) # pragma: no branch (26 coverage bug)
|
ret = fix_trailing_whitespace([path.strpath])
|
||||||
|
|
||||||
ret = fix_trailing_whitespace([filename])
|
|
||||||
assert ret == 1
|
assert ret == 1
|
||||||
assert open(filename).read() == output
|
assert path.read() == output
|
||||||
|
|
||||||
|
|
||||||
# filename, expected input, expected output
|
# filename, expected input, expected output
|
||||||
|
|
@ -60,14 +50,13 @@ MD_TESTS_2 = (
|
||||||
|
|
||||||
@pytest.mark.parametrize(('filename', 'input_s', 'output'), MD_TESTS_2)
|
@pytest.mark.parametrize(('filename', 'input_s', 'output'), MD_TESTS_2)
|
||||||
def test_markdown_linebreak_ext_opt(filename, input_s, output, tmpdir):
|
def test_markdown_linebreak_ext_opt(filename, input_s, output, tmpdir):
|
||||||
with cwd(tmpdir.strpath):
|
path = tmpdir.join(filename)
|
||||||
with open(filename, 'w') as file_obj:
|
path.write(input_s)
|
||||||
file_obj.write(input_s) # pragma: no branch (26 coverage bug)
|
ret = fix_trailing_whitespace((
|
||||||
|
'--markdown-linebreak-ext=TxT', path.strpath
|
||||||
ret = fix_trailing_whitespace(['--markdown-linebreak-ext=TxT',
|
))
|
||||||
filename])
|
|
||||||
assert ret == 1
|
assert ret == 1
|
||||||
assert open(filename).read() == output
|
assert path.read() == output
|
||||||
|
|
||||||
|
|
||||||
# filename, expected input, expected output
|
# filename, expected input, expected output
|
||||||
|
|
@ -79,25 +68,21 @@ MD_TESTS_3 = (
|
||||||
|
|
||||||
@pytest.mark.parametrize(('filename', 'input_s', 'output'), MD_TESTS_3)
|
@pytest.mark.parametrize(('filename', 'input_s', 'output'), MD_TESTS_3)
|
||||||
def test_markdown_linebreak_ext_opt_all(filename, input_s, output, tmpdir):
|
def test_markdown_linebreak_ext_opt_all(filename, input_s, output, tmpdir):
|
||||||
with cwd(tmpdir.strpath):
|
path = tmpdir.join(filename)
|
||||||
with open(filename, 'w') as file_obj:
|
path.write(input_s)
|
||||||
file_obj.write(input_s) # pragma: no branch (26 coverage bug)
|
|
||||||
|
|
||||||
# need to make sure filename is not treated as argument to option
|
# need to make sure filename is not treated as argument to option
|
||||||
ret = fix_trailing_whitespace(['--markdown-linebreak-ext=*',
|
ret = fix_trailing_whitespace([
|
||||||
filename])
|
'--markdown-linebreak-ext=*', path.strpath,
|
||||||
|
])
|
||||||
assert ret == 1
|
assert ret == 1
|
||||||
assert open(filename).read() == output
|
assert path.read() == output
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(('arg'), ('--', 'a.b', 'a/b'))
|
@pytest.mark.parametrize(('arg'), ('--', 'a.b', 'a/b'))
|
||||||
def test_markdown_linebreak_ext_badopt(arg):
|
def test_markdown_linebreak_ext_badopt(arg):
|
||||||
try:
|
with pytest.raises(SystemExit) as excinfo:
|
||||||
ret = fix_trailing_whitespace(['--markdown-linebreak-ext', arg])
|
fix_trailing_whitespace(['--markdown-linebreak-ext', arg])
|
||||||
except SystemExit:
|
assert excinfo.value.code == 2
|
||||||
ret = sys.exc_info()[1].code
|
|
||||||
finally:
|
|
||||||
assert ret == 2
|
|
||||||
|
|
||||||
|
|
||||||
# filename, expected input, expected output
|
# filename, expected input, expected output
|
||||||
|
|
@ -109,13 +94,11 @@ MD_TESTS_4 = (
|
||||||
|
|
||||||
@pytest.mark.parametrize(('filename', 'input_s', 'output'), MD_TESTS_4)
|
@pytest.mark.parametrize(('filename', 'input_s', 'output'), MD_TESTS_4)
|
||||||
def test_no_markdown_linebreak_ext_opt(filename, input_s, output, tmpdir):
|
def test_no_markdown_linebreak_ext_opt(filename, input_s, output, tmpdir):
|
||||||
with cwd(tmpdir.strpath):
|
path = tmpdir.join(filename)
|
||||||
with open(filename, 'w') as file_obj:
|
path.write(input_s)
|
||||||
file_obj.write(input_s) # pragma: no branch (26 coverage bug)
|
ret = fix_trailing_whitespace(['--no-markdown-linebreak-ext', path.strpath])
|
||||||
|
|
||||||
ret = fix_trailing_whitespace(['--no-markdown-linebreak-ext', filename])
|
|
||||||
assert ret == 1
|
assert ret == 1
|
||||||
assert open(filename).read() == output
|
assert path.read() == output
|
||||||
|
|
||||||
|
|
||||||
def test_returns_zero_for_no_changes():
|
def test_returns_zero_for_no_changes():
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue