diff --git a/docs/advanced.rst b/docs/advanced.rst index eead4c5994..77cdf5cc15 100644 --- a/docs/advanced.rst +++ b/docs/advanced.rst @@ -281,6 +281,7 @@ flag:: pytest==3.2.3 Adding the ``--hash`` flag will add package hashes to the output for extra security. +Adding the ``--exclude-markers`` flagwill exclude the markers from the output. The locked requirements are written to stdout, with shell output redirection used to write them to a file:: diff --git a/news/5092.behavior.rst b/news/5092.behavior.rst new file mode 100644 index 0000000000..987b120e15 --- /dev/null +++ b/news/5092.behavior.rst @@ -0,0 +1 @@ +Adjust ``pipenv requirements`` to add markers and add an ``--exclude-markers`` option to allow the exclusion of markers diff --git a/pipenv/cli/command.py b/pipenv/cli/command.py index c884df5260..baaadbadf0 100644 --- a/pipenv/cli/command.py +++ b/pipenv/cli/command.py @@ -763,8 +763,9 @@ def verify(state): "--dev-only", is_flag=True, default=False, help="Only add development requirements." ) @option("--hash", is_flag=True, default=False, help="Add package hashes.") +@option("--exclude-markers", is_flag=True, default=False, help="Exclude markers.") @pass_state -def requirements(state, dev=False, dev_only=False, hash=False): +def requirements(state, dev=False, dev_only=False, hash=False, exclude_markers=False): from pipenv.utils.dependencies import convert_deps_to_pip @@ -776,10 +777,10 @@ def requirements(state, dev=False, dev_only=False, hash=False): deps = {} - if not dev_only: - deps.update(lockfile["default"]) if dev or dev_only: deps.update(lockfile["develop"]) + if not dev_only: + deps.update(lockfile["default"]) pip_deps = convert_deps_to_pip( deps, @@ -787,7 +788,7 @@ def requirements(state, dev=False, dev_only=False, hash=False): r=False, include_index=False, include_hashes=hash, - include_markers=False, + include_markers=not exclude_markers, ) for d in pip_deps: diff --git a/tests/integration/test_requirements.py b/tests/integration/test_requirements.py index b58ca12c17..a200a5e6f1 100644 --- a/tests/integration/test_requirements.py +++ b/tests/integration/test_requirements.py @@ -71,6 +71,7 @@ def test_requirements_generates_requirements_from_lockfile_multiple_sources(Pipe assert '-i https://pypi.org/simple' in c.stdout assert '--extra-index-url https://some_other_source.org' in c.stdout + @pytest.mark.requirements def test_requirements_with_git_requirements(PipenvInstance): req_name, req_hash = 'example-repo', 'cc858e89f19bc0dbd70983f86b811ab625dc9292' @@ -97,6 +98,59 @@ def test_requirements_with_git_requirements(PipenvInstance): @pytest.mark.requirements +def test_requirements_markers_get_included(PipenvInstance): + package, version, markers = "werkzeug", "==2.1.2", "python_version >= '3.7'" + lockfile = { + "_meta": {"sources": []}, + "default": { + package: { + "hashes": [ + "sha256:1ce08e8093ed67d638d63879fd1ba3735817f7a80de3674d293f5984f25fb6e6", + "sha256:72a4b735692dd3135217911cbeaa1be5fa3f62bffb8745c5215420a03dc55255" + ], + "markers": markers, + "version": version + } + }, + "develop": {} + } + + with PipenvInstance(chdir=True) as p: + with open(p.lockfile_path, 'w') as f: + json.dump(lockfile, f) + + c = p.pipenv('requirements') + assert c.returncode == 0 + assert f'{package}{version}; {markers}' in c.stdout + + +@pytest.mark.requirements +def test_requirements_markers_get_excluded(PipenvInstance): + package, version, markers = "werkzeug", "==2.1.2", "python_version >= '3.7'" + lockfile = { + "_meta": {"sources": []}, + "default": { + package: { + "hashes": [ + "sha256:1ce08e8093ed67d638d63879fd1ba3735817f7a80de3674d293f5984f25fb6e6", + "sha256:72a4b735692dd3135217911cbeaa1be5fa3f62bffb8745c5215420a03dc55255" + ], + "markers": markers, + "version": version + } + }, + "develop": {} + } + + with PipenvInstance(chdir=True) as p: + with open(p.lockfile_path, 'w') as f: + json.dump(lockfile, f) + + c = p.pipenv('requirements --exclude-markers') + assert c.returncode == 0 + assert markers not in c.stdout + + def test_requirements_generates_requirements_from_lockfile_without_env_var_expansion( PipenvInstance, ):