Simplify the tests

This commit is contained in:
Anthony Sottile 2016-05-27 14:09:50 -07:00
parent b64436cdda
commit a99475afa0
14 changed files with 144 additions and 201 deletions

View file

@ -1,7 +1,6 @@
from __future__ import absolute_import
from __future__ import unicode_literals
import contextlib
import io
import os.path
@ -9,16 +8,6 @@ import os.path
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):
return os.path.join(TESTING_DIR, 'resources', path)

View file

@ -1,8 +1,6 @@
from __future__ import absolute_import
from __future__ import unicode_literals
import io
import pytest
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):
filename = tmpdir.join('test.py').strpath
with io.open(filename, 'w') as file_obj:
file_obj.write(input_src)
ret = main([filename, '-i', '-v'])
path = tmpdir.join('test.py')
path.write(input_src)
ret = main([path.strpath, '-i', '-v'])
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():
with io.open('setup.cfg', 'w') as setup_cfg:
setup_cfg.write('[pep8]\nignore=E221')
with io.open('test.py', 'w') as test_py:
test_py.write('print(1 + 2)\n')
assert main(['test.py', '-i', '-v']) == 0
def test_respects_config_file(tmpdir):
with tmpdir.as_cwd():
tmpdir.join('setup.cfg').write('[pep8]\nignore=E221')
tmpdir.join('test.py').write('print(1 + 2)\n')
assert main(['test.py', '-i', '-v']) == 0

View file

@ -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 main
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):
with cwd(temp_git_dir):
with temp_git_dir.as_cwd():
assert find_large_added_files(['f.py'], 0) == 0
def test_adding_something(temp_git_dir):
with cwd(temp_git_dir):
write_file('f.py', "print('hello world')")
with temp_git_dir.as_cwd():
temp_git_dir.join('f.py').write("print('hello world')")
cmd_output('git', 'add', 'f.py')
# 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):
with cwd(temp_git_dir):
write_file('f.py', 'a' * 10000)
with temp_git_dir.as_cwd():
temp_git_dir.join('f.py').write('a' * 10000)
# Should not fail when not added
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):
with cwd(temp_git_dir):
write_file('f.py', "print('hello world')")
with temp_git_dir.as_cwd():
temp_git_dir.join('f.py').write("print('hello world')")
cmd_output('git', 'add', 'f.py')
# 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):
with cwd(temp_git_dir):
with temp_git_dir.as_cwd():
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')
# Should not fail with default
@ -80,11 +78,11 @@ xfailif_no_gitlfs = pytest.mark.xfail(
@xfailif_no_gitlfs
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
cmd_output('git', 'commit', '--allow-empty', '-m', 'foo')
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', 'add', '.')
# Should succeed
@ -93,11 +91,11 @@ def test_allows_gitlfs(temp_git_dir): # pragma: no cover
@xfailif_no_gitlfs
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', 'track', 'a.bin', 'b.bin')
# 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', 'commit', '-am', 'foo')
# Now move it and make sure the hook still succeeds

View file

@ -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 main
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):
with cwd(temp_git_dir):
with temp_git_dir.as_cwd():
assert find_conflicting_filenames(['f.py']) == 0
def test_adding_something(temp_git_dir):
with cwd(temp_git_dir):
write_file('f.py', "print('hello world')")
with temp_git_dir.as_cwd():
temp_git_dir.join('f.py').write("print('hello world')")
cmd_output('git', 'add', 'f.py')
assert find_conflicting_filenames(['f.py']) == 0
def test_adding_something_with_conflict(temp_git_dir):
with cwd(temp_git_dir):
write_file('f.py', "print('hello world')")
with temp_git_dir.as_cwd():
temp_git_dir.join('f.py').write("print('hello world')")
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')
assert find_conflicting_filenames(['f.py', 'F.py']) == 1
def test_added_file_not_in_pre_commits_list(temp_git_dir):
with cwd(temp_git_dir):
write_file('f.py', "print('hello world')")
with temp_git_dir.as_cwd():
temp_git_dir.join('f.py').write("print('hello world')")
cmd_output('git', 'add', 'f.py')
assert find_conflicting_filenames(['g.py']) == 0
def test_file_conflicts_with_committed_file(temp_git_dir):
with cwd(temp_git_dir):
write_file('f.py', "print('hello world')")
with temp_git_dir.as_cwd():
temp_git_dir.join('f.py').write("print('hello world')")
cmd_output('git', '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')
assert find_conflicting_filenames(['F.py']) == 1
def test_integration(temp_git_dir):
with cwd(temp_git_dir):
with temp_git_dir.as_cwd():
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')
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')
assert main(argv=['F.py']) == 1

View file

@ -1,8 +1,6 @@
from __future__ import absolute_import
from __future__ import unicode_literals
import io
import pytest
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
def test_integration(tmpdir, capsys, contents, expected, expected_out):
tmpfilename = tmpdir.join('test.py').strpath
with io.open(tmpfilename, 'w') as tmpfile:
tmpfile.write(contents)
assert main([tmpfilename]) == expected
assert capsys.readouterr()[0] == expected_out.format(filename=tmpfilename)
f = tmpdir.join('test.py')
f.write(contents)
assert main([f.strpath]) == expected
assert capsys.readouterr()[0] == expected_out.format(filename=f.strpath)

View file

@ -1,7 +1,6 @@
from __future__ import absolute_import
from __future__ import unicode_literals
import io
import os
import shutil
@ -9,7 +8,6 @@ import pytest
from pre_commit_hooks.check_merge_conflict import detect_merge_conflict
from pre_commit_hooks.util import cmd_output
from testing.util import cwd
from testing.util import get_resource_path
from testing.util import write_file
@ -18,28 +16,33 @@ from testing.util import write_file
@pytest.yield_fixture
def f1_is_a_conflict_file(in_tmpdir):
def f1_is_a_conflict_file(tmpdir):
# Make a merge conflict
cmd_output('git', 'init', 'repo1')
with cwd('repo1'):
io.open('f1', 'w').close()
cmd_output('git', 'add', 'f1')
cmd_output('git', 'commit', '-m' 'commit1')
repo1 = tmpdir.join('repo1')
repo1_f1 = repo1.join('f1')
repo2 = tmpdir.join('repo2')
repo2_f1 = repo2.join('f1')
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
with cwd('repo1'):
write_file('f1', 'parent\n')
with repo1.as_cwd():
repo1_f1.write('parent\n')
cmd_output('git', 'commit', '-am', 'master commit2')
# Commit in clone and pull
with cwd('repo2'):
write_file('f1', 'child\n')
with repo2.as_cwd():
repo2_f1.write('child\n')
cmd_output('git', 'commit', '-am', 'clone commit2')
cmd_output('git', 'pull', retcode=None)
# We should end up in a merge conflict!
f1 = io.open('f1').read()
f1 = repo2_f1.read()
assert f1.startswith(
'<<<<<<< HEAD\n'
'child\n'
@ -60,30 +63,35 @@ def f1_is_a_conflict_file(in_tmpdir):
@pytest.yield_fixture
def repository_is_pending_merge(in_tmpdir):
def repository_is_pending_merge(tmpdir):
# Make a (non-conflicting) merge
cmd_output('git', 'init', 'repo1')
with cwd('repo1'):
io.open('f1', 'w').close()
cmd_output('git', 'add', 'f1')
repo1 = tmpdir.join('repo1')
repo1_f1 = repo1.join('f1')
repo2 = tmpdir.join('repo2')
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', 'clone', 'repo1', 'repo2')
cmd_output('git', 'clone', repo1.strpath, repo2.strpath)
# Commit in master
with cwd('repo1'):
write_file('f1', 'parent\n')
with repo1.as_cwd():
repo1_f1.write('parent\n')
cmd_output('git', 'commit', '-am', 'master commit2')
# Commit in clone and pull without committing
with cwd('repo2'):
write_file('f2', 'child\n')
cmd_output('git', 'add', 'f2')
with repo2.as_cwd():
repo2_f2.write('child\n')
cmd_output('git', 'add', repo2_f2.strpath)
cmd_output('git', 'commit', '-m', 'clone commit2')
cmd_output('git', 'pull', '--no-commit')
# We should end up in a pending merge
assert io.open('f1').read().startswith('parent\n')
assert io.open('f2').read().startswith('child\n')
assert repo2_f1.read() == 'parent\n'
assert repo2_f2.read() == 'child\n'
assert os.path.exists(os.path.join('.git', 'MERGE_HEAD'))
yield
@ -117,8 +125,6 @@ def test_ignores_binary_files():
assert detect_merge_conflict(['f1']) == 0
@pytest.mark.usefixtures('in_tmpdir')
def test_does_not_care_when_not_in_a_merge():
with io.open('README.md', 'w') as readme_file:
readme_file.write('problem\n=======\n')
def test_does_not_care_when_not_in_a_merge(tmpdir):
tmpdir.join('README.md').write('problem\n=======\n')
assert detect_merge_conflict(['README.md']) == 0

View file

@ -5,17 +5,10 @@ from __future__ import unicode_literals
import pytest
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
def temp_git_dir(tmpdir):
git_dir = tmpdir.join('gits').strpath
cmd_output('git', 'init', git_dir)
git_dir = tmpdir.join('gits')
cmd_output('git', 'init', git_dir.strpath)
yield git_dir

View file

@ -16,9 +16,10 @@ TESTS = (
@pytest.mark.parametrize(('filename', 'expected_retval'), TESTS)
def test_detect_aws_credentials(filename, expected_retval):
# with a valid credentials file
ret = main(
[get_resource_path(filename), "--credentials-file=testing/resources/sample_aws_credentials"]
)
ret = main((
get_resource_path(filename),
"--credentials-file=testing/resources/sample_aws_credentials",
))
assert ret == expected_retval

View file

@ -1,5 +1,3 @@
import os.path
import pytest
from pre_commit_hooks.detect_private_key import detect_private_key
@ -18,9 +16,6 @@ TESTS = (
@pytest.mark.parametrize(('input_s', 'expected_retval'), TESTS)
def test_detect_private_key(input_s, expected_retval, tmpdir):
path = os.path.join(tmpdir.strpath, 'file.txt')
with open(path, 'wb') as file_obj:
file_obj.write(input_s)
assert detect_private_key([path]) == expected_retval
path = tmpdir.join('file.txt')
path.write_binary(input_s)
assert detect_private_key([path.strpath]) == expected_retval

View file

@ -1,5 +1,4 @@
import io
import os.path
import pytest
@ -21,8 +20,7 @@ TESTS = (
@pytest.mark.parametrize(('input_s', 'expected_retval', 'output'), TESTS)
def test_fix_file(input_s, expected_retval, output):
file_obj = io.BytesIO()
file_obj.write(input_s)
file_obj = io.BytesIO(input_s)
ret = fix_file(file_obj)
assert file_obj.getvalue() == output
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)
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:
file_obj.write(input_s)
ret = end_of_file_fixer([file_path])
file_output = open(file_path, 'rb').read()
ret = end_of_file_fixer([path.strpath])
file_output = path.read_binary()
assert file_output == output
assert ret == expected_retval

View file

@ -1,4 +1,4 @@
import io
import shutil
import pytest
@ -28,8 +28,10 @@ def test_unsorted_pretty_format_json(filename, expected_retval):
def test_autofix_pretty_format_json(tmpdir):
srcfile = tmpdir.join('to_be_json_formatted.json')
with io.open(get_resource_path('not_pretty_formatted_json.json')) as f:
srcfile.write_text(f.read(), 'UTF-8')
shutil.copyfile(
get_resource_path('not_pretty_formatted_json.json'),
srcfile.strpath,
)
# now launch the autofix on that file
ret = pretty_format_json(['--autofix', srcfile.strpath])

View file

@ -1,5 +1,3 @@
import os.path
import pytest
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)
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:
file_obj.write(input_s)
assert fix_requirements_txt([path]) == expected_retval
assert open(path, 'rb').read() == output
assert fix_requirements_txt([path.strpath]) == expected_retval
assert path.read_binary() == output
def test_requirement_object():

View file

@ -40,11 +40,8 @@ TESTS = (
@pytest.mark.parametrize(('input_s', 'output', 'expected_retval'), TESTS)
def test_rewrite(input_s, output, expected_retval, tmpdir):
tmpfile = tmpdir.join('file.txt')
with open(tmpfile.strpath, 'w') as f:
f.write(input_s)
retval = main([tmpfile.strpath])
assert tmpfile.read() == output
path = tmpdir.join('file.txt')
path.write(input_s)
retval = main([path.strpath])
assert path.read() == output
assert retval == expected_retval

View file

@ -1,31 +1,23 @@
from __future__ import absolute_import
from __future__ import unicode_literals
import sys
import pytest
from pre_commit_hooks.trailing_whitespace_fixer import fix_trailing_whitespace
from testing.util import cwd
def test_fixes_trailing_whitespace(tmpdir):
with cwd(tmpdir.strpath):
for filename, contents in (
('foo.py', 'foo \nbar \n'),
('bar.py', 'bar\t\nbaz\t\n'),
):
with open(filename, 'w') as file_obj:
file_obj.write(contents) # pragma: no branch (26 coverage bug)
ret = fix_trailing_whitespace(['foo.py', 'bar.py'])
assert ret == 1
for filename, after_contents in (
('foo.py', 'foo\nbar\n'),
('bar.py', 'bar\nbaz\n'),
):
assert open(filename).read() == after_contents
@pytest.mark.parametrize(
('input_s', 'expected'),
(
('foo \nbar \n', 'foo\nbar\n'),
('bar\t\nbaz\t\n', 'bar\nbaz\n'),
),
)
def test_fixes_trailing_whitespace(input_s, expected, tmpdir):
path = tmpdir.join('file.txt')
path.write(input_s)
assert fix_trailing_whitespace((path.strpath,)) == 1
assert path.read() == expected
# filename, expected input, expected output
@ -39,13 +31,11 @@ MD_TESTS_1 = (
@pytest.mark.parametrize(('filename', 'input_s', 'output'), MD_TESTS_1)
def test_fixes_trailing_markdown_whitespace(filename, input_s, output, tmpdir):
with cwd(tmpdir.strpath):
with open(filename, 'w') as file_obj:
file_obj.write(input_s) # pragma: no branch (26 coverage bug)
ret = fix_trailing_whitespace([filename])
assert ret == 1
assert open(filename).read() == output
path = tmpdir.join(filename)
path.write(input_s)
ret = fix_trailing_whitespace([path.strpath])
assert ret == 1
assert path.read() == output
# filename, expected input, expected output
@ -60,14 +50,13 @@ MD_TESTS_2 = (
@pytest.mark.parametrize(('filename', 'input_s', 'output'), MD_TESTS_2)
def test_markdown_linebreak_ext_opt(filename, input_s, output, tmpdir):
with cwd(tmpdir.strpath):
with open(filename, 'w') as file_obj:
file_obj.write(input_s) # pragma: no branch (26 coverage bug)
ret = fix_trailing_whitespace(['--markdown-linebreak-ext=TxT',
filename])
assert ret == 1
assert open(filename).read() == output
path = tmpdir.join(filename)
path.write(input_s)
ret = fix_trailing_whitespace((
'--markdown-linebreak-ext=TxT', path.strpath
))
assert ret == 1
assert path.read() == output
# filename, expected input, expected output
@ -79,25 +68,21 @@ 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):
with cwd(tmpdir.strpath):
with open(filename, 'w') as file_obj:
file_obj.write(input_s) # pragma: no branch (26 coverage bug)
# need to make sure filename is not treated as argument to option
ret = fix_trailing_whitespace(['--markdown-linebreak-ext=*',
filename])
assert ret == 1
assert open(filename).read() == output
path = tmpdir.join(filename)
path.write(input_s)
# need to make sure filename is not treated as argument to option
ret = fix_trailing_whitespace([
'--markdown-linebreak-ext=*', path.strpath,
])
assert ret == 1
assert path.read() == output
@pytest.mark.parametrize(('arg'), ('--', 'a.b', 'a/b'))
def test_markdown_linebreak_ext_badopt(arg):
try:
ret = fix_trailing_whitespace(['--markdown-linebreak-ext', arg])
except SystemExit:
ret = sys.exc_info()[1].code
finally:
assert ret == 2
with pytest.raises(SystemExit) as excinfo:
fix_trailing_whitespace(['--markdown-linebreak-ext', arg])
assert excinfo.value.code == 2
# filename, expected input, expected output
@ -109,13 +94,11 @@ 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):
with cwd(tmpdir.strpath):
with open(filename, 'w') as file_obj:
file_obj.write(input_s) # pragma: no branch (26 coverage bug)
ret = fix_trailing_whitespace(['--no-markdown-linebreak-ext', filename])
assert ret == 1
assert open(filename).read() == output
path = tmpdir.join(filename)
path.write(input_s)
ret = fix_trailing_whitespace(['--no-markdown-linebreak-ext', path.strpath])
assert ret == 1
assert path.read() == output
def test_returns_zero_for_no_changes():