from pathlib import Path
import re


def test_versions(run):
    """Test versions.

    config.json - contains the HASS addon version
    Dockerfile  - installs the specific sunsynk library from pypi
    setup.py    - sunsynk library on pypi
    """

    def _get_version(filename, regex):
        txt = Path(filename).read_text()
        res = re.compile(regex).search(txt)
        assert res, "version not found in setup.py"
        return res.group(1)

    v_setup = _get_version(
        filename="setup.py",
        regex=r'VERSION = "(.+)"',
    )

    v_docker = _get_version(
        filename="hass-addon-sunsynk/Dockerfile",
        regex=r"sunsynk==(.+)",
    )

    v_config = _get_version(
        filename="hass-addon-sunsynk/config.json",
        regex=r'"version": ".+-(.+)"',
    )

    assert v_setup == v_docker
    assert v_setup == v_config