From 307cf81913ec8915833dc78007b26cac0bbf0266 Mon Sep 17 00:00:00 2001 From: Dawid Zych Date: Tue, 8 Jan 2019 15:24:28 +0100 Subject: [PATCH 1/3] Fix handling of init dependencies --- poetry/console/commands/init.py | 16 ++- tests/console/commands/test_init.py | 192 ++++++++++++++++++++++++++++ 2 files changed, 204 insertions(+), 4 deletions(-) diff --git a/poetry/console/commands/init.py b/poetry/console/commands/init.py index ab23da95c4e..976988f26c2 100644 --- a/poetry/console/commands/init.py +++ b/poetry/console/commands/init.py @@ -144,6 +144,10 @@ def handle(self): self.line("") requirements = {} + if self.option("dependency"): + requirements = self._format_requirements( + self._determine_requirements(self.option("dependency")) + ) question = "Would you like to define your main dependencies interactively?" help_message = ( @@ -160,12 +164,16 @@ def handle(self): if self.confirm(question, True): self.line(help_message) help_displayed = True - requirements = self._format_requirements( - self._determine_requirements(self.option("dependency")) + requirements.update( + self._format_requirements(self._determine_requirements([])) ) self.line("") dev_requirements = {} + if self.option("dev-dependency"): + dev_requirements = self._format_requirements( + self._determine_requirements(self.option("dev-dependency")) + ) question = ( "Would you like to define your development dependencies interactively?" @@ -174,8 +182,8 @@ def handle(self): if not help_displayed: self.line(help_message) - dev_requirements = self._format_requirements( - self._determine_requirements(self.option("dev-dependency")) + dev_requirements.update( + self._format_requirements(self._determine_requirements([])) ) self.line("") diff --git a/tests/console/commands/test_init.py b/tests/console/commands/test_init.py index 4ca7d44c44c..7d70594c514 100644 --- a/tests/console/commands/test_init.py +++ b/tests/console/commands/test_init.py @@ -484,3 +484,195 @@ def test_python_option(app, mocker, poetry): """ assert expected in tester.io.fetch_output() + + +def test_predefined_dependency(app, repo, mocker, poetry): + repo.add_package(get_package("pendulum", "2.0.0")) + + command = app.find("init") + command._pool = poetry.pool + + mocker.patch("poetry.utils._compat.Path.open") + p = mocker.patch("poetry.utils._compat.Path.cwd") + p.return_value = Path(__file__) + + tester = CommandTester(command) + tester.set_inputs( + [ + "my-package", # Package name + "1.2.3", # Version + "This is a description", # Description + "n", # Author + "MIT", # License + "~2.7 || ^3.6", # Python + "n", # Interactive packages + "n", # Interactive dev packages + "\n", # Generate + ] + ) + tester.execute([("command", command.name), ("--dependency", ["pendulum"])]) + + output = tester.get_display(True) + expected = """\ +[tool.poetry] +name = "my-package" +version = "1.2.3" +description = "This is a description" +authors = ["Your Name "] +license = "MIT" + +[tool.poetry.dependencies] +python = "~2.7 || ^3.6" +pendulum = "^2.0" + +[tool.poetry.dev-dependencies] +""" + + assert expected in output + + +def test_predefined_and_interactive_dependencies(app, repo, mocker, poetry): + repo.add_package(get_package("pendulum", "2.0.0")) + repo.add_package(get_package("pyramid", "1.10")) + + command = app.find("init") + command._pool = poetry.pool + + mocker.patch("poetry.utils._compat.Path.open") + p = mocker.patch("poetry.utils._compat.Path.cwd") + p.return_value = Path(__file__) + + tester = CommandTester(command) + tester.set_inputs( + [ + "my-package", # Package name + "1.2.3", # Version + "This is a description", # Description + "n", # Author + "MIT", # License + "~2.7 || ^3.6", # Python + "", # Interactive packages + "pyramid", # Search for package + "0", # First option + "", # Do not set constraint + "", # Stop searching for packages + "n", # Interactive dev packages + "\n", # Generate + ] + ) + tester.execute([("command", command.name), ("--dependency", ["pendulum"])]) + + output = tester.get_display(True) + expected = """\ +[tool.poetry] +name = "my-package" +version = "1.2.3" +description = "This is a description" +authors = ["Your Name "] +license = "MIT" + +[tool.poetry.dependencies] +python = "~2.7 || ^3.6" +pendulum = "^2.0" +pyramid = "^1.10" + +[tool.poetry.dev-dependencies] +""" + + assert expected in output + + +def test_predefined_dev_dependency(app, repo, mocker, poetry): + repo.add_package(get_package("pytest", "3.6.0")) + + command = app.find("init") + command._pool = poetry.pool + + mocker.patch("poetry.utils._compat.Path.open") + p = mocker.patch("poetry.utils._compat.Path.cwd") + p.return_value = Path(__file__) + + tester = CommandTester(command) + tester.set_inputs( + [ + "my-package", # Package name + "1.2.3", # Version + "This is a description", # Description + "n", # Author + "MIT", # License + "~2.7 || ^3.6", # Python + "n", # Interactive packages + "n", # Interactive dev packages + "\n", # Generate + ] + ) + tester.execute([("command", command.name), ("--dev-dependency", ["pytest"])]) + + output = tester.get_display(True) + expected = """\ +[tool.poetry] +name = "my-package" +version = "1.2.3" +description = "This is a description" +authors = ["Your Name "] +license = "MIT" + +[tool.poetry.dependencies] +python = "~2.7 || ^3.6" + +[tool.poetry.dev-dependencies] +pytest = "^3.6" +""" + + assert expected in output + + +def test_predefined_and_interactive_dev_dependencies(app, repo, mocker, poetry): + repo.add_package(get_package("pytest", "3.6.0")) + repo.add_package(get_package("pytest-requests", "0.2.0")) + + command = app.find("init") + command._pool = poetry.pool + + mocker.patch("poetry.utils._compat.Path.open") + p = mocker.patch("poetry.utils._compat.Path.cwd") + p.return_value = Path(__file__) + + tester = CommandTester(command) + tester.set_inputs( + [ + "my-package", # Package name + "1.2.3", # Version + "This is a description", # Description + "n", # Author + "MIT", # License + "~2.7 || ^3.6", # Python + "n", # Interactive packages + "", # Interactive dev packages + "pytest-requests", # Search for package + "0", # Select first option + "", # Do not set constraint + "", # Stop searching for dev packages + "\n", # Generate + ] + ) + tester.execute([("command", command.name), ("--dev-dependency", ["pytest"])]) + + output = tester.get_display(True) + expected = """\ +[tool.poetry] +name = "my-package" +version = "1.2.3" +description = "This is a description" +authors = ["Your Name "] +license = "MIT" + +[tool.poetry.dependencies] +python = "~2.7 || ^3.6" + +[tool.poetry.dev-dependencies] +pytest = "^3.6" +pytest-requests = "^0.2.0" +""" + + assert expected in output From 21dc791772ba3865afc033ea93088634e87bc780 Mon Sep 17 00:00:00 2001 From: Dawid Zych Date: Tue, 8 Jan 2019 21:35:17 +0100 Subject: [PATCH 2/3] Work around non-deterministic dependency ordering --- tests/console/commands/test_init.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/console/commands/test_init.py b/tests/console/commands/test_init.py index 7d70594c514..7fc9e18d2d2 100644 --- a/tests/console/commands/test_init.py +++ b/tests/console/commands/test_init.py @@ -573,13 +573,11 @@ def test_predefined_and_interactive_dependencies(app, repo, mocker, poetry): [tool.poetry.dependencies] python = "~2.7 || ^3.6" -pendulum = "^2.0" -pyramid = "^1.10" - -[tool.poetry.dev-dependencies] """ assert expected in output + assert 'pendulum = "^2.0"' in output + assert 'pyramid = "^1.10"' in output def test_predefined_dev_dependency(app, repo, mocker, poetry): @@ -671,8 +669,8 @@ def test_predefined_and_interactive_dev_dependencies(app, repo, mocker, poetry): python = "~2.7 || ^3.6" [tool.poetry.dev-dependencies] -pytest = "^3.6" -pytest-requests = "^0.2.0" """ assert expected in output + assert 'pytest-requests = "^0.2.0"' in output + assert 'pytest = "^3.6"' in output From d115b4791c519699b7d1e0a3456e5a1074c03eb4 Mon Sep 17 00:00:00 2001 From: finswimmer Date: Sat, 20 Jun 2020 20:33:05 +0200 Subject: [PATCH 3/3] fix (test_init): fix tests after rebase --- tests/console/commands/test_init.py | 142 +++++++++++++--------------- 1 file changed, 67 insertions(+), 75 deletions(-) diff --git a/tests/console/commands/test_init.py b/tests/console/commands/test_init.py index 7fc9e18d2d2..59c263d91ae 100644 --- a/tests/console/commands/test_init.py +++ b/tests/console/commands/test_init.py @@ -497,22 +497,19 @@ def test_predefined_dependency(app, repo, mocker, poetry): p.return_value = Path(__file__) tester = CommandTester(command) - tester.set_inputs( - [ - "my-package", # Package name - "1.2.3", # Version - "This is a description", # Description - "n", # Author - "MIT", # License - "~2.7 || ^3.6", # Python - "n", # Interactive packages - "n", # Interactive dev packages - "\n", # Generate - ] - ) - tester.execute([("command", command.name), ("--dependency", ["pendulum"])]) + inputs = [ + "my-package", # Package name + "1.2.3", # Version + "This is a description", # Description + "n", # Author + "MIT", # License + "~2.7 || ^3.6", # Python + "n", # Interactive packages + "n", # Interactive dev packages + "\n", # Generate + ] + tester.execute("--dependency pendulum", inputs="\n".join(inputs)) - output = tester.get_display(True) expected = """\ [tool.poetry] name = "my-package" @@ -523,12 +520,12 @@ def test_predefined_dependency(app, repo, mocker, poetry): [tool.poetry.dependencies] python = "~2.7 || ^3.6" -pendulum = "^2.0" +pendulum = "^2.0.0" [tool.poetry.dev-dependencies] """ - assert expected in output + assert expected in tester.io.fetch_output() def test_predefined_and_interactive_dependencies(app, repo, mocker, poetry): @@ -543,26 +540,24 @@ def test_predefined_and_interactive_dependencies(app, repo, mocker, poetry): p.return_value = Path(__file__) tester = CommandTester(command) - tester.set_inputs( - [ - "my-package", # Package name - "1.2.3", # Version - "This is a description", # Description - "n", # Author - "MIT", # License - "~2.7 || ^3.6", # Python - "", # Interactive packages - "pyramid", # Search for package - "0", # First option - "", # Do not set constraint - "", # Stop searching for packages - "n", # Interactive dev packages - "\n", # Generate - ] - ) - tester.execute([("command", command.name), ("--dependency", ["pendulum"])]) + inputs = [ + "my-package", # Package name + "1.2.3", # Version + "This is a description", # Description + "n", # Author + "MIT", # License + "~2.7 || ^3.6", # Python + "", # Interactive packages + "pyramid", # Search for package + "0", # First option + "", # Do not set constraint + "", # Stop searching for packages + "n", # Interactive dev packages + "\n", # Generate + ] + + tester.execute("--dependency pendulum", inputs="\n".join(inputs)) - output = tester.get_display(True) expected = """\ [tool.poetry] name = "my-package" @@ -574,9 +569,9 @@ def test_predefined_and_interactive_dependencies(app, repo, mocker, poetry): [tool.poetry.dependencies] python = "~2.7 || ^3.6" """ - + output = tester.io.fetch_output() assert expected in output - assert 'pendulum = "^2.0"' in output + assert 'pendulum = "^2.0.0"' in output assert 'pyramid = "^1.10"' in output @@ -591,22 +586,20 @@ def test_predefined_dev_dependency(app, repo, mocker, poetry): p.return_value = Path(__file__) tester = CommandTester(command) - tester.set_inputs( - [ - "my-package", # Package name - "1.2.3", # Version - "This is a description", # Description - "n", # Author - "MIT", # License - "~2.7 || ^3.6", # Python - "n", # Interactive packages - "n", # Interactive dev packages - "\n", # Generate - ] - ) - tester.execute([("command", command.name), ("--dev-dependency", ["pytest"])]) + inputs = [ + "my-package", # Package name + "1.2.3", # Version + "This is a description", # Description + "n", # Author + "MIT", # License + "~2.7 || ^3.6", # Python + "n", # Interactive packages + "n", # Interactive dev packages + "\n", # Generate + ] + + tester.execute("--dev-dependency pytest", inputs="\n".join(inputs)) - output = tester.get_display(True) expected = """\ [tool.poetry] name = "my-package" @@ -619,10 +612,10 @@ def test_predefined_dev_dependency(app, repo, mocker, poetry): python = "~2.7 || ^3.6" [tool.poetry.dev-dependencies] -pytest = "^3.6" +pytest = "^3.6.0" """ - assert expected in output + assert expected in tester.io.fetch_output() def test_predefined_and_interactive_dev_dependencies(app, repo, mocker, poetry): @@ -637,26 +630,24 @@ def test_predefined_and_interactive_dev_dependencies(app, repo, mocker, poetry): p.return_value = Path(__file__) tester = CommandTester(command) - tester.set_inputs( - [ - "my-package", # Package name - "1.2.3", # Version - "This is a description", # Description - "n", # Author - "MIT", # License - "~2.7 || ^3.6", # Python - "n", # Interactive packages - "", # Interactive dev packages - "pytest-requests", # Search for package - "0", # Select first option - "", # Do not set constraint - "", # Stop searching for dev packages - "\n", # Generate - ] - ) - tester.execute([("command", command.name), ("--dev-dependency", ["pytest"])]) + inputs = [ + "my-package", # Package name + "1.2.3", # Version + "This is a description", # Description + "n", # Author + "MIT", # License + "~2.7 || ^3.6", # Python + "n", # Interactive packages + "", # Interactive dev packages + "pytest-requests", # Search for package + "0", # Select first option + "", # Do not set constraint + "", # Stop searching for dev packages + "\n", # Generate + ] + + tester.execute("--dev-dependency pytest", inputs="\n".join(inputs)) - output = tester.get_display(True) expected = """\ [tool.poetry] name = "my-package" @@ -671,6 +662,7 @@ def test_predefined_and_interactive_dev_dependencies(app, repo, mocker, poetry): [tool.poetry.dev-dependencies] """ + output = tester.io.fetch_output() assert expected in output assert 'pytest-requests = "^0.2.0"' in output - assert 'pytest = "^3.6"' in output + assert 'pytest = "^3.6.0"' in output