# Maintainer: Santiago Torres-Arias <santiago @ usualplace>
# Maintainer: Carl Smedstad <carsme@archlinux.org>
# Contributor: Kaizhao Zhang <zhangkaizhao@gmail.com>
# Contributor: Phillip A. (flying-sheep)

pkgname=python-hatch
pkgver=1.16.5
pkgrel=1
pkgdesc="A modern project, package, and virtual env manager"
arch=('any')
url="https://github.com/pypa/hatch"
license=('MIT')
depends=(
  'python'
  'python-click'
  'python-hatchling'
  'python-httpx'
  'python-hyperlink'
  'python-keyring'
  'python-packaging'
  'python-pexpect'
  'python-platformdirs'
  'python-rich'
  'python-shellingham'
  'python-tomli-w'
  'python-tomlkit'
  'python-userpath'
  'python-uv'
  'python-virtualenv'
)
makedepends=(
  'python-build'
  'python-hatch-vcs'
  'python-installer'
  'python-wheel'
)
checkdepends=(
  'git'
  'libxcrypt-compat'
  'python-flit-core'
  'python-pyfakefs'
  'python-pytest'
  'python-pytest-mock'
  'rust'
)
source=("$url/archive/hatch-v$pkgver.tar.gz")
b2sums=('29093273d38024bbac9f9d6fc24ca41cf8ec942eecf9096a2385ea59c3cba403e3ad0d393a6edf68b4e116c28877f7b0a64b5cf594dd049f8547a21ffbf79a6a')

build() {
  cd hatch-hatch-v$pkgver
  python -m build --wheel --no-isolation

  # Create test environment here to be able to use it for creating completion
  # scripts.
  python -m venv --system-site-packages test-env
  test-env/bin/python -m installer dist/*.whl
  _HATCH_COMPLETE=bash_source test-env/bin/python -m hatch > completion.bash
  _HATCH_COMPLETE=fish_source test-env/bin/python -m hatch > completion.fish
  _HATCH_COMPLETE=zsh_source test-env/bin/python -m hatch > completion.zsh
}

check() {
  cd hatch-hatch-v$pkgver
  local pytest_args=(
    # Don't run hatchling tests, the tests in the tagged version of hatch are not
    # in sync with the tagged version of hatchling.
    --deselect=tests/cli/build/test_build.py::test_clean
    --deselect=tests/cli/build/test_build.py::test_clean_env_var
    --deselect=tests/cli/build/test_build.py::test_clean_hooks_after
    --deselect=tests/cli/build/test_build.py::test_clean_hooks_after_env_var
    --deselect=tests/cli/build/test_build.py::test_default
    --deselect=tests/cli/build/test_build.py::test_explicit_directory
    --deselect=tests/cli/build/test_build.py::test_explicit_directory_env_var
    --deselect=tests/cli/build/test_build.py::test_plugin_dependencies_unmet
    --deselect=tests/cli/config/test_set.py::test_project_location_basic_set_first_project
    --deselect=tests/cli/config/test_set.py::test_project_location_complex_set_first_project
    --deselect=tests/cli/self/test_self.py::test

    # Tests broken due to python-rich-14.3 incompatibility
    # https://github.com/pypa/hatch/issues/2170
    --deselect=tests/cli/dep/show/test_table.py
    --deselect=tests/cli/env/test_show.py
    --deselect=tests/cli/python/test_show.py
    --deselect=tests/cli/new/test_new.py::test_default_no_license_cache
    --deselect=tests/env/plugin/test_interface.py::TestDependencies::test_context_formatting
    --deselect=tests/env/plugin/test_interface.py::TestDependencies::test_project_dependencies_context_formatting
  )
  test-env/bin/python -m pytest --ignore=tests/backend "${pytest_args[@]}"
}

package() {
  cd hatch-hatch-v$pkgver
  python -m installer --destdir="$pkgdir" dist/*.whl
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE.txt

  install -vDm644 completion.bash "$pkgdir/usr/share/bash-completion/completions/hatch"
  install -vDm644 completion.fish "$pkgdir/usr/share/fish/vendor_completions.d/hatch.fish"
  install -vDm644 completion.zsh "$pkgdir/usr/share/zsh/site-functions/_hatch"
}
