mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-18 11:31:46 +04:00
Add support for requirements files in additional_dependencies
This commit is contained in:
parent
2006a508d8
commit
b591d27345
5 changed files with 409 additions and 5 deletions
|
|
@ -2,6 +2,7 @@ from __future__ import absolute_import
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import os.path
|
||||
import textwrap
|
||||
|
||||
from pre_commit.languages import python
|
||||
|
||||
|
|
@ -16,3 +17,205 @@ def test_norm_version_expanduser():
|
|||
expected_path = home + '/.pyenv/versions/3.4.3/bin/python'
|
||||
result = python.norm_version(path)
|
||||
assert result == expected_path
|
||||
|
||||
|
||||
def test_single_requirements_file(tempdir_factory):
|
||||
tmpdir = tempdir_factory.get()
|
||||
req1 = os.path.join(tmpdir, 'req1.txt')
|
||||
with open(req1, 'w') as wfh:
|
||||
wfh.write(textwrap.dedent('''
|
||||
# This is a comment in the pip file
|
||||
pep8
|
||||
'''))
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['-r', 'req1.txt'],
|
||||
) == ['pep8']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['-rreq1.txt'],
|
||||
) == ['pep8']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['--requirement', 'req1.txt'],
|
||||
) == ['pep8']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['--requirement=req1.txt'],
|
||||
) == ['pep8']
|
||||
|
||||
|
||||
def test_multiple_requirements_file(tempdir_factory):
|
||||
tmpdir = tempdir_factory.get()
|
||||
req1 = os.path.join(tmpdir, 'req1.txt')
|
||||
with open(req1, 'w') as wfh:
|
||||
wfh.write(textwrap.dedent('''
|
||||
# This is a comment in the pip file
|
||||
pep8
|
||||
'''))
|
||||
req2 = os.path.join(tmpdir, 'req2.txt')
|
||||
with open(req2, 'w') as wfh:
|
||||
wfh.write(textwrap.dedent('''
|
||||
# This is a comment in the pip file
|
||||
pre-commit
|
||||
'''))
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['-r', 'req1.txt', '-r', 'req2.txt'],
|
||||
) == ['pep8', 'pre-commit']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['-rreq1.txt', '-rreq2.txt'],
|
||||
) == ['pep8', 'pre-commit']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['--requirement', 'req1.txt', '--requirement', 'req2.txt'],
|
||||
) == ['pep8', 'pre-commit']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['--requirement=req1.txt', '--requirement=req2.txt'],
|
||||
) == ['pep8', 'pre-commit']
|
||||
|
||||
|
||||
def test_nested_requirements_file(tempdir_factory):
|
||||
tmpdir = tempdir_factory.get()
|
||||
req1 = os.path.join(tmpdir, 'req1.txt')
|
||||
with open(req1, 'w') as wfh:
|
||||
wfh.write(textwrap.dedent('''
|
||||
# This is a comment in the pip file
|
||||
pep8
|
||||
'''))
|
||||
req2 = os.path.join(tmpdir, 'req2.txt')
|
||||
with open(req2, 'w') as wfh:
|
||||
wfh.write(textwrap.dedent('''
|
||||
# This is a comment in the pip file
|
||||
-r req1.txt
|
||||
pre-commit
|
||||
'''))
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['-r', 'req2.txt'],
|
||||
) == ['pep8', 'pre-commit']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['-rreq2.txt'],
|
||||
) == ['pep8', 'pre-commit']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['--requirement', 'req2.txt'],
|
||||
) == ['pep8', 'pre-commit']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['--requirement=req2.txt'],
|
||||
) == ['pep8', 'pre-commit']
|
||||
|
||||
|
||||
def test_nested_requirements_files_subdir(tempdir_factory):
|
||||
tmpdir = tempdir_factory.get()
|
||||
req1 = os.path.join(tmpdir, 'req1.txt')
|
||||
with open(req1, 'w') as wfh:
|
||||
wfh.write(textwrap.dedent('''
|
||||
# This is a comment in the pip file
|
||||
pep8
|
||||
'''))
|
||||
reqsdir = os.path.join(tmpdir, 'requirements')
|
||||
os.makedirs(reqsdir)
|
||||
req2 = os.path.join(reqsdir, 'req2.txt')
|
||||
with open(req2, 'w') as wfh:
|
||||
wfh.write(textwrap.dedent('''
|
||||
# This is a comment in the pip file
|
||||
-r ../req1.txt
|
||||
pre-commit
|
||||
'''))
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['-r', 'requirements/req2.txt'],
|
||||
) == ['pep8', 'pre-commit']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['-rrequirements/req2.txt'],
|
||||
) == ['pep8', 'pre-commit']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['--requirement', 'requirements/req2.txt'],
|
||||
) == ['pep8', 'pre-commit']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['--requirement=requirements/req2.txt'],
|
||||
) == ['pep8', 'pre-commit']
|
||||
|
||||
|
||||
def test_mixed_requirements(tempdir_factory):
|
||||
tmpdir = tempdir_factory.get()
|
||||
req1 = os.path.join(tmpdir, 'req1.txt')
|
||||
with open(req1, 'w') as wfh:
|
||||
wfh.write(textwrap.dedent('''
|
||||
# This is a comment in the pip file
|
||||
pep8
|
||||
'''))
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['pre-commit', '-r', 'req1.txt'],
|
||||
) == ['pre-commit', 'pep8']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['-rreq1.txt', 'pre-commit'],
|
||||
) == ['pep8', 'pre-commit']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['--requirement', 'req1.txt', 'pre-commit'],
|
||||
) == ['pep8', 'pre-commit']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['pre-commit', '--requirement=req1.txt'],
|
||||
) == ['pre-commit', 'pep8']
|
||||
|
||||
|
||||
def test_options_in_requirements_file(tempdir_factory):
|
||||
tmpdir = tempdir_factory.get()
|
||||
req1 = os.path.join(tmpdir, 'req1.txt')
|
||||
with open(req1, 'w') as wfh:
|
||||
wfh.write(textwrap.dedent('''
|
||||
# This is a comment in the pip file
|
||||
--index-url=https://domain.tld/repository/pypi/simple/
|
||||
pep8
|
||||
'''))
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['-r', 'req1.txt'],
|
||||
) == ['--index-url=https://domain.tld/repository/pypi/simple/', 'pep8']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['-rreq1.txt'],
|
||||
) == ['--index-url=https://domain.tld/repository/pypi/simple/', 'pep8']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['--requirement', 'req1.txt'],
|
||||
) == ['--index-url=https://domain.tld/repository/pypi/simple/', 'pep8']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['--requirement=req1.txt'],
|
||||
) == ['--index-url=https://domain.tld/repository/pypi/simple/', 'pep8']
|
||||
assert python.collect_requirements(
|
||||
tmpdir,
|
||||
[
|
||||
'--index-url=https://domain.tld/repository/pypi/simple/',
|
||||
'-r', 'req1.txt',
|
||||
],
|
||||
) == ['--index-url=https://domain.tld/repository/pypi/simple/', 'pep8']
|
||||
with open(req1, 'w') as wfh:
|
||||
wfh.write(textwrap.dedent('''
|
||||
# This is a comment in the pip file
|
||||
--index-url https://domain.tld/repository/pypi/simple/
|
||||
pep8
|
||||
'''))
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['-r', 'req1.txt'],
|
||||
) == ['--index-url', 'https://domain.tld/repository/pypi/simple/', 'pep8']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['-rreq1.txt'],
|
||||
) == ['--index-url', 'https://domain.tld/repository/pypi/simple/', 'pep8']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['--requirement', 'req1.txt'],
|
||||
) == ['--index-url', 'https://domain.tld/repository/pypi/simple/', 'pep8']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['--requirement=req1.txt'],
|
||||
) == ['--index-url', 'https://domain.tld/repository/pypi/simple/', 'pep8']
|
||||
|
||||
|
||||
def test_missing_requirements_file(tempdir_factory):
|
||||
tmpdir = tempdir_factory.get()
|
||||
req1 = os.path.join(tmpdir, 'req1.txt')
|
||||
with open(req1, 'w') as wfh:
|
||||
wfh.write(textwrap.dedent('''
|
||||
# This is a comment in the pip file
|
||||
pep8
|
||||
'''))
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['-r', 'req1.txt', '-r', 'req2.txt'],
|
||||
) == ['pep8']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['-rreq1.txt', '-rreq2.txt'],
|
||||
) == ['pep8']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['--requirement', 'req1.txt', '--requirement', 'req2.txt'],
|
||||
) == ['pep8']
|
||||
assert python.collect_requirements(
|
||||
tmpdir, ['--requirement=req1.txt', '--requirement=req2.txt'],
|
||||
) == ['pep8']
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import io
|
|||
import os.path
|
||||
import re
|
||||
import shutil
|
||||
import textwrap
|
||||
|
||||
import mock
|
||||
import pytest
|
||||
|
|
@ -843,3 +844,50 @@ def test_manifest_hooks(tempdir_factory, store):
|
|||
'exclude_types': [],
|
||||
'verbose': False,
|
||||
}
|
||||
|
||||
|
||||
def test_python_additional_dependencies_requirements_file(
|
||||
tempdir_factory,
|
||||
store,
|
||||
):
|
||||
path = make_repo(tempdir_factory, 'python_hooks_repo')
|
||||
req1 = os.path.join(path, 'req1.txt')
|
||||
with open(req1, 'w') as wfh:
|
||||
wfh.write(textwrap.dedent('''
|
||||
# This is a comment in the pip file
|
||||
pep8
|
||||
'''))
|
||||
with cwd(path):
|
||||
config = make_config_from_repo(path)
|
||||
config['hooks'][0]['additional_dependencies'] = ['-r', 'req1.txt']
|
||||
repo = Repository.create(config, store)
|
||||
env, = repo._venvs()
|
||||
assert env == (
|
||||
mock.ANY, 'python', python.get_default_version(), ('pep8',),
|
||||
)
|
||||
config = make_config_from_repo(path)
|
||||
config['hooks'][0]['additional_dependencies'] = ['-rreq1.txt']
|
||||
repo = Repository.create(config, store)
|
||||
env, = repo._venvs()
|
||||
assert env == (
|
||||
mock.ANY, 'python', python.get_default_version(), ('pep8',),
|
||||
)
|
||||
config = make_config_from_repo(path)
|
||||
config['hooks'][0]['additional_dependencies'] = [
|
||||
'--requirement',
|
||||
'req1.txt',
|
||||
]
|
||||
repo = Repository.create(config, store)
|
||||
env, = repo._venvs()
|
||||
assert env == (
|
||||
mock.ANY, 'python', python.get_default_version(), ('pep8',),
|
||||
)
|
||||
config = make_config_from_repo(path)
|
||||
config['hooks'][0]['additional_dependencies'] = [
|
||||
'--requirement=req1.txt',
|
||||
]
|
||||
repo = Repository.create(config, store)
|
||||
env, = repo._venvs()
|
||||
assert env == (
|
||||
mock.ANY, 'python', python.get_default_version(), ('pep8',),
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue