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

_name=pytest-examples
pkgname=python-pytest-examples
pkgver=0.0.10
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
)
# tests missing in sdist tarball: https://github.com/pydantic/pytest-examples/issues/16
# source=(https://files.pythonhosted.org/packages/source/${_name::1}/$_name/$_name-$pkgver.tar.gz)
source=($_name-$pkgver.tar.gz::$url/archive/refs/tags/v$pkgver.tar.gz
        https://github.com/pydantic/pytest-examples/commit/cade1306.patch
        python-3.12.patch)
sha512sums=('af3f8314384f58a8aaa1fa9a4e8746d6a5b0dead8a6cacd582e1008c38276cce62b0bf778f12b399c1e732d9f270c6e5d22de785f035c07ad7c590826f7ef11e'
            '98674b2e93fb7dffa6ec0a756471b16d4515a9db0c3501d8a27a52bed586fcf9933e347ef4b0498b220276976045db442a5e51b010c2dd4396484359a12d0e5c'
            '35b482ba9eac0b81e1d84b09ecb5911c4db2b2413dd70444be39b2501543851be5b3a8f612d197dae2791f0651b8488ee5036752635f069133f7d7292de4fb09')
b2sums=('4813e3a558a4359b56fd4d21a93b82fa84814ae4adac1b448611ada44685edf0ee3eab70b083cead959fa2f1d44fc45774480325c3c1b84a7ddfd119c1974296'
        '6ebc1b9374b8663c8fa2d367d6a092b5277db2e88799b2305f5b620269dc561f58098e7243b4cd50da4aa5c53e2ded524a79d0e068f6bb24bb07d1dd96911fb6'
        '9181de3771e77604494e7c9d119e2476e4ddd00a7aca01baa44141ec466f8aa88cbc9eb98a869a7e33eaa1656ee0db1fa5e5ee5086c848958027e5837f8e7ded')

prepare() {
  cd $_name-$pkgver
  patch -p1 -i ../cade1306.patch # Fix tests with ruff 0.1.0
  patch -p1 -i ../python-3.12.patch # Fix tests with python 3.12 (ALT Linux)
}

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/"
}
