88import stat
99from plumbum import local
1010
11+ import pre_commit .constants as C
1112from pre_commit import git
1213from pre_commit .clientlib .validate_config import CONFIG_JSON_SCHEMA
1314from pre_commit .clientlib .validate_config import validate_config_extra
15+ from pre_commit .commands import autoupdate
1416from pre_commit .commands import install
1517from pre_commit .commands import RepositoryCannotBeUpdatedError
1618from pre_commit .commands import uninstall
1719from pre_commit .commands import _update_repository
1820from pre_commit .ordereddict import OrderedDict
1921from pre_commit .runner import Runner
22+ from pre_commit .yaml_extensions import ordered_dump
2023from testing .auto_namedtuple import auto_namedtuple
2124from 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
7492def 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
97131def 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):
114154def 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
0 commit comments