Skip to content

Commit da369be

Browse files
authored
Merge pull request #1771 from pre-commit/no_install_language_options
produce a more useful error message when non-installable things use language_version or additional_dependencies
2 parents ffed7be + 4f39946 commit da369be

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

pre_commit/repository.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ def _hook(
118118
if not ret['stages']:
119119
ret['stages'] = root_config['default_stages']
120120

121+
if languages[lang].ENVIRONMENT_DIR is None:
122+
if ret['language_version'] != C.DEFAULT:
123+
logger.error(
124+
f'The hook `{ret["id"]}` specifies `language_version` but is '
125+
f'using language `{lang}` which does not install an '
126+
f'environment. '
127+
f'Perhaps you meant to use a specific language?',
128+
)
129+
exit(1)
130+
if ret['additional_dependencies']:
131+
logger.error(
132+
f'The hook `{ret["id"]}` specifies `additional_dependencies` '
133+
f'but is using language `{lang}` which does not install an '
134+
f'environment. '
135+
f'Perhaps you meant to use a specific language?',
136+
)
137+
exit(1)
138+
121139
return ret
122140

123141

tests/repository_test.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,3 +942,49 @@ def test_dotnet_hook(tempdir_factory, store, repo):
942942
tempdir_factory, store, repo,
943943
'dotnet example hook', [], b'Hello from dotnet!\n',
944944
)
945+
946+
947+
def test_non_installable_hook_error_for_language_version(store, caplog):
948+
config = {
949+
'repo': 'local',
950+
'hooks': [{
951+
'id': 'system-hook',
952+
'name': 'system-hook',
953+
'language': 'system',
954+
'entry': 'python3 -c "import sys; print(sys.version)"',
955+
'language_version': 'python3.10',
956+
}],
957+
}
958+
with pytest.raises(SystemExit) as excinfo:
959+
_get_hook(config, store, 'system-hook')
960+
assert excinfo.value.code == 1
961+
962+
msg, = caplog.messages
963+
assert msg == (
964+
'The hook `system-hook` specifies `language_version` but is using '
965+
'language `system` which does not install an environment. '
966+
'Perhaps you meant to use a specific language?'
967+
)
968+
969+
970+
def test_non_installable_hook_error_for_additional_dependencies(store, caplog):
971+
config = {
972+
'repo': 'local',
973+
'hooks': [{
974+
'id': 'system-hook',
975+
'name': 'system-hook',
976+
'language': 'system',
977+
'entry': 'python3 -c "import sys; print(sys.version)"',
978+
'additional_dependencies': ['astpretty'],
979+
}],
980+
}
981+
with pytest.raises(SystemExit) as excinfo:
982+
_get_hook(config, store, 'system-hook')
983+
assert excinfo.value.code == 1
984+
985+
msg, = caplog.messages
986+
assert msg == (
987+
'The hook `system-hook` specifies `additional_dependencies` but is '
988+
'using language `system` which does not install an environment. '
989+
'Perhaps you meant to use a specific language?'
990+
)

0 commit comments

Comments
 (0)