Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple categories in exported requirements #5431

Merged
merged 9 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 11 additions & 5 deletions pipenv/cli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,8 +740,9 @@ def verify(state):
)
@option("--hash", is_flag=True, default=False, help="Add package hashes.")
@option("--exclude-markers", is_flag=True, default=False, help="Exclude markers.")
@option("--categories", is_flag=False, default='', help="Only add requirement of the specified categories.")
@pass_state
def requirements(state, dev=False, dev_only=False, hash=False, exclude_markers=False):
def requirements(state, dev=False, dev_only=False, hash=False, exclude_markers=False, categories=''):

from pipenv.utils.dependencies import convert_deps_to_pip

Expand All @@ -752,11 +753,16 @@ def requirements(state, dev=False, dev_only=False, hash=False, exclude_markers=F
echo(" ".join([prefix, package_index["url"]]))

deps = {}
categories_list = categories.split(',') if categories else []

if dev or dev_only:
deps.update(lockfile["develop"])
if not dev_only:
deps.update(lockfile["default"])
if categories_list:
for category in categories_list:
deps.update(lockfile.get(category, {}))
else:
if dev or dev_only:
deps.update(lockfile["develop"])
if not dev_only:
deps.update(lockfile["default"])

pip_deps = convert_deps_to_pip(
deps,
Expand Down
40 changes: 40 additions & 0 deletions tests/integration/test_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,46 @@ def test_requirements_generates_requirements_from_lockfile_multiple_sources(pipe
assert '--extra-index-url https://some_other_source.org' in c.stdout


@pytest.mark.requirements
def test_requirements_generates_requirements_from_lockfile_from_categories(pipenv_instance_private_pypi):
with pipenv_instance_private_pypi(chdir=True) as p:
packages = ('six', '1.12.0')
dev_packages = ('itsdangerous', '1.1.0')
test_packages = ('pytest', '7.1.3')
doc_packages = ('Sphinx', '5.3.0')

with open(p.pipfile_path, 'w') as f:
contents = f"""
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[packages]
{packages[0]}= "=={packages[1]}"
[dev-packages]
{dev_packages[0]}= "=={dev_packages[1]}"
[test-packages]
oz123 marked this conversation as resolved.
Show resolved Hide resolved
{test_packages[0]}= "=={test_packages[1]}"
[doc-packages]
{doc_packages[0]}= "=={doc_packages[1]}"
""".strip()
f.write(contents)
l = p.pipenv('lock')
assert l.returncode == 0

c = p.pipenv('requirements --dev-only')
assert c.returncode == 0
assert f'{packages[0]}=={packages[1]}' not in c.stdout
assert f'{test_packages[0]}=={test_packages[1]}' not in c.stdout
assert f'{dev_packages[0]}=={dev_packages[1]}' in c.stdout

d = p.pipenv('requirements --category test,doc')
oz123 marked this conversation as resolved.
Show resolved Hide resolved
assert d.returncode == 0
assert f'{packages[0]}=={packages[1]}' not in d.stdout
assert f'{dev_packages[0]}=={dev_packages[1]}' not in c.stdout
assert f'{test_packages[0]}=={test_packages[1]}' in d.stdout
assert f'{doc_packages[0]}=={doc_packages[1]}' in d.stdout

@pytest.mark.requirements
def test_requirements_with_git_requirements(pipenv_instance_pypi):
req_name, req_hash = 'example-repo', 'cc858e89f19bc0dbd70983f86b811ab625dc9292'
Expand Down