Skip to content

Commit

Permalink
Added support for v2 poetry (#53)
Browse files Browse the repository at this point in the history
* Added support for v2 poetry

* Fixed CI

* Updated pre-commit hooks
  • Loading branch information
ag14774 authored Jan 28, 2025
1 parent c940b03 commit 019a909
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 144 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ concurrency:

jobs:
tests:
name: ${{ matrix.os }} / ${{ matrix.python-version }} ${{ matrix.suffix }}
name: ${{ matrix.os }} / ${{ matrix.python-version }} / ${{ matrix.poetry-version }} ${{ matrix.suffix }}
runs-on: ${{ matrix.image }}
defaults:
run:
Expand All @@ -26,6 +26,7 @@ jobs:
matrix:
os: [Ubuntu, macOS, Windows]
python-version: ["3.9", "3.10", "3.11"]
poetry-version: ["1.8.5", "2.0.1", ""]
include:
- os: Ubuntu
image: ubuntu-latest
Expand All @@ -50,6 +51,8 @@ jobs:
- name: Bootstrap poetry
run: |
curl -sL https://install.python-poetry.org | python - -y ${{ matrix.bootstrap-args }}
env:
POETRY_VERSION: ${{ matrix.poetry-version }}

- name: Update PATH
if: ${{ matrix.os != 'Windows' }}
Expand All @@ -67,7 +70,7 @@ jobs:
id: cache
with:
path: .venv
key: venv-${{ runner.os }}-${{ steps.full-python-version.outputs.version }}-${{ hashFiles('**/poetry.lock') }}
key: venv-${{ runner.os }}-${{ steps.full-python-version.outputs.version }}-${{ matrix.poetry-version }}-${{ hashFiles('**/poetry.lock') }}

- name: Ensure cache is healthy
if: steps.cache.outputs.cache-hit == 'true'
Expand All @@ -76,6 +79,11 @@ jobs:
- name: Install dependencies
run: poetry install --with ci

- name: Install correct poetry version
if: matrix.poetry-version != ''
run: |
poetry run pip install "poetry==${{ matrix.poetry-version }}"
- name: Run pre-commit tests
run: |
poetry run pre-commit run --all-files
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# See https://pre-commit.com/ for usage and config
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
rev: v5.0.0
hooks:
- id: check-toml
- id: check-yaml
Expand Down
262 changes: 127 additions & 135 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion poetry_monoranger_plugin/lock_modifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def execute(self, event: ConsoleCommandEvent):
"""
command = event.command
assert isinstance(command, (LockCommand, InstallCommand, UpdateCommand)), (
f"{self.__class__.__name__} can only be used for `poetry lock`, `poetry install`, and `poetry update` commands"
f"{self.__class__.__name__} can only be used for `poetry lock`, `poetry install`, `poetry update`, `poetry sync` commands"
)

io = event.io
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ classifiers = [

[tool.poetry.dependencies]
python = "^3.9"
poetry = ">=1.8.0,<1.9.0"
poetry = ">=1.8.0"

[tool.poetry.group.dev.dependencies]
ruff = ">=0.6.9,<0.10.0"
Expand Down
5 changes: 5 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from typing import TYPE_CHECKING
from unittest.mock import patch

import poetry.__version__
import pytest
from cleo.events.console_command_event import ConsoleCommandEvent
from cleo.events.console_events import COMMAND
from cleo.io.inputs.argv_input import ArgvInput
Expand All @@ -26,6 +28,9 @@
from poetry.console.commands.command import Command
from poetry.utils.env.base_env import Env

POETRY_V2 = poetry.__version__.__version__.startswith("2")
only_poetry_v2 = pytest.mark.skipif(POETRY_V2 is False, reason="requires poetry 2.0.0 or higher")


class MockApplication(Application):
"""A mock application that stored the last command class that was executed
Expand Down
17 changes: 13 additions & 4 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import tarfile

from tests.helpers import is_system_env
import pytest

from tests.helpers import POETRY_V2, is_system_env


def test_add(repo_manager, poetry_run):
Expand Down Expand Up @@ -67,7 +69,10 @@ def test_update(repo_manager, poetry_run):
poetry_run(v1_dir, "pkg_one", "add numpy<=1.25")
pkg_one_pyproject = (v1_dir / "pkg_one" / "pyproject.toml").read_text()
(v1_dir / "pkg_one" / "pyproject.toml").write_text(pkg_one_pyproject.replace("<=1.25", "<=1.26.4"))
poetry_run(v1_dir, "pkg_one", "lock --no-update")
if POETRY_V2:
poetry_run(v1_dir, "pkg_one", "lock")
else:
poetry_run(v1_dir, "pkg_one", "lock --no-update")
# This results in a lockfile with numpy==1.25 but pyproject.toml permits up to 1.26.5

root_lock = (v1_dir / "poetry.lock").read_text()
Expand Down Expand Up @@ -109,7 +114,8 @@ def test_lock(repo_manager, poetry_run):
assert root_pyproject == (v1_dir / "pyproject.toml").read_text() # root pyproject.toml is not modified


def test_install(repo_manager, poetry_run):
@pytest.mark.parametrize("sync", [True, False])
def test_install(repo_manager, poetry_run, sync: bool):
# Arrange
v1_dir = repo_manager.get_repo("v1", preinstalled=False)
envs_before = repo_manager.get_envs(v1_dir)
Expand All @@ -121,7 +127,10 @@ def test_install(repo_manager, poetry_run):
assert is_system_env(env)

# Act
result = poetry_run(v1_dir, "pkg_one", "install")
if sync:
result = poetry_run(v1_dir, "pkg_one", "sync") if POETRY_V2 else poetry_run(v1_dir, "pkg_one", "install --sync")
else:
result = poetry_run(v1_dir, "pkg_one", "install")

# Assert
assert result.exit_code == 0
Expand Down

0 comments on commit 019a909

Please sign in to comment.