Move empty_git_dir out of pytest fixtures.

This commit is contained in:
Anthony Sottile 2014-06-15 15:22:29 -07:00
parent 7b1230df27
commit 047a933554
10 changed files with 159 additions and 99 deletions

14
testing/fixtures.py Normal file
View file

@ -0,0 +1,14 @@
from __future__ import absolute_import
from __future__ import unicode_literals
from plumbum import local
git = local['git']
def git_dir(tmpdir_factory):
path = tmpdir_factory.get()
with local.cwd(path):
git('init')
return path

View file

@ -75,7 +75,7 @@ def out_of_date_repo(python_hooks_repo):
config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA) config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA)
validate_config_extra(config_wrapped) validate_config_extra(config_wrapped)
config = config_wrapped[0] config = config_wrapped[0]
local['git']('commit', '--allow-empty', '-m', 'foo') local['git']['commit', '--allow-empty', '-m', 'foo']()
head_sha = get_head_sha(python_hooks_repo) head_sha = get_head_sha(python_hooks_repo)
with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj: with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj:
@ -125,8 +125,8 @@ def hook_disappearing_repo(python_hooks_repo):
get_resource_path('manifest_without_foo.yaml'), get_resource_path('manifest_without_foo.yaml'),
C.MANIFEST_FILE, C.MANIFEST_FILE,
) )
local['git']('add', '.') local['git']['add', '.']()
local['git']('commit', '-m', 'Remove foo') local['git']['commit', '-m', 'Remove foo']()
with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj: with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj:
file_obj.write( file_obj.write(

View file

@ -1,3 +1,4 @@
from __future__ import absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
import io import io
@ -8,10 +9,12 @@ import stat
from pre_commit.commands.install import install from pre_commit.commands.install import install
from pre_commit.runner import Runner from pre_commit.runner import Runner
from testing.fixtures import git_dir
def test_install_pre_commit(empty_git_dir): def test_install_pre_commit(tmpdir_factory):
runner = Runner(empty_git_dir) path = git_dir(tmpdir_factory)
runner = Runner(path)
ret = install(runner) ret = install(runner)
assert ret == 0 assert ret == 0
assert os.path.exists(runner.pre_commit_path) assert os.path.exists(runner.pre_commit_path)

View file

@ -75,11 +75,9 @@ def test_run_all_hooks_failing(
({'verbose': True}, ('foo.py\nHello World',), 0, True), ({'verbose': True}, ('foo.py\nHello World',), 0, True),
({'hook': 'bash_hook'}, ('Bash hook', 'Passed'), 0, True), ({'hook': 'bash_hook'}, ('Bash hook', 'Passed'), 0, True),
({'hook': 'nope'}, ('No hook with id `nope`',), 1, True), ({'hook': 'nope'}, ('No hook with id `nope`',), 1, True),
# All the files in the repo.
# This seems kind of weird but it is beacuse py.test reuses fixtures
( (
{'all_files': True, 'verbose': True}, {'all_files': True, 'verbose': True},
('hooks.yaml', 'bin/hook.sh', 'foo.py', 'dummy'), ('foo.py'),
0, 0,
True, True,
), ),

View file

@ -1,3 +1,4 @@
from __future__ import absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
import os.path import os.path
@ -5,16 +6,19 @@ import os.path
from pre_commit.runner import Runner from pre_commit.runner import Runner
from pre_commit.commands.install import install from pre_commit.commands.install import install
from pre_commit.commands.uninstall import uninstall from pre_commit.commands.uninstall import uninstall
from testing.fixtures import git_dir
def test_uninstall_pre_commit_does_not_blow_up_when_not_there(empty_git_dir): def test_uninstall_does_not_blow_up_when_not_there(tmpdir_factory):
runner = Runner(empty_git_dir) path = git_dir(tmpdir_factory)
runner = Runner(path)
ret = uninstall(runner) ret = uninstall(runner)
assert ret == 0 assert ret == 0
def test_uninstall(empty_git_dir): def test_uninstall(tmpdir_factory):
runner = Runner(empty_git_dir) path = git_dir(tmpdir_factory)
runner = Runner(path)
assert not os.path.exists(runner.pre_commit_path) assert not os.path.exists(runner.pre_commit_path)
install(runner) install(runner)
assert os.path.exists(runner.pre_commit_path) assert os.path.exists(runner.pre_commit_path)

View file

@ -1,4 +1,5 @@
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import unicode_literals
import mock import mock
import os import os
@ -16,11 +17,15 @@ from pre_commit.jsonschema_extensions import apply_defaults
from pre_commit.prefixed_command_runner import PrefixedCommandRunner from pre_commit.prefixed_command_runner import PrefixedCommandRunner
from pre_commit.runner import Runner from pre_commit.runner import Runner
from pre_commit.store import Store from pre_commit.store import Store
from testing.fixtures import git_dir
from testing.util import copy_tree_to_path from testing.util import copy_tree_to_path
from testing.util import get_head_sha from testing.util import get_head_sha
from testing.util import get_resource_path from testing.util import get_resource_path
git = local['git']
@pytest.yield_fixture @pytest.yield_fixture
def tmpdir_factory(tmpdir): def tmpdir_factory(tmpdir):
class TmpdirFactory(object): class TmpdirFactory(object):
@ -43,23 +48,19 @@ def in_tmpdir(tmpdir_factory):
yield path yield path
@pytest.yield_fixture
def empty_git_dir(in_tmpdir):
local['git']('init')
yield in_tmpdir
def add_and_commit(): def add_and_commit():
local['git']('add', '.') git('add', '.')
local['git']('commit', '-m', 'random commit {0}'.format(time.time())) git('commit', '-m', 'random commit {0}'.format(time.time()))
@pytest.yield_fixture @pytest.yield_fixture
def dummy_git_repo(empty_git_dir): def dummy_git_repo(tmpdir_factory):
path = git_dir(tmpdir_factory)
with local.cwd(path):
# This is needed otherwise there is no `HEAD` # This is needed otherwise there is no `HEAD`
local['touch']('dummy') local['touch']('dummy')
add_and_commit() add_and_commit()
yield empty_git_dir yield path
def _make_repo(repo_path, repo_source): def _make_repo(repo_path, repo_source):
@ -192,40 +193,48 @@ def _make_repo_from_configs(*configs):
@pytest.yield_fixture @pytest.yield_fixture
def repo_with_passing_hook(config_for_script_hooks_repo, empty_git_dir): def repo_with_passing_hook(config_for_script_hooks_repo, tmpdir_factory):
path = git_dir(tmpdir_factory)
with local.cwd(path):
_make_repo_from_configs(config_for_script_hooks_repo) _make_repo_from_configs(config_for_script_hooks_repo)
yield empty_git_dir yield path
@pytest.yield_fixture @pytest.yield_fixture
def repo_with_failing_hook(failing_hook_repo, empty_git_dir): def repo_with_failing_hook(failing_hook_repo, tmpdir_factory):
_make_repo_from_configs(_make_config(failing_hook_repo, 'failing_hook')) path = git_dir(tmpdir_factory)
yield empty_git_dir with local.cwd(path):
_make_repo_from_configs(
_make_config(failing_hook_repo, 'failing_hook')
)
yield path
@pytest.yield_fixture @pytest.yield_fixture
def in_merge_conflict(repo_with_passing_hook): def in_merge_conflict(repo_with_passing_hook):
local['git']('add', C.CONFIG_FILE) local['touch']('dummy')
local['git']('commit', '-m' 'add hooks file') git('add', 'dummy')
local['git']('clone', '.', 'foo') git('add', C.CONFIG_FILE)
git('commit', '-m' 'add hooks file')
git('clone', '.', 'foo')
with local.cwd('foo'): with local.cwd('foo'):
local['git']('checkout', 'origin/master', '-b', 'foo') git('checkout', 'origin/master', '-b', 'foo')
with open('conflict_file', 'w') as conflict_file: with open('conflict_file', 'w') as conflict_file:
conflict_file.write('herp\nderp\n') conflict_file.write('herp\nderp\n')
local['git']('add', 'conflict_file') git('add', 'conflict_file')
with open('foo_only_file', 'w') as foo_only_file: with open('foo_only_file', 'w') as foo_only_file:
foo_only_file.write('foo') foo_only_file.write('foo')
local['git']('add', 'foo_only_file') git('add', 'foo_only_file')
local['git']('commit', '-m', 'conflict_file') git('commit', '-m', 'conflict_file')
local['git']('checkout', 'origin/master', '-b', 'bar') git('checkout', 'origin/master', '-b', 'bar')
with open('conflict_file', 'w') as conflict_file: with open('conflict_file', 'w') as conflict_file:
conflict_file.write('harp\nddrp\n') conflict_file.write('harp\nddrp\n')
local['git']('add', 'conflict_file') git('add', 'conflict_file')
with open('bar_only_file', 'w') as bar_only_file: with open('bar_only_file', 'w') as bar_only_file:
bar_only_file.write('bar') bar_only_file.write('bar')
local['git']('add', 'bar_only_file') git('add', 'bar_only_file')
local['git']('commit', '-m', 'conflict_file') git('commit', '-m', 'conflict_file')
local['git']('merge', 'foo', retcode=None) git('merge', 'foo', retcode=None)
yield os.path.join(repo_with_passing_hook, 'foo') yield os.path.join(repo_with_passing_hook, 'foo')

View file

@ -1,20 +1,31 @@
from __future__ import absolute_import
import os.path
import pytest import pytest
from plumbum import local from plumbum import local
from pre_commit import git from pre_commit import git
from testing.fixtures import git_dir
def test_get_root(empty_git_dir): def test_get_root_at_root(tmpdir_factory):
assert git.get_root() == empty_git_dir path = git_dir(tmpdir_factory)
with local.cwd(path):
foo = local.path('foo') assert git.get_root() == path
foo.mkdir()
with local.cwd(foo):
assert git.get_root() == empty_git_dir
def test_is_not_in_merge_conflict(empty_git_dir): def test_get_root_deeper(tmpdir_factory):
path = git_dir(tmpdir_factory)
foo_path = os.path.join(path, 'foo')
os.mkdir(foo_path)
with local.cwd(foo_path):
assert git.get_root() == path
def test_is_not_in_merge_conflict(tmpdir_factory):
path = git_dir(tmpdir_factory)
with local.cwd(path):
assert git.is_in_merge_conflict() is False assert git.is_in_merge_conflict() is False

View file

@ -1,9 +1,13 @@
from __future__ import absolute_import
from __future__ import unicode_literals
import os import os
import os.path import os.path
import pytest from plumbum import local
import pre_commit.constants as C import pre_commit.constants as C
from pre_commit.runner import Runner from pre_commit.runner import Runner
from testing.fixtures import git_dir
def test_init_has_no_side_effects(tmpdir): def test_init_has_no_side_effects(tmpdir):
@ -13,24 +17,26 @@ def test_init_has_no_side_effects(tmpdir):
assert os.getcwd() == current_wd assert os.getcwd() == current_wd
def test_create_sets_correct_directory(empty_git_dir): def test_create_sets_correct_directory(tmpdir_factory):
path = git_dir(tmpdir_factory)
with local.cwd(path):
runner = Runner.create() runner = Runner.create()
assert runner.git_root == empty_git_dir assert runner.git_root == path
assert os.getcwd() == empty_git_dir assert os.getcwd() == path
@pytest.yield_fixture def test_create_changes_to_git_root(tmpdir_factory):
def git_dir_with_directory(empty_git_dir): path = git_dir(tmpdir_factory)
os.mkdir('foo') with local.cwd(path):
yield empty_git_dir # Change into some directory, create should set to root
foo_path = os.path.join(path, 'foo')
os.mkdir(foo_path)
os.chdir(foo_path)
assert os.getcwd() != path
def test_changes_to_root_of_git_dir(git_dir_with_directory):
os.chdir('foo')
assert os.getcwd() != git_dir_with_directory
runner = Runner.create() runner = Runner.create()
assert runner.git_root == git_dir_with_directory assert runner.git_root == path
assert os.getcwd() == git_dir_with_directory assert os.getcwd() == path
def test_config_file_path(): def test_config_file_path():

View file

@ -1,3 +1,4 @@
from __future__ import absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
import io import io
@ -10,6 +11,7 @@ from plumbum import local
from pre_commit.staged_files_only import staged_files_only from pre_commit.staged_files_only import staged_files_only
from testing.auto_namedtuple import auto_namedtuple from testing.auto_namedtuple import auto_namedtuple
from testing.fixtures import git_dir
from testing.util import get_resource_path from testing.util import get_resource_path
@ -22,12 +24,14 @@ def get_short_git_status():
@pytest.yield_fixture @pytest.yield_fixture
def foo_staged(empty_git_dir): def foo_staged(tmpdir_factory):
path = git_dir(tmpdir_factory)
with local.cwd(path):
with io.open('foo', 'w') as foo_file: with io.open('foo', 'w') as foo_file:
foo_file.write(FOO_CONTENTS) foo_file.write(FOO_CONTENTS)
local['git']('add', 'foo') local['git']('add', 'foo')
foo_filename = os.path.join(empty_git_dir, 'foo') foo_filename = os.path.join(path, 'foo')
yield auto_namedtuple(path=empty_git_dir, foo_filename=foo_filename) yield auto_namedtuple(path=path, foo_filename=foo_filename)
def _test_foo_state(path, foo_contents=FOO_CONTENTS, status='A'): def _test_foo_state(path, foo_contents=FOO_CONTENTS, status='A'):
@ -96,11 +100,13 @@ def test_foo_both_modify_conflicting(foo_staged, cmd_runner):
@pytest.yield_fixture @pytest.yield_fixture
def img_staged(empty_git_dir): def img_staged(tmpdir_factory):
img_filename = os.path.join(empty_git_dir, 'img.jpg') path = git_dir(tmpdir_factory)
with local.cwd(path):
img_filename = os.path.join(path, 'img.jpg')
shutil.copy(get_resource_path('img1.jpg'), img_filename) shutil.copy(get_resource_path('img1.jpg'), img_filename)
local['git']('add', 'img.jpg') local['git']('add', 'img.jpg')
yield auto_namedtuple(path=empty_git_dir, img_filename=img_filename) 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'):
@ -149,12 +155,14 @@ def test_img_conflict(img_staged, cmd_runner):
@pytest.yield_fixture @pytest.yield_fixture
def submodule_with_commits(empty_git_dir): def submodule_with_commits(tmpdir_factory):
path = git_dir(tmpdir_factory)
with local.cwd(path):
local['git']('commit', '--allow-empty', '-m', 'foo') local['git']('commit', '--allow-empty', '-m', 'foo')
sha1 = local['git']('rev-parse', 'HEAD').strip() sha1 = local['git']('rev-parse', 'HEAD').strip()
local['git']('commit', '--allow-empty', '-m', 'bar') local['git']('commit', '--allow-empty', '-m', 'bar')
sha2 = local['git']('rev-parse', 'HEAD').strip() sha2 = local['git']('rev-parse', 'HEAD').strip()
yield auto_namedtuple(path=empty_git_dir, sha1=sha1, sha2=sha2) yield auto_namedtuple(path=path, sha1=sha1, sha2=sha2)
def checkout_submodule(sha): def checkout_submodule(sha):
@ -163,13 +171,15 @@ def checkout_submodule(sha):
@pytest.yield_fixture @pytest.yield_fixture
def sub_staged(submodule_with_commits, empty_git_dir): def sub_staged(submodule_with_commits, tmpdir_factory):
path = git_dir(tmpdir_factory)
with local.cwd(path):
local['git']('submodule', 'add', submodule_with_commits.path, 'sub') local['git']('submodule', 'add', submodule_with_commits.path, 'sub')
checkout_submodule(submodule_with_commits.sha1) checkout_submodule(submodule_with_commits.sha1)
local['git']('add', 'sub') local['git']('add', 'sub')
yield auto_namedtuple( yield auto_namedtuple(
path=empty_git_dir, path=path,
sub_path=os.path.join(empty_git_dir, 'sub'), sub_path=os.path.join(path, 'sub'),
submodule=submodule_with_commits, submodule=submodule_with_commits,
) )

View file

@ -1,3 +1,6 @@
from __future__ import absolute_import
from __future__ import unicode_literals
import io import io
import mock import mock
import os import os
@ -10,6 +13,7 @@ from pre_commit import five
from pre_commit.store import _get_default_directory from pre_commit.store import _get_default_directory
from pre_commit.store import logger from pre_commit.store import logger
from pre_commit.store import Store from pre_commit.store import Store
from testing.fixtures import git_dir
from testing.util import get_head_sha from testing.util import get_head_sha
@ -71,13 +75,14 @@ def log_info_mock():
yield info_mock yield info_mock
def test_clone(store, empty_git_dir, log_info_mock): def test_clone(store, tmpdir_factory, log_info_mock):
with local.cwd(empty_git_dir): path = git_dir(tmpdir_factory)
with local.cwd(path):
local['git']('commit', '--allow-empty', '-m', 'foo') local['git']('commit', '--allow-empty', '-m', 'foo')
sha = get_head_sha(empty_git_dir) sha = get_head_sha(path)
local['git']('commit', '--allow-empty', '-m', 'bar') local['git']('commit', '--allow-empty', '-m', 'bar')
ret = store.clone(empty_git_dir, sha) ret = store.clone(path, sha)
# Should have printed some stuff # Should have printed some stuff
log_info_mock.assert_called_with('This may take a few minutes...') log_info_mock.assert_called_with('This may take a few minutes...')