# Copyright (c) OpenMMLab. All rights reserved.
import os.path as osp
import sys

import pkg_resources
import pytest

from mmengine.utils import get_installed_path, is_installed


def test_is_installed():
    # TODO: Windows CI may failed in unknown reason. Skip check the value
    is_installed('mmengine')

    # If there is `__init__.py` in the directory which is added into
    # `sys.path`, the directory will be recognized as a package.
    PYTHONPATH = osp.abspath(
        osp.join(osp.dirname(__file__), '..', '..', 'mmengine'))
    sys.path.append(PYTHONPATH)
    assert is_installed('optim')
    sys.path.pop()


def test_get_install_path():
    # TODO: Windows CI may failed in unknown reason. Skip check the value
    get_installed_path('mmengine')

    # get path for package "installed" by setting PYTHONPATH
    PYTHONPATH = osp.abspath(osp.join(osp.dirname(__file__), '..'))
    PYTHONPATH = osp.abspath(
        osp.join(osp.dirname(__file__), '..', '..', 'mmengine'))
    sys.path.append(PYTHONPATH)
    assert get_installed_path('optim') == osp.join(PYTHONPATH, 'optim')
    sys.path.pop()

    with pytest.raises(pkg_resources.DistributionNotFound):
        get_installed_path('unknown')

# Copyright (c) OpenMMLab. All rights reserved.
import os.path as osp
import sys
from pathlib import Path

from mmengine.utils import get_installed_path, is_installed


def test_is_installed():
    # TODO: Windows CI may failed in unknown reason. Skip check the value
    is_installed('mmengine')

    # package set by PYTHONPATH
    assert not is_installed('py_config')
    sys.path.append(osp.abspath(osp.join(osp.dirname(__file__), '..')))
    assert is_installed('test_utils')
    sys.path.pop()


def test_get_install_path(tmp_path: Path):
    # TODO: Windows CI may failed in unknown reason. Skip check the value
    get_installed_path('mmengine')

    # get path for package "installed" by setting PYTHONPATH
    PYTHONPATH = osp.abspath(osp.join(
        osp.dirname(__file__),
        '..',
    ))
    sys.path.append(PYTHONPATH)
    res_path = get_installed_path('test_utils')
    assert osp.join(PYTHONPATH, 'test_utils') == res_path

    # return the first path for namespace package
    # See more information about namespace package in:
    # https://packaging.python.org/en/latest/guides/packaging-namespace-packages/  # noqa:E501
    (tmp_path / 'test_utils').mkdir()
    sys.path.insert(-1, str(tmp_path))
    res_path = get_installed_path('test_utils')
    assert osp.abspath(osp.join(tmp_path, 'test_utils')) == res_path
    sys.path.pop()
    sys.path.pop()
