From 09fb2b706121b52992580caf9e34bfa9bd19488c Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 19 Mar 2024 09:26:08 +1300 Subject: [PATCH] Hatch (#6) * Update build system to Hatch and use ruff. Add pre-commit. * Add Ruff badge --- .config/coveragerc | 17 +++ .config/pre-commit-config.yaml | 25 ++++ .config/pytest.ini | 4 + .config/ruff.toml | 16 +++ .github/workflows/publish.yml | 19 +-- .github/workflows/run-python-tests.yml | 31 +++-- CONTRIBUTING.md | 55 ++++++++ README.md | 3 +- pyproject.toml | 167 +++++++++++++------------ requirements.txt | 16 ++- setup.cfg | 57 --------- test-requirements.txt | 3 - tests/pytest_loguru/test_plugin.py | 1 + 13 files changed, 252 insertions(+), 162 deletions(-) create mode 100644 .config/coveragerc create mode 100644 .config/pre-commit-config.yaml create mode 100644 .config/pytest.ini create mode 100644 .config/ruff.toml create mode 100644 CONTRIBUTING.md delete mode 100644 setup.cfg delete mode 100644 test-requirements.txt diff --git a/.config/coveragerc b/.config/coveragerc new file mode 100644 index 0000000..ca9d302 --- /dev/null +++ b/.config/coveragerc @@ -0,0 +1,17 @@ +[run] +source = src + +omit = */_version.py + +[report] +exclude_also = + from ._version + def __repr__ + if self.debug: + if settings.DEBUG + raise AssertionError + raise NotImplementedError + if 0: + if __name__ == .__main__.: + if TYPE_CHECKING: + @(abc\.)?abstractmethod diff --git a/.config/pre-commit-config.yaml b/.config/pre-commit-config.yaml new file mode 100644 index 0000000..c65a9c0 --- /dev/null +++ b/.config/pre-commit-config.yaml @@ -0,0 +1,25 @@ +default_language_version: + python: python3.11 +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-ast + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.3.2 + hooks: + # Run the linter. + - id: ruff + args: [--config, .config/ruff.toml, --fix] + # Run the formatter. + - id: ruff-format + args: [--config, .config/ruff.toml] + - repo: https://github.com/jazzband/pip-tools + rev: 7.4.1 + hooks: + - id: pip-compile + name: pip-compile requirements.txt + files: pyproject.toml + args: [pyproject.toml, --resolver=backtracking, --all-extras, --upgrade, -q, -o, requirements.txt] diff --git a/.config/pytest.ini b/.config/pytest.ini new file mode 100644 index 0000000..bb28fd3 --- /dev/null +++ b/.config/pytest.ini @@ -0,0 +1,4 @@ +[pytest] +pythonpath = ../src +addopts = "--color=yes" +log_cli = 1 diff --git a/.config/ruff.toml b/.config/ruff.toml new file mode 100644 index 0000000..0065986 --- /dev/null +++ b/.config/ruff.toml @@ -0,0 +1,16 @@ +line-length = 79 +exclude = ["_version.py"] + +[lint] +# List of rules: https://docs.astral.sh/ruff/rules/ +select = [ + "E", # pycodestyle - default + "F", # pyflakes - default + "I" # isort +] +ignore = [ + "E501" # Line too long +] + +[lint.isort] +known-local-folder = ["pytest_loguru"] diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 55f8c55..f113aac 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -9,22 +9,25 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Get history and tags for SCM versioning to work + - uses: actions/checkout@v4 + - name: Get history and tags for versioning to work run: | git fetch --prune --unshallow git fetch --depth=1 origin +refs/tags/*:refs/tags/* - name: Set up Python - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: '3.x' - name: Install dependencies run: | python -m pip install --upgrade pip - pip install --upgrade tox-gh-actions - - name: Publish with tox and twine + pip install --upgrade hatch + - name: Build with hatch + run: | + hatch build + - name: Publish with hatch env: - TWINE_USERNAME: ${{ secrets.TWINE_USERNAME }} - TWINE_PASSWORD: ${{ secrets.TWINE_PASSWORD }} + HATCH_INDEX_USER: ${{secrets.HATCH_INDEX_USER}} + HATCH_INDEX_AUTH: ${{secrets.HATCH_INDEX_AUTH}} run: | - tox -e publish + hatch publish diff --git a/.github/workflows/run-python-tests.yml b/.github/workflows/run-python-tests.yml index 647dfa2..ec3ccd5 100644 --- a/.github/workflows/run-python-tests.yml +++ b/.github/workflows/run-python-tests.yml @@ -1,12 +1,14 @@ # This workflow will install Python dependencies, lint and run tests # For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions -name: build +name: Run tests on: + workflow_dispatch: # add run button in github push: branches-ignore: - gh-pages + - 'dependabot/**' pull_request: branches-ignore: - gh-pages @@ -16,24 +18,31 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Set up Python - uses: actions/setup-python@v2 + uses: actions/setup-python@v5 with: python-version: '3.x' - name: Install dependencies run: | python -m pip install --upgrade pip - pip install --upgrade tox-gh-actions - - name: Test with tox/pytest + pip install --upgrade hatch + - name: Test with hatch/pytest run: | - tox + hatch run test:test + - name: Check styling + if: always() + run: | + hatch run lint:style - name: Publish Unit Test Results - uses: EnricoMi/publish-unit-test-result-action@v1 + uses: EnricoMi/publish-unit-test-result-action@v2 if: always() with: github_token: ${{ secrets.GITHUB_TOKEN }} - files: .tox/*.xml - - name: Upload Coverage Results - uses: codecov/codecov-action@v2 - if: success() + junit_files: test-results.xml + - name: Publish in Coveralls + uses: coverallsapp/github-action@v2 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + flag-name: tests + format: lcov diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..d1f3267 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,55 @@ +# Development + +## Environment + +Development is currently done using Python 3.11. We recommend using a virtual +environment such as ``venv``: + + python3.11 -m venv venv + source venv/bin/activate + +In your virtual environment, please install all packages for +development by running: + + pip install -r requirements.txt + +## Pre-Commit + +Also be sure to install `pre-commit`, which is run every time +you make a git commit: + + pre-commit install + +The configuration file for this project is in a +non-standard location. Thus, you will need to edit your +`.git/hooks/pre-commit` file to reflect this. Change +the line that begins with `ARGS` to: + + ARGS=(hook-impl --config=.config/pre-commit-config.yaml --hook-type=pre-commit) + +With pre-commit, all code is formatted according to +[ruff](https://github.com/astral-sh/ruff) guidelines. + +To check if your changes pass pre-commit without committing, run: + + pre-commit run --all-files --config=.config/pre-commit-config.yaml + +## Testing + +To run the tests and view coverage, execute: + + pytest -c .config/pytest.ini --cov hdx --cov-config .config/coveragerc + +## Packages + +[pip-tools](https://github.com/jazzband/pip-tools) is used for +package management. If you’ve introduced a new package to the +source code (i.e.anywhere in `src/`), please add it to the +`project.dependencies` section of +`pyproject.toml` with any known version constraints. + +Any changes to the dependencies will be automatically reflected in +`requirements.txt` with `pre-commit`, but you can re-generate +the file without committing by executing: + + pre-commit run pip-compile --all-files --config=.config/pre-commit-config.yaml diff --git a/README.md b/README.md index 474a9a1..8e14744 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,7 @@ [![PyPI](https://img.shields.io/pypi/v/pytest-loguru.svg)](https://pypi.python.org/pypi/pytest-loguru) [![Conda Version](https://img.shields.io/conda/vn/conda-forge/pytest-loguru.svg)](https://anaconda.org/conda-forge/pytest-loguru) [![Coverage Status](https://codecov.io/gh/mcarans/pytest-loguru/branch/main/graph/badge.svg?token=JpWZc5js4y)](https://codecov.io/gh/mcarans/pytest-loguru) -[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) -[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/) +[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff) ### pytest-loguru diff --git a/pyproject.toml b/pyproject.toml index 668573a..8532bf5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,87 +1,94 @@ [build-system] -requires = [ "setuptools>=45", "setuptools_scm[toml]>=6.2", "wheel"] -build-backend = "setuptools.build_meta" +requires = ["hatchling", "hatch-vcs"] +build-backend = "hatchling.build" -[tool.setuptools_scm] -write_to = "src/pytest_loguru/_version.py" +[project] +name = "pytest-loguru" +description = "Pytest Loguru" +authors = [{name = "Michael Rans"}] +license = "MIT" +keywords = ["loguru", "py.test", "pytest"] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Topic :: Software Development :: Libraries :: Python Modules", + "Framework :: Pytest", + "Intended Audience :: Developers", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Intended Audience :: Developers", + "License :: OSI Approved :: MIT License", + "Natural Language :: English", + "Operating System :: POSIX :: Linux", + "Operating System :: Unix", + "Operating System :: MacOS", + "Operating System :: Microsoft :: Windows", +] + +requires-python = ">=3.8" + +dependencies = ["loguru"] + +dynamic = ["version"] + +readme = "README.md" + +[project.urls] +Homepage = "https://github.com/mcarans/pytest-loguru" + +[project.entry-points.pytest11] +loguru = "pytest_loguru.plugin" + +[project.optional-dependencies] +test = ["pytest", "pytest-cov"] + +######### +# Hatch # +######### + +# Build + +[tool.hatch.build.targets.wheel] +packages = ["src/pytest_loguru"] + +[tool.hatch.build.hooks.vcs] +version-file = "src/pytest_loguru/_version.py" + +# Versioning + +[tool.hatch.version] +source = "vcs" + +[tool.hatch.version.raw-options] local_scheme = "no-local-version" version_scheme = "python-simplified-semver" -[tool.black] -line-length = 79 - -[tool.isort] -profile = "black" -line_length = 79 -multi_line_output = 3 - -[tool.flake8] -exclude = ["_version.py"] -ignore = ["E203", "E501", "W503"] -max-line-length = 79 -count = true -show_source = true - -[tool.pytest.ini_options] -pythonpath = "src" -addopts = "--color=yes" -log_cli = 1 - -[tool.coverage.paths] -source = ["src/pytest_loguru", "*/site-packages/pytest_loguru"] - -[tool.coverage.report] -omit = [ - "*/setup.py", - "*/python?.?/*", - "*/venv/*", - "*/site-packages/*", - "*/tests/*", - "*__init__*" -] +# Tests -exclude_lines = [ - "pragma: no cover", # Have to re-enable the standard pragma - "def __repr__", # Don't complain about missing - "if self.debug", # debug-only code - "raise AssertionError", # Don't complain if tests don't hit - "raise NotImplementedError", # defensive assertion code - "if 0:", # Don't complain if non-runnable code - "if __name__ == .__main__.:" # isn't run -] +[tool.hatch.envs.test] +features = ["test"] + +[tool.hatch.envs.test.scripts] +test = """ + pytest -c .config/pytest.ini --rootdir=. --junitxml=test-results.xml \ + --cov --cov-config=.config/coveragerc --no-cov-on-fail \ + --cov-report=lcov --cov-report=term-missing + """ + +[[tool.hatch.envs.test.matrix]] +python = ["3.12"] -[tool.tox] -legacy_tox_ini = """ -[tox] -envlist = py311, lint - -[gh-actions] -python = - 3: py311, lint - -[testenv] -package = wheel -wheel_build_env = .pkg -deps = - -r test-requirements.txt -commands = - pytest --cov=hdx --no-cov-on-fail --junitxml=.tox/test-results.xml --cov-report=xml --cov-report=term-missing - -[testenv:lint] -deps = - flake8 - flake8-isort - flake8-black - flake8-pyproject -commands = - flake8 --color=always src tests - -[testenv:publish] -package = sdist -pass_env = SSH_AUTH_SOCK, TWINE_USERNAME, TWINE_PASSWORD -deps = - twine - -commands = - twine upload {work_dir}/{package_env}/dist/* -""" +[tool.hatch.envs.lint] +detached = true +dependencies = ["ruff"] + +[tool.hatch.envs.lint.scripts] +style = [ + "ruff check --config .config/ruff.toml --diff {args:.}", + "ruff format --config .config/ruff.toml --diff {args:.}", +] diff --git a/requirements.txt b/requirements.txt index 8c0b043..22beb19 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,16 @@ -pytest==7.4.2 +# +# This file is autogenerated by pip-compile with Python 3.11 +# by the following command: +# +# pip-compile --all-extras --output-file=requirements.txt pyproject.toml +# +iniconfig==2.0.0 + # via pytest loguru==0.7.2 + # via pytest-loguru (pyproject.toml) +packaging==24.0 + # via pytest +pluggy==1.4.0 + # via pytest +pytest==8.1.1 + # via pytest-loguru (pyproject.toml) diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index 2a29bf2..0000000 --- a/setup.cfg +++ /dev/null @@ -1,57 +0,0 @@ -[metadata] -name = pytest-loguru -description = Pytest Loguru -version = attr: pytest_loguru.__version__ -author = Mike Rans -license = MIT -license_files = LICENSE -long_description = file: README.md -long_description_content_type = text/markdown; charset=UTF-8 -keywords = py.test, pytest, loguru -url = https://github.com/mcarans/pytest-loguru -platforms = any - -classifiers = - Development Status :: 5 - Production/Stable - Topic :: Software Development :: Libraries :: Python Modules - Programming Language :: Python - Programming Language :: Python :: 3 - Programming Language :: Python :: 3 :: Only - Programming Language :: Python :: 3.6 - Programming Language :: Python :: 3.7 - Programming Language :: Python :: 3.8 - Programming Language :: Python :: 3.9 - Programming Language :: Python :: 3.10 - Programming Language :: Python :: 3.11 - Intended Audience :: Developers - License :: OSI Approved :: MIT License - Natural Language :: English - Operating System :: POSIX :: Linux - Operating System :: Unix - Operating System :: MacOS - Operating System :: Microsoft :: Windows - Framework :: Pytest - -[options] -zip_safe = True -packages = find_namespace: -include_package_data = True -package_dir = - = src - -python_requires = >=3.6 - -install_requires = - pytest - loguru - -[options.packages.find] -where = src - -[options.entry_points] -pytest11 = - loguru = pytest_loguru.plugin - -[bdist_wheel] -universal = 1 - diff --git a/test-requirements.txt b/test-requirements.txt deleted file mode 100644 index 606e436..0000000 --- a/test-requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -pytest-cov==4.1.0 -tox==4.11.3 --r requirements.txt diff --git a/tests/pytest_loguru/test_plugin.py b/tests/pytest_loguru/test_plugin.py index 7b4a294..bb29df2 100644 --- a/tests/pytest_loguru/test_plugin.py +++ b/tests/pytest_loguru/test_plugin.py @@ -1,4 +1,5 @@ """Logging caplog test""" + import logging from loguru import logger