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

Skip to content

Commit 3baefd5

Browse files
committed
Convert autoupdate_test to use new fixture functions.
1 parent 047a933 commit 3baefd5

3 files changed

Lines changed: 108 additions & 85 deletions

File tree

pre_commit/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44

55
YAML_DUMP_KWARGS = {
66
'default_flow_style': False,
7+
# Use unicode
8+
'encoding': None,
79
'indent': 4,
810
}

testing/fixtures.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
from __future__ import absolute_import
22
from __future__ import unicode_literals
33

4+
import os.path
5+
from asottile.ordereddict import OrderedDict
46
from plumbum import local
57

8+
import pre_commit.constants as C
9+
from pre_commit.clientlib.validate_manifest import load_manifest
10+
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
11+
from pre_commit.clientlib.validate_config import validate_config_extra
12+
from pre_commit.jsonschema_extensions import apply_defaults
13+
from testing.util import copy_tree_to_path
14+
from testing.util import get_head_sha
15+
from testing.util import get_resource_path
16+
617

718
git = local['git']
819

@@ -12,3 +23,31 @@ def git_dir(tmpdir_factory):
1223
with local.cwd(path):
1324
git('init')
1425
return path
26+
27+
28+
def make_hooks_repo(tmpdir_factory, repo_source):
29+
path = git_dir(tmpdir_factory)
30+
copy_tree_to_path(get_resource_path(repo_source), path)
31+
with local.cwd(path):
32+
git('add', '.')
33+
git('commit', '-m', 'Add hooks')
34+
return path
35+
36+
37+
def make_config_from_repo(repo_path, sha=None, hooks=None, check=True):
38+
manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE))
39+
config = OrderedDict((
40+
('repo', repo_path),
41+
('sha', sha or get_head_sha(repo_path)),
42+
(
43+
'hooks',
44+
hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest],
45+
),
46+
))
47+
48+
if check:
49+
wrapped_config = apply_defaults([config], CONFIG_JSON_SCHEMA)
50+
validate_config_extra(wrapped_config)
51+
return wrapped_config[0]
52+
else:
53+
return config

tests/commands/autoupdate_test.py

Lines changed: 67 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,89 @@
11
from __future__ import unicode_literals
22

3-
import os
4-
import os.path
3+
import io
54
import pytest
65
import shutil
76
from asottile.ordereddict import OrderedDict
87
from asottile.yaml import ordered_dump
98
from plumbum import local
109

1110
import pre_commit.constants as C
12-
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
13-
from pre_commit.clientlib.validate_config import validate_config_extra
1411
from pre_commit.commands.autoupdate import _update_repository
1512
from pre_commit.commands.autoupdate import autoupdate
1613
from pre_commit.commands.autoupdate import RepositoryCannotBeUpdatedError
17-
from pre_commit.jsonschema_extensions import apply_defaults
18-
from pre_commit.jsonschema_extensions import remove_defaults
1914
from pre_commit.runner import Runner
2015
from testing.auto_namedtuple import auto_namedtuple
16+
from testing.fixtures import make_config_from_repo
17+
from testing.fixtures import make_hooks_repo
2118
from testing.util import get_head_sha
2219
from testing.util import get_resource_path
2320

2421

2522
@pytest.yield_fixture
26-
def up_to_date_repo(python_hooks_repo):
27-
config = OrderedDict((
28-
('repo', python_hooks_repo),
29-
('sha', get_head_sha(python_hooks_repo)),
30-
('hooks', [OrderedDict((('id', 'foo'),))]),
31-
))
32-
wrapped_config = apply_defaults([config], CONFIG_JSON_SCHEMA)
33-
validate_config_extra(wrapped_config)
34-
config = wrapped_config[0]
35-
36-
with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj:
37-
file_obj.write(
38-
ordered_dump(
39-
remove_defaults([config], CONFIG_JSON_SCHEMA),
40-
**C.YAML_DUMP_KWARGS
41-
)
42-
)
43-
44-
yield auto_namedtuple(
45-
repo_config=config,
46-
python_hooks_repo=python_hooks_repo,
47-
)
23+
def up_to_date_repo(tmpdir_factory):
24+
yield make_hooks_repo(tmpdir_factory, 'python_hooks_repo')
4825

4926

5027
def test_up_to_date_repo(up_to_date_repo, runner_with_mocked_store):
51-
input_sha = up_to_date_repo.repo_config['sha']
52-
ret = _update_repository(
53-
up_to_date_repo.repo_config, runner_with_mocked_store,
54-
)
28+
config = make_config_from_repo(up_to_date_repo)
29+
input_sha = config['sha']
30+
ret = _update_repository(config, runner_with_mocked_store)
5531
assert ret['sha'] == input_sha
5632

5733

58-
def test_autoupdate_up_to_date_repo(up_to_date_repo, mock_out_store_directory):
34+
def test_autoupdate_up_to_date_repo(
35+
up_to_date_repo, in_tmpdir, mock_out_store_directory,
36+
):
37+
# Write out the config
38+
config = make_config_from_repo(up_to_date_repo, check=False)
39+
with io.open(C.CONFIG_FILE, 'w') as config_file:
40+
config_file.write(ordered_dump([config], **C.YAML_DUMP_KWARGS))
41+
5942
before = open(C.CONFIG_FILE).read()
6043
assert '^$' not in before
61-
runner = Runner(up_to_date_repo.python_hooks_repo)
44+
runner = Runner('.')
6245
ret = autoupdate(runner)
6346
after = open(C.CONFIG_FILE).read()
6447
assert ret == 0
6548
assert before == after
6649

6750

6851
@pytest.yield_fixture
69-
def out_of_date_repo(python_hooks_repo):
70-
config = OrderedDict((
71-
('repo', python_hooks_repo),
72-
('sha', get_head_sha(python_hooks_repo)),
73-
('hooks', [OrderedDict((('id', 'foo'), ('files', '')))]),
74-
))
75-
config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA)
76-
validate_config_extra(config_wrapped)
77-
config = config_wrapped[0]
78-
local['git']['commit', '--allow-empty', '-m', 'foo']()
79-
head_sha = get_head_sha(python_hooks_repo)
80-
81-
with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj:
82-
file_obj.write(
83-
ordered_dump([config], **C.YAML_DUMP_KWARGS)
84-
)
52+
def out_of_date_repo(tmpdir_factory):
53+
path = make_hooks_repo(tmpdir_factory, 'python_hooks_repo')
54+
original_sha = get_head_sha(path)
55+
56+
# Make a commit
57+
with local.cwd(path):
58+
local['git']['commit', '--allow-empty', '-m', 'foo']()
59+
head_sha = get_head_sha(path)
8560

8661
yield auto_namedtuple(
87-
repo_config=config,
88-
head_sha=head_sha,
89-
python_hooks_repo=python_hooks_repo,
62+
path=path, original_sha=original_sha, head_sha=head_sha,
9063
)
9164

9265

9366
def test_out_of_date_repo(out_of_date_repo, runner_with_mocked_store):
94-
ret = _update_repository(
95-
out_of_date_repo.repo_config, runner_with_mocked_store,
67+
config = make_config_from_repo(
68+
out_of_date_repo.path, sha=out_of_date_repo.original_sha,
9669
)
70+
ret = _update_repository(config, runner_with_mocked_store)
71+
assert ret['sha'] != out_of_date_repo.original_sha
9772
assert ret['sha'] == out_of_date_repo.head_sha
9873

9974

10075
def test_autoupdate_out_of_date_repo(
101-
out_of_date_repo, mock_out_store_directory
76+
out_of_date_repo, in_tmpdir, mock_out_store_directory
10277
):
78+
# Write out the config
79+
config = make_config_from_repo(
80+
out_of_date_repo.path, sha=out_of_date_repo.original_sha, check=False,
81+
)
82+
with io.open(C.CONFIG_FILE, 'w') as config_file:
83+
config_file.write(ordered_dump([config], **C.YAML_DUMP_KWARGS))
84+
10385
before = open(C.CONFIG_FILE).read()
104-
runner = Runner(out_of_date_repo.python_hooks_repo)
86+
runner = Runner('.')
10587
ret = autoupdate(runner)
10688
after = open(C.CONFIG_FILE).read()
10789
assert ret == 0
@@ -112,47 +94,47 @@ def test_autoupdate_out_of_date_repo(
11294

11395

11496
@pytest.yield_fixture
115-
def hook_disappearing_repo(python_hooks_repo):
116-
config = OrderedDict((
117-
('repo', python_hooks_repo),
118-
('sha', get_head_sha(python_hooks_repo)),
119-
('hooks', [OrderedDict((('id', 'foo'),))]),
120-
))
121-
config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA)
122-
validate_config_extra(config_wrapped)
123-
config = config_wrapped[0]
124-
shutil.copy(
125-
get_resource_path('manifest_without_foo.yaml'),
126-
C.MANIFEST_FILE,
127-
)
128-
local['git']['add', '.']()
129-
local['git']['commit', '-m', 'Remove foo']()
130-
131-
with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj:
132-
file_obj.write(
133-
ordered_dump([config], **C.YAML_DUMP_KWARGS)
97+
def hook_disappearing_repo(tmpdir_factory):
98+
path = make_hooks_repo(tmpdir_factory, 'python_hooks_repo')
99+
original_sha = get_head_sha(path)
100+
101+
with local.cwd(path):
102+
shutil.copy(
103+
get_resource_path('manifest_without_foo.yaml'),
104+
C.MANIFEST_FILE,
134105
)
106+
local['git']('add', '.')
107+
local['git']('commit', '-m', 'Remove foo')
135108

136-
yield auto_namedtuple(
137-
repo_config=config,
138-
python_hooks_repo=python_hooks_repo,
139-
)
109+
yield auto_namedtuple(path=path, original_sha=original_sha)
140110

141111

142112
def test_hook_disppearing_repo_raises(
143113
hook_disappearing_repo, runner_with_mocked_store
144114
):
115+
config = make_config_from_repo(
116+
hook_disappearing_repo.path,
117+
sha=hook_disappearing_repo.original_sha,
118+
hooks=[OrderedDict((('id', 'foo'),))],
119+
)
145120
with pytest.raises(RepositoryCannotBeUpdatedError):
146-
_update_repository(
147-
hook_disappearing_repo.repo_config, runner_with_mocked_store,
148-
)
121+
_update_repository(config, runner_with_mocked_store)
149122

150123

151124
def test_autoupdate_hook_disappearing_repo(
152-
hook_disappearing_repo, mock_out_store_directory
125+
hook_disappearing_repo, in_tmpdir, mock_out_store_directory
153126
):
127+
config = make_config_from_repo(
128+
hook_disappearing_repo.path,
129+
sha=hook_disappearing_repo.original_sha,
130+
hooks=[OrderedDict((('id', 'foo'),))],
131+
check=False,
132+
)
133+
with io.open(C.CONFIG_FILE, 'w') as config_file:
134+
config_file.write(ordered_dump([config], **C.YAML_DUMP_KWARGS))
135+
154136
before = open(C.CONFIG_FILE).read()
155-
runner = Runner(hook_disappearing_repo.python_hooks_repo)
137+
runner = Runner('.')
156138
ret = autoupdate(runner)
157139
after = open(C.CONFIG_FILE).read()
158140
assert ret == 1

0 commit comments

Comments
 (0)