11from __future__ import unicode_literals
22
3+ import io
34import shutil
45
56import pytest
89from pre_commit .commands .autoupdate import _update_repository
910from pre_commit .commands .autoupdate import autoupdate
1011from pre_commit .commands .autoupdate import RepositoryCannotBeUpdatedError
11- from pre_commit .ordereddict import OrderedDict
1212from pre_commit .runner import Runner
1313from pre_commit .util import cmd_output
1414from 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
3535def 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
7575def 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
96111def 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 )
0 commit comments