Skip to content

Commit

Permalink
add pre-commit, fix python formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ojii committed Aug 22, 2024
1 parent a701f07 commit af7da22
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 26 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
rustup update
rustup component add clippy
rustup install stable
pipx install ruff
- name: Toolchain info
run: |
cargo --version --verbose
Expand All @@ -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
Expand Down
27 changes: 27 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
12 changes: 3 additions & 9 deletions imgsize.pyi
Original file line number Diff line number Diff line change
@@ -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: ...
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ test = [

[tool.maturin]
features = ["pyo3/extension-module"]

[tool.ruff.lint]
select = ["F", "E", "W", "I"]
8 changes: 4 additions & 4 deletions python-tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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")
12 changes: 6 additions & 6 deletions python-tests/test_apis.py
Original file line number Diff line number Diff line change
@@ -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]
15 changes: 8 additions & 7 deletions python-tests/test_sample_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit af7da22

Please sign in to comment.