|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from constructor.conda_interface import cc_platform |
| 8 | +from constructor.construct import parse as construct_parse |
| 9 | +from constructor.construct import render as construct_render |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from pathlib import Path |
| 13 | + |
| 14 | + |
| 15 | +CONSTRUCT_YAML = """\ |
| 16 | +name: Installer |
| 17 | +version: 1.0.0 |
| 18 | +specs: |
| 19 | + - python |
| 20 | + - miniforge_console_shortcut # [win] |
| 21 | +""" |
| 22 | + |
| 23 | +CONSTRUCT_YAML_JINJA = """\ |
| 24 | +name: Installer |
| 25 | +version: 1.0.0 |
| 26 | +specs: |
| 27 | + - python |
| 28 | +{%- if os.environ.get("__CONSTRUCTOR_INCLUDE_CONDA__") %} |
| 29 | + - conda |
| 30 | +{%- endif %} |
| 31 | +""" |
| 32 | + |
| 33 | +CONSTRUCY_YAML_BROKEN = """\ |
| 34 | +name: Installer |
| 35 | +version: 1.0.0 |
| 36 | +specs |
| 37 | +""" |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture |
| 41 | +def construct_yaml_file(tmp_path: Path) -> str: |
| 42 | + file_path = tmp_path / "construct.yaml" |
| 43 | + file_path.write_text(CONSTRUCT_YAML) |
| 44 | + return str(file_path) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.fixture |
| 48 | +def construct_yaml_file_jinja(tmp_path: Path) -> str: |
| 49 | + file_path = tmp_path / "construct.yaml" |
| 50 | + file_path.write_text(CONSTRUCT_YAML_JINJA) |
| 51 | + return str(file_path) |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.parametrize("platform", ("linux-64", "win-64")) |
| 55 | +def test_render(platform: str, construct_yaml_file: Path): |
| 56 | + rendered = construct_render(construct_yaml_file, platform) |
| 57 | + rendered_lines = rendered.splitlines() |
| 58 | + expected = CONSTRUCT_YAML.splitlines()[:-1] |
| 59 | + if platform == "win-64": |
| 60 | + expected.append(" - miniforge_console_shortcut") |
| 61 | + assert rendered_lines == expected |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize("platform", ("linux-64", "win-64")) |
| 65 | +def test_parse(platform: str, construct_yaml_file: Path): |
| 66 | + parsed = construct_parse(construct_yaml_file, platform) |
| 67 | + assert parsed["name"] == "Installer" |
| 68 | + assert parsed["version"] == "1.0.0" |
| 69 | + expected_specs = [ |
| 70 | + "python", |
| 71 | + *(("miniforge_console_shortcut",) if platform == "win-64" else ()), |
| 72 | + ] |
| 73 | + assert parsed["specs"] == expected_specs |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.parametrize("include_conda", (True, False)) |
| 77 | +def test_render_jinja( |
| 78 | + include_conda: bool, construct_yaml_file_jinja: Path, monkeypatch: pytest.MonkeyPatch |
| 79 | +): |
| 80 | + if include_conda: |
| 81 | + monkeypatch.setenv("__CONSTRUCTOR_INCLUDE_CONDA__", "1") |
| 82 | + rendered = construct_render(construct_yaml_file_jinja, cc_platform) |
| 83 | + rendered_lines = rendered.splitlines() |
| 84 | + expected = CONSTRUCT_YAML_JINJA.splitlines()[:-3] |
| 85 | + if include_conda: |
| 86 | + expected.append(" - conda") |
| 87 | + assert rendered_lines == expected |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.parametrize("include_conda", (True, False)) |
| 91 | +def test_parse_jinja( |
| 92 | + include_conda: bool, construct_yaml_file_jinja: Path, monkeypatch: pytest.MonkeyPatch |
| 93 | +): |
| 94 | + if include_conda: |
| 95 | + monkeypatch.setenv("__CONSTRUCTOR_INCLUDE_CONDA__", "1") |
| 96 | + parsed = construct_parse(construct_yaml_file_jinja, cc_platform) |
| 97 | + assert parsed["name"] == "Installer" |
| 98 | + assert parsed["version"] == "1.0.0" |
| 99 | + expected_specs = [ |
| 100 | + "python", |
| 101 | + *(("conda",) if include_conda else ()), |
| 102 | + ] |
| 103 | + assert parsed["specs"] == expected_specs |
| 104 | + |
| 105 | + |
| 106 | +def test_parse_error(tmp_path): |
| 107 | + construct_yaml_file = tmp_path / "construct.yaml" |
| 108 | + construct_yaml_file.write_text(CONSTRUCY_YAML_BROKEN) |
| 109 | + with pytest.raises(SystemExit) as exc: |
| 110 | + construct_parse(construct_yaml_file, cc_platform) |
| 111 | + assert exc.value.code != 0 |
| 112 | + assert "Unable to parse" in str(exc.getrepr()) |
0 commit comments