from pathlib import Path
import tomllib
import unittest

from setuptools import find_packages


class PackagingConfigTest(unittest.TestCase):
    def setUp(self):
        self.root = Path(__file__).resolve().parents[1]

    def test_find_packages_discovers_all_src_packages(self):
        expected_packages = sorted(
            ".".join(package_init.parent.relative_to(self.root).parts)
            for package_init in self.root.joinpath("src").rglob("__init__.py")
            if "__pycache__" not in package_init.parts
        )

        discovered_packages = sorted(find_packages(where=".", include=["src*"]))

        self.assertEqual(discovered_packages, expected_packages)

    def test_pyproject_uses_auto_package_discovery(self):
        pyproject = tomllib.loads((self.root / "pyproject.toml").read_text(encoding="utf-8"))
        setuptools_config = pyproject["tool"]["setuptools"]
        package_discovery = setuptools_config["packages"]["find"]

        self.assertNotIsInstance(setuptools_config.get("packages"), list)
        self.assertEqual(package_discovery["where"], ["."])
        self.assertEqual(package_discovery["include"], ["src*"])
        self.assertIs(package_discovery["namespaces"], False)


if __name__ == "__main__":
    unittest.main()
