Skip to content

Commit d46bbc4

Browse files
committed
Use in_git_dir in more places
1 parent 2af0b0b commit d46bbc4

File tree

7 files changed

+88
-143
lines changed

7 files changed

+88
-143
lines changed

tests/commands/install_uninstall_test.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -421,17 +421,14 @@ def test_replace_old_commit_script(tempdir_factory, store):
421421
assert NORMAL_PRE_COMMIT_RUN.match(output)
422422

423423

424-
def test_uninstall_doesnt_remove_not_our_hooks(tempdir_factory):
425-
path = git_dir(tempdir_factory)
426-
with cwd(path):
427-
mkdirp(os.path.join(path, '.git/hooks'))
428-
with io.open(os.path.join(path, '.git/hooks/pre-commit'), 'w') as f:
429-
f.write('#!/usr/bin/env bash\necho 1\n')
430-
make_executable(f.name)
424+
def test_uninstall_doesnt_remove_not_our_hooks(in_git_dir):
425+
pre_commit = in_git_dir.join('.git/hooks').ensure_dir().join('pre-commit')
426+
pre_commit.write('#!/usr/bin/env bash\necho 1\n')
427+
make_executable(pre_commit.strpath)
431428

432-
assert uninstall() == 0
429+
assert uninstall() == 0
433430

434-
assert os.path.exists(os.path.join(path, '.git/hooks/pre-commit'))
431+
assert pre_commit.exists()
435432

436433

437434
PRE_INSTALLED = re.compile(

tests/conftest.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ def in_tmpdir(tempdir_factory):
6565

6666
@pytest.fixture
6767
def in_git_dir(tmpdir):
68-
with tmpdir.as_cwd():
68+
repo = tmpdir.join('repo').ensure_dir()
69+
with repo.as_cwd():
6970
cmd_output('git', 'init')
70-
yield tmpdir
71+
yield repo
7172

7273

7374
def _make_conflict():

tests/git_test.py

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,34 @@
99
from pre_commit import git
1010
from pre_commit.error_handler import FatalError
1111
from pre_commit.util import cmd_output
12-
from testing.fixtures import git_dir
13-
from testing.util import cwd
1412

1513

16-
def test_get_root_at_root(tempdir_factory):
17-
path = git_dir(tempdir_factory)
18-
with cwd(path):
19-
assert os.path.normcase(git.get_root()) == os.path.normcase(path)
14+
def test_get_root_at_root(in_git_dir):
15+
expected = os.path.normcase(in_git_dir.strpath)
16+
assert os.path.normcase(git.get_root()) == expected
2017

2118

22-
def test_get_root_deeper(tempdir_factory):
23-
path = git_dir(tempdir_factory)
19+
def test_get_root_deeper(in_git_dir):
20+
expected = os.path.normcase(in_git_dir.strpath)
21+
with in_git_dir.join('foo').ensure_dir().as_cwd():
22+
assert os.path.normcase(git.get_root()) == expected
2423

25-
foo_path = os.path.join(path, 'foo')
26-
os.mkdir(foo_path)
27-
with cwd(foo_path):
28-
assert os.path.normcase(git.get_root()) == os.path.normcase(path)
2924

25+
def test_get_root_not_git_dir(in_tmpdir):
26+
with pytest.raises(FatalError):
27+
git.get_root()
3028

31-
def test_get_root_not_git_dir(tempdir_factory):
32-
with cwd(tempdir_factory.get()):
33-
with pytest.raises(FatalError):
34-
git.get_root()
3529

30+
def test_get_staged_files_deleted(in_git_dir):
31+
in_git_dir.join('test').ensure()
32+
cmd_output('git', 'add', 'test')
33+
cmd_output('git', 'commit', '-m', 'foo', '--allow-empty')
34+
cmd_output('git', 'rm', '--cached', 'test')
35+
assert git.get_staged_files() == []
3636

37-
def test_get_staged_files_deleted(tempdir_factory):
38-
path = git_dir(tempdir_factory)
39-
with cwd(path):
40-
open('test', 'a').close()
41-
cmd_output('git', 'add', 'test')
42-
cmd_output('git', 'commit', '-m', 'foo', '--allow-empty')
43-
cmd_output('git', 'rm', '--cached', 'test')
44-
assert git.get_staged_files() == []
4537

46-
47-
def test_is_not_in_merge_conflict(tempdir_factory):
48-
path = git_dir(tempdir_factory)
49-
with cwd(path):
50-
assert git.is_in_merge_conflict() is False
38+
def test_is_not_in_merge_conflict(in_git_dir):
39+
assert git.is_in_merge_conflict() is False
5140

5241

5342
def test_is_in_merge_conflict(in_merge_conflict):
@@ -114,11 +103,10 @@ def test_parse_merge_msg_for_conflicts(input, expected_output):
114103
assert ret == expected_output
115104

116105

117-
def test_get_changed_files(in_tmpdir):
118-
cmd_output('git', 'init', '.')
106+
def test_get_changed_files(in_git_dir):
119107
cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit')
120-
open('a.txt', 'a').close()
121-
open('b.txt', 'a').close()
108+
in_git_dir.join('a.txt').ensure()
109+
in_git_dir.join('b.txt').ensure()
122110
cmd_output('git', 'add', '.')
123111
cmd_output('git', 'commit', '-m', 'add some files')
124112
files = git.get_changed_files('HEAD', 'HEAD^')
@@ -143,15 +131,12 @@ def test_zsplit(s, expected):
143131

144132

145133
@pytest.fixture
146-
def non_ascii_repo(tmpdir):
147-
repo = tmpdir.join('repo').ensure_dir()
148-
with repo.as_cwd():
149-
cmd_output('git', 'init', '.')
150-
cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit')
151-
repo.join('интервью').ensure()
152-
cmd_output('git', 'add', '.')
153-
cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit')
154-
yield repo
134+
def non_ascii_repo(in_git_dir):
135+
cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit')
136+
in_git_dir.join('интервью').ensure()
137+
cmd_output('git', 'add', '.')
138+
cmd_output('git', 'commit', '--allow-empty', '-m', 'initial commit')
139+
yield in_git_dir
155140

156141

157142
def test_all_files_non_ascii(non_ascii_repo):

tests/meta_hooks/check_hooks_apply_test.py

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
from pre_commit.meta_hooks import check_hooks_apply
22
from testing.fixtures import add_config_to_repo
3-
from testing.fixtures import git_dir
4-
from testing.util import cwd
53

64

7-
def test_hook_excludes_everything(capsys, tempdir_factory, mock_store_dir):
5+
def test_hook_excludes_everything(capsys, in_git_dir, mock_store_dir):
86
config = {
97
'repos': [
108
{
@@ -19,17 +17,15 @@ def test_hook_excludes_everything(capsys, tempdir_factory, mock_store_dir):
1917
],
2018
}
2119

22-
repo = git_dir(tempdir_factory)
23-
add_config_to_repo(repo, config)
20+
add_config_to_repo(in_git_dir.strpath, config)
2421

25-
with cwd(repo):
26-
assert check_hooks_apply.main(()) == 1
22+
assert check_hooks_apply.main(()) == 1
2723

2824
out, _ = capsys.readouterr()
2925
assert 'check-useless-excludes does not apply to this repository' in out
3026

3127

32-
def test_hook_includes_nothing(capsys, tempdir_factory, mock_store_dir):
28+
def test_hook_includes_nothing(capsys, in_git_dir, mock_store_dir):
3329
config = {
3430
'repos': [
3531
{
@@ -44,17 +40,15 @@ def test_hook_includes_nothing(capsys, tempdir_factory, mock_store_dir):
4440
],
4541
}
4642

47-
repo = git_dir(tempdir_factory)
48-
add_config_to_repo(repo, config)
43+
add_config_to_repo(in_git_dir.strpath, config)
4944

50-
with cwd(repo):
51-
assert check_hooks_apply.main(()) == 1
45+
assert check_hooks_apply.main(()) == 1
5246

5347
out, _ = capsys.readouterr()
5448
assert 'check-useless-excludes does not apply to this repository' in out
5549

5650

57-
def test_hook_types_not_matched(capsys, tempdir_factory, mock_store_dir):
51+
def test_hook_types_not_matched(capsys, in_git_dir, mock_store_dir):
5852
config = {
5953
'repos': [
6054
{
@@ -69,19 +63,15 @@ def test_hook_types_not_matched(capsys, tempdir_factory, mock_store_dir):
6963
],
7064
}
7165

72-
repo = git_dir(tempdir_factory)
73-
add_config_to_repo(repo, config)
66+
add_config_to_repo(in_git_dir.strpath, config)
7467

75-
with cwd(repo):
76-
assert check_hooks_apply.main(()) == 1
68+
assert check_hooks_apply.main(()) == 1
7769

7870
out, _ = capsys.readouterr()
7971
assert 'check-useless-excludes does not apply to this repository' in out
8072

8173

82-
def test_hook_types_excludes_everything(
83-
capsys, tempdir_factory, mock_store_dir,
84-
):
74+
def test_hook_types_excludes_everything(capsys, in_git_dir, mock_store_dir):
8575
config = {
8676
'repos': [
8777
{
@@ -96,17 +86,15 @@ def test_hook_types_excludes_everything(
9686
],
9787
}
9888

99-
repo = git_dir(tempdir_factory)
100-
add_config_to_repo(repo, config)
89+
add_config_to_repo(in_git_dir.strpath, config)
10190

102-
with cwd(repo):
103-
assert check_hooks_apply.main(()) == 1
91+
assert check_hooks_apply.main(()) == 1
10492

10593
out, _ = capsys.readouterr()
10694
assert 'check-useless-excludes does not apply to this repository' in out
10795

10896

109-
def test_valid_exceptions(capsys, tempdir_factory, mock_store_dir):
97+
def test_valid_exceptions(capsys, in_git_dir, mock_store_dir):
11098
config = {
11199
'repos': [
112100
{
@@ -142,11 +130,9 @@ def test_valid_exceptions(capsys, tempdir_factory, mock_store_dir):
142130
],
143131
}
144132

145-
repo = git_dir(tempdir_factory)
146-
add_config_to_repo(repo, config)
133+
add_config_to_repo(in_git_dir.strpath, config)
147134

148-
with cwd(repo):
149-
assert check_hooks_apply.main(()) == 0
135+
assert check_hooks_apply.main(()) == 0
150136

151137
out, _ = capsys.readouterr()
152138
assert out == ''

tests/meta_hooks/check_useless_excludes_test.py

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
from pre_commit.meta_hooks import check_useless_excludes
22
from testing.fixtures import add_config_to_repo
3-
from testing.fixtures import git_dir
4-
from testing.util import cwd
53

64

7-
def test_useless_exclude_global(capsys, tempdir_factory):
5+
def test_useless_exclude_global(capsys, in_git_dir):
86
config = {
97
'exclude': 'foo',
108
'repos': [
@@ -15,18 +13,16 @@ def test_useless_exclude_global(capsys, tempdir_factory):
1513
],
1614
}
1715

18-
repo = git_dir(tempdir_factory)
19-
add_config_to_repo(repo, config)
16+
add_config_to_repo(in_git_dir.strpath, config)
2017

21-
with cwd(repo):
22-
assert check_useless_excludes.main(()) == 1
18+
assert check_useless_excludes.main(()) == 1
2319

2420
out, _ = capsys.readouterr()
2521
out = out.strip()
2622
assert "The global exclude pattern 'foo' does not match any files" == out
2723

2824

29-
def test_useless_exclude_for_hook(capsys, tempdir_factory):
25+
def test_useless_exclude_for_hook(capsys, in_git_dir):
3026
config = {
3127
'repos': [
3228
{
@@ -36,11 +32,9 @@ def test_useless_exclude_for_hook(capsys, tempdir_factory):
3632
],
3733
}
3834

39-
repo = git_dir(tempdir_factory)
40-
add_config_to_repo(repo, config)
35+
add_config_to_repo(in_git_dir.strpath, config)
4136

42-
with cwd(repo):
43-
assert check_useless_excludes.main(()) == 1
37+
assert check_useless_excludes.main(()) == 1
4438

4539
out, _ = capsys.readouterr()
4640
out = out.strip()
@@ -51,7 +45,7 @@ def test_useless_exclude_for_hook(capsys, tempdir_factory):
5145
assert expected == out
5246

5347

54-
def test_useless_exclude_with_types_filter(capsys, tempdir_factory):
48+
def test_useless_exclude_with_types_filter(capsys, in_git_dir):
5549
config = {
5650
'repos': [
5751
{
@@ -67,11 +61,9 @@ def test_useless_exclude_with_types_filter(capsys, tempdir_factory):
6761
],
6862
}
6963

70-
repo = git_dir(tempdir_factory)
71-
add_config_to_repo(repo, config)
64+
add_config_to_repo(in_git_dir.strpath, config)
7265

73-
with cwd(repo):
74-
assert check_useless_excludes.main(()) == 1
66+
assert check_useless_excludes.main(()) == 1
7567

7668
out, _ = capsys.readouterr()
7769
out = out.strip()
@@ -82,7 +74,7 @@ def test_useless_exclude_with_types_filter(capsys, tempdir_factory):
8274
assert expected == out
8375

8476

85-
def test_no_excludes(capsys, tempdir_factory):
77+
def test_no_excludes(capsys, in_git_dir):
8678
config = {
8779
'repos': [
8880
{
@@ -92,17 +84,15 @@ def test_no_excludes(capsys, tempdir_factory):
9284
],
9385
}
9486

95-
repo = git_dir(tempdir_factory)
96-
add_config_to_repo(repo, config)
87+
add_config_to_repo(in_git_dir.strpath, config)
9788

98-
with cwd(repo):
99-
assert check_useless_excludes.main(()) == 0
89+
assert check_useless_excludes.main(()) == 0
10090

10191
out, _ = capsys.readouterr()
10292
assert out == ''
10393

10494

105-
def test_valid_exclude(capsys, tempdir_factory):
95+
def test_valid_exclude(capsys, in_git_dir):
10696
config = {
10797
'repos': [
10898
{
@@ -117,11 +107,9 @@ def test_valid_exclude(capsys, tempdir_factory):
117107
],
118108
}
119109

120-
repo = git_dir(tempdir_factory)
121-
add_config_to_repo(repo, config)
110+
add_config_to_repo(in_git_dir.strpath, config)
122111

123-
with cwd(repo):
124-
assert check_useless_excludes.main(()) == 0
112+
assert check_useless_excludes.main(()) == 0
125113

126114
out, _ = capsys.readouterr()
127115
assert out == ''

0 commit comments

Comments
 (0)