Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 047a933

Browse files
committed
Move empty_git_dir out of pytest fixtures.
1 parent 7b1230d commit 047a933

10 files changed

Lines changed: 157 additions & 97 deletions

File tree

testing/fixtures.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from __future__ import absolute_import
2+
from __future__ import unicode_literals
3+
4+
from plumbum import local
5+
6+
7+
git = local['git']
8+
9+
10+
def git_dir(tmpdir_factory):
11+
path = tmpdir_factory.get()
12+
with local.cwd(path):
13+
git('init')
14+
return path

tests/commands/autoupdate_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def out_of_date_repo(python_hooks_repo):
7575
config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA)
7676
validate_config_extra(config_wrapped)
7777
config = config_wrapped[0]
78-
local['git']('commit', '--allow-empty', '-m', 'foo')
78+
local['git']['commit', '--allow-empty', '-m', 'foo']()
7979
head_sha = get_head_sha(python_hooks_repo)
8080

8181
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):
125125
get_resource_path('manifest_without_foo.yaml'),
126126
C.MANIFEST_FILE,
127127
)
128-
local['git']('add', '.')
129-
local['git']('commit', '-m', 'Remove foo')
128+
local['git']['add', '.']()
129+
local['git']['commit', '-m', 'Remove foo']()
130130

131131
with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj:
132132
file_obj.write(

tests/commands/install_test.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import absolute_import
12
from __future__ import unicode_literals
23

34
import io
@@ -8,10 +9,12 @@
89

910
from pre_commit.commands.install import install
1011
from pre_commit.runner import Runner
12+
from testing.fixtures import git_dir
1113

1214

13-
def test_install_pre_commit(empty_git_dir):
14-
runner = Runner(empty_git_dir)
15+
def test_install_pre_commit(tmpdir_factory):
16+
path = git_dir(tmpdir_factory)
17+
runner = Runner(path)
1518
ret = install(runner)
1619
assert ret == 0
1720
assert os.path.exists(runner.pre_commit_path)

tests/commands/run_test.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,9 @@ def test_run_all_hooks_failing(
7575
({'verbose': True}, ('foo.py\nHello World',), 0, True),
7676
({'hook': 'bash_hook'}, ('Bash hook', 'Passed'), 0, True),
7777
({'hook': 'nope'}, ('No hook with id `nope`',), 1, True),
78-
# All the files in the repo.
79-
# This seems kind of weird but it is beacuse py.test reuses fixtures
8078
(
8179
{'all_files': True, 'verbose': True},
82-
('hooks.yaml', 'bin/hook.sh', 'foo.py', 'dummy'),
80+
('foo.py'),
8381
0,
8482
True,
8583
),

tests/commands/uninstall_test.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1+
from __future__ import absolute_import
12
from __future__ import unicode_literals
23

34
import os.path
45

56
from pre_commit.runner import Runner
67
from pre_commit.commands.install import install
78
from pre_commit.commands.uninstall import uninstall
9+
from testing.fixtures import git_dir
810

911

10-
def test_uninstall_pre_commit_does_not_blow_up_when_not_there(empty_git_dir):
11-
runner = Runner(empty_git_dir)
12+
def test_uninstall_does_not_blow_up_when_not_there(tmpdir_factory):
13+
path = git_dir(tmpdir_factory)
14+
runner = Runner(path)
1215
ret = uninstall(runner)
1316
assert ret == 0
1417

1518

16-
def test_uninstall(empty_git_dir):
17-
runner = Runner(empty_git_dir)
19+
def test_uninstall(tmpdir_factory):
20+
path = git_dir(tmpdir_factory)
21+
runner = Runner(path)
1822
assert not os.path.exists(runner.pre_commit_path)
1923
install(runner)
2024
assert os.path.exists(runner.pre_commit_path)

tests/conftest.py

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from __future__ import absolute_import
2+
from __future__ import unicode_literals
23

34
import mock
45
import os
@@ -16,11 +17,15 @@
1617
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
1718
from pre_commit.runner import Runner
1819
from pre_commit.store import Store
20+
from testing.fixtures import git_dir
1921
from testing.util import copy_tree_to_path
2022
from testing.util import get_head_sha
2123
from testing.util import get_resource_path
2224

2325

26+
git = local['git']
27+
28+
2429
@pytest.yield_fixture
2530
def tmpdir_factory(tmpdir):
2631
class TmpdirFactory(object):
@@ -43,23 +48,19 @@ def in_tmpdir(tmpdir_factory):
4348
yield path
4449

4550

46-
@pytest.yield_fixture
47-
def empty_git_dir(in_tmpdir):
48-
local['git']('init')
49-
yield in_tmpdir
50-
51-
5251
def add_and_commit():
53-
local['git']('add', '.')
54-
local['git']('commit', '-m', 'random commit {0}'.format(time.time()))
52+
git('add', '.')
53+
git('commit', '-m', 'random commit {0}'.format(time.time()))
5554

5655

5756
@pytest.yield_fixture
58-
def dummy_git_repo(empty_git_dir):
59-
# This is needed otherwise there is no `HEAD`
60-
local['touch']('dummy')
61-
add_and_commit()
62-
yield empty_git_dir
57+
def dummy_git_repo(tmpdir_factory):
58+
path = git_dir(tmpdir_factory)
59+
with local.cwd(path):
60+
# This is needed otherwise there is no `HEAD`
61+
local['touch']('dummy')
62+
add_and_commit()
63+
yield path
6364

6465

6566
def _make_repo(repo_path, repo_source):
@@ -192,40 +193,48 @@ def _make_repo_from_configs(*configs):
192193

193194

194195
@pytest.yield_fixture
195-
def repo_with_passing_hook(config_for_script_hooks_repo, empty_git_dir):
196-
_make_repo_from_configs(config_for_script_hooks_repo)
197-
yield empty_git_dir
196+
def repo_with_passing_hook(config_for_script_hooks_repo, tmpdir_factory):
197+
path = git_dir(tmpdir_factory)
198+
with local.cwd(path):
199+
_make_repo_from_configs(config_for_script_hooks_repo)
200+
yield path
198201

199202

200203
@pytest.yield_fixture
201-
def repo_with_failing_hook(failing_hook_repo, empty_git_dir):
202-
_make_repo_from_configs(_make_config(failing_hook_repo, 'failing_hook'))
203-
yield empty_git_dir
204+
def repo_with_failing_hook(failing_hook_repo, tmpdir_factory):
205+
path = git_dir(tmpdir_factory)
206+
with local.cwd(path):
207+
_make_repo_from_configs(
208+
_make_config(failing_hook_repo, 'failing_hook')
209+
)
210+
yield path
204211

205212

206213
@pytest.yield_fixture
207214
def in_merge_conflict(repo_with_passing_hook):
208-
local['git']('add', C.CONFIG_FILE)
209-
local['git']('commit', '-m' 'add hooks file')
210-
local['git']('clone', '.', 'foo')
215+
local['touch']('dummy')
216+
git('add', 'dummy')
217+
git('add', C.CONFIG_FILE)
218+
git('commit', '-m' 'add hooks file')
219+
git('clone', '.', 'foo')
211220
with local.cwd('foo'):
212-
local['git']('checkout', 'origin/master', '-b', 'foo')
221+
git('checkout', 'origin/master', '-b', 'foo')
213222
with open('conflict_file', 'w') as conflict_file:
214223
conflict_file.write('herp\nderp\n')
215-
local['git']('add', 'conflict_file')
224+
git('add', 'conflict_file')
216225
with open('foo_only_file', 'w') as foo_only_file:
217226
foo_only_file.write('foo')
218-
local['git']('add', 'foo_only_file')
219-
local['git']('commit', '-m', 'conflict_file')
220-
local['git']('checkout', 'origin/master', '-b', 'bar')
227+
git('add', 'foo_only_file')
228+
git('commit', '-m', 'conflict_file')
229+
git('checkout', 'origin/master', '-b', 'bar')
221230
with open('conflict_file', 'w') as conflict_file:
222231
conflict_file.write('harp\nddrp\n')
223-
local['git']('add', 'conflict_file')
232+
git('add', 'conflict_file')
224233
with open('bar_only_file', 'w') as bar_only_file:
225234
bar_only_file.write('bar')
226-
local['git']('add', 'bar_only_file')
227-
local['git']('commit', '-m', 'conflict_file')
228-
local['git']('merge', 'foo', retcode=None)
235+
git('add', 'bar_only_file')
236+
git('commit', '-m', 'conflict_file')
237+
git('merge', 'foo', retcode=None)
229238
yield os.path.join(repo_with_passing_hook, 'foo')
230239

231240

tests/git_test.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
1+
from __future__ import absolute_import
2+
3+
import os.path
14
import pytest
25
from plumbum import local
36

47
from pre_commit import git
8+
from testing.fixtures import git_dir
59

610

7-
def test_get_root(empty_git_dir):
8-
assert git.get_root() == empty_git_dir
11+
def test_get_root_at_root(tmpdir_factory):
12+
path = git_dir(tmpdir_factory)
13+
with local.cwd(path):
14+
assert git.get_root() == path
915

10-
foo = local.path('foo')
11-
foo.mkdir()
1216

13-
with local.cwd(foo):
14-
assert git.get_root() == empty_git_dir
17+
def test_get_root_deeper(tmpdir_factory):
18+
path = git_dir(tmpdir_factory)
1519

20+
foo_path = os.path.join(path, 'foo')
21+
os.mkdir(foo_path)
22+
with local.cwd(foo_path):
23+
assert git.get_root() == path
1624

17-
def test_is_not_in_merge_conflict(empty_git_dir):
18-
assert git.is_in_merge_conflict() is False
25+
26+
def test_is_not_in_merge_conflict(tmpdir_factory):
27+
path = git_dir(tmpdir_factory)
28+
with local.cwd(path):
29+
assert git.is_in_merge_conflict() is False
1930

2031

2132
def test_is_in_merge_conflict(in_merge_conflict):

tests/runner_test.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
from __future__ import absolute_import
2+
from __future__ import unicode_literals
3+
14
import os
25
import os.path
3-
import pytest
6+
from plumbum import local
47

58
import pre_commit.constants as C
69
from pre_commit.runner import Runner
10+
from testing.fixtures import git_dir
711

812

913
def test_init_has_no_side_effects(tmpdir):
@@ -13,24 +17,26 @@ def test_init_has_no_side_effects(tmpdir):
1317
assert os.getcwd() == current_wd
1418

1519

16-
def test_create_sets_correct_directory(empty_git_dir):
17-
runner = Runner.create()
18-
assert runner.git_root == empty_git_dir
19-
assert os.getcwd() == empty_git_dir
20-
20+
def test_create_sets_correct_directory(tmpdir_factory):
21+
path = git_dir(tmpdir_factory)
22+
with local.cwd(path):
23+
runner = Runner.create()
24+
assert runner.git_root == path
25+
assert os.getcwd() == path
2126

22-
@pytest.yield_fixture
23-
def git_dir_with_directory(empty_git_dir):
24-
os.mkdir('foo')
25-
yield empty_git_dir
2627

28+
def test_create_changes_to_git_root(tmpdir_factory):
29+
path = git_dir(tmpdir_factory)
30+
with local.cwd(path):
31+
# Change into some directory, create should set to root
32+
foo_path = os.path.join(path, 'foo')
33+
os.mkdir(foo_path)
34+
os.chdir(foo_path)
35+
assert os.getcwd() != path
2736

28-
def test_changes_to_root_of_git_dir(git_dir_with_directory):
29-
os.chdir('foo')
30-
assert os.getcwd() != git_dir_with_directory
31-
runner = Runner.create()
32-
assert runner.git_root == git_dir_with_directory
33-
assert os.getcwd() == git_dir_with_directory
37+
runner = Runner.create()
38+
assert runner.git_root == path
39+
assert os.getcwd() == path
3440

3541

3642
def test_config_file_path():

0 commit comments

Comments
 (0)