Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read package name from pyproject.toml #188

Merged
merged 6 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Changelog = "https://github.com/cjolowicz/nox-poetry/releases"
[tool.poetry.dependencies]
python = "^3.6.1"
nox = "^2020.5.24"
tomlkit = "^0.7.0"

[tool.poetry.dev-dependencies]
pytest = "^6.1.2"
Expand Down
3 changes: 1 addition & 2 deletions src/nox_poetry/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ def build_package(session: Session, *, distribution_format: DistributionFormat)
url = f"file://{wheel.resolve().as_posix()}#sha256={digest}"

if distribution_format is DistributionFormat.SDIST:
name = poetry.version().split()[0]
url += f"&egg={name}"
url += f"&egg={poetry.config.name}"

return url

Expand Down
44 changes: 28 additions & 16 deletions src/nox_poetry/poetry.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Poetry interface."""
from enum import Enum
from pathlib import Path
from typing import Optional

import tomlkit
from nox.sessions import Session


Expand All @@ -12,6 +14,24 @@ class DistributionFormat(Enum):
SDIST = "sdist"


class Config:
"""Poetry configuration."""

def __init__(self, project: Path) -> None:
"""Initialize."""
path = project / "pyproject.toml"
text = path.read_text()
data = tomlkit.parse(text)
self._config = data["tool"]["poetry"]

@property
def name(self) -> str:
"""Return the package name."""
name = self._config["name"]
assert isinstance(name, str) # noqa: S101
return name


class Poetry:
"""Helper class for invoking Poetry inside a Nox session.

Expand All @@ -22,6 +42,14 @@ class Poetry:
def __init__(self, session: Session) -> None:
"""Initialize."""
self.session = session
self._config: Optional[Config] = None

@property
def config(self) -> Config:
"""Return the package configuration."""
if self._config is None:
self._config = Config(Path.cwd())
return self._config

def export(self, path: Path) -> None:
"""Export the lock file to requirements format.
Expand Down Expand Up @@ -69,19 +97,3 @@ def build(self, *, format: DistributionFormat) -> str:
)
assert isinstance(output, str) # noqa: S101
return output.split()[-1]

def version(self) -> str:
"""Return the package name and version.

Returns:
The package name and version.
"""
output = self.session.run(
"poetry",
"version",
external=True,
silent=True,
stderr=None,
)
assert isinstance(output, str) # noqa: S101
return output.strip()
8 changes: 8 additions & 0 deletions tests/unit/test_nox_poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import nox_poetry
from nox_poetry.poetry import DistributionFormat
from nox_poetry.poetry import Poetry


def test_install_package(session: Session) -> None:
Expand Down Expand Up @@ -33,3 +34,10 @@ def test_patch(session: Session) -> None:
import nox_poetry.patch # noqa: F401

Session.install(session, ".")


def test_poetry_config(session: Session) -> None:
"""It caches the configuration when accessed multiple times."""
poetry = Poetry(session)
poetry.config
assert poetry.config.name == "nox-poetry"