From 8a60635881cc8d953d11996e4067384da19db601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Fri, 26 Jul 2019 14:34:21 +0200 Subject: [PATCH] Allow to fetch test dependencies from tox --- README.md | 20 +++++++++++-- macros.pyproject | 2 +- pyproject-rpm-macros.spec | 6 +++- pyproject_buildrequires.py | 25 ++++++++++++---- test_pyproject_buildrequires.py | 9 +++--- testcases.yaml | 31 ++++++++++++++++++++ tests/python-pluggy.spec | 51 +++++++++++++++++++++++++++++++++ tests/tests.yml | 3 ++ 8 files changed, 131 insertions(+), 16 deletions(-) create mode 100644 tests/python-pluggy.spec diff --git a/README.md b/README.md index 148b1d8..fe1ce03 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,21 @@ For example, if upstream suggests installing test dependencies with %generate_buildrequires %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. +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 py37 + +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. + +[tox]: https://tox.readthedocs.io/ +[tox-current-env]: https://github.com/fedora-python/tox-current-env/ Limitations ----------- @@ -64,14 +79,13 @@ Limitations This macro changes shebang lines of every Python script in `%{buildroot}%{_bindir}` to `#! %{__python3} %{py3_shbang_opt}` (`#! /usr/bin/python3 -s`). We plan to preserve existing Python flags in shebangs, but the work is not yet finished. -The PEPs don't (yet) define a way to specify test dependencies and test runners. -That means you still need to handle test dependencies and `%check` on your own. - Extras are currently ignored. 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 a reasonable default. + [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 6e558b2..593096c 100644 --- a/macros.pyproject +++ b/macros.pyproject @@ -17,7 +17,7 @@ if [ -d %{buildroot}%{python3_sitearch} ]; then fi } -%pyproject_buildrequires(rx:) %{expand:\\\ +%pyproject_buildrequires(rx:t:) %{expand:\\\ echo 'python3-devel' echo 'python3dist(packaging)' echo 'python3dist(pip) >= 19' diff --git a/pyproject-rpm-macros.spec b/pyproject-rpm-macros.spec index 2952217..393f640 100644 --- a/pyproject-rpm-macros.spec +++ b/pyproject-rpm-macros.spec @@ -6,7 +6,7 @@ License: MIT # Keep the version at zero and increment only release Version: 0 -Release: 4%{?dist} +Release: 5%{?dist} Source0: macros.pyproject Source1: pyproject_buildrequires.py @@ -35,6 +35,7 @@ BuildRequires: python3dist(packaging) BuildRequires: python3dist(pytoml) BuildRequires: python3dist(pip) BuildRequires: python3dist(setuptools) +BuildRequires: python3dist(tox-current-env) BuildRequires: python3dist(wheel) %endif @@ -75,6 +76,9 @@ install -m 644 pyproject_buildrequires.py %{buildroot}%{_rpmconfigdir}/redhat/ %license LICENSE %changelog +* Fri Jul 26 2019 Miro HronĨok - 0-5 +- Allow to fetch test dependencies from tox + * Fri Jul 26 2019 Fedora Release Engineering - 0-4 - Rebuilt for https://fedoraproject.org/wiki/Fedora_31_Mass_Rebuild diff --git a/pyproject_buildrequires.py b/pyproject_buildrequires.py index f5bdcc6..1aa0540 100644 --- a/pyproject_buildrequires.py +++ b/pyproject_buildrequires.py @@ -174,6 +174,19 @@ def generate_run_requirements(backend, requirements): requirements.extend(requires, source=f'wheel metadata: {key}') +def generate_tox_requirements(toxenv, requirements): + requirements.extend(['tox-current-env'], source='tox itself') + tox_output = subprocess.run( + ['tox', '--print-deps-only', '-qre', toxenv], + encoding='utf-8', + stdout=subprocess.PIPE, + check=True, + ).stdout + lines = tox_output.splitlines() + summary = lines.index(35 * '_' + ' summary ' + 36 * '_') + requirements.extend(lines[:summary], source=f'tox --print-deps-only: {toxenv}') + + def python3dist(name, op=None, version=None): if op is None: if version is not None: @@ -191,6 +204,9 @@ def generate_requires( try: backend = get_backend(requirements) generate_build_requirements(backend, requirements) + if toxenv is not None: + include_runtime = True + generate_tox_requirements(toxenv, requirements) if include_runtime: generate_run_requirements(backend, requirements) except EndPass: @@ -207,8 +223,8 @@ def main(argv): ) parser.add_argument( '-t', '--toxenv', metavar='TOXENVS', - help='generate test tequirements from tox environment ' - + '(not implemented; implies --runtime)', + help=('generate test tequirements from tox environment ' + '(implies --runtime)'), ) parser.add_argument( '-x', '--extras', metavar='EXTRAS', default='', @@ -219,10 +235,6 @@ def main(argv): ) args = parser.parse_args(argv) - if args.toxenv: - args.runtime = True - print_err('-t (--toxenv) is not implemented') - exit(1) if args.extras and not args.runtime: print_err('-x (--extras) are only useful with -r (--runtime)') exit(1) @@ -238,6 +250,7 @@ def main(argv): generate_requires( freeze_output, include_runtime=args.runtime, + toxenv=args.toxenv, extras=args.extras, ) except Exception as e: diff --git a/test_pyproject_buildrequires.py b/test_pyproject_buildrequires.py index 9a7c983..9b08e83 100644 --- a/test_pyproject_buildrequires.py +++ b/test_pyproject_buildrequires.py @@ -22,17 +22,16 @@ def test_data(case_name, capsys, tmp_path, monkeypatch): if case.get('xfail'): pytest.xfail(case.get('xfail')) - if 'pyproject.toml' in case: - cwd.joinpath('pyproject.toml').write_text(case['pyproject.toml']) - - if 'setup.py' in case: - cwd.joinpath('setup.py').write_text(case['setup.py']) + for filename in 'pyproject.toml', 'setup.py', 'tox.ini': + if filename in case: + cwd.joinpath(filename).write_text(case[filename]) try: generate_requires( case['freeze_output'], include_runtime=case.get('include_runtime', False), extras=case.get('extras', ''), + toxenv=case.get('toxenv', None), ) except SystemExit as e: assert e.code == case['result'] diff --git a/testcases.yaml b/testcases.yaml index 2ca9136..7eac034 100644 --- a/testcases.yaml +++ b/testcases.yaml @@ -252,3 +252,34 @@ Run dependencies with multiple extras: python3dist(dep3) python3dist(dep4) result: 0 + +Tox depndencies: + freeze_output: | + setuptools==50 + wheel==1 + toxenv: py3 + setup.py: | + from setuptools import setup + setup( + name='test', + version='0.1', + install_requires=['inst'], + ) + tox.ini: | + [tox] + envlist = py36,py37,py38 + [testenv] + deps = + toxdep1 + toxdep2 + commands = + true + expected: | + python3dist(setuptools) >= 40.8 + python3dist(wheel) + python3dist(wheel) + python3dist(tox-current-env) + python3dist(toxdep1) + python3dist(toxdep2) + python3dist(inst) + result: 0 diff --git a/tests/python-pluggy.spec b/tests/python-pluggy.spec new file mode 100644 index 0000000..43f3926 --- /dev/null +++ b/tests/python-pluggy.spec @@ -0,0 +1,51 @@ +%global pypi_name pluggy +Name: python-%{pypi_name} +Version: 0.12.0 +Release: 1%{?dist} +Summary: The plugin manager stripped of pytest specific details + +License: MIT +URL: https://github.com/pytest-dev/pluggy +Source0: %{pypi_source} + +BuildArch: noarch +BuildRequires: pyproject-rpm-macros + +%description +%{summary}. + + +%package -n python3-%{pypi_name} +Summary: %{summary} +%{?python_provide:%python_provide python3-%{pypi_name}} + +%description -n python3-%{pypi_name} +%{summary}. + + +%prep +%autosetup -p1 -n %{pypi_name}-%{version} + + +%generate_buildrequires +%pyproject_buildrequires -t py%{python3_version_nodots}-pytestrelease + + +%build +%pyproject_wheel + + +%install +%pyproject_install + + +%check +export PYTHONPATH=%{buildroot}%{python3_sitelib} +%{__python3} -m pytest + + +%files -n python3-%{pypi_name} +%doc README.rst +%license LICENSE +%{python3_sitelib}/%{pypi_name}/ +%{python3_sitelib}/%{pypi_name}-%{version}.dist-info/ diff --git a/tests/tests.yml b/tests/tests.yml index cc6d0d3..cc2fe95 100644 --- a/tests/tests.yml +++ b/tests/tests.yml @@ -19,6 +19,9 @@ - entrypoints: dir: . run: ./mocktest.sh python-entrypoints + - pluggy: + dir: . + run: ./mocktest.sh python-pluggy required_packages: - mock - rpmdevtools