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

New CI and applied new ruff rules #6

Merged
merged 3 commits into from
Sep 26, 2024
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
85 changes: 85 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Inspired by https://github.com/python-poetry/poetry-plugin-export/blob/main/.github/workflows/main.yml

name: Tests

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

# Allow only one concurrent run per branch:
# Runs currently in progress will be cancelled if a new run is triggered and if the event is a pull request
concurrency:
group: tests-${{ github.head_ref || github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
tests:
name: ${{ matrix.os }} / ${{ matrix.python-version }} ${{ matrix.suffix }}
runs-on: ${{ matrix.image }}
defaults:
run:
shell: bash
strategy:
fail-fast: false
matrix:
os: [Ubuntu, macOS, Windows]
python-version: ["3.9", "3.10", "3.11"]
include:
- os: Ubuntu
image: ubuntu-latest
- os: Windows
image: windows-latest
- os: macOS
image: macos-latest

steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

# Store full python version in a variable 'version'
- name: Get full Python version
id: full-python-version
run: echo "version=$(python -c "import sys; print('-'.join(str(v) for v in sys.version_info))")" >> $GITHUB_OUTPUT

- name: Bootstrap poetry
run: |
curl -sL https://install.python-poetry.org | python - -y ${{ matrix.bootstrap-args }}

- name: Update PATH
if: ${{ matrix.os != 'Windows' }}
run: echo "$HOME/.local/bin" >> $GITHUB_PATH

- name: Update Path for Windows
if: ${{ matrix.os == 'Windows' }}
run: echo "$APPDATA\Python\Scripts" >> $GITHUB_PATH

- name: Configure poetry
run: poetry config virtualenvs.in-project true

- name: Set up cache
uses: actions/cache@v4
id: cache
with:
path: .venv
key: venv-${{ runner.os }}-${{ steps.full-python-version.outputs.version }}-${{ hashFiles('**/poetry.lock') }}

- name: Ensure cache is healthy
if: steps.cache.outputs.cache-hit == 'true'
run: timeout 10s poetry run pip --version || rm -rf .venv

- name: Install dependencies
run: poetry install --with ci

- name: Run pre-commit tests
run: |
poetry run pre-commit run --all-files

- name: Test with pytest
run: |
poetry run pytest -v
38 changes: 0 additions & 38 deletions .github/workflows/python-tests.yml

This file was deleted.

16 changes: 15 additions & 1 deletion poetry.lock

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

4 changes: 2 additions & 2 deletions poetry_monoranger_plugin/monorepo_adder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
import copy
from typing import TYPE_CHECKING

from cleo.events.console_terminate_event import ConsoleTerminateEvent
from poetry.console.commands.add import AddCommand
from poetry.console.commands.remove import RemoveCommand
from poetry.factory import Factory
from poetry.installation.installer import Installer
from poetry.poetry import Poetry
from tomlkit.toml_document import TOMLDocument

if TYPE_CHECKING:
from cleo.events.console_command_event import ConsoleCommandEvent
from tomlkit.toml_document import TOMLDocument
from cleo.events.console_terminate_event import ConsoleTerminateEvent

from poetry_monoranger_plugin.config import MonorangerConfig

Expand Down
2 changes: 1 addition & 1 deletion poetry_monoranger_plugin/path_rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def execute(self, event: ConsoleCommandEvent):
try:
pinned = self._pin_dependency(poetry, dependency)
except (RuntimeError, ValueError) as e:
io.write_line(f"<fg=yellow>Could not pin dependency {dependency.name}: {str(e)}</>")
io.write_line(f"<fg=yellow>Could not pin dependency {dependency.name}: {e!s}</>")
continue

main_deps_group.remove_dependency(dependency.name)
Expand Down
13 changes: 7 additions & 6 deletions poetry_monoranger_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from __future__ import annotations

import copy
from typing import TYPE_CHECKING, Any, Mapping
from typing import TYPE_CHECKING, Any

import cleo.events.console_events
from cleo.events.console_command_event import ConsoleCommandEvent
Expand All @@ -25,6 +25,8 @@
from poetry_monoranger_plugin.config import MonorangerConfig

if TYPE_CHECKING:
from collections.abc import Mapping

from cleo.events.event import Event
from cleo.events.event_dispatcher import EventDispatcher
from poetry.console.application import Application
Expand Down Expand Up @@ -52,8 +54,8 @@ class Monoranger(ApplicationPlugin):
def __init__(self):
super().__init__()

self.poetry: Poetry = None # type: ignore
self.plugin_conf: MonorangerConfig = None # type: ignore
self.poetry: Poetry = None # type: ignore[assignment]
self.plugin_conf: MonorangerConfig = None # type: ignore[assignment]
self.ctx: dict[type[PoetryCommand], Any] = {}

def activate(self, application: Application):
Expand Down Expand Up @@ -105,9 +107,8 @@ def console_command_event_listener(self, event: Event, event_name: str, dispatch
from poetry_monoranger_plugin.lock_modifier import LockModifier

# NOTE: consider moving this to a separate UpdateModifier class
if isinstance(command, UpdateCommand):
if not event.io.input._arguments.get("packages", None):
event.io.input._arguments["packages"] = [command.poetry.package.name]
if isinstance(command, UpdateCommand) and not event.io.input._arguments.get("packages", None):
event.io.input._arguments["packages"] = [command.poetry.package.name]
LockModifier(self.plugin_conf).execute(event)

if isinstance(command, (AddCommand, RemoveCommand)):
Expand Down
20 changes: 19 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ pre-commit = "^3.8.0"
pre-commit-hooks = "^4.6.0"
pytest = "^8.3.3"

[tool.poetry.group.ci]
optional = true

[tool.poetry.group.ci.dependencies]
pytest-github-actions-annotate-failures = "^0.2.0"

[tool.mypy]
disable_error_code = "import-untyped"
check_untyped_defs = true
Expand All @@ -25,7 +31,19 @@ line-length = 120
target-version = "py39"

[tool.ruff.lint]
extend-select = ["I", "D"]
extend-select = [
"B", # flake8-bugbear
"C4", # fkale8-comprehensions
"D", # pydocstyle
"I", # isort
"N", # pep8-naming
"PIE", # fkale8-pie
"PGH", # pygrep-hooks
"RUF", # ruff checks
"SIM", # flake8-simplify
"TCH", # flake8-type-checking
"UP", # pyupgrade
]
# Disabled rules:
# `D104`: requires documentation in __init__.py of public packages
# `D105`: requires documentation for magic methods
Expand Down
5 changes: 2 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pathlib import Path
from typing import Type
from unittest.mock import Mock

import pytest
Expand All @@ -13,7 +12,7 @@
def mock_event_gen():
from poetry.console.commands.command import Command

def _factory(command_cls: Type[Command], disable_cache: bool):
def _factory(command_cls: type[Command], disable_cache: bool):
from cleo.events.console_command_event import ConsoleCommandEvent

main_grp = DependencyGroup("main")
Expand Down Expand Up @@ -48,7 +47,7 @@ def _factory(command_cls: Type[Command], disable_cache: bool):
def mock_terminate_event_gen(mock_event_gen):
from poetry.console.commands.command import Command

def _factory(command_cls: Type[Command], disable_cache: bool):
def _factory(command_cls: type[Command], disable_cache: bool):
from cleo.events.console_terminate_event import ConsoleTerminateEvent

mock_event = mock_event_gen(command_cls, disable_cache)
Expand Down
2 changes: 0 additions & 2 deletions tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@

def test_import():
import poetry_monoranger_plugin # noqa: F401

pass
2 changes: 1 addition & 1 deletion tests/test_venv_modifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_executes_modifications_for_env_command(mock_event_gen, disable_cache: b

# create_poetry is called with the correct args
mock_create_poetry.assert_called_once()
assert mock_create_poetry.call_args[1]["cwd"] == Path("/monorepo_root")
assert mock_create_poetry.call_args[1]["cwd"] == Path("/monorepo_root").resolve()
assert mock_create_poetry.call_args[1]["io"] == mock_event.io
assert mock_create_poetry.call_args[1]["disable_cache"] == disable_cache

Expand Down