Some checks failed
CI (tests + ruff) and stable tag / unittest (py3.10) (push) Has been cancelled
CI (tests + ruff) and stable tag / unittest (py3.11) (push) Has been cancelled
CI (tests + ruff) and stable tag / unittest (py3.12) (push) Has been cancelled
CI (tests + ruff) and stable tag / unittest (py3.13) (push) Has been cancelled
CI (tests + ruff) and stable tag / ruff (py3.12) (push) Has been cancelled
CI (tests + ruff) and stable tag / Tag stable (if version commit) (push) Has been cancelled
https://chatgpt.com/share/69468609-0584-800f-a3e0-9d58210fb0e8
92 lines
2.9 KiB
Python
92 lines
2.9 KiB
Python
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import textwrap
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from p2pkg.__main__ import migrate_one
|
|
|
|
|
|
class TestMigration(unittest.TestCase):
|
|
def test_migrate_creates_package_and_exports_public_api(self) -> None:
|
|
with tempfile.TemporaryDirectory() as td:
|
|
root = Path(td)
|
|
mod = root / "roles_list.py"
|
|
mod.write_text(textwrap.dedent("""\
|
|
__all__ = ["add", "PUBLIC_CONST"]
|
|
PUBLIC_CONST = 123
|
|
_PRIVATE_CONST = 999
|
|
|
|
def add(a: int, b: int) -> int:
|
|
return a + b
|
|
|
|
def _hidden() -> int:
|
|
return 1
|
|
|
|
if __name__ == "__main__":
|
|
print("running as script")
|
|
"""),encoding="utf-8",
|
|
)
|
|
|
|
migrate_one(mod, use_git=False, repo_root=root)
|
|
|
|
pkg = root / "roles_list"
|
|
self.assertTrue((pkg / "__main__.py").exists())
|
|
self.assertTrue((pkg / "__init__.py").exists())
|
|
self.assertFalse(mod.exists())
|
|
|
|
# Import the package and ensure re-exports work
|
|
sys.path.insert(0, str(root))
|
|
try:
|
|
import roles_list # type: ignore
|
|
|
|
self.assertTrue(hasattr(roles_list, "add"))
|
|
self.assertEqual(roles_list.add(2, 3), 5)
|
|
self.assertTrue(hasattr(roles_list, "PUBLIC_CONST"))
|
|
self.assertEqual(roles_list.PUBLIC_CONST, 123)
|
|
|
|
# __all__ should be preserved
|
|
self.assertEqual(set(roles_list.__all__), {"add", "PUBLIC_CONST"})
|
|
self.assertFalse(hasattr(roles_list, "_hidden"))
|
|
finally:
|
|
sys.path.remove(str(root))
|
|
if "roles_list" in sys.modules:
|
|
del sys.modules["roles_list"]
|
|
|
|
def test_migrate_without_explicit_all_exports_public_names(self) -> None:
|
|
with tempfile.TemporaryDirectory() as td:
|
|
root = Path(td)
|
|
mod = root / "foo.py"
|
|
mod.write_text(textwrap.dedent("""\
|
|
VALUE = "ok"
|
|
|
|
def hello() -> str:
|
|
return "hi"
|
|
|
|
def _private() -> str:
|
|
return "no"
|
|
"""), encoding="utf-8")
|
|
migrate_one(mod, use_git=False, repo_root=root)
|
|
|
|
sys.path.insert(0, str(root))
|
|
try:
|
|
import foo # type: ignore
|
|
|
|
self.assertEqual(foo.hello(), "hi")
|
|
self.assertEqual(foo.VALUE, "ok")
|
|
self.assertIn("hello", foo.__all__)
|
|
self.assertIn("VALUE", foo.__all__)
|
|
self.assertNotIn("_private", foo.__all__)
|
|
finally:
|
|
sys.path.remove(str(root))
|
|
if "foo" in sys.modules:
|
|
del sys.modules["foo"]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(verbosity=2)
|