File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed
Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 1313import pre_commit .constants as C
1414from pre_commit .error_handler import FatalError
1515from pre_commit .languages .all import all_languages
16+ from pre_commit .util import parse_version
1617
1718
1819def check_type_tag (tag ):
@@ -23,6 +24,16 @@ def check_type_tag(tag):
2324 )
2425
2526
27+ def check_min_version (version ):
28+ if parse_version (version ) > parse_version (C .VERSION ):
29+ raise cfgv .ValidationError (
30+ 'pre-commit version {} is required but version {} is installed. '
31+ 'Perhaps run `pip install --upgrade pre-commit`.' .format (
32+ version , C .VERSION ,
33+ ),
34+ )
35+
36+
2637def _make_argparser (filenames_help ):
2738 parser = argparse .ArgumentParser ()
2839 parser .add_argument ('filenames' , nargs = '*' , help = filenames_help )
@@ -231,6 +242,11 @@ def _entry(modname):
231242 ),
232243 cfgv .Optional ('exclude' , cfgv .check_regex , '^$' ),
233244 cfgv .Optional ('fail_fast' , cfgv .check_bool , False ),
245+ cfgv .Optional (
246+ 'minimum_pre_commit_version' ,
247+ cfgv .check_and (cfgv .check_string , check_min_version ),
248+ '0' ,
249+ ),
234250)
235251
236252
Original file line number Diff line number Diff line change 33import cfgv
44import pytest
55
6+ import pre_commit .constants as C
67from pre_commit .clientlib import check_type_tag
78from pre_commit .clientlib import CONFIG_HOOK_DICT
89from pre_commit .clientlib import CONFIG_REPO_DICT
@@ -234,3 +235,23 @@ def test_meta_hook_invalid(config_repo):
234235def test_default_language_version_invalid (mapping ):
235236 with pytest .raises (cfgv .ValidationError ):
236237 cfgv .validate (mapping , DEFAULT_LANGUAGE_VERSION )
238+
239+
240+ def test_minimum_pre_commit_version_failing ():
241+ with pytest .raises (cfgv .ValidationError ) as excinfo :
242+ cfg = {'repos' : [], 'minimum_pre_commit_version' : '999' }
243+ cfgv .validate (cfg , CONFIG_SCHEMA )
244+ assert str (excinfo .value ) == (
245+ '\n '
246+ '==> At Config()\n '
247+ '==> At key: minimum_pre_commit_version\n '
248+ '=====> pre-commit version 999 is required but version {} is '
249+ 'installed. Perhaps run `pip install --upgrade pre-commit`.' .format (
250+ C .VERSION ,
251+ )
252+ )
253+
254+
255+ def test_minimum_pre_commit_version_passing ():
256+ cfg = {'repos' : [], 'minimum_pre_commit_version' : '0' }
257+ cfgv .validate (cfg , CONFIG_SCHEMA )
You can’t perform that action at this time.
0 commit comments