Skip to content

Commit

Permalink
tests: improve init command test setup
Browse files Browse the repository at this point in the history
This change ensures that we create closer to reality scenarios when
testing init command.

Relates-to: python-poetry#3073
  • Loading branch information
abn committed Oct 4, 2020
1 parent 32b01be commit 3539026
Showing 1 changed file with 43 additions and 15 deletions.
58 changes: 43 additions & 15 deletions tests/console/commands/test_init.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import os
import shutil
import sys

import pytest

from cleo import CommandTester

from poetry.repositories import Pool
from poetry.utils._compat import Path
from poetry.utils._compat import decode
from tests.helpers import TestApplication
from tests.helpers import get_package


Expand All @@ -13,14 +19,26 @@ def source_dir(tmp_path): # type: (Path) -> Path


@pytest.fixture(autouse=True)
def patches(mocker, source_dir):
patch = mocker.patch("poetry.utils._compat.Path.cwd")
patch.return_value = source_dir
def patches(mocker, source_dir, repo):
mocker.patch("poetry.utils._compat.Path.cwd", return_value=source_dir)
mocker.patch(
"poetry.console.commands.init.InitCommand._get_pool", return_value=Pool([repo])
)

cwd = Path()
os.chdir(str(source_dir))

try:
yield
finally:
os.chdir(str(cwd))


@pytest.fixture
def tester(command_tester_factory):
return command_tester_factory("init")
def tester():
# we need a test application without poetry here.
app = TestApplication(None)
return CommandTester(app.find("init"))


@pytest.fixture
Expand Down Expand Up @@ -265,10 +283,13 @@ def test_interactive_with_git_dependencies_and_other_name(tester, repo):
assert expected in tester.io.fetch_output()


def test_interactive_with_directory_dependency(tester, repo):
def test_interactive_with_directory_dependency(tester, repo, source_dir, fixture_dir):
repo.add_package(get_package("pendulum", "2.0.0"))
repo.add_package(get_package("pytest", "3.6.0"))

demo = fixture_dir("git") / "github.com" / "demo" / "demo"
shutil.copytree(str(demo), str(source_dir / "demo"))

inputs = [
"my-package", # Package name
"1.2.3", # Version
Expand All @@ -277,7 +298,7 @@ def test_interactive_with_directory_dependency(tester, repo):
"MIT", # License
"~2.7 || ^3.6", # Python
"", # Interactive packages
"../../fixtures/git/github.com/demo/demo", # Search for package
"./demo", # Search for package
"", # Stop searching for packages
"", # Interactive dev packages
"pytest", # Search for package
Expand All @@ -298,19 +319,23 @@ def test_interactive_with_directory_dependency(tester, repo):
[tool.poetry.dependencies]
python = "~2.7 || ^3.6"
demo = {path = "../../fixtures/git/github.com/demo/demo"}
demo = {path = "demo"}
[tool.poetry.dev-dependencies]
pytest = "^3.6.0"
"""

assert expected in tester.io.fetch_output()


def test_interactive_with_directory_dependency_and_other_name(tester, repo):
def test_interactive_with_directory_dependency_and_other_name(
tester, repo, source_dir, fixture_dir
):
repo.add_package(get_package("pendulum", "2.0.0"))
repo.add_package(get_package("pytest", "3.6.0"))

demo = fixture_dir("git") / "github.com" / "demo" / "pyproject-demo"
shutil.copytree(str(demo), str(source_dir / "pyproject-demo"))

inputs = [
"my-package", # Package name
"1.2.3", # Version
Expand All @@ -319,7 +344,7 @@ def test_interactive_with_directory_dependency_and_other_name(tester, repo):
"MIT", # License
"~2.7 || ^3.6", # Python
"", # Interactive packages
"../../fixtures/git/github.com/demo/pyproject-demo", # Search for package
"./pyproject-demo", # Search for package
"", # Stop searching for packages
"", # Interactive dev packages
"pytest", # Search for package
Expand All @@ -340,7 +365,7 @@ def test_interactive_with_directory_dependency_and_other_name(tester, repo):
[tool.poetry.dependencies]
python = "~2.7 || ^3.6"
demo = {path = "../../fixtures/git/github.com/demo/pyproject-demo"}
demo = {path = "pyproject-demo"}
[tool.poetry.dev-dependencies]
pytest = "^3.6.0"
Expand All @@ -349,10 +374,13 @@ def test_interactive_with_directory_dependency_and_other_name(tester, repo):
assert expected in tester.io.fetch_output()


def test_interactive_with_file_dependency(tester, repo):
def test_interactive_with_file_dependency(tester, repo, source_dir, fixture_dir):
repo.add_package(get_package("pendulum", "2.0.0"))
repo.add_package(get_package("pytest", "3.6.0"))

demo = fixture_dir("distributions") / "demo-0.1.0-py2.py3-none-any.whl"
shutil.copyfile(str(demo), str(source_dir / demo.name))

inputs = [
"my-package", # Package name
"1.2.3", # Version
Expand All @@ -361,7 +389,7 @@ def test_interactive_with_file_dependency(tester, repo):
"MIT", # License
"~2.7 || ^3.6", # Python
"", # Interactive packages
"../../fixtures/distributions/demo-0.1.0-py2.py3-none-any.whl", # Search for package
"./demo-0.1.0-py2.py3-none-any.whl", # Search for package
"", # Stop searching for packages
"", # Interactive dev packages
"pytest", # Search for package
Expand All @@ -382,7 +410,7 @@ def test_interactive_with_file_dependency(tester, repo):
[tool.poetry.dependencies]
python = "~2.7 || ^3.6"
demo = {path = "../../fixtures/distributions/demo-0.1.0-py2.py3-none-any.whl"}
demo = {path = "demo-0.1.0-py2.py3-none-any.whl"}
[tool.poetry.dev-dependencies]
pytest = "^3.6.0"
Expand Down

0 comments on commit 3539026

Please sign in to comment.