Commit b96a13f
authored
test(cli): foundation for CLI-contract unit-test campaign (#68)
* test(cli): comprehensive unit-test campaign for the CLI contract
Add unit-test coverage for every subcommand, flag, default, alias, and
helper in the socket-patch CLI, plus a new CLI_CONTRACT.md that documents
the surface as semver-significant.
Before this change the CLI crate had zero unit tests under src/ —
only network-dependent tests/e2e_*.rs suites gated on --ignored. A flag
rename, a default change, or a JSON key drift could land green and break
every shipped wrapper (npm @socketsecurity/socket-patch, pypi
socket-patch, cargo, prebuilt binaries).
## What's covered
Library surface (new src/lib.rs):
- Cli, Commands, looks_like_uuid, parse_with_uuid_fallback extracted
from main.rs so integration tests can verify the parser without
spawning the binary.
- main.rs becomes a thin wrapper that delegates to the lib.
- Cargo.toml gains [lib] alongside [[bin]].
Core helper extraction:
- socket_patch_core::manifest::operations::resolve_manifest_path
replaces the 5-line absolute-vs-relative join block previously
copy-pasted into apply/rollback/list/remove/repair (5 callers).
CLI_CONTRACT.md (new):
- Documents every subcommand, flag, default value, visible alias
(download, gc), hidden alias (--no-apply), JSON output shape, and
exit code as semver-significant.
- Pins the divergent defaults: --download-mode=diff for apply/get/scan
and --download-mode=file for repair, --batch-size=100 for scan.
- Spells out the bump policy and how to invoke scripts/version-sync.sh.
Helper unit tests (#[cfg(test)] mod tests in each file):
- src/lib.rs — looks_like_uuid (valid/invalid shapes, case-insensitive),
parse_with_uuid_fallback (success, fallback, fallback-fails preserves
original error, no double-rewrite).
- src/output.rs — format_severity, color, confirm (skip_prompt and
is_json short-circuits; interactive path intentionally not tested).
- src/ecosystem_dispatch.rs — partition_purls (filter, dedup, unknown
ecosystem dropped).
- commands/apply.rs — verify_status_str (all 4 VerifyStatus variants),
result_to_json (top-level + filesVerified key sets).
- commands/rollback.rs — find_patches_to_rollback (None=all, PURL match,
UUID match, no-match=empty).
- commands/get.rs — detect_identifier_type (UUID/CVE/GHSA/PURL/None,
case-insensitive for CVE+GHSA), select_patches (paid auto, free
single auto, free multi+is_json -> Err(1)).
Clap parser snapshot tests (new tests/cli_parse_*.rs):
- One file per command: apply, get, list, main, remove, repair,
rollback, scan, setup.
- Every flag (long + short) has at least one parser assertion.
- Every #[arg(default_value)] is asserted on the no-flag parse.
- The download visible alias on get is exercised.
- The gc visible alias on repair is exercised.
- The hidden --no-apply alias on get --save-only is exercised.
- --ecosystems CSV splitting is verified on apply/rollback/scan.
- The bare-UUID rewrite is exercised end-to-end via Cli::try_parse_from.
- Failure paths assert clap::error::ErrorKind variants.
Async run() integration tests:
- tests/cli_parse_list.rs covers missing manifest -> 1, empty -> 0,
populated -> 0, absolute-path override, and a subprocess JSON-shape
assertion against the compiled binary.
- tests/cli_parse_remove.rs covers missing-manifest -> 1.
- tests/cli_parse_setup.rs covers no-package-json -> 0 with the
JSON status:"no_files" shape pinned via subprocess.
## Verification
cargo build --workspace --all-features
cargo clippy --workspace --all-features -- -D warnings
cargo test --workspace --all-features
All clean. 79 new lib tests + 156 new integration tests added on top of
the existing 415 unit tests; cumulative 650 tests pass.
## Why squashed
Originally landed as a foundation PR (#68) plus 10 sibling test PRs
(#69-#78), one per command/file, dispatched in parallel. Squashing the
sibling PRs back into #68 so the contract + tests land as one
self-contained unit — reviewers see the full picture, and a future
revert touches one commit instead of eleven.
Assisted-by: Claude Code:claude-opus-4-7
* ci: tighten CI matrix — add macOS, release-mode tests, explicit build step
The CI workflow's unit-test job ran on ubuntu-latest + windows-latest;
extend the matrix with macos-latest so the new CLI parser tests are
exercised on every platform the binary ships for. socket-patch ships
prebuilt binaries for x86_64-apple-darwin and aarch64-apple-darwin (see
release.yml), so silent macOS-specific regressions in path handling,
TTY detection, or terminal escapes are real risks today.
Three changes:
- Add `macos-latest` to the test matrix.
- Add `fail-fast: false` so a failure on one OS doesn't mask failures
on the others.
- Add an explicit `cargo build --workspace --all-features` step
before `cargo test`. `cargo test` already builds, but a dedicated
build step gives a cleaner red signal when a build-only failure
happens (e.g. a feature-gated compile error) without the noise of
test-discovery output.
- New `test-release` job: `cargo test --workspace --all-features
--release` on ubuntu-latest. Catches optimization-level regressions
that debug mode hides (e.g. release-mode-only inlining changes that
affect assertion behavior). One OS keeps total CI time reasonable
while still locking in release-mode correctness.
Assisted-by: Claude Code:claude-opus-4-7
* fix(patch): reject POSIX-style absolute paths in archive entries on Windows
`read_archive_to_map` rejects entries whose path is absolute or contains
a `..` component, but the check used `Path::is_absolute()` alone. On
Windows that function requires a drive letter or UNC prefix, so a tar
entry like `/etc/passwd` is NOT considered absolute and would slip
through the guard — when later joined to the target directory, Windows
would treat it as relative to the current drive's root.
Add an explicit check for a leading `/` or `\` byte alongside
`Path::is_absolute()` so the guard rejects POSIX-style absolute paths
on every platform. The new test_read_archive_rejects_backslash_absolute_paths
case locks the symmetric backslash form in.
This was uncovered when the CI matrix was extended to actually run on
Windows. The existing test_read_archive_rejects_absolute_paths failed on
windows-latest because it constructed the archive with a POSIX-style
path that the platform-specific `is_absolute()` did not catch.
Assisted-by: Claude Code:claude-opus-4-71 parent 6288a37 commit b96a13f
24 files changed
Lines changed: 3150 additions & 108 deletions
File tree
- .github/workflows
- crates
- socket-patch-cli
- src
- commands
- tests
- socket-patch-core/src
- manifest
- patch
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| 41 | + | |
41 | 42 | | |
42 | | - | |
| 43 | + | |
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
| |||
62 | 63 | | |
63 | 64 | | |
64 | 65 | | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
65 | 69 | | |
66 | 70 | | |
67 | 71 | | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
68 | 98 | | |
69 | 99 | | |
70 | 100 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
10 | 14 | | |
11 | 15 | | |
12 | 16 | | |
| |||
0 commit comments