Blame SOURCES/test_pyproject_buildrequires.py

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