Skip to content

Commit

Permalink
Add tests; separate cli and core logic
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkLight1337 committed May 5, 2024
1 parent 2adedd7 commit 130ece4
Show file tree
Hide file tree
Showing 14 changed files with 953 additions and 85 deletions.
Binary file added .coverage
Binary file not shown.
26 changes: 26 additions & 0 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Setup environment
description: |
Sets up the development environment for this repository.
Notes:
1. You have to first checkout the repository.
2. To use the conda environment, you have to set `defaults.run.shell` to `bash -el {0}`. See [this page](https://github.com/marketplace/actions/setup-miniconda#important) for more details.
inputs:
python-version:
description: 'Python version'
required: true

runs:
using: composite
steps:
- name: Install Miniconda
uses: conda-incubator/setup-miniconda@v3
with:
auto-update-conda: true
python-version: ${{ inputs.python-version }}
activate-environment: 'pyimorg-${{ inputs.python-version }}'
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: '1.4.0'
65 changes: 65 additions & 0 deletions .github/workflows/pre-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: Verify pre-release

on:
pull_request:
branches:
- master
push:
branches:
- master

permissions:
contents: write

defaults:
run:
shell: bash -el {0}

jobs:
lint:
name: Lint code
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
runs-on: ubuntu-latest
steps:
- name: Checkout Git repository
uses: actions/checkout@v4
- name: Setup environment
uses: ./.github/actions/setup
with:
python-version: ${{ matrix.python-version }}
- name: Install repository
run: |
poetry lock --no-update
poetry install --with dev
- name: Check dependencies
run: poetry run -- deptry .
- name: Lint code
run: poetry run -- ruff check .
- name: Check type annotations
run: poetry run -- pyright
test:
name: Test code
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
python-version: ['3.8', '3.12']
runs-on: ${{ matrix.os }}
steps:
- name: Checkout Git repository
uses: actions/checkout@v4
- uses: ./.github/actions/setup
with:
python-version: ${{ matrix.python-version }}
- name: Install repository
run: |
poetry lock --no-update
poetry install --with dev
- name: Pytest with code coverage
run: poetry run -- pytest -n auto
- name: Archive code coverage results
uses: actions/upload-artifact@v4
with:
name: coverage-${{ matrix.os }}-${{ matrix.python-version }}
path: coverage/
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ Python (↓)
1. [Setup](#setup) the development environment.
2. Run `poetry run deptry . && poetry run ruff check . && poetry run pyright .` to lint the code.

### Test

1. [Setup](#setup) the development environment.
2. Run `poetry run -- pytest` to test the code and output the coverage report.
678 changes: 677 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

79 changes: 1 addition & 78 deletions pycm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,78 +1 @@
from __future__ import annotations

from collections import defaultdict
from collections.abc import Collection
from itertools import groupby
import json
import re
from urllib.request import urlopen

import click
import pandas as pd
from pkg_resources import Requirement, parse_version

__all__ = ['cli']

# Dummy requirement that cannot be satisfied
BAD_REQUIREMENT = Requirement.parse('python<0,>0')

def _get_requires_python_constraint(dist_data: dict[str, str]) -> Requirement | None:
requires_python_data: str | None = dist_data.get('requires_python')
if requires_python_data is None:
return None

return Requirement.parse(f'python{requires_python_data}')

def _get_python_requirements(dist_data: dict[str, str]) -> Collection[Requirement]:
requires_python_constraint = _get_requires_python_constraint(dist_data)

python_version_data: str | None = dist_data.get('python_version')
if python_version_data is None or python_version_data == 'source':
return [] if requires_python_constraint is None else [requires_python_constraint]

m = re.match(r'[a-z]{2}([0-9])([0-9]*)', python_version_data)
if m is None:
return [BAD_REQUIREMENT] if requires_python_constraint is None else [requires_python_constraint]

major, minor = m.groups()
if minor:
bdist_constraint = Requirement.parse(f'python=={major}.{minor}.*')
else:
bdist_constraint = Requirement.parse(f'python=={major}.*')

if requires_python_constraint is None:
return [bdist_constraint]
else:
return [requires_python_constraint, bdist_constraint]

@click.command()
@click.argument('package_name', type=str)
@click.option('--python', 'python_versions', metavar='<VERSIONS>', type=str, required=True, help='A comma-separated list of Python versions to check against')
def cli(package_name: str, python_versions: str) -> None:
"""Show the versions of a PyPI package that are compatible with each Python version."""
python_versions_lst = python_versions.split(',')
output = defaultdict(lambda: defaultdict(lambda: ''))

data = json.load(urlopen(f'https://pypi.org/pypi/{package_name}/json'))
for package_version, release_data in data['releases'].items():
for dist_data in release_data:
python_requirements = _get_python_requirements(dist_data)

for python_version in python_versions_lst:
if all(python_version in r for r in python_requirements):
output[python_version][package_version] = 'Y'

output_df = pd.DataFrame.from_dict(data=output, orient='index').fillna('')

# We only care about the latest patch version for each minor version
output_columns = tuple(str(c) for c in output_df.columns)
latest_patches = [
max(columns, key=parse_version)
for _, columns in groupby(output_columns, key=lambda s: parse_version(s).release[:2])
]

formatted_output_df = output_df \
.loc[sorted(output_df.index, key=parse_version), sorted(latest_patches, key=parse_version)] \
.rename_axis(index='Python (↓)', columns=f'{package_name} (→)')

click.echo(formatted_output_df)
from .pycm import *
2 changes: 1 addition & 1 deletion pycm/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from . import cli
from .cli import cli

cli()
15 changes: 15 additions & 0 deletions pycm/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from __future__ import annotations

import click

from .pycm import pycm

__all__ = ['cli']

@click.command()
@click.argument('package_name', type=str)
@click.option('--python', 'python_versions', metavar='<VERSIONS>', type=str, required=True, help='A comma-separated list of Python versions to check against')
def cli(package_name: str, python_versions: str) -> None:
"""Show the versions of a PyPI package that are compatible with each Python version."""
output_df = pycm(package_name, python_versions.split(','))
click.echo(output_df)
76 changes: 76 additions & 0 deletions pycm/pycm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from __future__ import annotations

from collections import defaultdict
from collections.abc import Collection
from itertools import groupby
import json
import re
from urllib.request import urlopen

from packaging.requirements import Requirement
from packaging.version import Version
import pandas as pd

__all__ = ['pycm']

# Dummy requirement that cannot be satisfied
BAD_REQUIREMENT = Requirement('python<0,>0')

def _get_requires_python_constraint(dist_data: dict[str, str]) -> Requirement | None:
requires_python_data: str | None = dist_data.get('requires_python')
if requires_python_data is None:
return None

return Requirement(f'python{requires_python_data}')

def _get_python_requirements(dist_data: dict[str, str]) -> Collection[Requirement]:
requires_python_constraint = _get_requires_python_constraint(dist_data)

python_version_data: str | None = dist_data.get('python_version')
if python_version_data is None or python_version_data == 'source':
return [] if requires_python_constraint is None else [requires_python_constraint]

m = re.match(r'[a-z]{2}([0-9])([0-9]*)', python_version_data)
if m is None:
return [BAD_REQUIREMENT] if requires_python_constraint is None else [requires_python_constraint]

major, minor = m.groups()
if minor:
bdist_constraint = Requirement(f'python=={major}.{minor}.*')
else:
bdist_constraint = Requirement(f'python=={major}.*')

if requires_python_constraint is None:
return [bdist_constraint]
else:
return [requires_python_constraint, bdist_constraint]

def pycm(package_name: str, python_versions: list[str], *, groupby_patch: bool = True) -> pd.DataFrame:
"""Show the versions of a PyPI package that are compatible with each Python version."""
output = defaultdict(lambda: defaultdict(lambda: ''))

data = json.load(urlopen(f'https://pypi.org/pypi/{package_name}/json'))
for package_version, release_data in data['releases'].items():
for dist_data in release_data:
python_requirements = _get_python_requirements(dist_data)

for python_version in python_versions:
if all(python_version in r.specifier for r in python_requirements):
output[python_version][package_version] = 'Y'

output_df = pd.DataFrame.from_dict(data=output, orient='index').fillna('')


if groupby_patch:
# We only care about the latest patch version for each minor version
output_columns = tuple(str(c) for c in output_df.columns)
output_columns = [
max(columns, key=Version)
for _, columns in groupby(output_columns, key=lambda s: Version(s).release[:2])
]
else:
output_columns = output_df.columns

return output_df \
.loc[sorted(output_df.index, key=Version), sorted(output_columns, key=Version)] \
.rename_axis(index='Python (↓)', columns=f'{package_name} (→)')
43 changes: 38 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "pycm"
version = "1.0.0"
version = "1.0.1"
description = "Command-line tool for checking Python compatibility"
license = "MIT"
authors = [
Expand All @@ -21,27 +21,37 @@ classifiers = [
]

[tool.poetry.scripts]
pycm = "pycm:cli"
pycm = "pycm.cli:cli"

[tool.poetry.dependencies]
python = "^3.8"

click = ">=8.1"
pandas = ">=2.0"
pandas = { version = ">=2.0", extras = ["html"] }
packaging = ">=24.0"

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

[tool.poetry.group.dev.dependencies]
pandas-stubs = ">=2.0"

deptry = ">=0.14"
ruff = ">=0.3"
pyright = ">=1.1.354"

pytest = ">=7.0"
pytest-cov = ">=3.0"
pytest-xdist = ">=3.5"

beautifulsoup4 = ">=4.12.3"
requests = ">=2.31.0"

[tool.deptry]
extend_exclude = ["__pycache__"]
extend_exclude = ["\\.coverage/", "test", "__pycache__"]

[tool.deptry.per_rule_ignores]
DEP001 = ["_typeshed", "pkg_resources"]
DEP001 = ["_typeshed"]

[tool.ruff]
line-length = 100
Expand Down Expand Up @@ -184,3 +194,26 @@ reportShadowedImports = "warning"
reportUninitializedInstanceVariable = "warning"
reportUnnecessaryTypeIgnoreComment = "information"
# reportUnusedCallResult = "warning"

[tool.pytest.ini_options]
addopts = "--cov=pycm --cov-report=term-missing --cov-report html:coverage --cov-branch --cov-context=test"
testpaths = ["test"]

[tool.coverage.html]
show_contexts = true

[tool.coverage.report]
exclude_also = [
"def __repr__",
"if self.debug:",
"if settings.DEBUG",
"raise AssertionError",
"raise NotImplementedError",
"if 0:",
"if __name__ == .__main__.:",
"if TYPE_CHECKING:",
"class .*\\bProtocol(\\[.*\\])?\\):",
"@(abc\\.)?abstractmethod",
"@(typing\\.)?overload",
]

Empty file added test/__init__.py
Empty file.
Empty file added test/conftest.py
Empty file.
5 changes: 5 additions & 0 deletions test/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import subprocess


def test_help():
assert subprocess.call(['python', '-m', 'pycm', '--help']) == 0
Loading

0 comments on commit 130ece4

Please sign in to comment.