11from __future__ import unicode_literals
22
3+ import io
34import shutil
45
56import pytest
910from pre_commit .commands .autoupdate import _update_repository
1011from pre_commit .commands .autoupdate import autoupdate
1112from pre_commit .commands .autoupdate import RepositoryCannotBeUpdatedError
12- from pre_commit .ordereddict import OrderedDict
1313from pre_commit .runner import Runner
1414from pre_commit .util import cmd_output
1515from 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
3939def 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
7979def 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
100115def 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