"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "tests/console/commands/test_init.py" between
poetry-1.1.15.tar.gz and poetry-1.2.0.tar.gz

About: Poetry is a tool for dependency management and packaging in Python.

test_init.py  (poetry-1.1.15):test_init.py  (poetry-1.2.0)
from __future__ import annotations
import os import os
import shutil import shutil
import sys import sys
from pathlib import Path
from typing import TYPE_CHECKING
import pytest import pytest
from cleo import CommandTester from cleo.testers.command_tester import CommandTester
from packaging.utils import canonicalize_name
from poetry.console.commands.init import InitCommand
from poetry.repositories import Pool from poetry.repositories import Pool
from poetry.utils._compat import Path
from poetry.utils._compat import decode from poetry.utils._compat import decode
from tests.helpers import TestApplication from tests.helpers import PoetryTestApplication
from tests.helpers import get_package from tests.helpers import get_package
if TYPE_CHECKING:
from collections.abc import Iterator
from _pytest.fixtures import FixtureRequest
from poetry.core.packages.package import Package
from pytest_mock import MockerFixture
from poetry.poetry import Poetry
from tests.helpers import TestRepository
from tests.types import FixtureDirGetter
@pytest.fixture @pytest.fixture
def source_dir(tmp_path): # type: (...) -> Path def source_dir(tmp_path: Path) -> Iterator[Path]:
cwd = os.getcwd() cwd = os.getcwd()
try: try:
os.chdir(str(tmp_path)) os.chdir(str(tmp_path))
yield Path(tmp_path.as_posix()) yield Path(tmp_path.as_posix())
finally: finally:
os.chdir(cwd) os.chdir(cwd)
@pytest.fixture @pytest.fixture
def patches(mocker, source_dir, repo): def patches(mocker: MockerFixture, source_dir: Path, repo: TestRepository) -> No
mocker.patch("poetry.utils._compat.Path.cwd", return_value=source_dir) ne:
mocker.patch("pathlib.Path.cwd", return_value=source_dir)
mocker.patch( mocker.patch(
"poetry.console.commands.init.InitCommand._get_pool", return_value=Pool( [repo]) "poetry.console.commands.init.InitCommand._get_pool", return_value=Pool( [repo])
) )
@pytest.fixture @pytest.fixture
def tester(patches): def tester(patches: None) -> CommandTester:
# we need a test application without poetry here. # we need a test application without poetry here.
app = TestApplication(None) app = PoetryTestApplication(None)
return CommandTester(app.find("init")) return CommandTester(app.find("init"))
@pytest.fixture @pytest.fixture
def init_basic_inputs(): def init_basic_inputs() -> str:
return "\n".join( return "\n".join(
[ [
"my-package", # Package name "my-package", # Package name
"1.2.3", # Version "1.2.3", # Version
"This is a description", # Description "This is a description", # Description
"n", # Author "n", # Author
"MIT", # License "MIT", # License
"~2.7 || ^3.6", # Python "~2.7 || ^3.6", # Python
"n", # Interactive packages "n", # Interactive packages
"n", # Interactive dev packages "n", # Interactive dev packages
"\n", # Generate "\n", # Generate
] ]
) )
@pytest.fixture() @pytest.fixture()
def init_basic_toml(): def init_basic_toml() -> str:
return """\ return """\
[tool.poetry] [tool.poetry]
name = "my-package" name = "my-package"
version = "1.2.3" version = "1.2.3"
description = "This is a description" description = "This is a description"
authors = ["Your Name <you@example.com>"] authors = ["Your Name <you@example.com>"]
license = "MIT" license = "MIT"
readme = "README.md"
packages = [{include = "my_package"}]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "~2.7 || ^3.6" python = "~2.7 || ^3.6"
[tool.poetry.dev-dependencies]
""" """
def test_basic_interactive(tester, init_basic_inputs, init_basic_toml): def test_basic_interactive(
tester: CommandTester, init_basic_inputs: str, init_basic_toml: str
):
tester.execute(inputs=init_basic_inputs) tester.execute(inputs=init_basic_inputs)
assert init_basic_toml in tester.io.fetch_output() assert init_basic_toml in tester.io.fetch_output()
def test_interactive_with_dependencies(tester, repo): def test_noninteractive(
app: PoetryTestApplication,
mocker: MockerFixture,
poetry: Poetry,
repo: TestRepository,
tmp_path: Path,
):
command = app.find("init")
command._pool = poetry.pool
repo.add_package(get_package("pytest", "3.6.0"))
p = mocker.patch("pathlib.Path.cwd")
p.return_value = tmp_path
tester = CommandTester(command)
args = "--name my-package --dependency pytest"
tester.execute(args=args, interactive=False)
expected = "Using version ^3.6.0 for pytest\n"
assert tester.io.fetch_output() == expected
assert tester.io.fetch_error() == ""
toml_content = (tmp_path / "pyproject.toml").read_text()
assert 'name = "my-package"' in toml_content
assert 'pytest = "^3.6.0"' in toml_content
def test_interactive_with_dependencies(tester: CommandTester, repo: TestReposito
ry):
repo.add_package(get_package("django-pendulum", "0.1.6-pre4")) repo.add_package(get_package("django-pendulum", "0.1.6-pre4"))
repo.add_package(get_package("pendulum", "2.0.0")) repo.add_package(get_package("pendulum", "2.0.0"))
repo.add_package(get_package("pytest", "3.6.0")) repo.add_package(get_package("pytest", "3.6.0"))
repo.add_package(get_package("flask", "2.0.0"))
inputs = [ inputs = [
"my-package", # Package name "my-package", # Package name
"1.2.3", # Version "1.2.3", # Version
"This is a description", # Description "This is a description", # Description
"n", # Author "n", # Author
"MIT", # License "MIT", # License
"~2.7 || ^3.6", # Python "~2.7 || ^3.6", # Python
"", # Interactive packages "", # Interactive packages
"pendulu", # Search for package "pendulu", # Search for package
"1", # Second option is pendulum "1", # Second option is pendulum
"", # Do not set constraint "", # Do not set constraint
"Flask",
"0",
"",
"", # Stop searching for packages "", # Stop searching for packages
"", # Interactive dev packages "", # Interactive dev packages
"pytest", # Search for package "pytest", # Search for package
"0", "0",
"", "",
"", "",
"\n", # Generate "\n", # Generate
] ]
tester.execute(inputs="\n".join(inputs)) tester.execute(inputs="\n".join(inputs))
expected = """\ expected = """\
[tool.poetry] [tool.poetry]
name = "my-package" name = "my-package"
version = "1.2.3" version = "1.2.3"
description = "This is a description" description = "This is a description"
authors = ["Your Name <you@example.com>"] authors = ["Your Name <you@example.com>"]
license = "MIT" license = "MIT"
readme = "README.md"
packages = [{include = "my_package"}]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "~2.7 || ^3.6" python = "~2.7 || ^3.6"
pendulum = "^2.0.0" pendulum = "^2.0.0"
flask = "^2.0.0"
[tool.poetry.dev-dependencies] [tool.poetry.group.dev.dependencies]
pytest = "^3.6.0" pytest = "^3.6.0"
""" """
assert expected in tester.io.fetch_output() assert expected in tester.io.fetch_output()
def test_empty_license(tester): # Regression test for https://github.com/python-poetry/poetry/issues/2355
def test_interactive_with_dependencies_and_no_selection(
tester: CommandTester, repo: TestRepository
):
repo.add_package(get_package("django-pendulum", "0.1.6-pre4"))
repo.add_package(get_package("pendulum", "2.0.0"))
repo.add_package(get_package("pytest", "3.6.0"))
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
"pendulu", # Search for package
"", # Do not select an option
"", # Stop searching for packages
"", # Interactive dev packages
"pytest", # Search for package
"", # Do not select an option
"",
"",
"\n", # Generate
]
tester.execute(inputs="\n".join(inputs))
expected = """\
[tool.poetry]
name = "my-package"
version = "1.2.3"
description = "This is a description"
authors = ["Your Name <you@example.com>"]
license = "MIT"
readme = "README.md"
packages = [{include = "my_package"}]
[tool.poetry.dependencies]
python = "~2.7 || ^3.6"
"""
assert expected in tester.io.fetch_output()
def test_empty_license(tester: CommandTester):
inputs = [ inputs = [
"my-package", # Package name "my-package", # Package name
"1.2.3", # Version "1.2.3", # Version
"", # Description "", # Description
"n", # Author "n", # Author
"", # License "", # License
"", # Python "", # Python
"n", # Interactive packages "n", # Interactive packages
"n", # Interactive dev packages "n", # Interactive dev packages
"\n", # Generate "\n", # Generate
] ]
tester.execute(inputs="\n".join(inputs)) tester.execute(inputs="\n".join(inputs))
expected = """\ python = ".".join(str(c) for c in sys.version_info[:2])
expected = f"""\
[tool.poetry] [tool.poetry]
name = "my-package" name = "my-package"
version = "1.2.3" version = "1.2.3"
description = "" description = ""
authors = ["Your Name <you@example.com>"] authors = ["Your Name <you@example.com>"]
readme = "README.md"
packages = [{{include = "my_package"}}]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "^{python}" python = "^{python}"
"""
[tool.poetry.dev-dependencies]
""".format(
python=".".join(str(c) for c in sys.version_info[:2])
)
assert expected in tester.io.fetch_output() assert expected in tester.io.fetch_output()
def test_interactive_with_git_dependencies(tester, repo): def test_interactive_with_git_dependencies(tester: CommandTester, repo: TestRepo sitory):
repo.add_package(get_package("pendulum", "2.0.0")) repo.add_package(get_package("pendulum", "2.0.0"))
repo.add_package(get_package("pytest", "3.6.0")) repo.add_package(get_package("pytest", "3.6.0"))
inputs = [ inputs = [
"my-package", # Package name "my-package", # Package name
"1.2.3", # Version "1.2.3", # Version
"This is a description", # Description "This is a description", # Description
"n", # Author "n", # Author
"MIT", # License "MIT", # License
"~2.7 || ^3.6", # Python "~2.7 || ^3.6", # Python
skipping to change at line 179 skipping to change at line 273
] ]
tester.execute(inputs="\n".join(inputs)) tester.execute(inputs="\n".join(inputs))
expected = """\ expected = """\
[tool.poetry] [tool.poetry]
name = "my-package" name = "my-package"
version = "1.2.3" version = "1.2.3"
description = "This is a description" description = "This is a description"
authors = ["Your Name <you@example.com>"] authors = ["Your Name <you@example.com>"]
license = "MIT" license = "MIT"
readme = "README.md"
packages = [{include = "my_package"}]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "~2.7 || ^3.6" python = "~2.7 || ^3.6"
demo = {git = "https://github.com/demo/demo.git"} demo = {git = "https://github.com/demo/demo.git"}
[tool.poetry.dev-dependencies] [tool.poetry.group.dev.dependencies]
pytest = "^3.6.0" pytest = "^3.6.0"
""" """
assert expected in tester.io.fetch_output() assert expected in tester.io.fetch_output()
def test_interactive_with_git_dependencies_with_reference(tester, repo): _generate_choice_list_packages_params: list[list[Package]] = [
[
get_package("flask-blacklist", "1.0.0"),
get_package("Flask-Shelve", "1.0.0"),
get_package("flask-pwa", "1.0.0"),
get_package("Flask-test1", "1.0.0"),
get_package("Flask-test2", "1.0.0"),
get_package("Flask-test3", "1.0.0"),
get_package("Flask-test4", "1.0.0"),
get_package("Flask-test5", "1.0.0"),
get_package("Flask", "1.0.0"),
get_package("Flask-test6", "1.0.0"),
get_package("Flask-test7", "1.0.0"),
],
[
get_package("flask-blacklist", "1.0.0"),
get_package("Flask-Shelve", "1.0.0"),
get_package("flask-pwa", "1.0.0"),
get_package("Flask-test1", "1.0.0"),
get_package("Flask", "1.0.0"),
],
]
@pytest.fixture(params=_generate_choice_list_packages_params)
def _generate_choice_list_packages(request: FixtureRequest) -> list[Package]:
return request.param
@pytest.mark.parametrize("package_name", ["flask", "Flask", "flAsK"])
def test_generate_choice_list(
tester: CommandTester,
package_name: str,
_generate_choice_list_packages: list[Package],
):
init_command = tester.command
packages = _generate_choice_list_packages
choices = init_command._generate_choice_list(
packages, canonicalize_name(package_name)
)
assert choices[0] == "Flask"
def test_interactive_with_git_dependencies_with_reference(
tester: CommandTester, repo: TestRepository
):
repo.add_package(get_package("pendulum", "2.0.0")) repo.add_package(get_package("pendulum", "2.0.0"))
repo.add_package(get_package("pytest", "3.6.0")) repo.add_package(get_package("pytest", "3.6.0"))
inputs = [ inputs = [
"my-package", # Package name "my-package", # Package name
"1.2.3", # Version "1.2.3", # Version
"This is a description", # Description "This is a description", # Description
"n", # Author "n", # Author
"MIT", # License "MIT", # License
"~2.7 || ^3.6", # Python "~2.7 || ^3.6", # Python
skipping to change at line 220 skipping to change at line 360
] ]
tester.execute(inputs="\n".join(inputs)) tester.execute(inputs="\n".join(inputs))
expected = """\ expected = """\
[tool.poetry] [tool.poetry]
name = "my-package" name = "my-package"
version = "1.2.3" version = "1.2.3"
description = "This is a description" description = "This is a description"
authors = ["Your Name <you@example.com>"] authors = ["Your Name <you@example.com>"]
license = "MIT" license = "MIT"
readme = "README.md"
packages = [{include = "my_package"}]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "~2.7 || ^3.6" python = "~2.7 || ^3.6"
demo = {git = "https://github.com/demo/demo.git", rev = "develop"} demo = {git = "https://github.com/demo/demo.git", rev = "develop"}
[tool.poetry.dev-dependencies] [tool.poetry.group.dev.dependencies]
pytest = "^3.6.0" pytest = "^3.6.0"
""" """
assert expected in tester.io.fetch_output() assert expected in tester.io.fetch_output()
def test_interactive_with_git_dependencies_and_other_name(tester, repo): def test_interactive_with_git_dependencies_and_other_name(
tester: CommandTester, repo: TestRepository
):
repo.add_package(get_package("pendulum", "2.0.0")) repo.add_package(get_package("pendulum", "2.0.0"))
repo.add_package(get_package("pytest", "3.6.0")) repo.add_package(get_package("pytest", "3.6.0"))
inputs = [ inputs = [
"my-package", # Package name "my-package", # Package name
"1.2.3", # Version "1.2.3", # Version
"This is a description", # Description "This is a description", # Description
"n", # Author "n", # Author
"MIT", # License "MIT", # License
"~2.7 || ^3.6", # Python "~2.7 || ^3.6", # Python
skipping to change at line 261 skipping to change at line 405
] ]
tester.execute(inputs="\n".join(inputs)) tester.execute(inputs="\n".join(inputs))
expected = """\ expected = """\
[tool.poetry] [tool.poetry]
name = "my-package" name = "my-package"
version = "1.2.3" version = "1.2.3"
description = "This is a description" description = "This is a description"
authors = ["Your Name <you@example.com>"] authors = ["Your Name <you@example.com>"]
license = "MIT" license = "MIT"
readme = "README.md"
packages = [{include = "my_package"}]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "~2.7 || ^3.6" python = "~2.7 || ^3.6"
demo = {git = "https://github.com/demo/pyproject-demo.git"} demo = {git = "https://github.com/demo/pyproject-demo.git"}
[tool.poetry.dev-dependencies] [tool.poetry.group.dev.dependencies]
pytest = "^3.6.0" pytest = "^3.6.0"
""" """
assert expected in tester.io.fetch_output() assert expected in tester.io.fetch_output()
def test_interactive_with_directory_dependency(tester, repo, source_dir, fixture def test_interactive_with_directory_dependency(
_dir): tester: CommandTester,
repo: TestRepository,
source_dir: Path,
fixture_dir: FixtureDirGetter,
):
repo.add_package(get_package("pendulum", "2.0.0")) repo.add_package(get_package("pendulum", "2.0.0"))
repo.add_package(get_package("pytest", "3.6.0")) repo.add_package(get_package("pytest", "3.6.0"))
demo = fixture_dir("git") / "github.com" / "demo" / "demo" demo = fixture_dir("git") / "github.com" / "demo" / "demo"
shutil.copytree(str(demo), str(source_dir / "demo")) shutil.copytree(str(demo), str(source_dir / "demo"))
inputs = [ inputs = [
"my-package", # Package name "my-package", # Package name
"1.2.3", # Version "1.2.3", # Version
"This is a description", # Description "This is a description", # Description
skipping to change at line 305 skipping to change at line 456
] ]
tester.execute(inputs="\n".join(inputs)) tester.execute(inputs="\n".join(inputs))
expected = """\ expected = """\
[tool.poetry] [tool.poetry]
name = "my-package" name = "my-package"
version = "1.2.3" version = "1.2.3"
description = "This is a description" description = "This is a description"
authors = ["Your Name <you@example.com>"] authors = ["Your Name <you@example.com>"]
license = "MIT" license = "MIT"
readme = "README.md"
packages = [{include = "my_package"}]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "~2.7 || ^3.6" python = "~2.7 || ^3.6"
demo = {path = "demo"} demo = {path = "demo"}
[tool.poetry.dev-dependencies] [tool.poetry.group.dev.dependencies]
pytest = "^3.6.0" pytest = "^3.6.0"
""" """
assert expected in tester.io.fetch_output() assert expected in tester.io.fetch_output()
def test_interactive_with_directory_dependency_and_other_name( def test_interactive_with_directory_dependency_and_other_name(
tester, repo, source_dir, fixture_dir tester: CommandTester,
repo: TestRepository,
source_dir: Path,
fixture_dir: FixtureDirGetter,
): ):
repo.add_package(get_package("pendulum", "2.0.0")) repo.add_package(get_package("pendulum", "2.0.0"))
repo.add_package(get_package("pytest", "3.6.0")) repo.add_package(get_package("pytest", "3.6.0"))
demo = fixture_dir("git") / "github.com" / "demo" / "pyproject-demo" demo = fixture_dir("git") / "github.com" / "demo" / "pyproject-demo"
shutil.copytree(str(demo), str(source_dir / "pyproject-demo")) shutil.copytree(str(demo), str(source_dir / "pyproject-demo"))
inputs = [ inputs = [
"my-package", # Package name "my-package", # Package name
"1.2.3", # Version "1.2.3", # Version
skipping to change at line 350 skipping to change at line 506
] ]
tester.execute(inputs="\n".join(inputs)) tester.execute(inputs="\n".join(inputs))
expected = """\ expected = """\
[tool.poetry] [tool.poetry]
name = "my-package" name = "my-package"
version = "1.2.3" version = "1.2.3"
description = "This is a description" description = "This is a description"
authors = ["Your Name <you@example.com>"] authors = ["Your Name <you@example.com>"]
license = "MIT" license = "MIT"
readme = "README.md"
packages = [{include = "my_package"}]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "~2.7 || ^3.6" python = "~2.7 || ^3.6"
demo = {path = "pyproject-demo"} demo = {path = "pyproject-demo"}
[tool.poetry.dev-dependencies] [tool.poetry.group.dev.dependencies]
pytest = "^3.6.0" pytest = "^3.6.0"
""" """
assert expected in tester.io.fetch_output() assert expected in tester.io.fetch_output()
def test_interactive_with_file_dependency(tester, repo, source_dir, fixture_dir) def test_interactive_with_file_dependency(
: tester: CommandTester,
repo: TestRepository,
source_dir: Path,
fixture_dir: FixtureDirGetter,
):
repo.add_package(get_package("pendulum", "2.0.0")) repo.add_package(get_package("pendulum", "2.0.0"))
repo.add_package(get_package("pytest", "3.6.0")) repo.add_package(get_package("pytest", "3.6.0"))
demo = fixture_dir("distributions") / "demo-0.1.0-py2.py3-none-any.whl" demo = fixture_dir("distributions") / "demo-0.1.0-py2.py3-none-any.whl"
shutil.copyfile(str(demo), str(source_dir / demo.name)) shutil.copyfile(str(demo), str(source_dir / demo.name))
inputs = [ inputs = [
"my-package", # Package name "my-package", # Package name
"1.2.3", # Version "1.2.3", # Version
"This is a description", # Description "This is a description", # Description
skipping to change at line 394 skipping to change at line 557
] ]
tester.execute(inputs="\n".join(inputs)) tester.execute(inputs="\n".join(inputs))
expected = """\ expected = """\
[tool.poetry] [tool.poetry]
name = "my-package" name = "my-package"
version = "1.2.3" version = "1.2.3"
description = "This is a description" description = "This is a description"
authors = ["Your Name <you@example.com>"] authors = ["Your Name <you@example.com>"]
license = "MIT" license = "MIT"
readme = "README.md"
packages = [{include = "my_package"}]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "~2.7 || ^3.6" python = "~2.7 || ^3.6"
demo = {path = "demo-0.1.0-py2.py3-none-any.whl"} demo = {path = "demo-0.1.0-py2.py3-none-any.whl"}
[tool.poetry.dev-dependencies] [tool.poetry.group.dev.dependencies]
pytest = "^3.6.0" pytest = "^3.6.0"
""" """
assert expected in tester.io.fetch_output() assert expected in tester.io.fetch_output()
def test_python_option(tester): def test_interactive_with_wrong_dependency_inputs(
tester: CommandTester, repo: TestRepository
):
repo.add_package(get_package("pendulum", "2.0.0"))
repo.add_package(get_package("pytest", "3.6.0"))
inputs = [
"my-package", # Package name
"1.2.3", # Version
"This is a description", # Description
"n", # Author
"MIT", # License
"^3.8", # Python
"", # Interactive packages
"pendulum 2.0.0 foo", # Package name and constraint (invalid)
"pendulum 2.0.0", # Package name and constraint (invalid)
"pendulum 2.0.0", # Package name and constraint (invalid)
"pendulum 2.0.0", # Package name and constraint (invalid)
"pendulum@^2.0.0", # Package name and constraint (valid)
"", # End package selection
"", # Interactive dev packages
"pytest 3.6.0 foo", # Dev package name and constraint (invalid)
"pytest 3.6.0", # Dev package name and constraint (invalid)
"pytest@3.6.0", # Dev package name and constraint (valid)
"", # End package selection
"\n", # Generate
]
tester.execute(inputs="\n".join(inputs))
expected = """\
[tool.poetry]
name = "my-package"
version = "1.2.3"
description = "This is a description"
authors = ["Your Name <you@example.com>"]
license = "MIT"
readme = "README.md"
packages = [{include = "my_package"}]
[tool.poetry.dependencies]
python = "^3.8"
pendulum = "^2.0.0"
[tool.poetry.group.dev.dependencies]
pytest = "3.6.0"
"""
assert expected in tester.io.fetch_output()
def test_python_option(tester: CommandTester):
inputs = [ inputs = [
"my-package", # Package name "my-package", # Package name
"1.2.3", # Version "1.2.3", # Version
"This is a description", # Description "This is a description", # Description
"n", # Author "n", # Author
"MIT", # License "MIT", # License
"n", # Interactive packages "n", # Interactive packages
"n", # Interactive dev packages "n", # Interactive dev packages
"\n", # Generate "\n", # Generate
] ]
tester.execute("--python '~2.7 || ^3.6'", inputs="\n".join(inputs)) tester.execute("--python '~2.7 || ^3.6'", inputs="\n".join(inputs))
expected = """\ expected = """\
[tool.poetry] [tool.poetry]
name = "my-package" name = "my-package"
version = "1.2.3" version = "1.2.3"
description = "This is a description" description = "This is a description"
authors = ["Your Name <you@example.com>"] authors = ["Your Name <you@example.com>"]
license = "MIT" license = "MIT"
readme = "README.md"
packages = [{include = "my_package"}]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "~2.7 || ^3.6" python = "~2.7 || ^3.6"
[tool.poetry.dev-dependencies]
""" """
assert expected in tester.io.fetch_output() assert expected in tester.io.fetch_output()
def test_predefined_dependency(tester, repo): def test_predefined_dependency(tester: CommandTester, repo: TestRepository):
repo.add_package(get_package("pendulum", "2.0.0")) repo.add_package(get_package("pendulum", "2.0.0"))
inputs = [ inputs = [
"my-package", # Package name "my-package", # Package name
"1.2.3", # Version "1.2.3", # Version
"This is a description", # Description "This is a description", # Description
"n", # Author "n", # Author
"MIT", # License "MIT", # License
"~2.7 || ^3.6", # Python "~2.7 || ^3.6", # Python
"n", # Interactive packages "n", # Interactive packages
skipping to change at line 457 skipping to change at line 671
] ]
tester.execute("--dependency pendulum", inputs="\n".join(inputs)) tester.execute("--dependency pendulum", inputs="\n".join(inputs))
expected = """\ expected = """\
[tool.poetry] [tool.poetry]
name = "my-package" name = "my-package"
version = "1.2.3" version = "1.2.3"
description = "This is a description" description = "This is a description"
authors = ["Your Name <you@example.com>"] authors = ["Your Name <you@example.com>"]
license = "MIT" license = "MIT"
readme = "README.md"
packages = [{include = "my_package"}]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "~2.7 || ^3.6" python = "~2.7 || ^3.6"
pendulum = "^2.0.0" pendulum = "^2.0.0"
[tool.poetry.dev-dependencies]
""" """
assert expected in tester.io.fetch_output() assert expected in tester.io.fetch_output()
def test_predefined_and_interactive_dependencies(tester, repo): def test_predefined_and_interactive_dependencies(
tester: CommandTester, repo: TestRepository
):
repo.add_package(get_package("pendulum", "2.0.0")) repo.add_package(get_package("pendulum", "2.0.0"))
repo.add_package(get_package("pyramid", "1.10")) repo.add_package(get_package("pyramid", "1.10"))
inputs = [ inputs = [
"my-package", # Package name "my-package", # Package name
"1.2.3", # Version "1.2.3", # Version
"This is a description", # Description "This is a description", # Description
"n", # Author "n", # Author
"MIT", # License "MIT", # License
"~2.7 || ^3.6", # Python "~2.7 || ^3.6", # Python
skipping to change at line 496 skipping to change at line 712
tester.execute("--dependency pendulum", inputs="\n".join(inputs)) tester.execute("--dependency pendulum", inputs="\n".join(inputs))
expected = """\ expected = """\
[tool.poetry] [tool.poetry]
name = "my-package" name = "my-package"
version = "1.2.3" version = "1.2.3"
description = "This is a description" description = "This is a description"
authors = ["Your Name <you@example.com>"] authors = ["Your Name <you@example.com>"]
license = "MIT" license = "MIT"
readme = "README.md"
packages = [{include = "my_package"}]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "~2.7 || ^3.6" python = "~2.7 || ^3.6"
""" """
output = tester.io.fetch_output() output = tester.io.fetch_output()
assert expected in output assert expected in output
assert 'pendulum = "^2.0.0"' in output assert 'pendulum = "^2.0.0"' in output
assert 'pyramid = "^1.10"' in output assert 'pyramid = "^1.10"' in output
def test_predefined_dev_dependency(tester, repo): def test_predefined_dev_dependency(tester: CommandTester, repo: TestRepository):
repo.add_package(get_package("pytest", "3.6.0")) repo.add_package(get_package("pytest", "3.6.0"))
inputs = [ inputs = [
"my-package", # Package name "my-package", # Package name
"1.2.3", # Version "1.2.3", # Version
"This is a description", # Description "This is a description", # Description
"n", # Author "n", # Author
"MIT", # License "MIT", # License
"~2.7 || ^3.6", # Python "~2.7 || ^3.6", # Python
"n", # Interactive packages "n", # Interactive packages
skipping to change at line 529 skipping to change at line 747
tester.execute("--dev-dependency pytest", inputs="\n".join(inputs)) tester.execute("--dev-dependency pytest", inputs="\n".join(inputs))
expected = """\ expected = """\
[tool.poetry] [tool.poetry]
name = "my-package" name = "my-package"
version = "1.2.3" version = "1.2.3"
description = "This is a description" description = "This is a description"
authors = ["Your Name <you@example.com>"] authors = ["Your Name <you@example.com>"]
license = "MIT" license = "MIT"
readme = "README.md"
packages = [{include = "my_package"}]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "~2.7 || ^3.6" python = "~2.7 || ^3.6"
[tool.poetry.dev-dependencies] [tool.poetry.group.dev.dependencies]
pytest = "^3.6.0" pytest = "^3.6.0"
""" """
assert expected in tester.io.fetch_output() assert expected in tester.io.fetch_output()
def test_predefined_and_interactive_dev_dependencies(tester, repo): def test_predefined_and_interactive_dev_dependencies(
tester: CommandTester, repo: TestRepository
):
repo.add_package(get_package("pytest", "3.6.0")) repo.add_package(get_package("pytest", "3.6.0"))
repo.add_package(get_package("pytest-requests", "0.2.0")) repo.add_package(get_package("pytest-requests", "0.2.0"))
inputs = [ inputs = [
"my-package", # Package name "my-package", # Package name
"1.2.3", # Version "1.2.3", # Version
"This is a description", # Description "This is a description", # Description
"n", # Author "n", # Author
"MIT", # License "MIT", # License
"~2.7 || ^3.6", # Python "~2.7 || ^3.6", # Python
skipping to change at line 568 skipping to change at line 790
tester.execute("--dev-dependency pytest", inputs="\n".join(inputs)) tester.execute("--dev-dependency pytest", inputs="\n".join(inputs))
expected = """\ expected = """\
[tool.poetry] [tool.poetry]
name = "my-package" name = "my-package"
version = "1.2.3" version = "1.2.3"
description = "This is a description" description = "This is a description"
authors = ["Your Name <you@example.com>"] authors = ["Your Name <you@example.com>"]
license = "MIT" license = "MIT"
readme = "README.md"
packages = [{include = "my_package"}]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "~2.7 || ^3.6" python = "~2.7 || ^3.6"
[tool.poetry.dev-dependencies] [tool.poetry.group.dev.dependencies]
pytest = "^3.6.0"
pytest-requests = "^0.2.0"
""" """
output = tester.io.fetch_output() output = tester.io.fetch_output()
assert expected in output assert expected in output
assert 'pytest-requests = "^0.2.0"' in output assert 'pytest-requests = "^0.2.0"' in output
assert 'pytest = "^3.6.0"' in output assert 'pytest = "^3.6.0"' in output
def test_add_package_with_extras_and_whitespace(tester): def test_predefined_all_options(tester: CommandTester, repo: TestRepository):
result = tester._command._parse_requirements(["databases[postgresql, sqlite] repo.add_package(get_package("pendulum", "2.0.0"))
"]) repo.add_package(get_package("pytest", "3.6.0"))
inputs = [
"1.2.3", # Version
"", # Author
"n", # Interactive packages
"n", # Interactive dev packages
"\n", # Generate
]
tester.execute(
"--name my-package "
"--description 'This is a description' "
"--author 'Foo Bar <foo@example.com>' "
"--python '^3.8' "
"--license MIT "
"--dependency pendulum "
"--dev-dependency pytest",
inputs="\n".join(inputs),
)
expected = """\
[tool.poetry]
name = "my-package"
version = "1.2.3"
description = "This is a description"
authors = ["Foo Bar <foo@example.com>"]
license = "MIT"
readme = "README.md"
packages = [{include = "my_package"}]
[tool.poetry.dependencies]
python = "^3.8"
pendulum = "^2.0.0"
[tool.poetry.group.dev.dependencies]
pytest = "^3.6.0"
"""
output = tester.io.fetch_output()
assert expected in output
def test_add_package_with_extras_and_whitespace(tester: CommandTester):
result = tester.command._parse_requirements(["databases[postgresql, sqlite]"
])
assert result[0]["name"] == "databases" assert result[0]["name"] == "databases"
assert len(result[0]["extras"]) == 2 assert len(result[0]["extras"]) == 2
assert "postgresql" in result[0]["extras"] assert "postgresql" in result[0]["extras"]
assert "sqlite" in result[0]["extras"] assert "sqlite" in result[0]["extras"]
def test_init_existing_pyproject_simple( def test_init_existing_pyproject_simple(
tester, source_dir, init_basic_inputs, init_basic_toml tester: CommandTester,
source_dir: Path,
init_basic_inputs: str,
init_basic_toml: str,
): ):
pyproject_file = source_dir / "pyproject.toml" pyproject_file = source_dir / "pyproject.toml"
existing_section = """ existing_section = """
[tool.black] [tool.black]
line-length = 88 line-length = 88
""" """
pyproject_file.write_text(decode(existing_section)) pyproject_file.write_text(decode(existing_section))
tester.execute(inputs=init_basic_inputs) tester.execute(inputs=init_basic_inputs)
assert ( assert f"{existing_section}\n{init_basic_toml}" in pyproject_file.read_text(
"{}\n{}".format(existing_section, init_basic_toml) in pyproject_file.rea )
d_text()
@pytest.mark.parametrize("linesep", ["\n", "\r\n"])
def test_init_existing_pyproject_consistent_linesep(
tester: CommandTester,
source_dir: Path,
init_basic_inputs: str,
init_basic_toml: str,
linesep: str,
):
pyproject_file = source_dir / "pyproject.toml"
existing_section = """
[tool.black]
line-length = 88
""".replace(
"\n", linesep
) )
with open(pyproject_file, "w", newline="") as f:
f.write(existing_section)
tester.execute(inputs=init_basic_inputs)
with open(pyproject_file, newline="") as f:
content = f.read()
init_basic_toml = init_basic_toml.replace("\n", linesep)
assert f"{existing_section}{linesep}{init_basic_toml}" in content
def test_init_non_interactive_existing_pyproject_add_dependency( def test_init_non_interactive_existing_pyproject_add_dependency(
tester, source_dir, init_basic_inputs, repo tester: CommandTester,
source_dir: Path,
init_basic_inputs: str,
repo: TestRepository,
): ):
pyproject_file = source_dir / "pyproject.toml" pyproject_file = source_dir / "pyproject.toml"
existing_section = """ existing_section = """
[tool.black] [tool.black]
line-length = 88 line-length = 88
""" """
pyproject_file.write_text(decode(existing_section)) pyproject_file.write_text(decode(existing_section))
repo.add_package(get_package("foo", "1.19.2")) repo.add_package(get_package("foo", "1.19.2"))
skipping to change at line 628 skipping to change at line 925
"--dependency foo", "--dependency foo",
interactive=False, interactive=False,
) )
expected = """\ expected = """\
[tool.poetry] [tool.poetry]
name = "my-package" name = "my-package"
version = "0.1.0" version = "0.1.0"
description = "" description = ""
authors = ["Your Name <you@example.com>"] authors = ["Your Name <you@example.com>"]
readme = "README.md"
packages = [{include = "my_package"}]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "^3.6" python = "^3.6"
foo = "^1.19.2" foo = "^1.19.2"
[tool.poetry.dev-dependencies]
""" """
assert "{}\n{}".format(existing_section, expected) in pyproject_file.read_te xt() assert f"{existing_section}\n{expected}" in pyproject_file.read_text()
def test_init_existing_pyproject_with_build_system_fails( def test_init_existing_pyproject_with_build_system_fails(
tester, source_dir, init_basic_inputs tester: CommandTester, source_dir: Path, init_basic_inputs: str
): ):
pyproject_file = source_dir / "pyproject.toml" pyproject_file = source_dir / "pyproject.toml"
existing_section = """ existing_section = """
[build-system] [build-system]
requires = ["setuptools >= 40.6.0", "wheel"] requires = ["setuptools >= 40.6.0", "wheel"]
build-backend = "setuptools.build_meta" build-backend = "setuptools.build_meta"
""" """
pyproject_file.write_text(decode(existing_section)) pyproject_file.write_text(decode(existing_section))
tester.execute(inputs=init_basic_inputs) tester.execute(inputs=init_basic_inputs)
assert ( assert (
tester.io.fetch_output().strip() tester.io.fetch_error().strip()
== "A pyproject.toml file with a defined build-system already exists." == "A pyproject.toml file with a defined build-system already exists."
) )
assert "{}".format(existing_section) in pyproject_file.read_text() assert existing_section in pyproject_file.read_text()
@pytest.mark.parametrize(
"name",
[
None,
"",
"foo",
" foo ",
"foo==2.0",
"foo@2.0",
" foo@2.0 ",
"foo 2.0",
" foo 2.0 ",
],
)
def test__validate_package_valid(name: str | None):
assert InitCommand._validate_package(name) == name
@pytest.mark.parametrize(
"name", ["foo bar 2.0", " foo bar 2.0 ", "foo bar foobar 2.0"]
)
def test__validate_package_invalid(name: str):
with pytest.raises(ValueError):
assert InitCommand._validate_package(name)
 End of changes. 69 change blocks. 
61 lines changed or deleted 358 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)