# Maintainer: David Runge <dvzrv@archlinux.org>
# Maintainer: Christian Heusel <gromit@archlinux.org>

_name=pytest-examples
pkgname=python-pytest-examples
pkgver=0.0.18
pkgrel=2
pkgdesc="Pytest plugin for testing examples in docstrings and markdown files"
arch=(any)
url="https://github.com/pydantic/pytest-examples"
license=(MIT)
depends=(
  python
  python-black
  python-pytest
  python-ruff
)
makedepends=(
  python-build
  python-hatchling
  python-installer
)
source=($_name-$pkgver.tar.gz::$url/archive/refs/tags/v$pkgver.tar.gz
        https://github.com/pydantic/pytest-examples/commit/60ae70d0.patch
        fix-tests.patch)
sha512sums=('2fa893ca977418df252a4c3d705054dbecc84b3cbcc90135aa29d6a3c77adfceb1452ad8fc43cf3def69ad2f5e333f678ae83acf2635c8e335ad812a5f852cc0'
            '1de8e2105fa22a304b353e65cda3475b2ef040ccf874a30a04f5c944de50711021ac699f6b3f60a548a82f9d4c996a8782d232aa5f2ab612e209e30c0dd5d8b5'
            '2700f3f66e24940b084da90cb27eb1b0164172ff32e8b8938d8c5413cc9f609fdddc559f4ee4ce72e56bab5bace70d86ae5f6f3ae7bf03328bd6defa643affa4')
b2sums=('6a4c4c436e8bf7148debd7cce5869710ba34f209fb152e4222a2ccddffd9d816ff14f5282dfd1f2b7bafe374cb294da75e563f8a285ceeccca702e71815e6db9'
        '025d75799377dc8ddbb3c591a7c1691dbb9ed796581c1ebd56f17109975f87aa80e397527933215c39ea09027c46824e8f9093a78e696d91edbd5fbd30228d44'
        'ad4dc221e1c0ac797ac45279f23a46b9c562376db827a6ba5682577a9e2302d1ffa7b15ff06be9658393d2cc324720c60331787083d19f0dad7e0a5f244a71c0')

prepare() {
  cd $_name-$pkgver
  patch -p1 -i ../60ae70d0.patch # Fix tests with new ruff
  patch -p1 -i ../fix-tests.patch
}

build() {
  cd $_name-$pkgver
  python -m build --wheel --no-isolation
}

check() {
  local pytest_options=(
    -vv
  )
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")

  cd $_name-$pkgver
  # install to temporary location, as importlib is used
  python -m installer --destdir=test_dir dist/*.whl
  export PYTHONPATH="$PWD/test_dir/$site_packages:$PYTHONPATH"
  pytest "${pytest_options[@]}"
}

package() {
  cd $_name-$pkgver
  python -m installer --destdir="$pkgdir" dist/*.whl
  install -vDm 644 LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 README.md -t "$pkgdir/usr/share/doc/$pkgname/"
}
