Skip to content

Commit f243e93

Browse files
committed
Autoupdate roundtrips. Resolves #210
1 parent 9a18f6a commit f243e93

9 files changed

Lines changed: 79 additions & 82 deletions

File tree

‎pre_commit/clientlib/validate_base.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import jsonschema
1010
import jsonschema.exceptions
11-
import yaml
11+
import ruamel.yaml
1212

1313
from pre_commit.jsonschema_extensions import apply_defaults
1414

@@ -35,7 +35,7 @@ def get_validator(
3535
the object read from the file. The function should either raise
3636
exception_type on failure.
3737
"""
38-
def validate(filename, load_strategy=yaml.load):
38+
def validate(filename, load_strategy=ruamel.yaml.load):
3939
if not os.path.exists(filename):
4040
raise exception_type('File {0} does not exist'.format(filename))
4141

‎pre_commit/commands/autoupdate.py‎

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
from __future__ import print_function
22
from __future__ import unicode_literals
33

4+
import functools
45
import sys
56

6-
from aspy.yaml import ordered_dump
7-
from aspy.yaml import ordered_load
7+
import ruamel.yaml
88

9-
import pre_commit.constants as C
109
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
1110
from pre_commit.clientlib.validate_config import load_config
1211
from pre_commit.jsonschema_extensions import remove_defaults
13-
from pre_commit.ordereddict import OrderedDict
1412
from pre_commit.repository import Repository
1513
from pre_commit.util import cmd_output
1614
from pre_commit.util import cwd
@@ -36,10 +34,10 @@ def _update_repository(repo_config, runner):
3634

3735
# Don't bother trying to update if our sha is the same
3836
if head_sha == repo_config['sha']:
39-
return repo_config
37+
return head_sha
4038

41-
# Construct a new config with the head sha
42-
new_config = OrderedDict(repo_config)
39+
# Modify the sha to be the head sha
40+
new_config = dict(repo_config)
4341
new_config['sha'] = head_sha
4442
new_repo = Repository.create(new_config, runner.store)
4543

@@ -52,49 +50,43 @@ def _update_repository(repo_config, runner):
5250
'{0}'.format(', '.join(sorted(hooks_missing)))
5351
)
5452

55-
return new_config
53+
return head_sha
5654

5755

5856
def autoupdate(runner):
5957
"""Auto-update the pre-commit config to the latest versions of repos."""
6058
retv = 0
61-
output_configs = []
6259
changed = False
6360

64-
input_configs = load_config(
61+
configs = load_config(
6562
runner.config_file_path,
66-
load_strategy=ordered_load,
63+
load_strategy=functools.partial(
64+
ruamel.yaml.load, Loader=ruamel.yaml.RoundTripLoader,
65+
),
6766
)
6867

69-
for repo_config in input_configs:
68+
for repo_config in configs:
7069
sys.stdout.write('Updating {0}...'.format(repo_config['repo']))
7170
sys.stdout.flush()
71+
original_sha = repo_config['sha']
7272
try:
73-
new_repo_config = _update_repository(repo_config, runner)
73+
new_sha = _update_repository(repo_config, runner)
7474
except RepositoryCannotBeUpdatedError as error:
7575
print(error.args[0])
76-
output_configs.append(repo_config)
7776
retv = 1
7877
continue
7978

80-
if new_repo_config['sha'] != repo_config['sha']:
79+
if new_sha != original_sha:
8180
changed = True
82-
print(
83-
'updating {0} -> {1}.'.format(
84-
repo_config['sha'], new_repo_config['sha'],
85-
)
86-
)
87-
output_configs.append(new_repo_config)
88-
else:
89-
print('already up to date.')
90-
output_configs.append(repo_config)
81+
print('updating {0} -> {1}.'.format(original_sha, new_sha))
82+
repo_config['sha'] = new_sha
9183

9284
if changed:
9385
with open(runner.config_file_path, 'w') as config_file:
9486
config_file.write(
95-
ordered_dump(
96-
remove_defaults(output_configs, CONFIG_JSON_SCHEMA),
97-
**C.YAML_DUMP_KWARGS
87+
ruamel.yaml.dump(
88+
remove_defaults(configs, CONFIG_JSON_SCHEMA),
89+
Dumper=ruamel.yaml.RoundTripDumper,
9890
)
9991
)
10092

‎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.

‎setup.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@
3939
},
4040
install_requires=[
4141
'argparse',
42-
'aspy.yaml',
4342
'cached-property',
4443
'jsonschema',
4544
'nodeenv>=0.11.1',
46-
'ordereddict',
47-
'pyyaml',
45+
'ruamel.yaml',
4846
'simplejson',
4947
'virtualenv',
5048
],

‎testing/fixtures.py‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
import io
55
import os.path
66

7-
from aspy.yaml import ordered_dump
7+
import ruamel.yaml
88

99
import pre_commit.constants as C
1010
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
1111
from pre_commit.clientlib.validate_config import validate_config_extra
1212
from pre_commit.clientlib.validate_manifest import load_manifest
1313
from pre_commit.jsonschema_extensions import apply_defaults
14-
from pre_commit.ordereddict import OrderedDict
1514
from pre_commit.util import cmd_output
1615
from pre_commit.util import cwd
1716
from testing.util import copy_tree_to_path
@@ -37,14 +36,11 @@ def make_repo(tmpdir_factory, repo_source):
3736

3837
def make_config_from_repo(repo_path, sha=None, hooks=None, check=True):
3938
manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE))
40-
config = OrderedDict((
41-
('repo', repo_path),
42-
('sha', sha or get_head_sha(repo_path)),
43-
(
44-
'hooks',
45-
hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest],
46-
),
47-
))
39+
config = {
40+
'sha': sha or get_head_sha(repo_path),
41+
'repo': repo_path,
42+
'hooks': hooks or [{'id': hook['id']} for hook in manifest],
43+
}
4844

4945
if check:
5046
wrapped_config = apply_defaults([config], CONFIG_JSON_SCHEMA)
@@ -55,9 +51,14 @@ def make_config_from_repo(repo_path, sha=None, hooks=None, check=True):
5551

5652

5753
def write_config(directory, config):
58-
assert type(config) is OrderedDict
5954
with io.open(os.path.join(directory, C.CONFIG_FILE), 'w') as config_file:
60-
config_file.write(ordered_dump([config], **C.YAML_DUMP_KWARGS))
55+
config_file.write(ruamel.yaml.safe_dump(
56+
[config],
57+
default_flow_style=False,
58+
# Use unicode
59+
encoding=None,
60+
indent=4,
61+
))
6162

6263

6364
def make_consuming_repo(tmpdir_factory, repo_source):

‎tests/clientlib/validate_base_test.py‎

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

3+
import functools
4+
35
import pytest
4-
from aspy.yaml import ordered_load
6+
import ruamel.yaml
57

68
from pre_commit.clientlib.validate_base import get_validator
7-
from pre_commit.ordereddict import OrderedDict
89
from testing.util import get_resource_path
910

1011

@@ -68,6 +69,8 @@ def test_returns_object_after_validating(noop_validator):
6869
def test_load_strategy(noop_validator):
6970
ret = noop_validator(
7071
get_resource_path('ordering_data_test.yaml'),
71-
load_strategy=ordered_load,
72+
load_strategy=functools.partial(
73+
ruamel.yaml.load, Loader=ruamel.yaml.RoundTripLoader,
74+
),
7275
)
73-
assert type(ret) is OrderedDict
76+
assert type(ret) is ruamel.yaml.comments.CommentedMap

‎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
@@ -8,7 +9,6 @@
89
from pre_commit.commands.autoupdate import _update_repository
910
from pre_commit.commands.autoupdate import autoupdate
1011
from pre_commit.commands.autoupdate import RepositoryCannotBeUpdatedError
11-
from pre_commit.ordereddict import OrderedDict
1212
from pre_commit.runner import Runner
1313
from pre_commit.util import cmd_output
1414
from pre_commit.util import cwd
@@ -29,7 +29,7 @@ def test_up_to_date_repo(up_to_date_repo, runner_with_mocked_store):
2929
config = make_config_from_repo(up_to_date_repo)
3030
input_sha = config['sha']
3131
ret = _update_repository(config, runner_with_mocked_store)
32-
assert ret['sha'] == input_sha
32+
assert ret == input_sha
3333

3434

3535
def test_autoupdate_up_to_date_repo(
@@ -68,30 +68,45 @@ def test_out_of_date_repo(out_of_date_repo, runner_with_mocked_store):
6868
out_of_date_repo.path, sha=out_of_date_repo.original_sha,
6969
)
7070
ret = _update_repository(config, runner_with_mocked_store)
71-
assert ret['sha'] != out_of_date_repo.original_sha
72-
assert ret['sha'] == out_of_date_repo.head_sha
71+
assert ret != out_of_date_repo.original_sha
72+
assert ret == out_of_date_repo.head_sha
7373

7474

7575
def test_autoupdate_out_of_date_repo(
76-
out_of_date_repo, in_tmpdir, mock_out_store_directory
76+
out_of_date_repo, in_tmpdir, mock_out_store_directory,
7777
):
7878
# Write out the config
7979
config = make_config_from_repo(
8080
out_of_date_repo.path, sha=out_of_date_repo.original_sha, check=False,
8181
)
8282
write_config('.', config)
8383

84-
before = open(C.CONFIG_FILE).read()
84+
before = io.open(C.CONFIG_FILE).read()
8585
runner = Runner('.')
8686
ret = autoupdate(runner)
87-
after = open(C.CONFIG_FILE).read()
87+
after = io.open(C.CONFIG_FILE).read()
8888
assert ret == 0
8989
assert before != after
9090
# Make sure we don't add defaults
9191
assert 'exclude' not in after
9292
assert out_of_date_repo.head_sha in after
9393

9494

95+
def test_autoupdate_preserves_comments(
96+
out_of_date_repo, in_tmpdir, mock_out_store_directory,
97+
):
98+
config = make_config_from_repo(
99+
out_of_date_repo.path, sha=out_of_date_repo.original_sha, check=False,
100+
)
101+
write_config('.', config)
102+
with io.open(C.CONFIG_FILE, 'a') as config_file:
103+
config_file.write("# I'm a comment!\n")
104+
runner = Runner('.')
105+
autoupdate(runner)
106+
after = io.open(C.CONFIG_FILE).read()
107+
assert after.endswith("# I'm a comment!\n")
108+
109+
95110
@pytest.yield_fixture
96111
def hook_disappearing_repo(tmpdir_factory):
97112
path = make_repo(tmpdir_factory, 'python_hooks_repo')
@@ -114,7 +129,7 @@ def test_hook_disppearing_repo_raises(
114129
config = make_config_from_repo(
115130
hook_disappearing_repo.path,
116131
sha=hook_disappearing_repo.original_sha,
117-
hooks=[OrderedDict((('id', 'foo'),))],
132+
hooks=[{'id': 'foo'}],
118133
)
119134
with pytest.raises(RepositoryCannotBeUpdatedError):
120135
_update_repository(config, runner_with_mocked_store)
@@ -126,7 +141,7 @@ def test_autoupdate_hook_disappearing_repo(
126141
config = make_config_from_repo(
127142
hook_disappearing_repo.path,
128143
sha=hook_disappearing_repo.original_sha,
129-
hooks=[OrderedDict((('id', 'foo'),))],
144+
hooks=[{'id': 'foo'}],
130145
check=False,
131146
)
132147
write_config('.', config)

‎tests/commands/run_test.py‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
import mock
1010
import pytest
11+
import ruamel.yaml
1112

13+
import pre_commit.constants as C
1214
from pre_commit.commands.install_uninstall import install
1315
from pre_commit.commands.run import _get_skips
1416
from pre_commit.commands.run import _has_unmerged_paths
@@ -19,6 +21,7 @@
1921
from pre_commit.util import cwd
2022
from testing.auto_namedtuple import auto_namedtuple
2123
from testing.fixtures import make_consuming_repo
24+
from testing.fixtures import write_config
2225

2326

2427
@pytest.yield_fixture
@@ -260,8 +263,9 @@ def test_multiple_hooks_same_id(
260263
):
261264
with cwd(repo_with_passing_hook):
262265
# Add bash hook on there again
263-
with io.open('.pre-commit-config.yaml', 'a+') as config_file:
264-
config_file.write(' - id: bash_hook\n')
266+
config, = ruamel.yaml.load(io.open(C.CONFIG_FILE).read())
267+
config['hooks'].append({'id': 'bash_hook'})
268+
write_config('.', config)
265269
cmd_output('git', 'add', '.pre-commit-config.yaml')
266270
stage_a_file()
267271

@@ -274,11 +278,10 @@ def test_stdout_write_bug_py26(
274278
repo_with_failing_hook, mock_out_store_directory, tmpdir_factory,
275279
):
276280
with cwd(repo_with_failing_hook):
277-
# Add bash hook on there again
278-
with io.open(
279-
'.pre-commit-config.yaml', 'a+', encoding='UTF-8',
280-
) as config_file:
281-
config_file.write(' args: ["☃"]\n')
281+
# Add unicode args to bash_hook
282+
config, = ruamel.yaml.load(io.open(C.CONFIG_FILE).read())
283+
config['hooks'][0]['args'] = ['☃']
284+
write_config('.', config)
282285
cmd_output('git', 'add', '.pre-commit-config.yaml')
283286
stage_a_file()
284287

@@ -313,10 +316,9 @@ def test_lots_of_files(mock_out_store_directory, tmpdir_factory):
313316
git_path = make_consuming_repo(tmpdir_factory, 'python_hooks_repo')
314317
with cwd(git_path):
315318
# Override files so we run against them
316-
with io.open(
317-
'.pre-commit-config.yaml', 'a+',
318-
) as config_file:
319-
config_file.write(' files: ""\n')
319+
config, = ruamel.yaml.load(io.open(C.CONFIG_FILE).read())
320+
config['hooks'][0]['files'] = ''
321+
write_config('.', config)
320322

321323
# Write a crap ton of files
322324
for i in range(400):

0 commit comments

Comments
 (0)