From 58b91cb5e2d0347bfd53ef3616c8c2292dfd5704 Mon Sep 17 00:00:00 2001 From: Gaetan Semet Date: Mon, 10 Feb 2020 17:55:20 +0100 Subject: [PATCH] add '--no-freeze' option to export (#2009) * add '--no-freeze' option to export * add tests for 'export --no-freeze' --- poetry/console/commands/export.py | 2 + poetry/utils/exporter.py | 8 ++- tests/console/commands/test_export.py | 85 +++++++++++++++++++++++---- 3 files changed, 81 insertions(+), 14 deletions(-) diff --git a/poetry/console/commands/export.py b/poetry/console/commands/export.py index 37cfc1394e8..48944f3ad25 100644 --- a/poetry/console/commands/export.py +++ b/poetry/console/commands/export.py @@ -23,6 +23,7 @@ class ExportCommand(Command): multiple=True, ), option("with-credentials", None, "Include credentials for extra indices."), + option("no-freeze", None, "Do not freeze version"), ] def handle(self): @@ -65,4 +66,5 @@ def handle(self): dev=self.option("dev"), extras=self.option("extras"), with_credentials=self.option("with-credentials"), + no_freeze=self.option("no-freeze"), ) diff --git a/poetry/utils/exporter.py b/poetry/utils/exporter.py index 9341962d4fb..1954f2f190b 100644 --- a/poetry/utils/exporter.py +++ b/poetry/utils/exporter.py @@ -32,7 +32,8 @@ def export( dev=False, extras=None, with_credentials=False, - ): # type: (str, Path, Union[IO, str], bool, bool, bool) -> None + no_freeze=False, + ): # type: (str, Path, Union[IO, str], bool, bool, bool, bool) -> None if fmt not in self.ACCEPTED_FORMATS: raise ValueError("Invalid export format: {}".format(fmt)) @@ -43,6 +44,7 @@ def export( dev=dev, extras=extras, with_credentials=with_credentials, + no_freeze=no_freeze, ) def _export_requirements_txt( @@ -53,6 +55,7 @@ def _export_requirements_txt( dev=False, extras=None, with_credentials=False, + no_freeze=False, ): # type: (Path, Union[IO, str], bool, bool, bool) -> None indexes = set() content = "" @@ -96,6 +99,9 @@ def _export_requirements_txt( line = "{}".format(package.source_url) if package.develop and package.source_type == "directory": line = "-e " + line + elif no_freeze: + dependency = package.to_dependency() + line = package.name else: dependency = package.to_dependency() line = "{}=={}".format(package.name, package.version) diff --git a/tests/console/commands/test_export.py b/tests/console/commands/test_export.py index 58049a1bba9..140b772403c 100644 --- a/tests/console/commands/test_export.py +++ b/tests/console/commands/test_export.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals +from textwrap import dedent + import pytest from cleo.testers import CommandTester @@ -90,9 +92,11 @@ def test_export_exports_requirements_txt_file_locks_if_no_lock_file(app, repo): assert app.poetry.locker.lock.exists() - expected = """\ -foo==1.0.0 -""" + expected = dedent( + """ + foo==1.0.0 + """ + ).lstrip() assert expected == content assert "The lock file does not exist. Locking." in tester.io.fetch_output() @@ -121,9 +125,11 @@ def test_export_exports_requirements_txt_uses_lock_file(app, repo): assert app.poetry.locker.lock.exists() - expected = """\ -foo==1.0.0 -""" + expected = dedent( + """ + foo==1.0.0 + """ + ).lstrip() assert expected == content assert "The lock file does not exist. Locking." not in tester.io.fetch_output() @@ -161,9 +167,11 @@ def test_export_prints_to_stdout_by_default(app, repo): tester.execute("--format requirements.txt") - expected = """\ -foo==1.0.0 -""" + expected = dedent( + """ + foo==1.0.0 + """ + ).lstrip() assert expected == tester.io.fetch_output() @@ -183,9 +191,60 @@ def test_export_includes_extras_by_flag(app, repo): tester.execute("--format requirements.txt --extras feature_bar") - expected = """\ -bar==1.1.0 -foo==1.0.0 -""" + expected = dedent( + """ + bar==1.1.0 + foo==1.0.0 + """ + ).lstrip() + + assert expected == tester.io.fetch_output() + + +def test_export_no_freeze(app, repo): + repo.add_package(get_package("foo", "1.0.0")) + repo.add_package(get_package("bar", "1.1.0")) + + command = app.find("lock") + tester = CommandTester(command) + tester.execute() + + assert app.poetry.locker.lock.exists() + + command = app.find("export") + tester = CommandTester(command) + + tester.execute("--format requirements.txt --no-freeze") + + expected = dedent( + """ + foo + """ + ).lstrip() + + assert expected == tester.io.fetch_output() + + +def test_export_no_freeze_and_extra(app, repo): + repo.add_package(get_package("foo", "1.0.0")) + repo.add_package(get_package("bar", "1.1.0")) + + command = app.find("lock") + tester = CommandTester(command) + tester.execute() + + assert app.poetry.locker.lock.exists() + + command = app.find("export") + tester = CommandTester(command) + + tester.execute("--format requirements.txt --extras feature_bar --no-freeze") + + expected = dedent( + """ + bar + foo + """ + ).lstrip() assert expected == tester.io.fetch_output()