diff --git a/tests/commands/install_uninstall_test.py b/tests/commands/install_uninstall_test.py index 401a1dec..ce74a2ea 100644 --- a/tests/commands/install_uninstall_test.py +++ b/tests/commands/install_uninstall_test.py @@ -421,17 +421,14 @@ def test_replace_old_commit_script(tempdir_factory, store): assert NORMAL_PRE_COMMIT_RUN.match(output) -def test_uninstall_doesnt_remove_not_our_hooks(tempdir_factory): - path = git_dir(tempdir_factory) - with cwd(path): - mkdirp(os.path.join(path, '.git/hooks')) - 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) +def test_uninstall_doesnt_remove_not_our_hooks(in_git_dir): + pre_commit = in_git_dir.join('.git/hooks').ensure_dir().join('pre-commit') + pre_commit.write('#!/usr/bin/env bash\necho 1\n') + make_executable(pre_commit.strpath) - 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( diff --git a/tests/conftest.py b/tests/conftest.py index 95fc410e..49fbf3fc 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -65,9 +65,10 @@ def in_tmpdir(tempdir_factory): @pytest.fixture def in_git_dir(tmpdir): - with tmpdir.as_cwd(): + repo = tmpdir.join('repo').ensure_dir() + with repo.as_cwd(): cmd_output('git', 'init') - yield tmpdir + yield repo def _make_conflict(): diff --git a/tests/git_test.py b/tests/git_test.py index 58f14f50..2a9bda4a 100644 --- a/tests/git_test.py +++ b/tests/git_test.py @@ -9,45 +9,34 @@ import pytest from pre_commit import git from pre_commit.error_handler import FatalError 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): - path = git_dir(tempdir_factory) - with cwd(path): - assert os.path.normcase(git.get_root()) == os.path.normcase(path) +def test_get_root_at_root(in_git_dir): + expected = os.path.normcase(in_git_dir.strpath) + assert os.path.normcase(git.get_root()) == expected -def test_get_root_deeper(tempdir_factory): - path = git_dir(tempdir_factory) - - foo_path = os.path.join(path, 'foo') - os.mkdir(foo_path) - with cwd(foo_path): - assert os.path.normcase(git.get_root()) == os.path.normcase(path) +def test_get_root_deeper(in_git_dir): + expected = os.path.normcase(in_git_dir.strpath) + with in_git_dir.join('foo').ensure_dir().as_cwd(): + assert os.path.normcase(git.get_root()) == expected -def test_get_root_not_git_dir(tempdir_factory): - with cwd(tempdir_factory.get()): - with pytest.raises(FatalError): - git.get_root() +def test_get_root_not_git_dir(in_tmpdir): + with pytest.raises(FatalError): + git.get_root() -def test_get_staged_files_deleted(tempdir_factory): - path = git_dir(tempdir_factory) - with cwd(path): - open('test', 'a').close() - cmd_output('git', 'add', 'test') - cmd_output('git', 'commit', '-m', 'foo', '--allow-empty') - cmd_output('git', 'rm', '--cached', 'test') - assert git.get_staged_files() == [] +def test_get_staged_files_deleted(in_git_dir): + in_git_dir.join('test').ensure() + cmd_output('git', 'add', 'test') + cmd_output('git', 'commit', '-m', 'foo', '--allow-empty') + cmd_output('git', 'rm', '--cached', 'test') + assert git.get_staged_files() == [] -def test_is_not_in_merge_conflict(tempdir_factory): - path = git_dir(tempdir_factory) - with cwd(path): - assert git.is_in_merge_conflict() is False +def test_is_not_in_merge_conflict(in_git_dir): + assert git.is_in_merge_conflict() is False 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 -def test_get_changed_files(in_tmpdir): - cmd_output('git', 'init', '.') +def test_get_changed_files(in_git_dir): cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit') - open('a.txt', 'a').close() - open('b.txt', 'a').close() + in_git_dir.join('a.txt').ensure() + in_git_dir.join('b.txt').ensure() cmd_output('git', 'add', '.') cmd_output('git', 'commit', '-m', 'add some files') files = git.get_changed_files('HEAD', 'HEAD^') @@ -143,15 +131,12 @@ def test_zsplit(s, expected): @pytest.fixture -def non_ascii_repo(tmpdir): - repo = tmpdir.join('repo').ensure_dir() - with repo.as_cwd(): - cmd_output('git', 'init', '.') - cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit') - repo.join('интервью').ensure() - cmd_output('git', 'add', '.') - cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit') - yield repo +def non_ascii_repo(in_git_dir): + cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit') + in_git_dir.join('интервью').ensure() + cmd_output('git', 'add', '.') + cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit') + yield in_git_dir def test_all_files_non_ascii(non_ascii_repo): diff --git a/tests/meta_hooks/check_hooks_apply_test.py b/tests/meta_hooks/check_hooks_apply_test.py index d48d9d7a..06bdd045 100644 --- a/tests/meta_hooks/check_hooks_apply_test.py +++ b/tests/meta_hooks/check_hooks_apply_test.py @@ -1,10 +1,8 @@ from pre_commit.meta_hooks import check_hooks_apply 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 = { '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(repo, config) + add_config_to_repo(in_git_dir.strpath, config) - with cwd(repo): - assert check_hooks_apply.main(()) == 1 + assert check_hooks_apply.main(()) == 1 out, _ = capsys.readouterr() 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 = { '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(repo, config) + add_config_to_repo(in_git_dir.strpath, config) - with cwd(repo): - assert check_hooks_apply.main(()) == 1 + assert check_hooks_apply.main(()) == 1 out, _ = capsys.readouterr() 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 = { '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(repo, config) + add_config_to_repo(in_git_dir.strpath, config) - with cwd(repo): - assert check_hooks_apply.main(()) == 1 + assert check_hooks_apply.main(()) == 1 out, _ = capsys.readouterr() assert 'check-useless-excludes does not apply to this repository' in out -def test_hook_types_excludes_everything( - capsys, tempdir_factory, mock_store_dir, -): +def test_hook_types_excludes_everything(capsys, in_git_dir, mock_store_dir): config = { 'repos': [ { @@ -96,17 +86,15 @@ def test_hook_types_excludes_everything( ], } - repo = git_dir(tempdir_factory) - add_config_to_repo(repo, config) + add_config_to_repo(in_git_dir.strpath, config) - with cwd(repo): - assert check_hooks_apply.main(()) == 1 + assert check_hooks_apply.main(()) == 1 out, _ = capsys.readouterr() 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 = { 'repos': [ { @@ -142,11 +130,9 @@ def test_valid_exceptions(capsys, tempdir_factory, mock_store_dir): ], } - repo = git_dir(tempdir_factory) - add_config_to_repo(repo, config) + add_config_to_repo(in_git_dir.strpath, config) - with cwd(repo): - assert check_hooks_apply.main(()) == 0 + assert check_hooks_apply.main(()) == 0 out, _ = capsys.readouterr() assert out == '' diff --git a/tests/meta_hooks/check_useless_excludes_test.py b/tests/meta_hooks/check_useless_excludes_test.py index b2cc1873..4adaacd3 100644 --- a/tests/meta_hooks/check_useless_excludes_test.py +++ b/tests/meta_hooks/check_useless_excludes_test.py @@ -1,10 +1,8 @@ from pre_commit.meta_hooks import check_useless_excludes 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 = { 'exclude': 'foo', 'repos': [ @@ -15,18 +13,16 @@ def test_useless_exclude_global(capsys, tempdir_factory): ], } - repo = git_dir(tempdir_factory) - add_config_to_repo(repo, config) + add_config_to_repo(in_git_dir.strpath, config) - with cwd(repo): - assert check_useless_excludes.main(()) == 1 + assert check_useless_excludes.main(()) == 1 out, _ = capsys.readouterr() out = out.strip() 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 = { 'repos': [ { @@ -36,11 +32,9 @@ def test_useless_exclude_for_hook(capsys, tempdir_factory): ], } - repo = git_dir(tempdir_factory) - add_config_to_repo(repo, config) + add_config_to_repo(in_git_dir.strpath, config) - with cwd(repo): - assert check_useless_excludes.main(()) == 1 + assert check_useless_excludes.main(()) == 1 out, _ = capsys.readouterr() out = out.strip() @@ -51,7 +45,7 @@ def test_useless_exclude_for_hook(capsys, tempdir_factory): 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 = { 'repos': [ { @@ -67,11 +61,9 @@ def test_useless_exclude_with_types_filter(capsys, tempdir_factory): ], } - repo = git_dir(tempdir_factory) - add_config_to_repo(repo, config) + add_config_to_repo(in_git_dir.strpath, config) - with cwd(repo): - assert check_useless_excludes.main(()) == 1 + assert check_useless_excludes.main(()) == 1 out, _ = capsys.readouterr() out = out.strip() @@ -82,7 +74,7 @@ def test_useless_exclude_with_types_filter(capsys, tempdir_factory): assert expected == out -def test_no_excludes(capsys, tempdir_factory): +def test_no_excludes(capsys, in_git_dir): config = { 'repos': [ { @@ -92,17 +84,15 @@ def test_no_excludes(capsys, tempdir_factory): ], } - repo = git_dir(tempdir_factory) - add_config_to_repo(repo, config) + add_config_to_repo(in_git_dir.strpath, config) - with cwd(repo): - assert check_useless_excludes.main(()) == 0 + assert check_useless_excludes.main(()) == 0 out, _ = capsys.readouterr() assert out == '' -def test_valid_exclude(capsys, tempdir_factory): +def test_valid_exclude(capsys, in_git_dir): config = { 'repos': [ { @@ -117,11 +107,9 @@ def test_valid_exclude(capsys, tempdir_factory): ], } - repo = git_dir(tempdir_factory) - add_config_to_repo(repo, config) + add_config_to_repo(in_git_dir.strpath, config) - with cwd(repo): - assert check_useless_excludes.main(()) == 0 + assert check_useless_excludes.main(()) == 0 out, _ = capsys.readouterr() assert out == '' diff --git a/tests/repository_test.py b/tests/repository_test.py index 92964037..606bfe75 100644 --- a/tests/repository_test.py +++ b/tests/repository_test.py @@ -2,7 +2,6 @@ from __future__ import absolute_import from __future__ import unicode_literals import collections -import io import os.path import re import shutil @@ -24,7 +23,6 @@ from pre_commit.languages import rust from pre_commit.repository import Repository from pre_commit.util import cmd_output 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_repo 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): - path = git_dir(tempdir_factory) - with cwd(path): - with io.open('setup.cfg', 'w') as setup_cfg: - setup_cfg.write('[install]\ninstall_scripts=/usr/sbin\n') +def test_python_hook_weird_setup_cfg(in_git_dir, tempdir_factory, store): + in_git_dir.join('setup.cfg').write('[install]\ninstall_scripts=/usr/sbin') - _test_hook_repo( - tempdir_factory, store, 'python_hooks_repo', - 'foo', [os.devnull], - b"['" + five.to_bytes(os.devnull) + b"']\nHello World\n", - ) + _test_hook_repo( + tempdir_factory, store, 'python_hooks_repo', + 'foo', [os.devnull], + b"['" + five.to_bytes(os.devnull) + b"']\nHello World\n", + ) @xfailif_no_venv @@ -444,14 +439,12 @@ def _norm_pwd(path): )[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 - path = git_dir(tempdir_factory) - with cwd(path): - _test_hook_repo( - tempdir_factory, store, 'prints_cwd_repo', - 'prints_cwd', ['-L'], _norm_pwd(path) + b'\n', - ) + _test_hook_repo( + tempdir_factory, store, 'prints_cwd_repo', + 'prints_cwd', ['-L'], _norm_pwd(in_git_dir.strpath) + b'\n', + ) def test_lots_of_files(tempdir_factory, store): diff --git a/tests/staged_files_only_test.py b/tests/staged_files_only_test.py index 73a6b585..9f226a41 100644 --- a/tests/staged_files_only_test.py +++ b/tests/staged_files_only_test.py @@ -31,14 +31,11 @@ def get_short_git_status(): @pytest.fixture -def foo_staged(tempdir_factory): - path = git_dir(tempdir_factory) - with cwd(path): - with io.open('foo', 'w') as foo_file: - foo_file.write(FOO_CONTENTS) - cmd_output('git', 'add', 'foo') - foo_filename = os.path.join(path, 'foo') - yield auto_namedtuple(path=path, foo_filename=foo_filename) +def foo_staged(in_git_dir): + foo = in_git_dir.join('foo') + foo.write(FOO_CONTENTS) + cmd_output('git', 'add', 'foo') + yield auto_namedtuple(path=in_git_dir.strpath, foo_filename=foo.strpath) def _test_foo_state( @@ -134,13 +131,11 @@ def test_foo_both_modify_conflicting(foo_staged, patch_dir): @pytest.fixture -def img_staged(tempdir_factory): - path = git_dir(tempdir_factory) - with cwd(path): - img_filename = os.path.join(path, 'img.jpg') - shutil.copy(get_resource_path('img1.jpg'), img_filename) - cmd_output('git', 'add', 'img.jpg') - yield auto_namedtuple(path=path, img_filename=img_filename) +def img_staged(in_git_dir): + img = in_git_dir.join('img.jpg') + shutil.copy(get_resource_path('img1.jpg'), img.strpath) + cmd_output('git', 'add', 'img.jpg') + yield auto_namedtuple(path=in_git_dir.strpath, img_filename=img.strpath) def _test_img_state(path, expected_file='img1.jpg', status='A'):