11from __future__ import unicode_literals
22
3- import os
4- import os .path
3+ import io
54import pytest
65import shutil
76from asottile .ordereddict import OrderedDict
87from asottile .yaml import ordered_dump
98from plumbum import local
109
1110import pre_commit .constants as C
12- from pre_commit .clientlib .validate_config import CONFIG_JSON_SCHEMA
13- from pre_commit .clientlib .validate_config import validate_config_extra
1411from pre_commit .commands .autoupdate import _update_repository
1512from pre_commit .commands .autoupdate import autoupdate
1613from pre_commit .commands .autoupdate import RepositoryCannotBeUpdatedError
17- from pre_commit .jsonschema_extensions import apply_defaults
18- from pre_commit .jsonschema_extensions import remove_defaults
1914from pre_commit .runner import Runner
2015from testing .auto_namedtuple import auto_namedtuple
16+ from testing .fixtures import make_config_from_repo
17+ from testing .fixtures import make_hooks_repo
2118from testing .util import get_head_sha
2219from testing .util import get_resource_path
2320
2421
2522@pytest .yield_fixture
26- def up_to_date_repo (python_hooks_repo ):
27- config = OrderedDict ((
28- ('repo' , python_hooks_repo ),
29- ('sha' , get_head_sha (python_hooks_repo )),
30- ('hooks' , [OrderedDict ((('id' , 'foo' ),))]),
31- ))
32- wrapped_config = apply_defaults ([config ], CONFIG_JSON_SCHEMA )
33- validate_config_extra (wrapped_config )
34- config = wrapped_config [0 ]
35-
36- with open (os .path .join (python_hooks_repo , C .CONFIG_FILE ), 'w' ) as file_obj :
37- file_obj .write (
38- ordered_dump (
39- remove_defaults ([config ], CONFIG_JSON_SCHEMA ),
40- ** C .YAML_DUMP_KWARGS
41- )
42- )
43-
44- yield auto_namedtuple (
45- repo_config = config ,
46- python_hooks_repo = python_hooks_repo ,
47- )
23+ def up_to_date_repo (tmpdir_factory ):
24+ yield make_hooks_repo (tmpdir_factory , 'python_hooks_repo' )
4825
4926
5027def test_up_to_date_repo (up_to_date_repo , runner_with_mocked_store ):
51- input_sha = up_to_date_repo .repo_config ['sha' ]
52- ret = _update_repository (
53- up_to_date_repo .repo_config , runner_with_mocked_store ,
54- )
28+ config = make_config_from_repo (up_to_date_repo )
29+ input_sha = config ['sha' ]
30+ ret = _update_repository (config , runner_with_mocked_store )
5531 assert ret ['sha' ] == input_sha
5632
5733
58- def test_autoupdate_up_to_date_repo (up_to_date_repo , mock_out_store_directory ):
34+ def test_autoupdate_up_to_date_repo (
35+ up_to_date_repo , in_tmpdir , mock_out_store_directory ,
36+ ):
37+ # Write out the config
38+ config = make_config_from_repo (up_to_date_repo , check = False )
39+ with io .open (C .CONFIG_FILE , 'w' ) as config_file :
40+ config_file .write (ordered_dump ([config ], ** C .YAML_DUMP_KWARGS ))
41+
5942 before = open (C .CONFIG_FILE ).read ()
6043 assert '^$' not in before
61- runner = Runner (up_to_date_repo . python_hooks_repo )
44+ runner = Runner ('.' )
6245 ret = autoupdate (runner )
6346 after = open (C .CONFIG_FILE ).read ()
6447 assert ret == 0
6548 assert before == after
6649
6750
6851@pytest .yield_fixture
69- def out_of_date_repo (python_hooks_repo ):
70- config = OrderedDict ((
71- ('repo' , python_hooks_repo ),
72- ('sha' , get_head_sha (python_hooks_repo )),
73- ('hooks' , [OrderedDict ((('id' , 'foo' ), ('files' , '' )))]),
74- ))
75- config_wrapped = apply_defaults ([config ], CONFIG_JSON_SCHEMA )
76- validate_config_extra (config_wrapped )
77- config = config_wrapped [0 ]
78- local ['git' ]['commit' , '--allow-empty' , '-m' , 'foo' ]()
79- head_sha = get_head_sha (python_hooks_repo )
80-
81- with open (os .path .join (python_hooks_repo , C .CONFIG_FILE ), 'w' ) as file_obj :
82- file_obj .write (
83- ordered_dump ([config ], ** C .YAML_DUMP_KWARGS )
84- )
52+ def out_of_date_repo (tmpdir_factory ):
53+ path = make_hooks_repo (tmpdir_factory , 'python_hooks_repo' )
54+ original_sha = get_head_sha (path )
55+
56+ # Make a commit
57+ with local .cwd (path ):
58+ local ['git' ]['commit' , '--allow-empty' , '-m' , 'foo' ]()
59+ head_sha = get_head_sha (path )
8560
8661 yield auto_namedtuple (
87- repo_config = config ,
88- head_sha = head_sha ,
89- python_hooks_repo = python_hooks_repo ,
62+ path = path , original_sha = original_sha , head_sha = head_sha ,
9063 )
9164
9265
9366def test_out_of_date_repo (out_of_date_repo , runner_with_mocked_store ):
94- ret = _update_repository (
95- out_of_date_repo .repo_config , runner_with_mocked_store ,
67+ config = make_config_from_repo (
68+ out_of_date_repo .path , sha = out_of_date_repo . original_sha ,
9669 )
70+ ret = _update_repository (config , runner_with_mocked_store )
71+ assert ret ['sha' ] != out_of_date_repo .original_sha
9772 assert ret ['sha' ] == out_of_date_repo .head_sha
9873
9974
10075def test_autoupdate_out_of_date_repo (
101- out_of_date_repo , mock_out_store_directory
76+ out_of_date_repo , in_tmpdir , mock_out_store_directory
10277):
78+ # Write out the config
79+ config = make_config_from_repo (
80+ out_of_date_repo .path , sha = out_of_date_repo .original_sha , check = False ,
81+ )
82+ with io .open (C .CONFIG_FILE , 'w' ) as config_file :
83+ config_file .write (ordered_dump ([config ], ** C .YAML_DUMP_KWARGS ))
84+
10385 before = open (C .CONFIG_FILE ).read ()
104- runner = Runner (out_of_date_repo . python_hooks_repo )
86+ runner = Runner ('.' )
10587 ret = autoupdate (runner )
10688 after = open (C .CONFIG_FILE ).read ()
10789 assert ret == 0
@@ -112,47 +94,47 @@ def test_autoupdate_out_of_date_repo(
11294
11395
11496@pytest .yield_fixture
115- def hook_disappearing_repo (python_hooks_repo ):
116- config = OrderedDict ((
117- ('repo' , python_hooks_repo ),
118- ('sha' , get_head_sha (python_hooks_repo )),
119- ('hooks' , [OrderedDict ((('id' , 'foo' ),))]),
120- ))
121- config_wrapped = apply_defaults ([config ], CONFIG_JSON_SCHEMA )
122- validate_config_extra (config_wrapped )
123- config = config_wrapped [0 ]
124- shutil .copy (
125- get_resource_path ('manifest_without_foo.yaml' ),
126- C .MANIFEST_FILE ,
127- )
128- local ['git' ]['add' , '.' ]()
129- local ['git' ]['commit' , '-m' , 'Remove foo' ]()
130-
131- with open (os .path .join (python_hooks_repo , C .CONFIG_FILE ), 'w' ) as file_obj :
132- file_obj .write (
133- ordered_dump ([config ], ** C .YAML_DUMP_KWARGS )
97+ def hook_disappearing_repo (tmpdir_factory ):
98+ path = make_hooks_repo (tmpdir_factory , 'python_hooks_repo' )
99+ original_sha = get_head_sha (path )
100+
101+ with local .cwd (path ):
102+ shutil .copy (
103+ get_resource_path ('manifest_without_foo.yaml' ),
104+ C .MANIFEST_FILE ,
134105 )
106+ local ['git' ]('add' , '.' )
107+ local ['git' ]('commit' , '-m' , 'Remove foo' )
135108
136- yield auto_namedtuple (
137- repo_config = config ,
138- python_hooks_repo = python_hooks_repo ,
139- )
109+ yield auto_namedtuple (path = path , original_sha = original_sha )
140110
141111
142112def test_hook_disppearing_repo_raises (
143113 hook_disappearing_repo , runner_with_mocked_store
144114):
115+ config = make_config_from_repo (
116+ hook_disappearing_repo .path ,
117+ sha = hook_disappearing_repo .original_sha ,
118+ hooks = [OrderedDict ((('id' , 'foo' ),))],
119+ )
145120 with pytest .raises (RepositoryCannotBeUpdatedError ):
146- _update_repository (
147- hook_disappearing_repo .repo_config , runner_with_mocked_store ,
148- )
121+ _update_repository (config , runner_with_mocked_store )
149122
150123
151124def test_autoupdate_hook_disappearing_repo (
152- hook_disappearing_repo , mock_out_store_directory
125+ hook_disappearing_repo , in_tmpdir , mock_out_store_directory
153126):
127+ config = make_config_from_repo (
128+ hook_disappearing_repo .path ,
129+ sha = hook_disappearing_repo .original_sha ,
130+ hooks = [OrderedDict ((('id' , 'foo' ),))],
131+ check = False ,
132+ )
133+ with io .open (C .CONFIG_FILE , 'w' ) as config_file :
134+ config_file .write (ordered_dump ([config ], ** C .YAML_DUMP_KWARGS ))
135+
154136 before = open (C .CONFIG_FILE ).read ()
155- runner = Runner (hook_disappearing_repo . python_hooks_repo )
137+ runner = Runner ('.' )
156138 ret = autoupdate (runner )
157139 after = open (C .CONFIG_FILE ).read ()
158140 assert ret == 1
0 commit comments