def test_iter_requirements(report: DependencyReport, depinfo: Package) -> None:
    """Test that requirements can be iterated as expected."""
    assert next(report.iter_requirements(max_depth=0)) == (0, depinfo)
    assert {pkg.name for _, pkg in report.iter_requirements()}.issuperset(
        {"importlib-metadata"}
    )
    assert {pkg.name for pkg in report.build_tools}.issuperset({"pip", "setuptools"})
def test_iter_unique_requirements(report: DependencyReport) -> None:
    """Test that unique requirements can be iterated as expected."""
    assert {name for name, _ in report.iter_unique_requirements(max_depth=0)} == {
        "depinfo"
    }
    assert {name for name, _ in report.iter_unique_requirements()}.issuperset(
        {"depinfo", "importlib-metadata"}
    )
def test_init(
    attributes: Dict[str, str], platform: Platform, python: Python, depinfo: Package
) -> None:
    """Test that the dependency report model can be initialized correctly."""
    pkg = DependencyReport(root=depinfo, platform=platform, python=python, **attributes)
    for attr, value in attributes.items():
        assert getattr(pkg, attr) == value
    def run(
        cls,
        package_name: str,
        display_format: DisplayFormat = DisplayFormat.Simple,
        build_tools: Iterable[str] = (
            "conda",
            "flit",
            "hatch",
            "mamba",
            "pbr",
            "pip",
            "poetry",
            "setuptools",
            "wheel",
        ),
        max_depth: int = 1,
    ) -> None:
        """
        Display the given package's dependencies in the desired format.

        Args:
            package_name: The package name for which to generate dependency information.
            display_format: One of the supported display formats.
            build_tools: A list of build packages to include.
            max_depth: The maximum desired depth of requirements nesting.

        """
        report = DependencyReport.from_root(
            root=package_name,
            build_tools=build_tools,
            max_depth=max_depth,
        )
        DisplayServiceRegistry.display_service(
            display_format=display_format).display(report=report,
                                                   max_depth=max_depth)
def test_from_root() -> None:
    """Test dependency report creation from a root name."""
    report = DependencyReport.from_root("depinfo", ("pip", "setuptools"))
    assert {pkg.name for _, pkg in report.iter_requirements(max_depth=0)}.issuperset(
        {"depinfo"}
    )
    assert {pkg.name for _, pkg in report.iter_requirements()}.issuperset(
        {"importlib-metadata"}
    )
    assert {pkg.name for pkg in report.build_tools}.issuperset({"pip", "setuptools"})
def report(platform: Platform, python: Python, depinfo: Package) -> DependencyReport:
    """Provide a dependency report fixture."""
    tools = ["pip", "setuptools"]
    packages = {depinfo.name: depinfo}
    for name in depinfo.requirements + tools:
        packages[name] = Package.from_name(name)
    return DependencyReport(
        root=depinfo,
        platform=platform,
        python=python,
        build_tools=[packages[name] for name in tools],
        packages=packages,
    )
Ejemplo n.º 7
0
    def display(cls,
                report: DependencyReport,
                max_depth: int = 1,
                **kwargs) -> None:
        """
        Display a dependency report to a desired maximum depth as markdown tables.

        Args:
            report: A dependency report instance.
            max_depth:  The maximum desired depth (default 1).
            **kwargs: Keyword arguments are ignored.

        """
        print("\n".join([
            "",
            "### Platform Information",
            "",
            *cls._format_table(
                ["", ""],
                [
                    (report.platform.name, report.platform.version),
                    (report.python.name, report.python.version),
                ],
            ),
        ]))
        requirements = sorted(
            report.iter_unique_requirements(missing_version="**missing**",
                                            max_depth=max_depth),
            key=itemgetter(0),
        )
        print("\n".join([
            "",
            "### Dependency Information",
            "",
            *cls._format_table(["Package", "Version"], requirements),
        ]))
        tools = sorted(
            ((pkg.name, pkg.version)
             for pkg in report.build_tools if pkg.version is not None),
            key=itemgetter(0),
        )
        print("\n".join([
            "",
            "### Build Tools Information",
            "",
            *cls._format_table(["Package", "Version"], tools),
        ]))
    def display(cls,
                report: DependencyReport,
                max_depth: int = 1,
                **kwargs) -> None:
        """
        Display a dependency report to a desired maximum depth as simple tables.

        Args:
            report: A dependency report instance.
            max_depth:  The maximum desired depth (default 1).
            **kwargs: Keyword arguments are ignored.

        """
        print("\n".join([
            "",
            "Platform Information",
            "--------------------",
            *cls._format_pairs([
                (report.platform.name, report.platform.version),
                (report.python.name, report.python.version),
            ]),
        ]))
        requirements = sorted(
            report.iter_unique_requirements(missing_version="missing",
                                            max_depth=max_depth),
            key=itemgetter(0),
        )
        print("\n".join([
            "",
            "Dependency Information",
            "----------------------",
            *cls._format_pairs(requirements),
        ]))
        print("\n".join([
            "",
            "Build Tools Information",
            "-----------------------",
            *cls._format_pairs(
                sorted(
                    ((pkg.name, pkg.version)
                     for pkg in report.build_tools if pkg.version is not None),
                    key=itemgetter(0),
                )),
        ]))