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

fix: -o should be relative to the cwd #130

Merged
merged 4 commits into from
Oct 1, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/poetry_plugin_export/command.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from pathlib import Path

from cleo.helpers import option
from poetry.console.commands.group_command import GroupCommand
from poetry.core.packages.dependency_group import MAIN_GROUP
Expand Down Expand Up @@ -99,4 +101,4 @@ def handle(self) -> None:
exporter.with_hashes(not self.option("without-hashes"))
exporter.with_credentials(self.option("with-credentials"))
exporter.with_urls(not self.option("without-urls"))
exporter.export(fmt, self.poetry.file.parent, output or self.io)
exporter.export(fmt, Path.cwd(), output or self.io)
7 changes: 4 additions & 3 deletions tests/command/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING

import pytest
Expand All @@ -15,6 +14,8 @@


if TYPE_CHECKING:
from pathlib import Path

from poetry.installation.executor import Executor
from poetry.poetry import Poetry
from poetry.utils.env import Env
Expand All @@ -30,8 +31,8 @@ def app(poetry: Poetry) -> PoetryTestApplication:


@pytest.fixture
def env(tmp_dir: str) -> MockEnv:
path = Path(tmp_dir) / ".venv"
def env(tmp_path: Path) -> MockEnv:
path = tmp_path / ".venv"
path.mkdir(parents=True)
return MockEnv(path=path, is_venv=True)

Expand Down
19 changes: 12 additions & 7 deletions tests/command/test_command_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@


if TYPE_CHECKING:
from pathlib import Path

from _pytest.monkeypatch import MonkeyPatch
from cleo.testers.command_tester import CommandTester
from poetry.poetry import Poetry
Expand Down Expand Up @@ -89,10 +91,13 @@ def tester(
return command_tester_factory("export", poetry=poetry)


def _export_requirements(tester: CommandTester, poetry: Poetry) -> None:
tester.execute("--format requirements.txt --output requirements.txt")
def _export_requirements(tester: CommandTester, poetry: Poetry, tmp_path: Path) -> None:
from tests.helpers import as_cwd

with as_cwd(tmp_path):
tester.execute("--format requirements.txt --output requirements.txt")

requirements = poetry.file.parent / "requirements.txt"
requirements = tmp_path / "requirements.txt"
assert requirements.exists()

with requirements.open(encoding="utf-8") as f:
Expand All @@ -108,17 +113,17 @@ def _export_requirements(tester: CommandTester, poetry: Poetry) -> None:


def test_export_exports_requirements_txt_file_locks_if_no_lock_file(
tester: CommandTester, poetry: Poetry
tester: CommandTester, poetry: Poetry, tmp_path: Path
) -> None:
assert not poetry.locker.lock.exists()
_export_requirements(tester, poetry)
_export_requirements(tester, poetry, tmp_path)
assert "The lock file does not exist. Locking." in tester.io.fetch_error()


def test_export_exports_requirements_txt_uses_lock_file(
tester: CommandTester, poetry: Poetry, do_lock: None
tester: CommandTester, poetry: Poetry, tmp_path: Path, do_lock: None
) -> None:
_export_requirements(tester, poetry)
_export_requirements(tester, poetry, tmp_path)
assert "The lock file does not exist. Locking." not in tester.io.fetch_error()


Expand Down
40 changes: 9 additions & 31 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from __future__ import annotations

import shutil
import sys
import tempfile

from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
from typing import Iterator

import pytest

Expand All @@ -27,7 +24,6 @@
from poetry.poetry import Poetry
from pytest_mock import MockerFixture

from tests.types import FixtureDirGetter
from tests.types import ProjectFactory


Expand All @@ -52,15 +48,11 @@ def all(self) -> dict[str, Any]:


@pytest.fixture
def config_cache_dir(tmp_dir: str) -> Path:
path = Path(tmp_dir) / ".cache" / "pypoetry"
def config_cache_dir(tmp_path: Path) -> Path:
path = tmp_path / ".cache" / "pypoetry"
path.mkdir(parents=True)
return path


@pytest.fixture
def config_virtualenvs_path(config_cache_dir: Path) -> Path:
return config_cache_dir / "virtualenvs"
return path


@pytest.fixture
Expand Down Expand Up @@ -102,25 +94,13 @@ def config(


@pytest.fixture
def tmp_dir() -> Iterator[str]:
dir_ = tempfile.mkdtemp(prefix="poetry_")

yield dir_

shutil.rmtree(dir_)
def fixture_root() -> Path:
return Path(__file__).parent / "fixtures"


@pytest.fixture
def fixture_base() -> Path:
return Path(__file__).parent.joinpath("fixtures")


@pytest.fixture
def fixture_dir(fixture_base: Path) -> FixtureDirGetter:
def _fixture_dir(name: str) -> Path:
return fixture_base / name

return _fixture_dir
def fixture_root_uri(fixture_root: Path) -> str:
return fixture_root.as_uri()


@pytest.fixture()
Expand Down Expand Up @@ -150,14 +130,12 @@ def default_python(current_python: tuple[int, int, int]) -> str:

@pytest.fixture
def project_factory(
tmp_dir: str,
tmp_path: Path,
config: Config,
repo: Repository,
installed: Repository,
default_python: str,
) -> ProjectFactory:
workspace = Path(tmp_dir)

def _factory(
name: str,
dependencies: dict[str, str] | None = None,
Expand All @@ -166,7 +144,7 @@ def _factory(
poetry_lock_content: str | None = None,
install_deps: bool = True,
) -> Poetry:
project_dir = workspace / f"poetry-fixture-{name}"
project_dir = tmp_path / f"poetry-fixture-{name}"
dependencies = dependencies or {}
dev_dependencies = dev_dependencies or {}

Expand Down
14 changes: 14 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from __future__ import annotations

import os

from contextlib import contextmanager
from typing import TYPE_CHECKING
from typing import Any
from typing import Iterator

from poetry.console.application import Application
from poetry.core.toml.file import TOMLFile
Expand Down Expand Up @@ -109,3 +113,13 @@ def _execute_update(self, operation: Operation) -> int:

def _execute_remove(self, operation: Operation) -> int:
return 0


@contextmanager
def as_cwd(path: Path) -> Iterator[Path]:
old_cwd = os.getcwd()
os.chdir(path)
try:
yield path
finally:
os.chdir(old_cwd)
Loading