Blame SOURCES/test_pyproject_buildrequires.py

7ef706
from pathlib import Path
7ef706
import importlib.metadata
7ef706
7ef706
import pytest
7ef706
import yaml
7ef706
7ef706
from pyproject_buildrequires import generate_requires
7ef706
7ef706
7ef706
testcases = {}
7ef706
with Path(__file__).parent.joinpath('pyproject_buildrequires_testcases.yaml').open() as f:
7ef706
    testcases = yaml.safe_load(f)
7ef706
7ef706
7ef706
@pytest.mark.parametrize('case_name', testcases)
20b2b6
def test_data(case_name, capfd, tmp_path, monkeypatch):
7ef706
    case = testcases[case_name]
7ef706
7ef706
    cwd = tmp_path.joinpath('cwd')
7ef706
    cwd.mkdir()
7ef706
    monkeypatch.chdir(cwd)
20b2b6
    wheeldir = cwd.joinpath('wheeldir')
20b2b6
    wheeldir.mkdir()
7ef706
7ef706
    if case.get('xfail'):
7ef706
        pytest.xfail(case.get('xfail'))
7ef706
7ef706
    for filename in case:
7ef706
        file_types = ('.toml', '.py', '.in', '.ini', '.txt')
7ef706
        if filename.endswith(file_types):
7ef706
            cwd.joinpath(filename).write_text(case[filename])
7ef706
7ef706
    for name, value in case.get('environ', {}).items():
7ef706
        monkeypatch.setenv(name, value)
7ef706
7ef706
    def get_installed_version(dist_name):
7ef706
        try:
7ef706
            return str(case['installed'][dist_name])
7ef706
        except (KeyError, TypeError):
7ef706
            raise importlib.metadata.PackageNotFoundError(
7ef706
                f'info not found for {dist_name}'
7ef706
            )
7ef706
    requirement_files = case.get('requirement_files', [])
7ef706
    requirement_files = [open(f) for f in requirement_files]
7ef706
    use_build_system = case.get('use_build_system', True)
7ef706
    try:
7ef706
        generate_requires(
7ef706
            get_installed_version=get_installed_version,
7ef706
            include_runtime=case.get('include_runtime', use_build_system),
20b2b6
            build_wheel=case.get('build_wheel', False),
20b2b6
            wheeldir=str(wheeldir),
7ef706
            extras=case.get('extras', []),
7ef706
            toxenv=case.get('toxenv', None),
7ef706
            generate_extras=case.get('generate_extras', False),
7ef706
            requirement_files=requirement_files,
7ef706
            use_build_system=use_build_system,
7ef706
        )
7ef706
    except SystemExit as e:
7ef706
        assert e.code == case['result']
7ef706
    except Exception as e:
7ef706
        if 'except' not in case:
7ef706
            raise
7ef706
        assert type(e).__name__ == case['except']
7ef706
    else:
7ef706
        assert 0 == case['result']
7ef706
7ef706
        # this prevents us from accidentally writing "empty" tests
7ef706
        # if we ever need to do that, we can remove the check or change it:
7ef706
        assert 'expected' in case or 'stderr_contains' in case
7ef706
20b2b6
        out, err = capfd.readouterr()
7ef706
7ef706
        if 'expected' in case:
7ef706
            assert out == case['expected']
7ef706
7ef706
        # stderr_contains may be a string or list of strings
7ef706
        stderr_contains = case.get('stderr_contains')
7ef706
        if stderr_contains is not None:
7ef706
            if isinstance(stderr_contains, str):
7ef706
                stderr_contains = [stderr_contains]
7ef706
            for expected_substring in stderr_contains:
20b2b6
                assert expected_substring.format(**locals()) in err
7ef706
    finally:
7ef706
        for req in requirement_files:
7ef706
            req.close()