tests.test_cli_sbom module¶

mpm sbom CLI tests.

Drives the subcommand end to end: format resolution, per-standard exports to console and file, the --query filter and --minimal mode. The SPDX and CycloneDX renderers it delegates to are unit-tested in tests.test_sbom.

tests.test_cli_sbom.PROXY_VARS = ('ALL_PROXY', 'HTTPS_PROXY', 'HTTP_PROXY', 'all_proxy', 'http_proxy', 'https_proxy')¶

Proxy variables httpx reads, cleared so a host proxy never reaches a test.

tests.test_cli_sbom.OSV_RECORD = {'id': 'GHSA-aaaa-bbbb-cccc', 'modified': '2021-06-01T00:00:00Z', 'summary': 'Cross-site scripting in Example'}¶

The fields of the advisory tests.test_sbom_vulnerabilities uses that a CycloneDX vulnerability entry needs.

class tests.test_cli_sbom.AliasedFakeManager[source]¶

Bases: FakeManager

A fake recording a PyPI coordinate for each of its packages.

Homebrew does the same for the upstream registry a formula builds from: the coordinate is an alias the vulnerability scan queries in place of the manager’s own purl, which no advisory database knows.

Initialize cli_errors list.

package_metadata_batch(packages)[source]¶

Yield (package, metadata) pairs, enriched where possible.

The metadata carries whatever rich per-package data this manager can surface.

Called by mpm sbom in --bundled mode to populate licenses, checksums, download URLs, supplier/originator, and the declared dependency graph. The base implementation yields meta_package_manager.package.EMPTY_METADATA for each package and stays compatible with managers that do not (yet) expose richer metadata: their SBOM entries stay at the minimal Package level, matching the historical and --minimal modes.

Manager subclasses override this with their native query path:

  • bulk shell-outs when the CLI accepts a package list (brew info --json=v2 --installed, dpkg-query -W, apt-cache show);

  • on-disk parsing when the metadata already lives on the filesystem (pip’s .dist-info directories, Homebrew’s per-formula sbom.spdx.json, dpkg’s .md5sums).

The yielded pairs do not need to preserve the input order; the SBOM renderer matches by Package identity. Implementations are expected to swallow per-package extraction errors and yield meta_package_manager.package.EMPTY_METADATA for the affected packages rather than failing the whole scan: a single misbehaving formula must not abort an enrichment pass spanning hundreds of packages.

Todo

Today every extractor is local-only (shell-outs to the manager’s CLI, plus on-disk reads). When extractors start reaching for network resources (PyPI’s JSON API, npm’s registry, crates.io, GitHub’s security advisories) the --bundled flag will no longer be a fine-grained enough knob: some users will want enrichment but not network traffic (offline scans, CI without egress). The natural split is a future --network/--no-network flag layered under --bundled to gate the network-touching code paths specifically, leaving local enrichment always-on for --bundled.

cli_names: tuple[str, ...] = ('aliasedfakemanager',)¶

List of CLI names the package manager is known as.

This list of recognized CLI names is ordered by priority. That way we can influence the search of the right binary.

..hint::

This was helpful in the case of the Python transition from 2.x to 3.x, where multiple versions of the same executable were named python or python3.

By default, this property’s value is derived from the manager’s ID (see the MetaPackageManager.__init__ method above).

documentation_url: str | None = None¶

Entry point of the project’s own documentation, or None when it has none.

The manual, the documentation site, or the man page of the CLI mpm drives: whichever page a user reads to learn the commands this manager wraps. Listed between homepage_url and repository_url in the links of the manager’s page, so a reader reaching for the tool’s reference finds it in the box of facts rather than in the prose below it.

Left unset where a link would state a fact twice or invent one: a tool documenting itself in its repository alone, whose README repository_url already reaches. A page about the ecosystem rather than the tool does not qualify either.

Where a project’s only site is its manual, that address belongs here and homepage_url stays unset: the slot names what the page is, and a reader reaching for the reference looks under the documentation link.

Never inherited, like every other link: the AUR helpers extending pacman each document themselves.

homepage_url: str | None = None¶

Home page of the project, only used in documentation for reference.

Left unset when the home page is the project’s repository, which repository_url names on its own, and when it is the project’s manual, which documentation_url names. Never inherited, like the other links.

id: str = 'aliasedfakemanager'¶

Package manager’s ID.

Derived by defaults from the lower-cased class name in which underscores _ are replaced by dashes -.

This ID must be unique among all package manager definitions and lower-case, as they’re used as feature flags for the mpm CLI.

name: str = 'AliasedFakeManager'¶

Return package manager’s common name.

Default value is based on class name.

repository_url: str | None = None¶

Repository holding the project’s code, or None when it has no public one.

Linked from the manager’s documentation page. Also what the weekly metrics sample reads: docs/docs_update.py writes it into the [tool.repomatic.metrics] subjects of pyproject.toml when its forge answers the sampler, through a mirror for a forge that does not.

Never inherited, for the same reason as wikipedia_url: the AUR helpers extending pacman each live in a repository of their own.

virtual: bool = False¶

Should we expose the package manager to the user?

Virtual package manager are just skeleton classes used to factorize code among managers of the same family.

wikipedia_url: str | None = None¶

English Wikipedia article about the project, or None when it has none.

Listed last in the links of the manager’s documentation page. The article covers the project that home page names, or is a redirect Wikipedia keeps under the project’s name, landing on the part of a broader article that describes it: Cargo_(software) opens the Cargo section of the Rust article. An article that merely mentions the project does not count, nor does one about a related subject: lazy is a Neovim plugin manager, and the Neovim article is not about it.

Never inherited: the metaclass resets it to None on any class not declaring its own. A subclass often wraps a distinct project, like the six AUR helpers extending pacman, and would otherwise present pacman’s article as its own. Documentation-only, like homepage_url.

class tests.test_cli_sbom.UnreadableMetadataFakeManager[source]¶

Bases: FakeManager

A fake whose metadata extractor fails.

Initialize cli_errors list.

package_metadata_batch(packages)[source]¶

Yield (package, metadata) pairs, enriched where possible.

The metadata carries whatever rich per-package data this manager can surface.

Called by mpm sbom in --bundled mode to populate licenses, checksums, download URLs, supplier/originator, and the declared dependency graph. The base implementation yields meta_package_manager.package.EMPTY_METADATA for each package and stays compatible with managers that do not (yet) expose richer metadata: their SBOM entries stay at the minimal Package level, matching the historical and --minimal modes.

Manager subclasses override this with their native query path:

  • bulk shell-outs when the CLI accepts a package list (brew info --json=v2 --installed, dpkg-query -W, apt-cache show);

  • on-disk parsing when the metadata already lives on the filesystem (pip’s .dist-info directories, Homebrew’s per-formula sbom.spdx.json, dpkg’s .md5sums).

The yielded pairs do not need to preserve the input order; the SBOM renderer matches by Package identity. Implementations are expected to swallow per-package extraction errors and yield meta_package_manager.package.EMPTY_METADATA for the affected packages rather than failing the whole scan: a single misbehaving formula must not abort an enrichment pass spanning hundreds of packages.

Todo

Today every extractor is local-only (shell-outs to the manager’s CLI, plus on-disk reads). When extractors start reaching for network resources (PyPI’s JSON API, npm’s registry, crates.io, GitHub’s security advisories) the --bundled flag will no longer be a fine-grained enough knob: some users will want enrichment but not network traffic (offline scans, CI without egress). The natural split is a future --network/--no-network flag layered under --bundled to gate the network-touching code paths specifically, leaving local enrichment always-on for --bundled.

cli_names: tuple[str, ...] = ('unreadablemetadatafakemanager',)¶

List of CLI names the package manager is known as.

This list of recognized CLI names is ordered by priority. That way we can influence the search of the right binary.

..hint::

This was helpful in the case of the Python transition from 2.x to 3.x, where multiple versions of the same executable were named python or python3.

By default, this property’s value is derived from the manager’s ID (see the MetaPackageManager.__init__ method above).

documentation_url: str | None = None¶

Entry point of the project’s own documentation, or None when it has none.

The manual, the documentation site, or the man page of the CLI mpm drives: whichever page a user reads to learn the commands this manager wraps. Listed between homepage_url and repository_url in the links of the manager’s page, so a reader reaching for the tool’s reference finds it in the box of facts rather than in the prose below it.

Left unset where a link would state a fact twice or invent one: a tool documenting itself in its repository alone, whose README repository_url already reaches. A page about the ecosystem rather than the tool does not qualify either.

Where a project’s only site is its manual, that address belongs here and homepage_url stays unset: the slot names what the page is, and a reader reaching for the reference looks under the documentation link.

Never inherited, like every other link: the AUR helpers extending pacman each document themselves.

homepage_url: str | None = None¶

Home page of the project, only used in documentation for reference.

Left unset when the home page is the project’s repository, which repository_url names on its own, and when it is the project’s manual, which documentation_url names. Never inherited, like the other links.

id: str = 'unreadablemetadatafakemanager'¶

Package manager’s ID.

Derived by defaults from the lower-cased class name in which underscores _ are replaced by dashes -.

This ID must be unique among all package manager definitions and lower-case, as they’re used as feature flags for the mpm CLI.

name: str = 'UnreadableMetadataFakeManager'¶

Return package manager’s common name.

Default value is based on class name.

repository_url: str | None = None¶

Repository holding the project’s code, or None when it has no public one.

Linked from the manager’s documentation page. Also what the weekly metrics sample reads: docs/docs_update.py writes it into the [tool.repomatic.metrics] subjects of pyproject.toml when its forge answers the sampler, through a mirror for a forge that does not.

Never inherited, for the same reason as wikipedia_url: the AUR helpers extending pacman each live in a repository of their own.

virtual: bool = False¶

Should we expose the package manager to the user?

Virtual package manager are just skeleton classes used to factorize code among managers of the same family.

wikipedia_url: str | None = None¶

English Wikipedia article about the project, or None when it has none.

Listed last in the links of the manager’s documentation page. The article covers the project that home page names, or is a redirect Wikipedia keeps under the project’s name, landing on the part of a broader article that describes it: Cargo_(software) opens the Cargo section of the Rust article. An article that merely mentions the project does not count, nor does one about a related subject: lazy is a Neovim plugin manager, and the Neovim article is not about it.

Never inherited: the metaclass resets it to None on any class not declaring its own. A subclass often wraps a distinct project, like the six AUR helpers extending pacman, and would otherwise present pacman’s article as its own. Documentation-only, like homepage_url.

tests.test_cli_sbom.osv(monkeypatch, tmp_path)[source]¶

Mock the OSV API, with no host proxy and the client cache in tmp_path.

tests.test_cli_sbom.subcmd()[source]¶
tests.test_cli_sbom.evaluate_signals(mid, stdout, stderr)[source]¶
tests.test_cli_sbom.check_selection(result, selected=('am', 'antidote', 'antigen', 'antigravity-cli-plugins', 'apk', 'app-manager', 'apt', 'apt-mint', 'aptitude', 'asdf', 'aura', 'basalt', 'bin', 'bob', 'bpkg', 'brew', 'bun', 'cargo', 'choosenim', 'claude-code-plugins', 'clib', 'composer', 'conda', 'cpan', 'cran', 'deb-get', 'dkp-pacman', 'dnf', 'dnf5', 'dotnet', 'elan', 'emacs', 'emerge', 'eopkg', 'fisher', 'flatpak', 'fwupd', 'gcloud', 'gem', 'getnf', 'gext', 'gh-ext', 'ghcup', 'go', 'guix', 'gup', 'haxelib', 'hyprpm', 'jpm', 'julia', 'juliaup', 'krew', 'lazy', 'luarocks', 'lure', 'mamba', 'mason', 'micro', 'microdnf', 'micromamba', 'miktex', 'mise', 'nala', 'nimble', 'nix', 'npm', 'octave', 'oh-my-fish', 'ollama', 'opam', 'opkg', 'pacman', 'pacstall', 'pamac', 'paru', 'pear', 'pearl', 'pi', 'pikaur', 'pip', 'pipx', 'pipxu', 'pixi', 'pkcon', 'pkgit', 'pkgm', 'platformio-core', 'pmerge', 'pnpm', 'protonplus', 'pwsh-gallery', 'pyenv', 'raco', 'roswell', 'rpm-ostree', 'rustup', 'sdkman', 'sheldon', 'skills', 'snap', 'soar', 'spack', 'steamcmd', 'stew', 'tlmgr', 'topgrade', 'trizen', 'uv', 'uvx', 'vagrant', 'vcpkg', 'vim-pack', 'vscode', 'vscodium', 'whalebrew', 'xbps', 'yarn', 'yarn-berry', 'yay', 'yazi', 'yum', 'zef', 'zerobrew', 'zeroinstall', 'zim', 'zinit', 'zplug', 'zvm', 'zypper'), reference_set=('am', 'antidote', 'antigen', 'antigravity-cli-plugins', 'apk', 'app-manager', 'apt', 'apt-mint', 'aptitude', 'asdf', 'aura', 'basalt', 'bin', 'bob', 'bpkg', 'brew', 'bun', 'cargo', 'choosenim', 'claude-code-plugins', 'clib', 'composer', 'conda', 'cpan', 'cran', 'deb-get', 'dkp-pacman', 'dnf', 'dnf5', 'dotnet', 'elan', 'emacs', 'emerge', 'eopkg', 'fisher', 'flatpak', 'fwupd', 'gcloud', 'gem', 'getnf', 'gext', 'gh-ext', 'ghcup', 'go', 'guix', 'gup', 'haxelib', 'hyprpm', 'jpm', 'julia', 'juliaup', 'krew', 'lazy', 'luarocks', 'lure', 'mamba', 'mason', 'micro', 'microdnf', 'micromamba', 'miktex', 'mise', 'nala', 'nimble', 'nix', 'npm', 'octave', 'oh-my-fish', 'ollama', 'opam', 'opkg', 'pacman', 'pacstall', 'pamac', 'paru', 'pear', 'pearl', 'pi', 'pikaur', 'pip', 'pipx', 'pipxu', 'pixi', 'pkcon', 'pkgit', 'pkgm', 'platformio-core', 'pmerge', 'pnpm', 'protonplus', 'pwsh-gallery', 'pyenv', 'raco', 'roswell', 'rpm-ostree', 'rustup', 'sdkman', 'sheldon', 'skills', 'snap', 'soar', 'spack', 'steamcmd', 'stew', 'tlmgr', 'topgrade', 'trizen', 'uv', 'uvx', 'vagrant', 'vcpkg', 'vim-pack', 'vscode', 'vscodium', 'whalebrew', 'xbps', 'yarn', 'yarn-berry', 'yay', 'yazi', 'yum', 'zef', 'zerobrew', 'zeroinstall', 'zim', 'zinit', 'zplug', 'zvm', 'zypper'), strict_selection_match=True, *, signals=<function evaluate_signals>)¶

Selection assertions reading this subcommand’s own signals.

tests.test_cli_sbom.test_default_spdx_json_output_to_console(invoke, subcmd)[source]¶
tests.test_cli_sbom.test_unrecognized_extension_without_format(invoke, subcmd)[source]¶

A target path with no recognized extension fails early.

It fails before any package collection happens, with an actionable message pointing at --format.

tests.test_cli_sbom.test_unrecognized_extension_with_explicit_format(invoke, subcmd, fake_pool, tmp_path)[source]¶

An explicit --format overrides extension auto-detection.

It does so even when the filename carries no recognizable suffix.

The subject is the filename, so the inventory behind it is the fake one: collecting the host’s real packages to prove a suffix was overridden costs the better part of a minute and answers nothing extra.

tests.test_cli_sbom.test_output_to_file(request, invoke, subcmd, fake_pool, export_format, standard_name)[source]¶

Every standard and format pair renders and lands where it was asked to.

Runs on the fake pool: twelve cases serializing one inventory differ in the writer, not in the packages, and re-collecting the host’s real ones per case made this the most expensive block left in the suite (up to eighty-six seconds a case). Selection is asserted by the live-inventory test above, once, as it is shared by every subcommand.

Todo

Drop the Python 3.16 xfail of the CycloneDX XML case once py-serializable releases the fix of madpah/serializable#200. The xfail is strict, so that release turns this case red on Python 3.16.

tests.test_cli_sbom.test_sbom_query_filter(invoke, fake_pool, args, listed)[source]¶
tests.test_cli_sbom.test_minimal_mode_skips_metadata_extractor(invoke)[source]¶

--minimal avoids the rich-metadata code path.

The export then matches the historical bare shape, even for managers that implement an extractor (Homebrew, pip).

tests.test_cli_sbom.test_cli_import_defers_sbom_libraries()[source]¶

Importing the CLI must not pull the SBOM writer libraries.

cli.py imports every subcommand module up front to register it, so a module-level import of the writers in cli_sbom would spend their (heavy) third-party import cost on every mpm invocation, for a dependency only mpm sbom ever uses. The imports therefore live inside the subcommand body, the one place in the codebase deliberately breaking the imports-at-module-level rule, and this test is what keeps them from drifting back up. packageurl-python is checked too: it comes with the [sbom-offline] extra, which the default install does not carry.

Runs in a fresh interpreter: the test session has long since imported everything, so sys.modules here proves nothing.

tests.test_cli_sbom.test_sbom_command_loads_the_deferred_libraries(invoke, fake_pool)[source]¶

The other half of the contract: deferring them must not break the export.

tests.test_cli_sbom.test_overwrite_is_ignored_on_stdout(invoke, fake_pool)[source]¶
tests.test_cli_sbom.test_format_must_match_the_extension(invoke, fake_pool)[source]¶
tests.test_cli_sbom.test_missing_writer_stack_is_a_usage_error(invoke, fake_pool, monkeypatch, standard, flag, support)[source]¶

A standard whose writer library failed to import stops the export.

tests.test_cli_sbom.test_metadata_failure_falls_back_to_minimal_data(invoke, patch_pool_with)[source]¶

A failing metadata extractor costs the enrichment, not the export.

tests.test_cli_sbom.test_network_attaches_the_advisories_of_an_alias(invoke, patch_pool_with, osv)[source]¶

--network queries OSV with the PyPI alias of each package.

It attaches the advisory reported for an alias to the package behind it.

tests.test_cli_sbom.test_network_failure_keeps_the_export(invoke, patch_pool_with, osv)[source]¶

An OSV failure drops the vulnerability data, and the export goes on.

tests.test_cli_sbom.test_network_scan_needs_the_online_extra(invoke, fake_pool, monkeypatch)[source]¶

Without httpx, --network warns and exports the SBOM without the scan.