From ff5c8642b307e818d4c9ac99fd0c7b21e39c4bd2 Mon Sep 17 00:00:00 2001 From: Samuel Bennett Date: Fri, 8 Mar 2024 13:28:22 +0000 Subject: [PATCH] Upstreaming precommit lessons from sunpy-template (#63) * Upstreaming precommit lessons from sunpy-template * Adds py12 tox test suite * Adds py12 enviroment to parent tox * Update virtualenv handling in test to pytest-venv --- .github/workflows/tests.yml | 1 + tests/test_package.py | 17 ++++++++++------- tox.ini | 4 ++-- {{ cookiecutter.package_name }}/docs/conf.py | 3 ++- {{ cookiecutter.package_name }}/setup.py | 2 +- {{ cookiecutter.package_name }}/tox.ini | 2 +- .../{{ cookiecutter.module_name }}/__init__.py | 10 ++++++---- .../example_mod.py | 3 ++- 8 files changed, 25 insertions(+), 17 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 5491469..408dbcf 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -18,4 +18,5 @@ jobs: - macos: py39-test - linux: py310-test - linux: py311-test + - linux: py312-test - linux: build_docs diff --git a/tests/test_package.py b/tests/test_package.py index 8e3d961..0dfba15 100644 --- a/tests/test_package.py +++ b/tests/test_package.py @@ -3,6 +3,7 @@ import subprocess import pytest +from pytest_venv import venv @pytest.fixture @@ -32,7 +33,7 @@ def test_examples_present(installed_cookiejar): assert primes == cprimes -def test_dev_version_number(virtualenv, bake_examples_compiled_dev_version): +def test_dev_version_number(venv, bake_examples_compiled_dev_version): cj = bake_examples_compiled_dev_version path = str(cj.project_path) @@ -54,12 +55,13 @@ def test_dev_version_number(virtualenv, bake_examples_compiled_dev_version): subprocess.run(["git", "-C", path, "tag", "v0.1"], check=True) # Create a new virtualenv with the package installed - virtualenv.run(f"pip install setuptools_scm") - virtualenv.run(f"pip install -e {path}") + venv.install('setuptools_scm') + venv.install(path, editable=True) # assert it's actually correct - dynamic_version = virtualenv.run('python -c "import packagename; print(packagename.__version__)"', capture=True).strip() - assert dynamic_version == "0.1" + dynamic_version = subprocess.run([venv.python, '-c', 'import packagename; print(packagename.__version__)'], + capture_output=True) + assert dynamic_version.stdout.decode().strip() == "0.1" with open(cj.project_path / "README.md", "a") as fobj: fobj.seek(0, io.SEEK_END) @@ -69,5 +71,6 @@ def test_dev_version_number(virtualenv, bake_examples_compiled_dev_version): subprocess.run(["git", "-C", path, "commit", "-m", "second"], check=True) # assert it's actually correct - dynamic_version = virtualenv.run('python -c "import packagename; print(packagename.__version__)"', capture=True).strip() - assert dynamic_version.startswith("0.2.dev1") + dynamic_version = subprocess.run([venv.python, '-c', 'import packagename; print(packagename.__version__)'], + capture_output=True) + assert dynamic_version.stdout.decode().startswith("0.2.dev1") diff --git a/tox.ini b/tox.ini index 504a53d..766e92c 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] envlist = - test + py{38,39,310,311,312}-test build-docs [testenv] @@ -16,7 +16,7 @@ skip_install = true deps = cookiecutter pytest-cookies - pytest-virtualenv + pytest-venv tox[testing] commands = diff --git a/{{ cookiecutter.package_name }}/docs/conf.py b/{{ cookiecutter.package_name }}/docs/conf.py index 0992c2a..b15cce2 100644 --- a/{{ cookiecutter.package_name }}/docs/conf.py +++ b/{{ cookiecutter.package_name }}/docs/conf.py @@ -6,11 +6,12 @@ import datetime - # -- Project information ----------------------------------------------------- # The full version, including alpha/beta/rc tags from {{ cookiecutter.module_name }} import __version__ + + release = __version__ project = "{{ cookiecutter.package_name }}" diff --git a/{{ cookiecutter.package_name }}/setup.py b/{{ cookiecutter.package_name }}/setup.py index c98cd51..7499bb1 100755 --- a/{{ cookiecutter.package_name }}/setup.py +++ b/{{ cookiecutter.package_name }}/setup.py @@ -1,8 +1,8 @@ #!/usr/bin/env python -from setuptools import setup {%- if cookiecutter.use_compiled_extensions == 'y' %} from extension_helpers import get_extensions {%- endif %} +from setuptools import setup setup( {%- if cookiecutter.use_compiled_extensions == 'y' -%} ext_modules=get_extensions() diff --git a/{{ cookiecutter.package_name }}/tox.ini b/{{ cookiecutter.package_name }}/tox.ini index e257ea2..c5fbc4d 100644 --- a/{{ cookiecutter.package_name }}/tox.ini +++ b/{{ cookiecutter.package_name }}/tox.ini @@ -1,7 +1,7 @@ [tox] min_version = 4.0 envlist = - py{38,39,310,311}-test + py{38,39,310,311,312}-test py38-test-oldestdeps build_docs diff --git a/{{ cookiecutter.package_name }}/{{ cookiecutter.module_name }}/__init__.py b/{{ cookiecutter.package_name }}/{{ cookiecutter.module_name }}/__init__.py index 5bd9399..86d71b2 100644 --- a/{{ cookiecutter.package_name }}/{{ cookiecutter.module_name }}/__init__.py +++ b/{{ cookiecutter.package_name }}/{{ cookiecutter.module_name }}/__init__.py @@ -1,9 +1,11 @@ +{%- if cookiecutter.include_example_code == 'y' -%} +from .example_mod import do_primes +{%- endif %} from .version import version as __version__ -{%- if cookiecutter.include_example_code == 'y' %} -from .example_mod import do_primes +{% if cookiecutter.include_example_code == 'y' -%} # Then you can be explicit to control what ends up in the namespace, __all__ = ['do_primes'] -{% else %} +{% else -%} __all__ = [] -{%- endif -%} +{% endif -%} diff --git a/{{ cookiecutter.package_name }}/{{ cookiecutter.module_name }}/example_mod.py b/{{ cookiecutter.package_name }}/{{ cookiecutter.module_name }}/example_mod.py index c262053..b2eff69 100644 --- a/{{ cookiecutter.package_name }}/{{ cookiecutter.module_name }}/example_mod.py +++ b/{{ cookiecutter.package_name }}/{{ cookiecutter.module_name }}/example_mod.py @@ -54,9 +54,10 @@ def do_primes(n, usecython=False): def main(args=None): - from astropy.utils.compat import argparse from time import time + from astropy.utils.compat import argparse + parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('-c', '--use-cython', dest='cy', action='store_true', help='Use the Cython-based Prime number generator.')