Skip to content

Commit aa558c5

Browse files
Use tests/common's binary() and git_sha256 in CLI tests (#824) (#1124)
* Start refactor for #824 Assisted-by: Claude Code:claude-opus-5-5 * Use common's binary() and git_sha256 in tests 72 CLI test files kept private copies of binary() and git_sha256 that tests/common already provides (five shapes, all returning the same path and the same Git-blob SHA-256). They now import common's, and the rollback and e2e_vex_lockfile binaries declare common. No test behavior changes; files that open PRs change keep their copies for a later slice (#824). Assisted-by: Claude Code:claude-opus-5-5 * Guard against new private test helper copies A ratchet in the cli test binary fails when a test file outside tests/common defines its own binary() or git_sha256, unless it is on the pending list (files open PRs change, plus the vlt_* shared modules). Stale entries don't fail, so migrating one never turns another PR red. Assisted-by: Claude Code:claude-opus-5-5 * Label the setup-php pin with its real tag Ports #1118's one-line fix: zizmor's ref-version-mismatch audit fails every new PR head on main's `# v2` label for this pin. No-op once #1118 lands. Assisted-by: Claude Code:claude-opus-5-5 * Keep a spawn spelling out of the helper guard The detector test's near-miss sample spelled a bare binary spawn, which spawn_env_hygiene's raw-spawn scan reads as a real one and fails test-release and coverage. Use a plain binary() call instead. Assisted-by: Claude Code:claude-opus-5-5 --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent d410ee7 commit aa558c5

76 files changed

Lines changed: 450 additions & 583 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎crates/socket-patch-cli/tests/apply/apply_invariants.rs‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@
1515
//! crawler won't match, which trips the "no packages found / offline"
1616
//! branches and exercises the invariants without needing a real fixture.
1717
18+
use crate::common::{binary, git_sha256};
19+
1820
use std::path::{Path, PathBuf};
1921
use std::process::Command;
2022

2123
use sha2::{Digest, Sha256};
2224

23-
fn binary() -> PathBuf {
24-
env!("CARGO_BIN_EXE_socket-patch").into()
25-
}
26-
2725
/// Minimal manifest with one synthetic patch entry. The PURL points at a
2826
/// package that won't be found on disk; the `afterHash` blob is missing
2927
/// from `.socket/blobs/`. This forces every branch we want to test —
@@ -151,17 +149,6 @@ const SCOPED_NPM_PURL: &str = "pkg:npm/scopedpkg@1.0.0";
151149
const SCOPED_ORIGINAL: &[u8] = b"module.exports = function vulnerable() { return 'pwn'; };\n";
152150
const SCOPED_PATCHED: &[u8] = b"module.exports = function safe() { return 'ok'; };\n";
153151

154-
/// Git SHA-256: `SHA256("blob <len>\0" ++ content)`. Computed
155-
/// independently here so the manifest hashes are NOT derived from the
156-
/// code under test (no circular oracle).
157-
fn git_sha256(content: &[u8]) -> String {
158-
let header = format!("blob {}\0", content.len());
159-
let mut hasher = Sha256::new();
160-
hasher.update(header.as_bytes());
161-
hasher.update(content);
162-
hex::encode(hasher.finalize())
163-
}
164-
165152
/// Lay down a project with TWO manifest patches:
166153
/// - an npm patch that is fully applicable offline (package installed,
167154
/// patched blob present in `.socket/blobs/`), and

‎crates/socket-patch-cli/tests/apply/cli_gem_variant_mismatch_policy.rs‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
//! modified"). When NO variant matches, the base still fails with
2727
//! "no matching variant found" and the file stays untouched.
2828
29+
use crate::common::{binary, git_sha256};
30+
2931
use std::path::{Path, PathBuf};
3032
use std::process::Command;
3133

32-
use sha2::{Digest, Sha256};
33-
3434
const SINGLETON_PURL: &str = "pkg:gem/rack@3.1.0";
3535
const UUID_SINGLETON: &str = "31313131-3131-4131-8131-313131313131";
3636

@@ -48,19 +48,6 @@ const LINUX_MARKER: &[u8] = b"\n# SOCKET-LINUX-PATCH\n";
4848
const DARWIN_BEFORE: &[u8] = b"# nokogiri.rb from the arm64-darwin gem\n";
4949
const DARWIN_MARKER: &[u8] = b"\n# DARWIN-MARKER\n";
5050

51-
fn binary() -> PathBuf {
52-
env!("CARGO_BIN_EXE_socket-patch").into()
53-
}
54-
55-
/// Git-SHA256: SHA256("blob <len>\0" ++ content).
56-
fn git_sha256(content: &[u8]) -> String {
57-
let header = format!("blob {}\0", content.len());
58-
let mut hasher = Sha256::new();
59-
hasher.update(header.as_bytes());
60-
hasher.update(content);
61-
hex::encode(hasher.finalize())
62-
}
63-
6451
fn with_marker(base: &[u8], marker: &[u8]) -> Vec<u8> {
6552
let mut v = base.to_vec();
6653
v.extend_from_slice(marker);

‎crates/socket-patch-cli/tests/apply/in_process_gem_config_warning.rs‎

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,14 @@
1010
//! * non-JSON runs print ONE stderr warning, gated on `!--silent`
1111
//! (`--silent` = errors only — a bare crawler eprintln violated that).
1212
13-
use std::path::{Path, PathBuf};
14-
use std::process::Command;
13+
use crate::common::{binary, git_sha256};
1514

16-
use sha2::{Digest, Sha256};
15+
use std::path::Path;
16+
use std::process::Command;
1717

1818
const PURL: &str = "pkg:gem/rack@3.1.0";
1919
const CODE: &str = "gem_bundle_config_path_ignored";
2020

21-
fn binary() -> PathBuf {
22-
env!("CARGO_BIN_EXE_socket-patch").into()
23-
}
24-
25-
fn git_sha256(content: &[u8]) -> String {
26-
let header = format!("blob {}\0", content.len());
27-
let mut hasher = Sha256::new();
28-
hasher.update(header.as_bytes());
29-
hasher.update(content);
30-
hex::encode(hasher.finalize())
31-
}
32-
3321
/// Project fixture: Gemfile + `.bundle/config` pointing `BUNDLE_PATH` at
3422
/// an ABSOLUTE directory outside the project (holding a real store, so
3523
/// the only reason it goes undiscovered is the containment skip), plus —

‎crates/socket-patch-cli/tests/apply/in_process_gem_fallback_home.rs‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,18 @@
2020
2121
#![cfg(unix)]
2222

23+
use crate::common::{binary, git_sha256};
24+
2325
use std::path::{Path, PathBuf};
2426
use std::process::Command;
2527

26-
use sha2::{Digest, Sha256};
27-
2828
const BASE_PURL: &str = "pkg:gem/rack@3.1.0";
2929
const QUALIFIED_PURL: &str = "pkg:gem/rack@3.1.0?platform=ruby";
3030
const SKIP_CODE: &str = "gem_fallback_home_skipped";
3131

3232
const ORIGINAL: &[u8] = b"module Rack\n VERSION = 'VULNERABLE'\nend\n";
3333
const MARKER: &[u8] = b"# SOCKET-PATCHED-FALLBACK\n";
3434

35-
fn binary() -> PathBuf {
36-
env!("CARGO_BIN_EXE_socket-patch").into()
37-
}
38-
39-
fn git_sha256(content: &[u8]) -> String {
40-
let header = format!("blob {}\0", content.len());
41-
let mut hasher = Sha256::new();
42-
hasher.update(header.as_bytes());
43-
hasher.update(content);
44-
hex::encode(hasher.finalize())
45-
}
46-
4735
fn patched_bytes() -> Vec<u8> {
4836
let mut v = ORIGINAL.to_vec();
4937
v.extend_from_slice(MARKER);

‎crates/socket-patch-cli/tests/apply/in_process_gem_multicopy.rs‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,13 @@
1717
//! physical copy is patched (and later restored) AND that the JSON summary
1818
//! counts every copy.
1919
20+
use crate::common::{binary, git_sha256};
21+
2022
use std::path::{Path, PathBuf};
2123
use std::process::Command;
2224

23-
use sha2::{Digest, Sha256};
24-
2525
const PURL: &str = "pkg:gem/rack@3.1.0";
2626

27-
fn binary() -> PathBuf {
28-
env!("CARGO_BIN_EXE_socket-patch").into()
29-
}
30-
31-
/// Git-SHA256: SHA256("blob <len>\0" ++ content).
32-
fn git_sha256(content: &[u8]) -> String {
33-
let header = format!("blob {}\0", content.len());
34-
let mut hasher = Sha256::new();
35-
hasher.update(header.as_bytes());
36-
hasher.update(content);
37-
hex::encode(hasher.finalize())
38-
}
39-
4027
/// Write a gem copy at `gem_dir` with `lib/rack.rb` holding `bytes`,
4128
/// returning the file path.
4229
fn write_copy(gem_dir: &Path, bytes: &[u8]) -> PathBuf {

‎crates/socket-patch-cli/tests/apply/in_process_variant_apply_failure.rs‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,15 @@
1919
//! Requires: `python3` with `venv` and `pip` on PATH. Skipped (visibly)
2020
//! when python3 is missing — same contract as `in_process_pypi_apply`.
2121
22+
use crate::common::{binary, git_sha256};
23+
2224
use std::path::{Path, PathBuf};
2325
use std::process::Command;
2426

25-
use sha2::{Digest, Sha256};
26-
2727
const PYPI_PACKAGE: &str = "six";
2828
const PYPI_VERSION: &str = "1.16.0";
2929
const UUID: &str = "12121212-1212-4121-8121-121212121212";
3030

31-
fn binary() -> PathBuf {
32-
env!("CARGO_BIN_EXE_socket-patch").into()
33-
}
34-
3531
/// Spawn the CLI with the ambient environment scrubbed, so the flags each
3632
/// test passes are the only thing deciding behaviour.
3733
///
@@ -72,14 +68,6 @@ fn run_apply_scrubbed(args: &[&str]) -> std::process::Output {
7268
cmd.output().expect("run socket-patch apply")
7369
}
7470

75-
fn git_sha256(content: &[u8]) -> String {
76-
let header = format!("blob {}\0", content.len());
77-
let mut hasher = Sha256::new();
78-
hasher.update(header.as_bytes());
79-
hasher.update(content);
80-
hex::encode(hasher.finalize())
81-
}
82-
8371
fn find_python() -> Option<&'static str> {
8472
for cmd in ["python3", "python", "py"] {
8573
let ok = Command::new(cmd)

‎crates/socket-patch-cli/tests/cli/cli_dry_run_paths_e2e.rs‎

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33
//! asserts the JSON envelope's `dryRun: true` field — covering the
44
//! dry-run flag-propagation branches each command's `run` has.
55
6-
use std::path::{Path, PathBuf};
6+
use crate::common::{binary, git_sha256};
77

8-
use sha2::{Digest, Sha256};
9-
10-
fn binary() -> PathBuf {
11-
env!("CARGO_BIN_EXE_socket-patch").into()
12-
}
8+
use std::path::Path;
139

1410
fn make_socket_with_empty_manifest(root: &std::path::Path) {
1511
let socket = root.join(".socket");
@@ -18,17 +14,6 @@ fn make_socket_with_empty_manifest(root: &std::path::Path) {
1814
std::fs::create_dir_all(socket.join("blobs")).unwrap();
1915
}
2016

21-
/// Git SHA-256: `SHA256("blob <len>\0" ++ content)`. Computed
22-
/// independently here so the manifest hashes are NOT derived from the
23-
/// code under test (no circular oracle).
24-
fn git_sha256(content: &[u8]) -> String {
25-
let header = format!("blob {}\0", content.len());
26-
let mut hasher = Sha256::new();
27-
hasher.update(header.as_bytes());
28-
hasher.update(content);
29-
hex::encode(hasher.finalize())
30-
}
31-
3217
const DRYRUN_PURL: &str = "pkg:npm/dryrunpkg@1.0.0";
3318
const DRYRUN_ORIGINAL: &[u8] = b"module.exports = function vulnerable() { return 'pwn'; };\n";
3419
const DRYRUN_PATCHED: &[u8] = b"module.exports = function safe() { return 'ok'; };\n";

‎crates/socket-patch-cli/tests/cli/covgap_output.rs‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
1515
#![cfg(unix)]
1616

17-
use std::path::{Path, PathBuf};
17+
use crate::common::binary;
18+
19+
use std::path::Path;
1820
use std::time::Duration;
1921

2022
use portable_pty::{native_pty_system, CommandBuilder, PtySize};
@@ -30,10 +32,6 @@ const ORG_SLUG: &str = "test-org";
3032
const UUID_A: &str = "11111111-1111-4111-8111-111111111111";
3133
const UUID_B: &str = "22222222-2222-4222-8222-222222222222";
3234

33-
fn binary() -> PathBuf {
34-
env!("CARGO_BIN_EXE_socket-patch").into()
35-
}
36-
3735
/// Spawn the socket-patch binary inside a PTY, send `input`, and collect
3836
/// all output until the child exits. Returns `(exit_code, output)`.
3937
///

‎crates/socket-patch-cli/tests/cli/interactive_prompts_e2e.rs‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77
88
#![cfg(unix)]
99

10-
use std::path::{Path, PathBuf};
10+
use crate::common::binary;
11+
12+
use std::path::Path;
1113
use std::time::Duration;
1214

1315
use portable_pty::{native_pty_system, CommandBuilder, PtySize};
1416

15-
fn binary() -> PathBuf {
16-
env!("CARGO_BIN_EXE_socket-patch").into()
17-
}
18-
1917
/// Spawn the socket-patch binary inside a PTY, send `input`, and
2018
/// collect all output until the child exits. Returns `(exit_code,
2119
/// output)`. The timeout is enforced via a watchdog thread that

‎crates/socket-patch-cli/tests/cli/main.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ mod covgap_commands_list;
1818
mod covgap_output;
1919
mod interactive_prompts_e2e;
2020
mod output_modes_e2e;
21+
mod shared_helper_copies;
2122
mod telemetry_e2e;

0 commit comments

Comments
 (0)