2021-11-04 06:44:20 +00:00
|
|
|
from pathlib import Path
|
|
|
|
import importlib.metadata
|
|
|
|
|
2023-09-21 19:56:30 +00:00
|
|
|
import packaging.version
|
2021-11-04 06:44:20 +00:00
|
|
|
import pytest
|
2023-09-21 19:56:30 +00:00
|
|
|
import setuptools
|
2021-11-04 06:44:20 +00:00
|
|
|
import yaml
|
|
|
|
|
|
|
|
from pyproject_buildrequires import generate_requires
|
|
|
|
|
2023-09-21 19:56:30 +00:00
|
|
|
SETUPTOOLS_VERSION = packaging.version.parse(setuptools.__version__)
|
|
|
|
SETUPTOOLS_60 = SETUPTOOLS_VERSION >= packaging.version.parse('60')
|
2021-11-04 06:44:20 +00:00
|
|
|
|
|
|
|
testcases = {}
|
|
|
|
with Path(__file__).parent.joinpath('pyproject_buildrequires_testcases.yaml').open() as f:
|
|
|
|
testcases = yaml.safe_load(f)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('case_name', testcases)
|
2022-09-27 15:15:22 +00:00
|
|
|
def test_data(case_name, capfd, tmp_path, monkeypatch):
|
2021-11-04 06:44:20 +00:00
|
|
|
case = testcases[case_name]
|
|
|
|
|
|
|
|
cwd = tmp_path.joinpath('cwd')
|
|
|
|
cwd.mkdir()
|
|
|
|
monkeypatch.chdir(cwd)
|
2022-09-27 15:15:22 +00:00
|
|
|
wheeldir = cwd.joinpath('wheeldir')
|
|
|
|
wheeldir.mkdir()
|
2023-09-21 19:56:30 +00:00
|
|
|
output = tmp_path.joinpath('output.txt')
|
2021-11-04 06:44:20 +00:00
|
|
|
|
|
|
|
if case.get('xfail'):
|
|
|
|
pytest.xfail(case.get('xfail'))
|
|
|
|
|
2023-09-21 19:56:30 +00:00
|
|
|
if case.get('skipif') and eval(case.get('skipif')):
|
|
|
|
pytest.skip(case.get('skipif'))
|
|
|
|
|
2021-11-04 06:44:20 +00:00
|
|
|
for filename in case:
|
2023-09-21 19:56:30 +00:00
|
|
|
file_types = ('.toml', '.py', '.in', '.ini', '.txt', '.cfg')
|
2021-11-04 06:44:20 +00:00
|
|
|
if filename.endswith(file_types):
|
|
|
|
cwd.joinpath(filename).write_text(case[filename])
|
|
|
|
|
|
|
|
for name, value in case.get('environ', {}).items():
|
|
|
|
monkeypatch.setenv(name, value)
|
|
|
|
|
|
|
|
def get_installed_version(dist_name):
|
|
|
|
try:
|
|
|
|
return str(case['installed'][dist_name])
|
|
|
|
except (KeyError, TypeError):
|
|
|
|
raise importlib.metadata.PackageNotFoundError(
|
|
|
|
f'info not found for {dist_name}'
|
|
|
|
)
|
|
|
|
requirement_files = case.get('requirement_files', [])
|
|
|
|
requirement_files = [open(f) for f in requirement_files]
|
2022-03-01 13:21:17 +00:00
|
|
|
use_build_system = case.get('use_build_system', True)
|
2021-11-04 06:44:20 +00:00
|
|
|
try:
|
|
|
|
generate_requires(
|
|
|
|
get_installed_version=get_installed_version,
|
2022-03-01 13:21:17 +00:00
|
|
|
include_runtime=case.get('include_runtime', use_build_system),
|
2022-09-27 15:15:22 +00:00
|
|
|
build_wheel=case.get('build_wheel', False),
|
|
|
|
wheeldir=str(wheeldir),
|
2021-11-04 06:44:20 +00:00
|
|
|
extras=case.get('extras', []),
|
|
|
|
toxenv=case.get('toxenv', None),
|
|
|
|
generate_extras=case.get('generate_extras', False),
|
|
|
|
requirement_files=requirement_files,
|
2022-03-01 13:21:17 +00:00
|
|
|
use_build_system=use_build_system,
|
2023-09-21 19:56:30 +00:00
|
|
|
output=output,
|
|
|
|
config_settings=case.get('config_settings'),
|
2021-11-04 06:44:20 +00:00
|
|
|
)
|
|
|
|
except SystemExit as e:
|
|
|
|
assert e.code == case['result']
|
|
|
|
except Exception as e:
|
|
|
|
if 'except' not in case:
|
|
|
|
raise
|
|
|
|
assert type(e).__name__ == case['except']
|
|
|
|
else:
|
|
|
|
assert 0 == case['result']
|
|
|
|
|
|
|
|
# this prevents us from accidentally writing "empty" tests
|
|
|
|
# if we ever need to do that, we can remove the check or change it:
|
|
|
|
assert 'expected' in case or 'stderr_contains' in case
|
|
|
|
|
2022-09-27 15:15:22 +00:00
|
|
|
out, err = capfd.readouterr()
|
2023-09-21 19:56:30 +00:00
|
|
|
dependencies = output.read_text()
|
2021-11-04 06:44:20 +00:00
|
|
|
|
|
|
|
if 'expected' in case:
|
2023-03-28 11:46:55 +00:00
|
|
|
expected = case['expected']
|
|
|
|
if isinstance(expected, list):
|
|
|
|
# at least one of them needs to match
|
2023-09-21 19:56:30 +00:00
|
|
|
assert dependencies in expected
|
2023-03-28 11:46:55 +00:00
|
|
|
else:
|
2023-09-21 19:56:30 +00:00
|
|
|
assert dependencies == expected
|
2021-11-04 06:44:20 +00:00
|
|
|
|
|
|
|
# stderr_contains may be a string or list of strings
|
|
|
|
stderr_contains = case.get('stderr_contains')
|
|
|
|
if stderr_contains is not None:
|
|
|
|
if isinstance(stderr_contains, str):
|
|
|
|
stderr_contains = [stderr_contains]
|
|
|
|
for expected_substring in stderr_contains:
|
2022-09-27 15:15:22 +00:00
|
|
|
assert expected_substring.format(**locals()) in err
|
2021-11-04 06:44:20 +00:00
|
|
|
finally:
|
|
|
|
for req in requirement_files:
|
|
|
|
req.close()
|