forked from pre-commit/pre-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
138 lines (109 loc) · 3.93 KB
/
conftest.py
File metadata and controls
138 lines (109 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from __future__ import absolute_import
from __future__ import unicode_literals
import io
import logging
import os
import os.path
import mock
import pytest
from pre_commit import five
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
from pre_commit.runner import Runner
from pre_commit.store import Store
from pre_commit.util import cmd_output
from pre_commit.util import cwd
from testing.fixtures import git_dir
from testing.fixtures import make_consuming_repo
@pytest.yield_fixture
def tempdir_factory(tmpdir):
class TmpdirFactory(object):
def __init__(self):
self.tmpdir_count = 0
def get(self):
path = tmpdir.join(five.text(self.tmpdir_count)).strpath
self.tmpdir_count += 1
os.mkdir(path)
return path
yield TmpdirFactory()
@pytest.yield_fixture
def in_tmpdir(tempdir_factory):
path = tempdir_factory.get()
with cwd(path):
yield path
def _make_conflict():
cmd_output('git', 'checkout', 'origin/master', '-b', 'foo')
with io.open('conflict_file', 'w') as conflict_file:
conflict_file.write('herp\nderp\n')
cmd_output('git', 'add', 'conflict_file')
with io.open('foo_only_file', 'w') as foo_only_file:
foo_only_file.write('foo')
cmd_output('git', 'add', 'foo_only_file')
cmd_output('git', 'commit', '-m', 'conflict_file')
cmd_output('git', 'checkout', 'origin/master', '-b', 'bar')
with io.open('conflict_file', 'w') as conflict_file:
conflict_file.write('harp\nddrp\n')
cmd_output('git', 'add', 'conflict_file')
with io.open('bar_only_file', 'w') as bar_only_file:
bar_only_file.write('bar')
cmd_output('git', 'add', 'bar_only_file')
cmd_output('git', 'commit', '-m', 'conflict_file')
cmd_output('git', 'merge', 'foo', retcode=None)
@pytest.yield_fixture
def in_merge_conflict(tempdir_factory):
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
with cwd(path):
cmd_output('touch', 'dummy')
cmd_output('git', 'add', 'dummy')
cmd_output('git', 'commit', '-m', 'Add config.')
conflict_path = tempdir_factory.get()
cmd_output('git', 'clone', path, conflict_path)
with cwd(conflict_path):
_make_conflict()
yield os.path.join(conflict_path)
@pytest.yield_fixture
def in_conflicting_submodule(tempdir_factory):
git_dir_1 = git_dir(tempdir_factory)
git_dir_2 = git_dir(tempdir_factory)
with cwd(git_dir_2):
cmd_output('git', 'commit', '--allow-empty', '-m', 'init!')
with cwd(git_dir_1):
cmd_output('git', 'submodule', 'add', git_dir_2, 'sub')
with cwd(os.path.join(git_dir_1, 'sub')):
_make_conflict()
yield
@pytest.yield_fixture(scope='session', autouse=True)
def dont_write_to_home_directory():
"""pre_commit.store.Store will by default write to the home directory
We'll mock out `Store.get_default_directory` to raise invariantly so we
don't construct a `Store` object that writes to our home directory.
"""
class YouForgotToExplicitlyChooseAStoreDirectory(AssertionError):
pass
with mock.patch.object(
Store,
'get_default_directory',
side_effect=YouForgotToExplicitlyChooseAStoreDirectory,
):
yield
@pytest.yield_fixture
def mock_out_store_directory(tempdir_factory):
tmpdir = tempdir_factory.get()
with mock.patch.object(
Store,
'get_default_directory',
return_value=tmpdir,
):
yield tmpdir
@pytest.yield_fixture
def store(tempdir_factory):
yield Store(os.path.join(tempdir_factory.get(), '.pre-commit'))
@pytest.yield_fixture
def cmd_runner(tempdir_factory):
yield PrefixedCommandRunner(tempdir_factory.get())
@pytest.yield_fixture
def runner_with_mocked_store(mock_out_store_directory):
yield Runner('/')
@pytest.yield_fixture
def log_info_mock():
with mock.patch.object(logging.getLogger('pre_commit'), 'info') as mck:
yield mck