Skip to content

test(cli): unit-test partition_purls ecosystem filter#70

Closed
Mikola Lysenko (mikolalysenko) wants to merge 2 commits into
tests/cli-contract-foundationfrom
tests/cli-parse-ecosystem
Closed

test(cli): unit-test partition_purls ecosystem filter#70
Mikola Lysenko (mikolalysenko) wants to merge 2 commits into
tests/cli-contract-foundationfrom
tests/cli-parse-ecosystem

Conversation

@mikolalysenko
Copy link
Copy Markdown
Contributor

Summary

Adds nine unit tests for partition_purls in crates/socket-patch-cli/src/ecosystem_dispatch.rs. partition_purls is the helper that splits the user-supplied PURL list by ecosystem and enforces the --ecosystems CSV allow-list flag on apply, rollback, and scan, so it is on the hot path of the CLI contract.

The new #[cfg(test)] mod tests block exercises only partition_purls. The neighboring find_packages_for_purls, find_packages_for_rollback, and crawl_all_ecosystems helpers are intentionally not unit-tested here because they touch the filesystem — they are covered by integration tests elsewhere.

Cases

  • No filter, single npm PURL -> Ecosystem::Npm bucket has the PURL.
  • No filter, mixed npm/pypi/cargo -> three buckets, each with the matching PURL.
  • No filter, empty input -> empty map.
  • No filter, duplicate PURLs -> both copies preserved in the npm bucket (the function does not dedupe).
  • No filter, unrecognized pkg:weirdo/... -> dropped, empty map.
  • Allow-list ["npm"] against npm+pypi -> only the npm bucket.
  • Allow-list ["pypi"] against npm-only -> empty map.
  • Allow-list ["npm","pypi"] -> both buckets.
  • Empty allow-list Some(&[]) -> empty map (matches nothing).

PURL prefixes are verified against Ecosystem::from_purl in crates/socket-patch-core/src/crawlers/types.rs; allow-list values match Ecosystem::cli_name() in the same file.

Test plan

  • cargo build --workspace --all-features clean
  • cargo clippy --workspace --all-features -- -D warnings clean
  • cargo test --workspace --all-features --lib — all 9 new tests pass, existing tests still pass

Stacked on #67 (tests/cli-contract-foundation).

Sets up the foundation for a comprehensive unit-test campaign on the
socket-patch CLI. No behavior change to the binary.

Two changes:

1. Expose the clap parser as a library so integration tests can verify
   the public CLI contract without spawning the binary:
   - new crates/socket-patch-cli/src/lib.rs hosting Cli, Commands,
     looks_like_uuid, and parse_with_uuid_fallback (extracted from main.rs)
   - Cargo.toml gains [lib] entry alongside the existing [[bin]]
   - main.rs is now a thin wrapper that delegates to the lib

2. De-duplicate the manifest-path resolution block that was copy-pasted
   into 5 commands (apply, rollback, list, remove, repair). New helper
   socket_patch_core::manifest::operations::resolve_manifest_path
   handles the absolute-vs-relative join in one place, with 3 unit tests.

3. New CLI_CONTRACT.md next to the crate documenting every subcommand,
   flag, default, alias, JSON shape, and exit code as semver-significant
   surface. Adds a comment block above pub enum Commands pointing to it
   so anyone editing the parser sees the contract reminder.

Verified: cargo build/clippy/test --workspace --all-features all clean
(415 unit tests pass, including 3 new resolve_manifest_path tests).

Foundation for follow-up PRs that add the per-command parser snapshot
tests and helper unit tests.

Assisted-by: Claude Code:claude-opus-4-7
Adds a #[cfg(test)] mod tests block at the bottom of
crates/socket-patch-cli/src/ecosystem_dispatch.rs covering the
partition_purls helper that splits PURLs by ecosystem and applies
the --ecosystems allow-list filter for apply/rollback/scan.

Nine cases:
  - No filter, single npm PURL groups into Ecosystem::Npm.
  - No filter, mixed npm/pypi/cargo PURLs each land in their own bucket.
  - No filter, empty input returns an empty map.
  - No filter, duplicate PURLs are preserved (no dedupe).
  - No filter, unrecognized "pkg:weirdo/..." is dropped.
  - Allow-list ["npm"] excludes pypi PURLs.
  - Allow-list ["pypi"] against an npm-only input returns empty.
  - Allow-list ["npm","pypi"] keeps both buckets.
  - Empty allow-list Some(&[]) matches nothing.

No production code changed; no filesystem-touching helpers exercised.

Verified: cargo build/clippy/test --workspace --all-features all clean
(9 new tests pass).

Assisted-by: Claude Code:claude-opus-4-7
@mikolalysenko
Copy link
Copy Markdown
Contributor Author

Squashed into #68. All tests from this PR are now part of the single combined commit on tests/cli-contract-foundation.

@mikolalysenko Mikola Lysenko (mikolalysenko) deleted the tests/cli-parse-ecosystem branch May 20, 2026 19:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant