import sys
from pathlib import Path

import pytest


@pytest.fixture(autouse=True, scope="function")
def cleanup():
    for p in (Path(__file__).parent / "targets").iterdir():
        sys.modules.pop(f"tests.targets.{p.stem}", None)
    yield


def test_find_missing():
    import tests.targets.module_full as mod

    # __all__ is autogenerated
    assert not mod.monkay.find_missing(all_var=mod.__all__, search_pathes=["tests.targets.fn_module"])
    # we can also use bools
    assert not mod.monkay.find_missing(all_var=True, search_pathes=["tests.targets.fn_module"])
    assert mod.monkay.find_missing(
        search_pathes=["tests.targets.not_existing", "tests.targets.module_preloaded1"],
    ) == {
        "tests.targets.not_existing": {"search_path_import"},
        "tests.targets.module_preloaded1.not_included_export": {"search_path_extra"},
    }
    assert mod.monkay.find_missing(all_var={}, search_pathes=["tests.targets.module_full_preloaded1"]) == {
        "bar": {"not_in_all_var"},
        "bar2": {
            "not_in_all_var",
        },
        "dynamic": {"not_in_all_var"},
        "settings": {"not_in_all_var"},
        "deprecated": {"not_in_all_var"},
        "tests.targets.module_full_preloaded1": {
            "missing_all_var",
        },
    }
    assert mod.monkay.find_missing(search_pathes=["tests.targets.module_missing_all"]) == {
        "tests.targets.module_missing_all": {"missing_all_var"},
    }

    assert mod.monkay.find_missing(search_pathes=["tests.targets.module_broken_all"]) == {
        "tests.targets.module_broken_all.broken": {"missing_attr", "search_path_extra"},
    }


def test_find_missing_no_all():
    from .targets import module_missing_all as mod

    assert mod.monkay.find_missing() == {
        "bar": {"not_in_all_var"},
        "tests.targets.module_missing_all": {"missing_all_var"},
    }


def test_find_missing_shadowed():
    from .targets import module_shadowed as mod

    assert mod.monkay.find_missing() == {
        "bar": {"shadowed"},
    }
