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

Skip to content

Commit 26d563e

Browse files
committed
Autoupdate works. Closes #44.
1 parent 49da1ba commit 26d563e

5 files changed

Lines changed: 104 additions & 7 deletions

File tree

pre_commit/commands.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
import stat
77
from plumbum import local
88

9+
import pre_commit.constants as C
10+
from pre_commit.clientlib.validate_config import load_config
911
from pre_commit.ordereddict import OrderedDict
1012
from pre_commit.repository import Repository
13+
from pre_commit.yaml_extensions import ordered_dump
14+
from pre_commit.yaml_extensions import ordered_load
1115

1216

1317
def install(runner):
@@ -75,4 +79,41 @@ def _update_repository(repo_config):
7579

7680
def autoupdate(runner):
7781
"""Auto-update the pre-commit config to the latest versions of repos."""
78-
pass
82+
retv = 0
83+
output_configs = []
84+
changed = False
85+
86+
input_configs = load_config(
87+
runner.config_file_path,
88+
load_strategy=ordered_load,
89+
)
90+
91+
for repo_config in input_configs:
92+
print('Updating {0}...'.format(repo_config['repo']), end='')
93+
try:
94+
new_repo_config = _update_repository(repo_config)
95+
except RepositoryCannotBeUpdatedError as e:
96+
print(e.args[0])
97+
output_configs.append(repo_config)
98+
retv = 1
99+
continue
100+
101+
if new_repo_config['sha'] != repo_config['sha']:
102+
changed = True
103+
print(
104+
'updating {0} -> {1}.'.format(
105+
repo_config['sha'], new_repo_config['sha'],
106+
)
107+
)
108+
output_configs.append(new_repo_config)
109+
else:
110+
print('already up to date.')
111+
output_configs.append(repo_config)
112+
113+
if changed:
114+
with open(runner.config_file_path, 'w') as config_file:
115+
config_file.write(
116+
ordered_dump(output_configs, **C.YAML_DUMP_KWARGS)
117+
)
118+
119+
return retv

pre_commit/constants.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@
1010
'ruby',
1111
'node',
1212
])
13+
14+
15+
YAML_DUMP_KWARGS = {
16+
'default_flow_style': False,
17+
'indent': 4,
18+
}

pre_commit/yaml_extensions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def constructor(loader, node):
1818

1919

2020
def ordered_dump(s, **kwargs):
21-
class OrderedDumper(yaml.dumper.Dumper): pass
21+
class OrderedDumper(yaml.dumper.SafeDumper): pass
2222
def dict_representer(dumper, data):
2323
return dumper.represent_mapping(
2424
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,

tests/commands_test.py

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88
import stat
99
from plumbum import local
1010

11+
import pre_commit.constants as C
1112
from pre_commit import git
1213
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
1314
from pre_commit.clientlib.validate_config import validate_config_extra
15+
from pre_commit.commands import autoupdate
1416
from pre_commit.commands import install
1517
from pre_commit.commands import RepositoryCannotBeUpdatedError
1618
from pre_commit.commands import uninstall
1719
from pre_commit.commands import _update_repository
1820
from pre_commit.ordereddict import OrderedDict
1921
from pre_commit.runner import Runner
22+
from pre_commit.yaml_extensions import ordered_dump
2023
from testing.auto_namedtuple import auto_namedtuple
2124
from testing.util import get_resource_path
2225

@@ -54,10 +57,16 @@ def up_to_date_repo(python_hooks_repo):
5457
config = OrderedDict((
5558
('repo', python_hooks_repo),
5659
('sha', git.get_head_sha(python_hooks_repo)),
57-
('hooks', [{'id': 'foo', 'files': ''}]),
60+
('hooks', [OrderedDict((('id', 'foo'), ('files', '')))]),
5861
))
5962
jsonschema.validate([config], CONFIG_JSON_SCHEMA)
6063
validate_config_extra([config])
64+
65+
with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj:
66+
file_obj.write(
67+
ordered_dump([config], **C.YAML_DUMP_KWARGS)
68+
)
69+
6170
yield auto_namedtuple(
6271
repo_config=config,
6372
python_hooks_repo=python_hooks_repo,
@@ -70,17 +79,32 @@ def test_up_to_date_repo(up_to_date_repo):
7079
assert ret['sha'] == input_sha
7180

7281

82+
def test_autoupdate_up_to_date_repo(up_to_date_repo):
83+
before = open(C.CONFIG_FILE).read()
84+
runner = Runner(up_to_date_repo.python_hooks_repo)
85+
ret = autoupdate(runner)
86+
after = open(C.CONFIG_FILE).read()
87+
assert ret == 0
88+
assert before == after
89+
90+
7391
@pytest.yield_fixture
7492
def out_of_date_repo(python_hooks_repo):
7593
config = OrderedDict((
7694
('repo', python_hooks_repo),
7795
('sha', git.get_head_sha(python_hooks_repo)),
78-
('hooks', [{'id': 'foo', 'files': ''}]),
96+
('hooks', [OrderedDict((('id', 'foo'), ('files', '')))]),
7997
))
8098
jsonschema.validate([config], CONFIG_JSON_SCHEMA)
8199
validate_config_extra([config])
82100
local['git']['commit', '--allow-empty', '-m', 'foo']()
83101
head_sha = git.get_head_sha(python_hooks_repo)
102+
103+
with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj:
104+
file_obj.write(
105+
ordered_dump([config], **C.YAML_DUMP_KWARGS)
106+
)
107+
84108
yield auto_namedtuple(
85109
repo_config=config,
86110
head_sha=head_sha,
@@ -93,18 +117,34 @@ def test_out_of_date_repo(out_of_date_repo):
93117
assert ret['sha'] == out_of_date_repo.head_sha
94118

95119

120+
def test_autoupdate_out_of_date_repo(out_of_date_repo):
121+
before = open(C.CONFIG_FILE).read()
122+
runner = Runner(out_of_date_repo.python_hooks_repo)
123+
ret = autoupdate(runner)
124+
after = open(C.CONFIG_FILE).read()
125+
assert ret == 0
126+
assert before != after
127+
assert out_of_date_repo.head_sha in after
128+
129+
96130
@pytest.yield_fixture
97131
def hook_disappearing_repo(python_hooks_repo):
98132
config = OrderedDict((
99133
('repo', python_hooks_repo),
100134
('sha', git.get_head_sha(python_hooks_repo)),
101-
('hooks', [{'id': 'foo', 'files': ''}]),
135+
('hooks', [OrderedDict((('id', 'foo'), ('files', '')))]),
102136
))
103137
jsonschema.validate([config], CONFIG_JSON_SCHEMA)
104138
validate_config_extra([config])
105-
shutil.copy(get_resource_path('manifest_without_foo.yaml'), 'manifest.yaml')
139+
shutil.copy(get_resource_path('manifest_without_foo.yaml'), C.MANIFEST_FILE)
106140
local['git']['add', '.']()
107141
local['git']['commit', '-m', 'Remove foo']()
142+
143+
with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj:
144+
file_obj.write(
145+
ordered_dump([config], **C.YAML_DUMP_KWARGS)
146+
)
147+
108148
yield auto_namedtuple(
109149
repo_config=config,
110150
python_hooks_repo=python_hooks_repo,
@@ -114,3 +154,12 @@ def hook_disappearing_repo(python_hooks_repo):
114154
def test_hook_disppearing_repo_raises(hook_disappearing_repo):
115155
with pytest.raises(RepositoryCannotBeUpdatedError):
116156
_update_repository(hook_disappearing_repo.repo_config)
157+
158+
159+
def test_autoupdate_hook_disappearing_repo(hook_disappearing_repo):
160+
before = open(C.CONFIG_FILE).read()
161+
runner = Runner(hook_disappearing_repo.python_hooks_repo)
162+
ret = autoupdate(runner)
163+
after = open(C.CONFIG_FILE).read()
164+
assert ret == 1
165+
assert before == after

tests/yaml_extensions_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
import pre_commit.constants as C
23
from pre_commit.ordereddict import OrderedDict
34
from pre_commit.yaml_extensions import ordered_dump
45
from pre_commit.yaml_extensions import ordered_load
@@ -25,7 +26,7 @@ def test_ordered_dump():
2526
OrderedDict(
2627
(('a', 'herp'), ('c', 'derp'), ('b', 'harp'), ('d', 'darp'))
2728
),
28-
default_flow_style=False,
29+
**C.YAML_DUMP_KWARGS
2930
)
3031
assert ret == (
3132
'a: herp\n'

0 commit comments

Comments
 (0)