55//! purl, a file). The human output of the same paths is pinned in
66//! `scan_render.rs`.
77//!
8- //! 1. `resolve_mode_flags`' cross-mode conflicts and the early errors;
8+ //! 1. the early errors (`--offline`, usage errors) ;
99//! 2. hosted `--prune` and the paid-tier counters;
1010//! 3. detail-fetch failures (total and partial) and empty detail answers;
1111//! 4. vendor-owned purls, report-only runs and the `--sync` GC;
@@ -25,19 +25,6 @@ use crate::common::envelope::{
2525use crate :: scan_fixture:: * ;
2626use crate :: { vlt_hosted_common as hosted, vlt_vendored} ;
2727
28- /// The first `warnings[]` entry with `code`; panics with the envelope when
29- /// there is none.
30- fn warning < ' a > ( v : & ' a serde_json:: Value , code : & str ) -> & ' a serde_json:: Value {
31- v[ "warnings" ]
32- . as_array ( )
33- . and_then ( |ws| ws. iter ( ) . find ( |w| w[ "code" ] == code) )
34- . unwrap_or_else ( || panic ! ( "warnings[] must carry {code}: {v:#}" ) )
35- }
36-
37- fn warning_detail < ' a > ( v : & ' a serde_json:: Value , code : & str ) -> & ' a str {
38- warning ( v, code) [ "detail" ] . as_str ( ) . unwrap_or_default ( )
39- }
40-
4128/// `scan --json` against the mock API; returns the exit code and the parsed
4229/// envelope (after checking the shared envelope invariants).
4330fn run_json ( cwd : & Path , api_url : & str , extra : & [ & str ] ) -> ( i32 , serde_json:: Value ) {
@@ -53,67 +40,6 @@ fn run_json(cwd: &Path, api_url: &str, extra: &[&str]) -> (i32, serde_json::Valu
5340 ( code, v)
5441}
5542
56- // ---------------------------------------------------------------------------
57- // resolve_mode_flags: the `--sync` cross-mode conflict
58- // ---------------------------------------------------------------------------
59- // `--sync` means `--mode agent --prune`, so it contradicts any other
60- // `--mode`. Clap parses each combination fine (no value-dependent conflict
61- // is expressible); the fold is what rejects them, naming both flags.
62-
63- mod mode_fold {
64- use clap:: Parser ;
65- use socket_patch_cli:: commands:: scan:: { resolve_mode_flags, ScanArgs } ;
66- use socket_patch_cli:: { Cli , Commands } ;
67-
68- /// Every env var that could reroute the parse or the fold (superset:
69- /// the shared scrub lists from `args.rs`).
70- fn with_clean_env < T > ( f : impl FnOnce ( ) -> T ) -> T {
71- let vars: Vec < & str > = socket_patch_cli:: args:: GLOBAL_ARG_ENV_VARS
72- . iter ( )
73- . chain ( socket_patch_cli:: args:: LOCAL_ARG_ENV_VARS . iter ( ) )
74- . copied ( )
75- . collect ( ) ;
76- let saved: Vec < ( & str , Option < String > ) > =
77- vars. iter ( ) . map ( |k| ( * k, std:: env:: var ( k) . ok ( ) ) ) . collect ( ) ;
78- for k in & vars {
79- std:: env:: remove_var ( k) ;
80- }
81- let result = f ( ) ;
82- for ( k, orig) in saved {
83- match orig {
84- Some ( v) => std:: env:: set_var ( k, v) ,
85- None => std:: env:: remove_var ( k) ,
86- }
87- }
88- result
89- }
90-
91- fn fold_err ( extra : & [ & str ] ) -> String {
92- let mut argv = vec ! [ "socket-patch" , "scan" ] ;
93- argv. extend_from_slice ( extra) ;
94- let cli = with_clean_env ( || Cli :: try_parse_from ( & argv) ) . expect ( "parse" ) ;
95- let mut args: ScanArgs = match cli. command {
96- Commands :: Scan ( a) => a,
97- _ => panic ! ( "expected Scan" ) ,
98- } ;
99- resolve_mode_flags ( & mut args) . expect_err ( "cross-mode contradiction must error" )
100- }
101-
102- /// Each `--mode` against `--sync` errors, naming the mode through
103- /// `cli_name()` and the conflicting boolean.
104- #[ test]
105- #[ serial_test:: serial]
106- fn mode_with_sync_boolean_errors_naming_both_flags ( ) {
107- for mode in [ "hosted" , "vendored" ] {
108- let err = fold_err ( & [ "--mode" , mode, "--sync" ] ) ;
109- assert ! (
110- err. contains( & format!( "--mode {mode}" ) ) && err. contains( "--sync" ) ,
111- "the error must name --mode {mode} and --sync: {err}"
112- ) ;
113- }
114- }
115- }
116-
11743/// `--offline` and usage errors print the full envelope too.
11844#[ test]
11945fn scan_json_early_errors_print_full_envelopes ( ) {
@@ -145,6 +71,7 @@ fn scan_hosted_global_json_is_global_scope_unsupported() {
14571 let ( code, stdout, _) = run_scan ( tmp. path ( ) , & [ "--mode" , "hosted" , "--global" , "--json" ] ) ;
14672 assert_eq ! ( code, 2 , "{stdout}" ) ;
14773 let v = parse_json_envelope ( & stdout) ;
74+ assert_envelope_invariants ( & v, "scan" ) ;
14875 assert_eq ! ( v[ "status" ] , "error" , "{v}" ) ;
14976 assert_eq ! (
15077 envelope_error_code( & v) ,
@@ -179,7 +106,7 @@ async fn scan_hosted_prune_zero_package_json_carries_the_warning() {
179106 assert_eq ! ( v[ "redirect" ] [ "mode" ] , "hosted" ) ;
180107 assert_eq ! ( v[ "summary" ] [ "applied" ] , 0 , "nothing pinned: {v}" ) ;
181108 assert ! (
182- warning_detail( & v, "redirect_prune_ignored" ) . contains( "--prune" ) ,
109+ hosted :: warning_detail( & v, "redirect_prune_ignored" ) . contains( "--prune" ) ,
183110 "the warning must name the ignored flag: {v:#}"
184111 ) ;
185112 assert ! (
@@ -372,7 +299,7 @@ async fn scan_partial_failure_with_empty_results_exits_alike() {
372299 let tmp = project ( ) ;
373300 let ( json, v) = run_json ( tmp. path ( ) , & mock. uri ( ) , & extra) ;
374301 assert ! (
375- warning_detail( & v, "patch_details_failed" ) . contains( bad) ,
302+ hosted :: warning_detail( & v, "patch_details_failed" ) . contains( bad) ,
376303 "{v:#}"
377304 ) ;
378305 assert_eq ! ( human, json, "human and --json exit alike: {stderr}\n {v:#}" ) ;
@@ -382,8 +309,8 @@ async fn scan_partial_failure_with_empty_results_exits_alike() {
382309// Vendor-owned purls, report-only runs and the GC
383310// ---------------------------------------------------------------------------
384311
385- /// A purl the vendored ledger owns is skipped by an agent-mode scan: no
386- /// download , no manifest, and no apply event for it .
312+ /// A purl the vendored ledger owns is skipped by an agent-mode scan: one
313+ /// `skipped` event coded `vendored` , no download and no manifest .
387314#[ tokio:: test]
388315async fn scan_agent_json_skips_vendored_purls_without_downloading ( ) {
389316 let mock = MockServer :: start ( ) . await ;
@@ -394,11 +321,14 @@ async fn scan_agent_json_skips_vendored_purls_without_downloading() {
394321
395322 let ( code, v) = run_json ( tmp. path ( ) , & mock. uri ( ) , & [ "--mode" , "agent" , "--yes" ] ) ;
396323 assert_eq ! ( code, 0 , "{v:#}" ) ;
397- assert ! (
398- !event_triples( & v)
399- . iter( )
400- . any( |( purl, action, _) | purl == MINIMIST && action != "skipped" ) ,
401- "a vendor-owned purl is only ever skipped: {v:#}"
324+ assert_eq ! (
325+ event_triples( & v)
326+ . into_iter( )
327+ . filter( |( purl, _, _) | purl == MINIMIST )
328+ . map( |( _, action, code) | ( action, code) )
329+ . collect:: <Vec <_>>( ) ,
330+ [ ( "skipped" . to_string( ) , "vendored" . to_string( ) ) ] ,
331+ "a vendor-owned purl is only skipped, with the `vendored` code: {v:#}"
402332 ) ;
403333 assert_eq ! ( v[ "summary" ] [ "applied" ] , 0 , "{v:#}" ) ;
404334 assert_eq ! ( view_gets( & recorded( & mock) . await ) , 0 , "{v:#}" ) ;
@@ -646,7 +576,7 @@ async fn scan_agent_json_mismatch_overwrite_reaches_the_apply_block() {
646576 let ( code, v) = run_json ( tmp. path ( ) , & mock. uri ( ) , & [ "--mode" , "agent" ] ) ;
647577 assert_eq ! ( code, 0 , "{v:#}" ) ;
648578 assert_eq ! ( v[ "summary" ] [ "applied" ] , 1 , "{v}" ) ;
649- let detail = warning_detail ( & v, "content_mismatch_overwritten" ) ;
579+ let detail = hosted :: warning_detail ( & v, "content_mismatch_overwritten" ) ;
650580 assert ! (
651581 detail. contains( purl) && detail. contains( "package/index.js" ) ,
652582 "the overwrite must name the purl and the file: {v:#}"
@@ -674,7 +604,7 @@ async fn scan_agent_json_over_live_hosted_wiring_warns_retained() {
674604 let ( code, v) = run_json ( tmp. path ( ) , & mock. uri ( ) , & [ "--mode" , "agent" , "--yes" ] ) ;
675605 assert_eq ! ( code, 0 , "{v:#}" ) ;
676606 assert ! (
677- warning_detail( & v, "hosted_wiring_retained" ) . contains( SILENT_TARGET ) ,
607+ hosted :: warning_detail( & v, "hosted_wiring_retained" ) . contains( SILENT_TARGET ) ,
678608 "the warning names the package: {v:#}"
679609 ) ;
680610 assert_eq ! ( event_purls( & v, "applied" ) , [ SILENT_TARGET ] , "{v:#}" ) ;
@@ -756,7 +686,7 @@ fn scan_invalid_bun_lockb_warns_instead_of_silent_success() {
756686 assert_eq ! ( v[ "scannedPackages" ] , 0 , "mode={mode:?}: {v}" ) ;
757687 assert_eq ! ( v[ "lockfileOnlyPackages" ] , 0 , "mode={mode:?}: {v}" ) ;
758688 assert ! (
759- warning_detail( & v, "bun_lockb_invalid" ) . contains( "bun.lockb" ) ,
689+ hosted :: warning_detail( & v, "bun_lockb_invalid" ) . contains( "bun.lockb" ) ,
760690 "mode={mode:?}: the format error must name its file: {v:#}"
761691 ) ;
762692 assert ! (
@@ -786,7 +716,7 @@ async fn scan_nonempty_keeps_the_bun_lockb_discovery_warning_in_every_mode() {
786716 assert_eq ! ( v[ "scannedPackages" ] , 1 , "mode={mode:?}: {v}" ) ;
787717 assert_eq ! ( v[ "status" ] , "success" , "mode={mode:?}: {v}" ) ;
788718 assert ! (
789- warning_detail( & v, "bun_lockb_invalid" ) . contains( "bun.lockb" ) ,
719+ hosted :: warning_detail( & v, "bun_lockb_invalid" ) . contains( "bun.lockb" ) ,
790720 "mode={mode:?}: {v:#}"
791721 ) ;
792722 }
0 commit comments