2020-10-14 23:43:13 +00:00
|
|
|
from pathlib import Path
|
2021-02-04 13:32:12 +00:00
|
|
|
import importlib.metadata
|
2020-10-14 23:43:13 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
from pyproject_buildrequires import generate_requires
|
|
|
|
|
|
|
|
|
|
|
|
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-04-29 10:42:37 +00:00
|
|
|
def test_data(case_name, capfd, tmp_path, monkeypatch):
|
2020-10-14 23:43:13 +00:00
|
|
|
case = testcases[case_name]
|
|
|
|
|
|
|
|
cwd = tmp_path.joinpath('cwd')
|
|
|
|
cwd.mkdir()
|
|
|
|
monkeypatch.chdir(cwd)
|
2022-04-29 10:42:37 +00:00
|
|
|
wheeldir = cwd.joinpath('wheeldir')
|
|
|
|
wheeldir.mkdir()
|
2020-10-14 23:43:13 +00:00
|
|
|
|
|
|
|
if case.get('xfail'):
|
|
|
|
pytest.xfail(case.get('xfail'))
|
|
|
|
|
2021-07-08 11:08:04 +00:00
|
|
|
for filename in case:
|
|
|
|
file_types = ('.toml', '.py', '.in', '.ini', '.txt')
|
|
|
|
if filename.endswith(file_types):
|
2020-10-14 23:43:13 +00:00
|
|
|
cwd.joinpath(filename).write_text(case[filename])
|
|
|
|
|
2021-07-23 08:00:01 +00:00
|
|
|
for name, value in case.get('environ', {}).items():
|
|
|
|
monkeypatch.setenv(name, value)
|
|
|
|
|
2020-10-14 23:43:13 +00:00
|
|
|
def get_installed_version(dist_name):
|
|
|
|
try:
|
|
|
|
return str(case['installed'][dist_name])
|
|
|
|
except (KeyError, TypeError):
|
2021-02-04 13:32:12 +00:00
|
|
|
raise importlib.metadata.PackageNotFoundError(
|
2020-10-14 23:43:13 +00:00
|
|
|
f'info not found for {dist_name}'
|
|
|
|
)
|
2021-07-08 11:08:04 +00:00
|
|
|
requirement_files = case.get('requirement_files', [])
|
|
|
|
requirement_files = [open(f) for f in requirement_files]
|
2022-01-14 15:26:40 +00:00
|
|
|
use_build_system = case.get('use_build_system', True)
|
2020-10-14 23:43:13 +00:00
|
|
|
try:
|
|
|
|
generate_requires(
|
|
|
|
get_installed_version=get_installed_version,
|
2022-01-14 15:26:40 +00:00
|
|
|
include_runtime=case.get('include_runtime', use_build_system),
|
2022-04-29 10:42:37 +00:00
|
|
|
build_wheel=case.get('build_wheel', False),
|
|
|
|
wheeldir=str(wheeldir),
|
2021-02-04 13:32:12 +00:00
|
|
|
extras=case.get('extras', []),
|
2020-10-14 23:43:13 +00:00
|
|
|
toxenv=case.get('toxenv', None),
|
2021-02-04 13:32:12 +00:00
|
|
|
generate_extras=case.get('generate_extras', False),
|
2021-07-08 11:08:04 +00:00
|
|
|
requirement_files=requirement_files,
|
2022-01-14 15:26:40 +00:00
|
|
|
use_build_system=use_build_system,
|
2020-10-14 23:43:13 +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']
|
|
|
|
|
2021-07-13 10:23:35 +00:00
|
|
|
# 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-04-29 10:42:37 +00:00
|
|
|
out, err = capfd.readouterr()
|
2021-07-13 10:23:35 +00:00
|
|
|
|
|
|
|
if 'expected' in case:
|
|
|
|
assert out == case['expected']
|
2021-07-23 08:00:01 +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-04-29 10:42:37 +00:00
|
|
|
assert expected_substring.format(**locals()) in err
|
2021-07-08 11:08:04 +00:00
|
|
|
finally:
|
|
|
|
for req in requirement_files:
|
|
|
|
req.close()
|