Use cpanminus if available for perl pre-commit dependencies

cpanminus is broadly compatible with our syntax use of cpan, but can
offer additional benefits such as Pkg::Name@Version definitions, as well
as built-in modules to reduce the environment requirements on the system
when resolving dependencies.

Tests have been updated to ensure functionality works with either
utility.
This commit is contained in:
Daniel Porter 2024-04-24 12:25:30 +01:00
parent 8189370615
commit 51248d1727
2 changed files with 19 additions and 4 deletions

View file

@ -45,6 +45,13 @@ def install_environment(
lang_base.assert_version_default('perl', version)
with in_env(prefix, version):
lang_base.setup_cmd(
prefix, ('cpan', '-T', '.', *additional_dependencies),
)
if lang_base.exe_exists('cpanm'):
lang_base.setup_cmd(
prefix,
('cpanm', '.', *additional_dependencies),
)
else:
lang_base.setup_cmd(
prefix,
('cpan', '-T', '.', *additional_dependencies),
)

View file

@ -1,5 +1,9 @@
from __future__ import annotations
from unittest.mock import patch
import pytest
from pre_commit.languages import perl
from pre_commit.store import _make_local_repo
from pre_commit.util import make_executable
@ -56,7 +60,10 @@ sub hello {
assert ret == (0, b'Hello from perl-commit Perl!\n')
def test_perl_additional_dependencies(tmp_path):
@pytest.mark.parametrize('cpanm', [True, False])
@patch('pre_commit.lang_base.exe_exists')
def test_perl_additional_dependencies(mock_exe_exists, cpanm, tmp_path):
mock_exe_exists.return_value = cpanm
_make_local_repo(str(tmp_path))
ret, out = run_language(
@ -65,5 +72,6 @@ def test_perl_additional_dependencies(tmp_path):
'perltidy --version',
deps=('SHANCOCK/Perl-Tidy-20211029.tar.gz',),
)
mock_exe_exists.assert_called_once_with('cpanm')
assert ret == 0
assert out.startswith(b'This is perltidy, v20211029')