Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pre_commit/commands/migrate_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ def _migrate_sha_to_rev(contents: str) -> str:
return re.sub(r'(\n\s+)sha:', r'\1rev:', contents)


def _migrate_python_venv(contents: str) -> str:
return re.sub(
r'(\n\s+)language: python_venv\b',
r'\1language: python',
contents,
)


def migrate_config(config_file: str, quiet: bool = False) -> int:
with open(config_file) as f:
orig_contents = contents = f.read()
Expand All @@ -55,6 +63,7 @@ def migrate_config(config_file: str, quiet: bool = False) -> int:

contents = _migrate_map(contents)
contents = _migrate_sha_to_rev(contents)
contents = _migrate_python_venv(contents)

if contents != orig_contents:
with open(config_file, 'w') as f:
Expand Down
9 changes: 9 additions & 0 deletions pre_commit/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import logging
import os
import shlex
from typing import Any
from typing import Sequence

Expand Down Expand Up @@ -68,6 +69,14 @@ def _hook_install(hook: Hook) -> None:
logger.info('Once installed this environment will be reused.')
logger.info('This may take a few minutes...')

if hook.language == 'python_venv':
logger.warning(
f'`repo: {hook.src}` uses deprecated `language: python_venv`. '
f'This is an alias for `language: python`. '
f'Often `pre-commit autoupdate --repo {shlex.quote(hook.src)}` '
f'will fix this.',
)

lang = languages[hook.language]
assert lang.ENVIRONMENT_DIR is not None

Expand Down

This file was deleted.

9 changes: 0 additions & 9 deletions testing/resources/python_venv_hooks_repo/foo.py

This file was deleted.

10 changes: 0 additions & 10 deletions testing/resources/python_venv_hooks_repo/setup.py

This file was deleted.

33 changes: 33 additions & 0 deletions tests/commands/migrate_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,39 @@ def test_migrate_config_sha_to_rev(tmpdir):
)


def test_migrate_config_language_python_venv(tmp_path):
src = '''\
repos:
- repo: local
hooks:
- id: example
name: example
entry: example
language: python_venv
- id: example
name: example
entry: example
language: system
'''
expected = '''\
repos:
- repo: local
hooks:
- id: example
name: example
entry: example
language: python
- id: example
name: example
entry: example
language: system
'''
cfg = tmp_path.joinpath('cfg.yaml')
cfg.write_text(src)
assert migrate_config(str(cfg)) == 0
assert cfg.read_text() == expected


def test_migrate_config_invalid_yaml(tmpdir):
contents = '['
cfg = tmpdir.join(C.CONFIG_FILE)
Expand Down
7 changes: 7 additions & 0 deletions tests/languages/all_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from __future__ import annotations

from pre_commit.languages.all import languages


def test_python_venv_is_an_alias_to_python():
assert languages['python_venv'] is languages['python']
20 changes: 15 additions & 5 deletions tests/repository_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,21 @@ def test_python_hook_weird_setup_cfg(in_git_dir, tempdir_factory, store):
)


def test_python_venv(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'python_venv_hooks_repo',
'foo', [os.devnull],
f'[{os.devnull!r}]\nHello World\n'.encode(),
def test_python_venv_deprecation(store, caplog):
config = {
'repo': 'local',
'hooks': [{
'id': 'example',
'name': 'example',
'language': 'python_venv',
'entry': 'echo hi',
}],
}
_get_hook(config, store, 'example')
assert caplog.messages[-1] == (
'`repo: local` uses deprecated `language: python_venv`. '
'This is an alias for `language: python`. '
'Often `pre-commit autoupdate --repo local` will fix this.'
)


Expand Down