Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
251 changes: 47 additions & 204 deletions crates/socket-patch-cli/tests/cli_global_args.rs

Large diffs are not rendered by default.

327 changes: 37 additions & 290 deletions crates/socket-patch-cli/tests/cli_parse_get.rs

Large diffs are not rendered by default.

21 changes: 13 additions & 8 deletions crates/socket-patch-cli/tests/cli_parse_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use clap::Parser;
use socket_patch_cli::commands::list::{run, ListArgs};

#[path = "common/clap_parse.rs"]
mod clap_parse;
#[path = "common/mod.rs"]
mod common;
use common::hermetic::binary_command;
use socket_patch_cli::{Cli, Commands};
use clap_parse::try_parse_keeping_opt_outs as try_parse;
use common::hermetic;
use hermetic::binary_command;
use socket_patch_cli::Commands;
use socket_patch_core::manifest::schema::{
PatchFileInfo, PatchManifest, PatchRecord, VulnerabilityInfo,
};
Expand All @@ -31,7 +34,7 @@ use socket_patch_core::manifest::schema::{
fn parse_list(extra: &[&str]) -> ListArgs {
let mut argv = vec!["socket-patch", "list"];
argv.extend_from_slice(extra);
let cli = Cli::try_parse_from(&argv).expect("parse");
let cli = try_parse(&argv).expect("parse");
match cli.command {
Commands::List(a) => a,
_ => panic!("expected List"),
Expand All @@ -44,10 +47,12 @@ fn parse_list(extra: &[&str]) -> ListArgs {

#[test]
fn defaults_match_contract() {
// The `run()` tests below need the forced opt-outs, so parses here see
// `SOCKET_TELEMETRY_DISABLED=1`.
let args = parse_list(&[]);
assert_eq!(args.common.cwd, PathBuf::from("."));
assert_eq!(args.common.manifest_path, ".socket/manifest.json");
assert!(!args.common.json);
let mut want = clap_parse::global_defaults();
want.no_telemetry = true;
assert_eq!(clap_parse::Globals(args.common), want);
}

#[test]
Expand All @@ -70,7 +75,7 @@ fn json_flag_sets_true() {

#[test]
fn unknown_flag_is_rejected() {
let err = match Cli::try_parse_from(["socket-patch", "list", "--nope"]) {
let err = match try_parse(&["socket-patch", "list", "--nope"]) {
Ok(_) => panic!("unknown flag must fail"),
Err(e) => e,
};
Expand Down
1 change: 1 addition & 0 deletions crates/socket-patch-cli/tests/cli_parse_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod hermetic;
/// the real CLI. Routing through the wrapper means each name/alias/error-kind
/// assertion below also exercises the code path users actually hit.
fn parse(argv: &[&str]) -> Result<Cli, clap::Error> {
hermetic::scrub_process_socket_env();
parse_argv_with_shortcuts(argv.iter().map(|s| s.to_string()).collect())
}

Expand Down
36 changes: 19 additions & 17 deletions crates/socket-patch-cli/tests/cli_parse_remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,36 @@
//! These tests deliberately avoid spawning the binary so they run in the
//! default `cargo test` set (no `--ignored` required) and stay fast.

use clap::Parser;
use socket_patch_cli::commands::remove::{run, RemoveArgs};
use socket_patch_cli::{Cli, Commands};
use socket_patch_cli::Commands;
use std::path::PathBuf;

#[path = "common/clap_parse.rs"]
mod clap_parse;
#[path = "common/hermetic.rs"]
mod hermetic;

use clap_parse::try_parse_keeping_opt_outs as try_parse;
use clap_parse::{global_defaults, Globals};

fn parse_remove(extra: &[&str]) -> RemoveArgs {
let mut argv = vec!["socket-patch", "remove"];
argv.extend_from_slice(extra);
let cli = Cli::try_parse_from(&argv).expect("parse");
let cli = try_parse(&argv).expect("parse");
match cli.command {
Commands::Remove(a) => a,
_ => panic!("expected Remove"),
}
}

/// The `run()` tests below need the forced opt-outs, so parses here see
/// `SOCKET_TELEMETRY_DISABLED=1`.
fn opt_out_defaults() -> Globals {
let mut want = global_defaults();
want.no_telemetry = true;
want
}

// ---------------------------------------------------------------------------
// Defaults
// ---------------------------------------------------------------------------
Expand All @@ -33,13 +45,8 @@ fn parse_remove(extra: &[&str]) -> RemoveArgs {
fn defaults_with_purl_positional() {
let args = parse_remove(&["pkg:npm/foo@1"]);
assert_eq!(args.identifier, "pkg:npm/foo@1");
assert_eq!(args.common.cwd, PathBuf::from("."));
assert_eq!(args.common.manifest_path, ".socket/manifest.json");
assert!(!args.skip_rollback);
assert!(!args.common.yes);
assert!(!args.common.global);
assert_eq!(args.common.global_prefix, None);
assert!(!args.common.json);
assert_eq!(Globals(args.common), opt_out_defaults());
}

#[test]
Expand All @@ -49,13 +56,8 @@ fn positional_uuid_stored_in_identifier() {
// Everything else still at default — `remove` does not auto-detect the
// identifier shape at parse time; the runtime branch on `pkg:` happens
// inside `run()`.
assert_eq!(args.common.cwd, PathBuf::from("."));
assert_eq!(args.common.manifest_path, ".socket/manifest.json");
assert!(!args.skip_rollback);
assert!(!args.common.yes);
assert!(!args.common.global);
assert_eq!(args.common.global_prefix, None);
assert!(!args.common.json);
assert_eq!(Globals(args.common), opt_out_defaults());
}

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -153,7 +155,7 @@ fn all_flags_combined() {

#[test]
fn missing_required_positional_is_error() {
let result = Cli::try_parse_from(["socket-patch", "remove"]);
let result = try_parse(&["socket-patch", "remove"]);
let err = match result {
Ok(_) => panic!("remove without identifier must fail"),
Err(e) => e,
Expand All @@ -163,7 +165,7 @@ fn missing_required_positional_is_error() {

#[test]
fn unknown_flag_is_error() {
let result = Cli::try_parse_from([
let result = try_parse(&[
"socket-patch",
"remove",
"pkg:npm/foo@1",
Expand Down
Loading
Loading