# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=pytest-xdist
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.31.0
pkgrel=1
pkgdesc="py.test xdist plugin for distributed testing and loop-on-failing modes (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('MIT')
url='https://bitbucket.org/pytest-dev/pytest-xdist'
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-pytest-forked"
         "${MINGW_PACKAGE_PREFIX}-python-execnet")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-pytest"
             "${MINGW_PACKAGE_PREFIX}-python-filelock"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools-scm")
options=('staticlibs' 'strip' '!debug')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/pytest-dev/pytest-xdist/archive/v${pkgver}.tar.gz")
sha512sums=('48c18802f5573ed4ceb061e82aea2046e3b19ad54e595ef666e095409d393cc301e729edd34eab999eae85184fc22c9d5fcda6ae334d183265e4baddcb6dfa46')

prepare() {
  cd "${srcdir}"
  rm -rf python-build-${CARCH} | true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"

  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
}

build() {
  msg "Python build for ${CARCH}"  
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  # Hack entry points by installing it
  local OLD_PYTHON_PATH=$PYTHONPATH

  msg "Python test for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install --root="$PWD/tmp_install" --optimize=1 --skip-build

  _PATHONPATH1="${srcdir}/python-build-${CARCH}/tmp_install${MINGW_PREFIX}/lib/python3.8/site-packages"
  _PATHONPATH2="${OLD_PYTHON_PATH};${srcdir}/python-build-${CARCH}/tests"
  PYTHONPATH="${_PATHONPATH1};${_PATHONPATH2}" py.test || true

}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
