diff --git a/README.md b/README.md index 7d5b7a9..c473c7e 100644 --- a/README.md +++ b/README.md @@ -56,30 +56,31 @@ For example, if upstream suggests installing test dependencies with %pyproject_buildrequires -r -x testing For projects that specify test requirements in their [tox] configuration, -these can be added using the `-t` flag followed by the tox environment. -The recommended tox environment (such as `py37` assuming the Fedora's Python version is 3.7) +these can be added using the `-t` flag (default tox environment) +or the `-e` flag followed by the tox environment. +The default tox environment (such as `py37` assuming the Fedora's Python version is 3.7) is available in the `%{toxenv}` macro. For example, if upstream suggests running the tests on Python 3.7 with `tox -e py37`, the test deps would be generated by: %generate_buildrequires - %pyproject_buildrequires -t %{toxenv} + %pyproject_buildrequires -t If upstream uses a custom derived environment, such as `py37-unit`, use: - %pyproject_buildrequires -t %{toxenv}-unit + %pyproject_buildrequires -e %{toxenv}-unit Or specify more environments if needed: - %pyproject_buildrequires -t %{toxenv}-unit,%{toxenv}-integration + %pyproject_buildrequires -e %{toxenv}-unit,%{toxenv}-integration -The `-t` option redefines `%{toxenv}` for further reuse. +The `-e` option redefines `%{toxenv}` for further reuse. Use `%{default_toxenv}` to get the default value. Note that `-t` implies `-r`, because tox normally assumes the package is installed including all the runtime dependencies. -The `-t` option uses [tox-current-env]'s `--print-deps-only` behind the scenes. +The `-t`/`-e` option uses [tox-current-env]'s `--print-deps-to-file` behind the scenes. [tox]: https://tox.readthedocs.io/ [tox-current-env]: https://github.com/fedora-python/tox-current-env/ @@ -122,8 +123,8 @@ Or (note the two sequential `--`s): %tox -- -- --flag-for-posargs -**Warning:** This macro assumes you have used `%pyproject_buildrequires -t` in -`%generate_buildrequires`. If not, you need to add: +**Warning:** This macro assumes you have used `%pyproject_buildrequires -t` or `-e` +in `%generate_buildrequires`. If not, you need to add: BuildRequires: python3dist(tox-current-env) @@ -141,7 +142,5 @@ Some valid Python version specifiers are not supported. The `-x` flag does not yet support multiple (comma-separated) extras. -The `-t` flag does not yet support being used without a value. - [PEP 517]: https://www.python.org/dev/peps/pep-0517/ [PEP 518]: https://www.python.org/dev/peps/pep-0518/ diff --git a/macros.pyproject b/macros.pyproject index 0a4f37b..1d2fc43 100644 --- a/macros.pyproject +++ b/macros.pyproject @@ -20,8 +20,8 @@ fi %default_toxenv py%{python3_version_nodots} %toxenv %{default_toxenv} -%pyproject_buildrequires(rx:t:) %{expand:\\\ -%{-t:%{expand:%global toxenv %{-t*}}} +%pyproject_buildrequires(rxte:) %{expand:\\\ +%{-e:%{expand:%global toxenv %{-e*}}} echo 'python3-devel' echo 'python3dist(packaging)' echo 'python3dist(pip) >= 19' @@ -29,7 +29,7 @@ echo 'python3dist(pytoml)' # setuptools assumes no pre-existing dist-info rm -rfv *.dist-info/ if [ -f %{__python3} ]; then - %{__python3} -I %{_rpmconfigdir}/redhat/pyproject_buildrequires.py %{?**} + RPM_TOXENV=%{toxenv} %{__python3} -I %{_rpmconfigdir}/redhat/pyproject_buildrequires.py %{?**} fi } diff --git a/pyproject-rpm-macros.spec b/pyproject-rpm-macros.spec index 1a31c0a..4fb50d7 100644 --- a/pyproject-rpm-macros.spec +++ b/pyproject-rpm-macros.spec @@ -35,7 +35,7 @@ BuildRequires: python3dist(packaging) BuildRequires: python3dist(pytoml) BuildRequires: python3dist(pip) BuildRequires: python3dist(setuptools) -BuildRequires: python3dist(tox-current-env) +BuildRequires: python3dist(tox-current-env) >= 0.0.2 BuildRequires: python3dist(wheel) %endif diff --git a/pyproject_buildrequires.py b/pyproject_buildrequires.py index 943f6d3..e9eddce 100644 --- a/pyproject_buildrequires.py +++ b/pyproject_buildrequires.py @@ -1,3 +1,4 @@ +import os import sys import importlib import argparse @@ -176,13 +177,18 @@ def generate_run_requirements(backend, requirements): def generate_tox_requirements(toxenv, requirements): - requirements.extend(['tox-current-env >= 0.0.2'], source='tox itself') + requirements.add('tox-current-env >= 0.0.2', source='tox itself') + requirements.check(source='tox itself') with tempfile.NamedTemporaryFile('r') as depfile: - with hook_call(): - subprocess.run( - ['tox', '--print-deps-to-file', depfile.name, '-qre', toxenv], - check=True, - ) + r = subprocess.run( + ['tox', '--print-deps-to-file', depfile.name, '-qre', toxenv], + check=True, + encoding='utf-8', + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + ) + if r.stdout: + print_err(r.stdout) requirements.extend(depfile.read().splitlines(), source=f'tox --print-deps-only: {toxenv}') @@ -222,7 +228,12 @@ def main(argv): help='Generate run-time requirements', ) parser.add_argument( - '-t', '--toxenv', metavar='TOXENVS', + '-e', '--toxenv', metavar='TOXENVS', default=None, + help=('specify tox environments' + '(implies --tox)'), + ) + parser.add_argument( + '-t', '--tox', action='store_true', help=('generate test tequirements from tox environment ' '(implies --runtime)'), ) @@ -235,8 +246,15 @@ def main(argv): ) args = parser.parse_args(argv) + if args.toxenv: + args.tox = True + + if args.tox: args.runtime = True + args.toxenv = (args.toxenv or os.getenv('RPM_TOXENV') or + f'py{sys.version_info.major}{sys.version_info.minor}') + if args.extras and not args.runtime: print_err('-x (--extras) are only useful with -r (--runtime)') exit(1) @@ -255,7 +273,7 @@ def main(argv): toxenv=args.toxenv, extras=args.extras, ) - except Exception as e: + except Exception: # Log the traceback explicitly (it's useful debug info) traceback.print_exc() exit(1) diff --git a/testcases.yaml b/testcases.yaml index b751d1f..2c90bd0 100644 --- a/testcases.yaml +++ b/testcases.yaml @@ -257,6 +257,8 @@ Tox depndencies: freeze_output: | setuptools==50 wheel==1 + tox==3.5.3 + tox-current-env==0.0.2 toxenv: py3 setup.py: | from setuptools import setup diff --git a/tests/python-pluggy.spec b/tests/python-pluggy.spec index c77ff13..449c23d 100644 --- a/tests/python-pluggy.spec +++ b/tests/python-pluggy.spec @@ -28,7 +28,7 @@ Summary: %{summary} %generate_buildrequires -%pyproject_buildrequires -t %{toxenv}-pytestrelease +%pyproject_buildrequires -e %{toxenv}-pytestrelease %build diff --git a/tests/python-pytest.spec b/tests/python-pytest.spec index d6ad7ab..0567c18 100644 --- a/tests/python-pytest.spec +++ b/tests/python-pytest.spec @@ -27,7 +27,7 @@ py.test provides simple, yet powerful testing for Python. %generate_buildrequires -%pyproject_buildrequires -r -x testing +%pyproject_buildrequires -x testing -t %build @@ -40,8 +40,7 @@ py.test provides simple, yet powerful testing for Python. %check # Only run one test (which uses a test-only dependency, hypothesis). # (Unfortunately, some other tests still fail.) -export PYTHONPATH=%{buildroot}%{python3_sitelib} -%{__python3} -m pytest -k metafunc +%tox -- -- -k metafunc %files -n python3-%{pypi_name}