Skip to content

Commit

Permalink
Test for imported modules
Browse files Browse the repository at this point in the history
  • Loading branch information
StSav012 committed Apr 2, 2024
1 parent 2d5fa7b commit 3421b3c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
41 changes: 39 additions & 2 deletions test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,48 @@
from __future__ import annotations


def _third_party_modules() -> list[str]:
import site
import sys

prefixes: list[str] = site.getsitepackages([sys.exec_prefix, sys.prefix])
third_party_modules: list[str] = []
for module_name, module in sys.modules.copy().items():
paths = getattr(module, "__path__", [])
if (
"." not in module_name
and module_name != "_distutils_hack"
and paths
and getattr(module, "__package__", "")
and any(p.startswith(prefix) for p in paths for prefix in prefixes)
):
third_party_modules.append(module_name)

return third_party_modules


def test_cli():
import sys
from importlib.util import find_spec

from pycatsearch import main_cli as main

return main()
third_party_modules: list[str]

third_party_modules = _third_party_modules()
assert third_party_modules == [], third_party_modules

assert main() != 0

sys.argv.append("catalog.json.gz")
assert main() != 0

sys.argv.extend("--min-frequency 118749 --max-frequency 118751 -n oxygen".split())
assert main() == 0

third_party_modules = _third_party_modules()
assert third_party_modules == ["orjson"] * (find_spec("orjson") is not None), third_party_modules


if __name__ == "__main__":
exit(test_cli())
test_cli()
56 changes: 54 additions & 2 deletions test/test_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,63 @@
from __future__ import annotations


def _third_party_modules() -> list[str]:
import site
import sys

prefixes: list[str] = site.getsitepackages([sys.exec_prefix, sys.prefix])
third_party_modules: list[str] = []
for module_name, module in sys.modules.copy().items():
paths = getattr(module, "__path__", [])
if (
"." not in module_name
and module_name != "_distutils_hack"
and paths
and getattr(module, "__package__", "")
and any(p.startswith(prefix) for p in paths for prefix in prefixes)
):
third_party_modules.append(module_name)

return third_party_modules


def test_gui():
from pycatsearch import main_gui as main

return main()
third_party_modules: list[str]

third_party_modules = _third_party_modules()
assert third_party_modules == [], third_party_modules

# intentionally fail the app to let it import everything it needs
from qtpy.QtCore import QCoreApplication

app = QCoreApplication()
assert (r := main()) != 0, r
assert app is not None

expected_third_party_modules: list[str] = [
"packaging",
"shiboken6",
"PySide6",
"PySide2",
"PyQt6",
"PyQt5",
"qtpy",
"orjson",
"multidict",
"attr",
"idna",
"yarl",
"frozenlist",
"aiosignal",
"aiohttp",
"qtawesome",
]
third_party_modules = _third_party_modules()
assert third_party_modules
assert set(third_party_modules).issubset(expected_third_party_modules), third_party_modules


if __name__ == "__main__":
exit(test_gui())
test_gui()

0 comments on commit 3421b3c

Please sign in to comment.