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: #3073
  • Loading branch information
abn authored and finswimmer committed Oct 5, 2020
1 parent 32b01be commit 1ed3828
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions tests/console/commands/test_init.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
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


@pytest.fixture
def source_dir(tmp_path): # type: (Path) -> Path
yield Path(tmp_path.as_posix())
def source_dir(tmp_path): # type: (...) -> Path
cwd = os.getcwd()

try:
os.chdir(str(tmp_path))
yield Path(tmp_path.as_posix())
finally:
os.chdir(cwd)

@pytest.fixture(autouse=True)
def patches(mocker, source_dir):
patch = mocker.patch("poetry.utils._compat.Path.cwd")
patch.return_value = source_dir

@pytest.fixture
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])
)


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


@pytest.fixture
Expand Down Expand Up @@ -265,10 +281,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 +296,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 +317,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 +342,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 +363,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 +372,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 +387,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 +408,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 1ed3828

Please sign in to comment.