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
httpxreads, 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_vulnerabilitiesuses that a CycloneDX vulnerability entry needs.
- class tests.test_cli_sbom.AliasedFakeManager[source]¶
Bases:
FakeManagerA 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_errorslist.- 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 sbomin--bundledmode to populate licenses, checksums, download URLs, supplier/originator, and the declared dependency graph. The base implementation yieldsmeta_package_manager.package.EMPTY_METADATAfor each package and stays compatible with managers that do not (yet) expose richer metadata: their SBOM entries stay at the minimalPackagelevel, matching the historical and--minimalmodes.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-infodirectories, Homebrew’s per-formulasbom.spdx.json, dpkg’s.md5sums).
The yielded pairs do not need to preserve the input order; the SBOM renderer matches by
Packageidentity. Implementations are expected to swallow per-package extraction errors and yieldmeta_package_manager.package.EMPTY_METADATAfor 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
--bundledflag 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-networkflag layered under--bundledto 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
pythonorpython3.
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
Nonewhen it has none.The manual, the documentation site, or the man page of the CLI
mpmdrives: whichever page a user reads to learn the commands this manager wraps. Listed betweenhomepage_urlandrepository_urlin 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_urlalready 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_urlstays 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
pacmaneach 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_urlnames on its own, and when it is the project’s manual, whichdocumentation_urlnames. 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
Nonewhen it has no public one.Linked from the manager’s documentation page. Also what the weekly metrics sample reads:
docs/docs_update.pywrites it into the[tool.repomatic.metrics] subjectsofpyproject.tomlwhen 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 extendingpacmaneach 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
Nonewhen 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:lazyis a Neovim plugin manager, and the Neovim article is not about it.Never inherited: the metaclass resets it to
Noneon any class not declaring its own. A subclass often wraps a distinct project, like the six AUR helpers extendingpacman, and would otherwise present pacman’s article as its own. Documentation-only, likehomepage_url.
- class tests.test_cli_sbom.UnreadableMetadataFakeManager[source]¶
Bases:
FakeManagerA fake whose metadata extractor fails.
Initialize
cli_errorslist.- 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 sbomin--bundledmode to populate licenses, checksums, download URLs, supplier/originator, and the declared dependency graph. The base implementation yieldsmeta_package_manager.package.EMPTY_METADATAfor each package and stays compatible with managers that do not (yet) expose richer metadata: their SBOM entries stay at the minimalPackagelevel, matching the historical and--minimalmodes.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-infodirectories, Homebrew’s per-formulasbom.spdx.json, dpkg’s.md5sums).
The yielded pairs do not need to preserve the input order; the SBOM renderer matches by
Packageidentity. Implementations are expected to swallow per-package extraction errors and yieldmeta_package_manager.package.EMPTY_METADATAfor 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
--bundledflag 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-networkflag layered under--bundledto 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
pythonorpython3.
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
Nonewhen it has none.The manual, the documentation site, or the man page of the CLI
mpmdrives: whichever page a user reads to learn the commands this manager wraps. Listed betweenhomepage_urlandrepository_urlin 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_urlalready 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_urlstays 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
pacmaneach 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_urlnames on its own, and when it is the project’s manual, whichdocumentation_urlnames. 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
Nonewhen it has no public one.Linked from the manager’s documentation page. Also what the weekly metrics sample reads:
docs/docs_update.pywrites it into the[tool.repomatic.metrics] subjectsofpyproject.tomlwhen 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 extendingpacmaneach 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
Nonewhen 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:lazyis a Neovim plugin manager, and the Neovim article is not about it.Never inherited: the metaclass resets it to
Noneon any class not declaring its own. A subclass often wraps a distinct project, like the six AUR helpers extendingpacman, and would otherwise present pacman’s article as its own. Documentation-only, likehomepage_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.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_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
--formatoverrides 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.16xfailof the CycloneDX XML case once py-serializable releases the fix of madpah/serializable#200. Thexfailis strict, so that release turns this case red on Python3.16.
- tests.test_cli_sbom.test_minimal_mode_skips_metadata_extractor(invoke)[source]¶
--minimalavoids 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.pyimports every subcommand module up front to register it, so a module-level import of the writers incli_sbomwould spend their (heavy) third-party import cost on everympminvocation, for a dependency onlympm sbomever 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.moduleshere 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_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]¶
--networkqueries OSV with the PyPI alias of each package.It attaches the advisory reported for an alias to the package behind it.