diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6df2218..ceda1f3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,6 +12,7 @@ jobs: rustup update rustup component add clippy rustup install stable + pipx install ruff - name: Toolchain info run: | cargo --version --verbose @@ -21,6 +22,8 @@ jobs: run: | cargo fmt -- --check cargo clippy -- --deny warnings + ruff check --no-fix + ruff format --check - name: Rust Test run: | cargo check diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..31c2617 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,27 @@ +repos: + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.6.1 + hooks: + - id: ruff + args: [ --fix ] + - id: ruff-format + - repo: local + hooks: + - id: cargo-format + name: Cargo format + entry: cargo fmt + language: system + files: \.rs$ + pass_filenames: false + - id: cargo-clippy + name: Cargo clippy + entry: cargo clippy -- --deny warnings + language: system + files: \.rs$ + pass_filenames: false + - id: cargo-check + name: Cargo check + entry: cargo check + language: system + files: \.rs$ + pass_filenames: false \ No newline at end of file diff --git a/imgsize.pyi b/imgsize.pyi index f58105c..205d9a6 100644 --- a/imgsize.pyi +++ b/imgsize.pyi @@ -1,30 +1,24 @@ from typing import Any, Iterable, TypedDict - class SizeDict(TypedDict): width: int height: int mime_type: int is_animated: int - class Size: width: int height: int mime_type: int is_animated: int - def __init__(self, width: int, height: int, mime_type: str, is_animated: bool) -> None: ... - + def __init__( + self, width: int, height: int, mime_type: str, is_animated: bool + ) -> None: ... def as_dict(self) -> SizeDict: ... - def __repr__(self) -> str: ... - def __eq__(self, other: Any) -> bool: ... - def __iter__(self) -> Iterable[int]: ... - def __hash__(self) -> int: ... - def get_size(data: bytes) -> Size | None: ... diff --git a/pyproject.toml b/pyproject.toml index bfb0a01..83717aa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,3 +20,6 @@ test = [ [tool.maturin] features = ["pyo3/extension-module"] + +[tool.ruff.lint] +select = ["F", "E", "W", "I"] \ No newline at end of file diff --git a/python-tests/conftest.py b/python-tests/conftest.py index b1f3489..de6868e 100644 --- a/python-tests/conftest.py +++ b/python-tests/conftest.py @@ -1,15 +1,15 @@ +import sys from importlib import import_module from pathlib import Path from subprocess import check_call import pytest -import sys ROOT = Path(__file__).parent.parent -@pytest.fixture(scope='session') +@pytest.fixture(scope="session") def imgsize(): assert sys.prefix != sys.base_prefix, "must be in virtualenv" - check_call(['maturin', 'develop', '--manifest-path', ROOT / 'Cargo.toml']) - return import_module('imgsize') + check_call(["maturin", "develop", "--manifest-path", ROOT / "Cargo.toml"]) + return import_module("imgsize") diff --git a/python-tests/test_apis.py b/python-tests/test_apis.py index 0091df3..a89bcb5 100644 --- a/python-tests/test_apis.py +++ b/python-tests/test_apis.py @@ -1,10 +1,10 @@ def test_eq(imgsize): - assert imgsize.Size(1, 2, 'a', True) == imgsize.Size(1, 2, 'a', True) - assert imgsize.Size(1, 2, 'a', True) != imgsize.Size(2, 2, 'a', True) - assert imgsize.Size(1, 2, 'a', True) != imgsize.Size(1, 1, 'a', True) - assert imgsize.Size(1, 2, 'a', True) != imgsize.Size(1, 2, 'b', True) - assert imgsize.Size(1, 2, 'a', True) != imgsize.Size(1, 2, 'a', False) + assert imgsize.Size(1, 2, "a", True) == imgsize.Size(1, 2, "a", True) + assert imgsize.Size(1, 2, "a", True) != imgsize.Size(2, 2, "a", True) + assert imgsize.Size(1, 2, "a", True) != imgsize.Size(1, 1, "a", True) + assert imgsize.Size(1, 2, "a", True) != imgsize.Size(1, 2, "b", True) + assert imgsize.Size(1, 2, "a", True) != imgsize.Size(1, 2, "a", False) def test_iter(imgsize): - assert list(imgsize.Size(1, 2, 'a', True)) == [1, 2] + assert list(imgsize.Size(1, 2, "a", True)) == [1, 2] diff --git a/python-tests/test_sample_files.py b/python-tests/test_sample_files.py index c8c9318..2f3583e 100644 --- a/python-tests/test_sample_files.py +++ b/python-tests/test_sample_files.py @@ -7,17 +7,18 @@ def find_examples(): - test_data_dir = ROOT / 'src' / 'test-data' - for input_path in test_data_dir.glob('*.input'): - output_path = input_path.with_suffix('.output') - if not output_path.exists(): continue - with input_path.open('rb') as fobj: + test_data_dir = ROOT / "src" / "test-data" + for input_path in test_data_dir.glob("*.input"): + output_path = input_path.with_suffix(".output") + if not output_path.exists(): + continue + with input_path.open("rb") as fobj: data = fobj.read(BYTES_TO_READ) - with output_path.open('r') as fobj: + with output_path.open("r") as fobj: output = json.load(fobj) yield pytest.param(data, output, id=input_path.stem) -@pytest.mark.parametrize('input,output', find_examples()) +@pytest.mark.parametrize("input,output", find_examples()) def test_sample_files(input, output, imgsize): assert imgsize.get_size(input).as_dict() == output