Skip to content

Commit 176371b

Browse files
committed
Autoupdate roundtrips. Resolves #210
1 parent 1397445 commit 176371b

11 files changed

Lines changed: 83 additions & 99 deletions

File tree

‎pre_commit/clientlib/validate_base.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import jsonschema
1010
import jsonschema.exceptions
1111
import pkg_resources
12-
import yaml
1312

1413
from pre_commit.jsonschema_extensions import apply_defaults
14+
from pre_commit.util import yaml_load
1515

1616

1717
def is_regex_valid(regex):
@@ -36,14 +36,14 @@ def get_validator(
3636
the object read from the file. The function should either raise
3737
exception_type on failure.
3838
"""
39-
def validate(filename, load_strategy=yaml.load):
39+
def validate(filename):
4040
if not os.path.exists(filename):
4141
raise exception_type('File {0} does not exist'.format(filename))
4242

4343
file_contents = open(filename, 'r').read()
4444

4545
try:
46-
obj = load_strategy(file_contents)
46+
obj = yaml_load(file_contents)
4747
except Exception as e:
4848
raise exception_type(
4949
'Invalid yaml: {0}\n{1}'.format(os.path.relpath(filename), e),

‎pre_commit/commands/autoupdate.py‎

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@
33

44
import sys
55

6-
from aspy.yaml import ordered_dump
7-
from aspy.yaml import ordered_load
8-
9-
import pre_commit.constants as C
106
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
117
from pre_commit.clientlib.validate_config import is_local_hooks
128
from pre_commit.clientlib.validate_config import load_config
139
from pre_commit.jsonschema_extensions import remove_defaults
14-
from pre_commit.ordereddict import OrderedDict
1510
from pre_commit.repository import Repository
1611
from pre_commit.util import cmd_output
1712
from pre_commit.util import cwd
13+
from pre_commit.util import yaml_dump
1814

1915

2016
class RepositoryCannotBeUpdatedError(RuntimeError):
@@ -37,10 +33,10 @@ def _update_repository(repo_config, runner):
3733

3834
# Don't bother trying to update if our sha is the same
3935
if head_sha == repo_config['sha']:
40-
return repo_config
36+
return head_sha
4137

42-
# Construct a new config with the head sha
43-
new_config = OrderedDict(repo_config)
38+
# Modify the sha to be the head sha
39+
new_config = dict(repo_config)
4440
new_config['sha'] = head_sha
4541
new_repo = Repository.create(new_config, runner.store)
4642

@@ -53,53 +49,40 @@ def _update_repository(repo_config, runner):
5349
'{0}'.format(', '.join(sorted(hooks_missing)))
5450
)
5551

56-
return new_config
52+
return head_sha
5753

5854

5955
def autoupdate(runner):
6056
"""Auto-update the pre-commit config to the latest versions of repos."""
6157
retv = 0
62-
output_configs = []
6358
changed = False
6459

65-
input_configs = load_config(
66-
runner.config_file_path,
67-
load_strategy=ordered_load,
68-
)
60+
configs = load_config(runner.config_file_path)
6961

70-
for repo_config in input_configs:
62+
for repo_config in configs:
7163
if is_local_hooks(repo_config):
72-
output_configs.append(repo_config)
7364
continue
7465
sys.stdout.write('Updating {0}...'.format(repo_config['repo']))
7566
sys.stdout.flush()
67+
original_sha = repo_config['sha']
7668
try:
77-
new_repo_config = _update_repository(repo_config, runner)
69+
new_sha = _update_repository(repo_config, runner)
7870
except RepositoryCannotBeUpdatedError as error:
7971
print(error.args[0])
80-
output_configs.append(repo_config)
8172
retv = 1
8273
continue
8374

84-
if new_repo_config['sha'] != repo_config['sha']:
75+
if new_sha != original_sha:
8576
changed = True
86-
print(
87-
'updating {0} -> {1}.'.format(
88-
repo_config['sha'], new_repo_config['sha'],
89-
)
90-
)
91-
output_configs.append(new_repo_config)
77+
print('updating {0} -> {1}.'.format(original_sha, new_sha))
78+
repo_config['sha'] = new_sha
9279
else:
9380
print('already up to date.')
94-
output_configs.append(repo_config)
9581

9682
if changed:
9783
with open(runner.config_file_path, 'w') as config_file:
9884
config_file.write(
99-
ordered_dump(
100-
remove_defaults(output_configs, CONFIG_JSON_SCHEMA),
101-
**C.YAML_DUMP_KWARGS
102-
)
85+
yaml_dump(remove_defaults(configs, CONFIG_JSON_SCHEMA))
10386
)
10487

10588
return retv

‎pre_commit/constants.py‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,3 @@
44
CONFIG_FILE = '.pre-commit-config.yaml'
55

66
MANIFEST_FILE = 'hooks.yaml'
7-
8-
YAML_DUMP_KWARGS = {
9-
'default_flow_style': False,
10-
# Use unicode
11-
'encoding': None,
12-
'indent': 4,
13-
}

‎pre_commit/ordereddict.py‎

Lines changed: 0 additions & 7 deletions
This file was deleted.

‎pre_commit/util.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import tempfile
1313

1414
import pkg_resources
15+
import ruamel.yaml
1516

1617
from pre_commit import five
1718

@@ -210,3 +211,11 @@ def handle_remove_readonly(func, path, exc): # pragma: no cover (windows)
210211
else:
211212
raise
212213
shutil.rmtree(path, ignore_errors=False, onerror=handle_remove_readonly)
214+
215+
216+
yaml_load = functools.partial(
217+
ruamel.yaml.load, Loader=ruamel.yaml.RoundTripLoader,
218+
)
219+
yaml_dump = functools.partial(
220+
ruamel.yaml.dump, Dumper=ruamel.yaml.RoundTripDumper, encoding=None,
221+
)

‎setup.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,10 @@
4040
},
4141
install_requires=[
4242
'argparse',
43-
'aspy.yaml',
4443
'cached-property',
4544
'jsonschema',
4645
'nodeenv>=0.11.1',
47-
'ordereddict',
48-
'pyyaml',
46+
'ruamel.yaml',
4947
'virtualenv',
5048
],
5149
entry_points={

‎testing/fixtures.py‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55
import io
66
import os.path
77

8-
from aspy.yaml import ordered_dump
9-
from aspy.yaml import ordered_load
8+
import ruamel.yaml
9+
from ruamel.yaml.compat import ordereddict
1010

1111
import pre_commit.constants as C
1212
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
1313
from pre_commit.clientlib.validate_config import validate_config_extra
1414
from pre_commit.clientlib.validate_manifest import load_manifest
1515
from pre_commit.jsonschema_extensions import apply_defaults
16-
from pre_commit.ordereddict import OrderedDict
1716
from pre_commit.util import cmd_output
1817
from pre_commit.util import cwd
18+
from pre_commit.util import yaml_dump
19+
from pre_commit.util import yaml_load
1920
from testing.util import copy_tree_to_path
2021
from testing.util import get_head_sha
2122
from testing.util import get_resource_path
@@ -41,10 +42,10 @@ def make_repo(tempdir_factory, repo_source):
4142
def modify_manifest(path):
4243
"""Modify the manifest yielded by this context to write to hooks.yaml."""
4344
manifest_path = os.path.join(path, C.MANIFEST_FILE)
44-
manifest = ordered_load(io.open(manifest_path).read())
45+
manifest = yaml_load(io.open(manifest_path).read())
4546
yield manifest
4647
with io.open(manifest_path, 'w') as manifest_file:
47-
manifest_file.write(ordered_dump(manifest, **C.YAML_DUMP_KWARGS))
48+
manifest_file.write(yaml_dump(manifest))
4849
cmd_output('git', 'commit', '-am', 'update hooks.yaml', cwd=path)
4950

5051

@@ -54,18 +55,18 @@ def modify_config(path='.', commit=True):
5455
.pre-commit-config.yaml
5556
"""
5657
config_path = os.path.join(path, C.CONFIG_FILE)
57-
config = ordered_load(io.open(config_path).read())
58+
config = yaml_load(io.open(config_path).read())
5859
yield config
5960
with io.open(config_path, 'w', encoding='UTF-8') as config_file:
60-
config_file.write(ordered_dump(config, **C.YAML_DUMP_KWARGS))
61+
config_file.write(yaml_dump(config))
6162
if commit:
6263
cmd_output('git', 'commit', '-am', 'update config', cwd=path)
6364

6465

6566
def config_with_local_hooks():
66-
return OrderedDict((
67+
return ordereddict((
6768
('repo', 'local'),
68-
('hooks', [OrderedDict((
69+
('hooks', [ordereddict((
6970
('id', 'do_not_commit'),
7071
('name', 'Block if "DO NOT COMMIT" is found'),
7172
('entry', 'DO NOT COMMIT'),
@@ -77,13 +78,10 @@ def config_with_local_hooks():
7778

7879
def make_config_from_repo(repo_path, sha=None, hooks=None, check=True):
7980
manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE))
80-
config = OrderedDict((
81-
('repo', repo_path),
81+
config = ordereddict((
8282
('sha', sha or get_head_sha(repo_path)),
83-
(
84-
'hooks',
85-
hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest],
86-
),
83+
('repo', repo_path),
84+
('hooks', hooks or [{'id': hook['id']} for hook in manifest]),
8785
))
8886

8987
if check:
@@ -96,10 +94,14 @@ def make_config_from_repo(repo_path, sha=None, hooks=None, check=True):
9694

9795
def write_config(directory, config):
9896
if type(config) is not list:
99-
assert type(config) is OrderedDict
97+
assert type(config) is ordereddict
10098
config = [config]
10199
with io.open(os.path.join(directory, C.CONFIG_FILE), 'w') as config_file:
102-
config_file.write(ordered_dump(config, **C.YAML_DUMP_KWARGS))
100+
config_file.write(ruamel.yaml.safe_dump(
101+
config,
102+
# Use unicode
103+
encoding=None,
104+
))
103105

104106

105107
def add_config_to_repo(git_path, config):

‎tests/clientlib/validate_base_test.py‎

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

33
import pytest
4-
from aspy.yaml import ordered_load
54

65
from pre_commit.clientlib.validate_base import get_validator
7-
from pre_commit.ordereddict import OrderedDict
86
from testing.util import get_resource_path
97

108

@@ -63,11 +61,3 @@ def test_raises_when_additional_validation_fails(additional_validator):
6361
def test_returns_object_after_validating(noop_validator):
6462
ret = noop_validator(get_resource_path('array_yaml_file.yaml'))
6563
assert ret == ['foo', 'bar']
66-
67-
68-
def test_load_strategy(noop_validator):
69-
ret = noop_validator(
70-
get_resource_path('ordering_data_test.yaml'),
71-
load_strategy=ordered_load,
72-
)
73-
assert type(ret) is OrderedDict

‎tests/commands/autoupdate_test.py‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import unicode_literals
22

3+
import io
34
import shutil
45

56
import pytest
@@ -9,7 +10,6 @@
910
from pre_commit.commands.autoupdate import _update_repository
1011
from pre_commit.commands.autoupdate import autoupdate
1112
from pre_commit.commands.autoupdate import RepositoryCannotBeUpdatedError
12-
from pre_commit.ordereddict import OrderedDict
1313
from pre_commit.runner import Runner
1414
from pre_commit.util import cmd_output
1515
from pre_commit.util import cwd
@@ -33,7 +33,7 @@ def test_up_to_date_repo(up_to_date_repo, runner_with_mocked_store):
3333
config = make_config_from_repo(up_to_date_repo)
3434
input_sha = config['sha']
3535
ret = _update_repository(config, runner_with_mocked_store)
36-
assert ret['sha'] == input_sha
36+
assert ret == input_sha
3737

3838

3939
def test_autoupdate_up_to_date_repo(
@@ -72,30 +72,45 @@ def test_out_of_date_repo(out_of_date_repo, runner_with_mocked_store):
7272
out_of_date_repo.path, sha=out_of_date_repo.original_sha,
7373
)
7474
ret = _update_repository(config, runner_with_mocked_store)
75-
assert ret['sha'] != out_of_date_repo.original_sha
76-
assert ret['sha'] == out_of_date_repo.head_sha
75+
assert ret != out_of_date_repo.original_sha
76+
assert ret == out_of_date_repo.head_sha
7777

7878

7979
def test_autoupdate_out_of_date_repo(
80-
out_of_date_repo, in_tmpdir, mock_out_store_directory
80+
out_of_date_repo, in_tmpdir, mock_out_store_directory,
8181
):
8282
# Write out the config
8383
config = make_config_from_repo(
8484
out_of_date_repo.path, sha=out_of_date_repo.original_sha, check=False,
8585
)
8686
write_config('.', config)
8787

88-
before = open(C.CONFIG_FILE).read()
88+
before = io.open(C.CONFIG_FILE).read()
8989
runner = Runner('.')
9090
ret = autoupdate(runner)
91-
after = open(C.CONFIG_FILE).read()
91+
after = io.open(C.CONFIG_FILE).read()
9292
assert ret == 0
9393
assert before != after
9494
# Make sure we don't add defaults
9595
assert 'exclude' not in after
9696
assert out_of_date_repo.head_sha in after
9797

9898

99+
def test_autoupdate_preserves_comments(
100+
out_of_date_repo, in_tmpdir, mock_out_store_directory,
101+
):
102+
config = make_config_from_repo(
103+
out_of_date_repo.path, sha=out_of_date_repo.original_sha, check=False,
104+
)
105+
write_config('.', config)
106+
with io.open(C.CONFIG_FILE, 'a') as config_file:
107+
config_file.write("# I'm a comment!\n")
108+
runner = Runner('.')
109+
autoupdate(runner)
110+
after = io.open(C.CONFIG_FILE).read()
111+
assert after.endswith("# I'm a comment!\n")
112+
113+
99114
@pytest.yield_fixture
100115
def hook_disappearing_repo(tempdir_factory):
101116
path = make_repo(tempdir_factory, 'python_hooks_repo')
@@ -118,7 +133,7 @@ def test_hook_disppearing_repo_raises(
118133
config = make_config_from_repo(
119134
hook_disappearing_repo.path,
120135
sha=hook_disappearing_repo.original_sha,
121-
hooks=[OrderedDict((('id', 'foo'),))],
136+
hooks=[{'id': 'foo'}],
122137
)
123138
with pytest.raises(RepositoryCannotBeUpdatedError):
124139
_update_repository(config, runner_with_mocked_store)
@@ -130,7 +145,7 @@ def test_autoupdate_hook_disappearing_repo(
130145
config = make_config_from_repo(
131146
hook_disappearing_repo.path,
132147
sha=hook_disappearing_repo.original_sha,
133-
hooks=[OrderedDict((('id', 'foo'),))],
148+
hooks=[{'id': 'foo'}],
134149
check=False,
135150
)
136151
write_config('.', config)

0 commit comments

Comments
 (0)