cargo-upkeep 0.4.4

Unified Rust project maintenance CLI (cargo subcommand)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//! CLI parsing and logging setup.

pub mod commands;

use crate::core::error::{ErrorCode, Result, UpkeepError};
use clap::{Args, Parser, Subcommand};
use tracing_subscriber::EnvFilter;

#[derive(Debug, Parser)]
#[command(
    name = "cargo-upkeep",
    version,
    about = "Unified Rust project maintenance CLI",
    arg_required_else_help = true
)]
pub struct Cli {
    #[arg(short, long, global = true)]
    pub verbose: bool,
    #[arg(long, global = true)]
    pub log_level: Option<String>,
    #[arg(long, global = true)]
    pub json: bool,
    #[command(subcommand)]
    pub command: Command,
}

#[derive(Debug, Subcommand)]
pub enum Command {
    #[command(subcommand, about = "Run upkeep subcommands")]
    Upkeep(UpkeepCommand),
    #[command(about = "Detect workspace, tooling, and CI")]
    Detect,
    #[command(about = "Report RustSec vulnerabilities")]
    Audit,
    Deps {
        #[arg(
            long,
            help = "Include RustSec advisories for direct workspace deps (requires Cargo.lock)"
        )]
        security: bool,
    },
    #[command(about = "Compute project quality score")]
    Quality {
        #[arg(
            long,
            help = "Exit nonzero when any metric could not be measured (a run that measured nothing always exits nonzero)"
        )]
        require_complete: bool,
    },
    #[command(about = "Find unused dependencies")]
    Unused,
    #[command(
        name = "unsafe-code",
        alias = "unsafe",
        about = "Report unsafe code usage"
    )]
    UnsafeCode,
    #[command(about = "Render dependency tree with filters")]
    Tree(TreeArgs),
    #[command(about = "Report outdated and vulnerable Python dependencies")]
    Python(PythonArgs),
}

#[derive(Debug, Subcommand)]
pub enum UpkeepCommand {
    #[command(about = "Detect workspace, tooling, and CI")]
    Detect,
    #[command(about = "Report RustSec vulnerabilities")]
    Audit,
    Deps {
        #[arg(
            long,
            help = "Include RustSec advisories for direct workspace deps (requires Cargo.lock)"
        )]
        security: bool,
    },
    #[command(about = "Compute project quality score")]
    Quality {
        #[arg(
            long,
            help = "Exit nonzero when any metric could not be measured (a run that measured nothing always exits nonzero)"
        )]
        require_complete: bool,
    },
    #[command(about = "Find unused dependencies")]
    Unused,
    #[command(
        name = "unsafe-code",
        alias = "unsafe",
        about = "Report unsafe code usage"
    )]
    UnsafeCode,
    #[command(about = "Render dependency tree with filters")]
    Tree(TreeArgs),
    #[command(about = "Report outdated and vulnerable Python dependencies")]
    Python(PythonArgs),
}

#[derive(Debug, Args)]
pub struct TreeArgs {
    #[arg(long, help = "Limit recursion depth")]
    pub depth: Option<usize>,
    #[arg(long, help = "Only show duplicate crates")]
    pub duplicates: bool,
    #[arg(long, help = "Invert tree to show reverse dependencies")]
    pub invert: Option<String>,
    #[arg(long, help = "Include enabled features")]
    pub features: bool,
    #[arg(long = "no-dev", help = "Exclude dev-dependencies")]
    pub no_dev: bool,
}

// The two opt-in policy gates on `python`.
//
// One struct shared by both `Command::Python` and `UpkeepCommand::Python`,
// deliberately. Those enums declare every other subcommand's flags twice, and a
// flag added to one of the two copies exists under only one invocation form
// while compiling and unit-testing clean (#34). Sharing the struct makes that
// particular divergence unrepresentable; the variant still has to be added to
// both enums and to `main`'s mapping, which
// `cli_python_flags_are_plumbed_through_both_invocation_forms` covers.
//
// Written as `//` rather than `///` on purpose, here and on the fields below:
// clap's derive turns a doc comment into user-facing help text, so a rationale
// aimed at the next maintainer ends up printed by `--help`.
#[derive(Debug, Args)]
pub struct PythonArgs {
    // `None` is the flag not passed; `Some(vec![])` is the bare form, meaning
    // every capability. The bare form is the interactive one: adding a
    // capability is an additive change that does not bump `schema_version`, so
    // a bare `--require-complete` starts failing on unchanged code the first
    // time a release adds a capability whose tool the runner lacks. Naming the
    // capabilities pins the gate to what the pipeline asked for.
    #[arg(
        long,
        value_name = "CAPABILITIES",
        value_enum,
        value_delimiter = ',',
        num_args = 0..,
        require_equals = true,
        help = "Exit nonzero when a capability was not measured; optionally name which (--require-complete=outdated,security)"
    )]
    pub require_complete: Option<Vec<CapabilityArg>>,

    #[arg(
        long,
        value_name = "THRESHOLD",
        value_enum,
        help = "Exit nonzero when any finding is at or above this severity (an unknown severity satisfies every threshold)"
    )]
    pub fail_on_vulnerability: Option<ThresholdArg>,
}

// A capability nameable on `--require-complete`.
//
// Mirrors `PythonCapability` rather than reusing it: that type is the serialized
// schema, and deriving `ValueEnum` on it would make a future rename of a CLI
// value look like a free change when it is a schema change.
#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
#[clap(rename_all = "snake_case")]
pub enum CapabilityArg {
    Outdated,
    Security,
}

// A `--fail-on-vulnerability` threshold.
//
// `low` and `any` accept the same set today, because every graded severity is at
// or above `low` and `unknown` satisfies everything. Both names are kept because
// they say different things about intent, and a future severity below `low`
// would separate them.
#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
#[clap(rename_all = "snake_case")]
pub enum ThresholdArg {
    Critical,
    High,
    Moderate,
    Low,
    Any,
}

pub fn init_logging(verbose: bool, log_level: Option<&str>) -> Result<()> {
    let filter = match log_level {
        Some(level) => EnvFilter::try_new(level).map_err(|err| {
            UpkeepError::context(
                ErrorCode::Config,
                format!("invalid log level filter: {level}"),
                err,
            )
        })?,
        None => {
            if verbose {
                EnvFilter::new("info")
            } else {
                EnvFilter::new("warn")
            }
        }
    };

    tracing_subscriber::fmt()
        .with_env_filter(filter)
        .with_writer(std::io::stderr)
        .try_init()
        .map_err(|err| {
            UpkeepError::message(
                ErrorCode::Config,
                format!("failed to initialize logging: {err}"),
            )
        })?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{CapabilityArg, Cli, Command, ThresholdArg, TreeArgs, UpkeepCommand};
    use crate::core::error::ErrorCode;
    use clap::{error::ErrorKind, Parser};

    #[test]
    fn parses_upkeep_subcommand() {
        let cli = Cli::try_parse_from(["cargo-upkeep", "upkeep", "detect"]).unwrap();
        match cli.command {
            Command::Upkeep(UpkeepCommand::Detect) => {}
            _ => panic!("unexpected subcommand"),
        }
    }

    #[test]
    fn parses_direct_subcommand() {
        let cli = Cli::try_parse_from(["cargo-upkeep", "detect"]).unwrap();
        match cli.command {
            Command::Detect => {}
            _ => panic!("unexpected subcommand"),
        }
    }

    #[test]
    fn parses_tree_flags() {
        let cli = Cli::try_parse_from([
            "cargo-upkeep",
            "tree",
            "--depth",
            "2",
            "--duplicates",
            "--invert",
            "serde",
            "--features",
            "--no-dev",
        ])
        .unwrap();

        match cli.command {
            Command::Tree(args) => {
                assert_eq!(args.depth, Some(2));
                assert!(args.duplicates);
                assert_eq!(args.invert.as_deref(), Some("serde"));
                assert!(args.features);
                assert!(args.no_dev);
            }
            _ => panic!("unexpected subcommand"),
        }
    }

    #[test]
    fn parses_tree_upkeep_flags() {
        let cli = Cli::try_parse_from(["cargo-upkeep", "upkeep", "tree", "--depth", "1"]).unwrap();

        match cli.command {
            Command::Upkeep(UpkeepCommand::Tree(TreeArgs { depth, .. })) => {
                assert_eq!(depth, Some(1));
            }
            _ => panic!("unexpected subcommand"),
        }
    }

    #[test]
    fn parses_global_flags() {
        let cli = Cli::try_parse_from([
            "cargo-upkeep",
            "--json",
            "--verbose",
            "--log-level",
            "debug",
            "detect",
        ])
        .unwrap();

        assert!(cli.json);
        assert!(cli.verbose);
        assert_eq!(cli.log_level.as_deref(), Some("debug"));
    }

    /// `Command` and `UpkeepCommand` declare `quality` separately, so a flag
    /// added to one silently exists only on `cargo-upkeep quality` or only on
    /// `cargo upkeep quality`. Both forms are asserted here for that reason.
    #[test]
    fn parses_quality_require_complete_in_both_forms() {
        let cli = Cli::try_parse_from(["cargo-upkeep", "quality", "--require-complete"]).unwrap();
        assert!(matches!(
            cli.command,
            Command::Quality {
                require_complete: true
            }
        ));

        let cli = Cli::try_parse_from(["cargo-upkeep", "upkeep", "quality", "--require-complete"])
            .unwrap();
        assert!(matches!(
            cli.command,
            Command::Upkeep(UpkeepCommand::Quality {
                require_complete: true
            })
        ));

        // Opt-in: absent unless asked for.
        let cli = Cli::try_parse_from(["cargo-upkeep", "quality"]).unwrap();
        assert!(matches!(
            cli.command,
            Command::Quality {
                require_complete: false
            }
        ));
    }

    /// `--require-complete` has three states, and the bare one is not "absent".
    ///
    /// `None` is the flag not passed. `Some(vec![])` is the bare form, meaning
    /// every capability. `Some(vec![..])` names the gate's subjects. Collapsing
    /// the first two — the shape `Option<Vec<_>>` invites — would make the bare
    /// form a no-op and silently disable the gate.
    #[test]
    fn parses_python_require_complete_in_all_three_states() {
        let args = |argv: &[&str]| match Cli::try_parse_from(argv).unwrap().command {
            Command::Python(args) => args,
            _ => panic!("unexpected subcommand"),
        };

        assert_eq!(args(&["cargo-upkeep", "python"]).require_complete, None);
        assert_eq!(
            args(&["cargo-upkeep", "python", "--require-complete"]).require_complete,
            Some(Vec::new()),
            "the bare form means every capability, which is not the same as absent"
        );
        assert_eq!(
            args(&[
                "cargo-upkeep",
                "python",
                "--require-complete=outdated,security"
            ])
            .require_complete,
            Some(vec![CapabilityArg::Outdated, CapabilityArg::Security])
        );
        assert_eq!(
            args(&["cargo-upkeep", "python", "--require-complete=security"]).require_complete,
            Some(vec![CapabilityArg::Security])
        );
    }

    /// A capability list is a value list, so a space-separated capability must
    /// not be swallowed as one.
    ///
    /// `require_equals` is what enforces that. Without it, `--require-complete`
    /// followed by a positional would eat it, and `num_args = 0..` would make the
    /// bare form greedy over anything that followed.
    #[test]
    fn python_require_complete_requires_an_equals_sign() {
        let err = Cli::try_parse_from(["cargo-upkeep", "python", "--require-complete", "outdated"])
            .unwrap_err();
        assert_eq!(err.kind(), ErrorKind::UnknownArgument);
    }

    /// Both gates reject a value they do not know, which is exit 2 rather than a
    /// silently ignored flag.
    #[test]
    fn python_gates_reject_unknown_values() {
        for argv in [
            &["cargo-upkeep", "python", "--require-complete=bogus"][..],
            &["cargo-upkeep", "python", "--fail-on-vulnerability", "bogus"][..],
        ] {
            let err = Cli::try_parse_from(argv).unwrap_err();
            assert_eq!(err.kind(), ErrorKind::InvalidValue, "{argv:?}");
        }
    }

    /// `Command` and `UpkeepCommand` declare `python` separately, so the parse
    /// has to be asserted under both invocation forms — the same reason
    /// `parses_quality_require_complete_in_both_forms` exists.
    #[test]
    fn parses_python_gates_in_both_forms() {
        let cli = Cli::try_parse_from([
            "cargo-upkeep",
            "python",
            "--require-complete=security",
            "--fail-on-vulnerability",
            "high",
        ])
        .unwrap();
        match cli.command {
            Command::Python(args) => {
                assert_eq!(args.require_complete, Some(vec![CapabilityArg::Security]));
                assert_eq!(args.fail_on_vulnerability, Some(ThresholdArg::High));
            }
            _ => panic!("unexpected subcommand"),
        }

        let cli = Cli::try_parse_from([
            "cargo-upkeep",
            "upkeep",
            "python",
            "--require-complete=security",
            "--fail-on-vulnerability",
            "high",
        ])
        .unwrap();
        match cli.command {
            Command::Upkeep(UpkeepCommand::Python(args)) => {
                assert_eq!(args.require_complete, Some(vec![CapabilityArg::Security]));
                assert_eq!(args.fail_on_vulnerability, Some(ThresholdArg::High));
            }
            _ => panic!("unexpected subcommand"),
        }
    }

    #[test]
    fn parses_unsafe_aliases() {
        let cli = Cli::try_parse_from(["cargo-upkeep", "unsafe"]).unwrap();
        assert!(matches!(cli.command, Command::UnsafeCode));

        let cli = Cli::try_parse_from(["cargo-upkeep", "unsafe-code"]).unwrap();
        assert!(matches!(cli.command, Command::UnsafeCode));
    }

    #[test]
    fn missing_subcommand_returns_error() {
        let err = Cli::try_parse_from(["cargo-upkeep"]).unwrap_err();
        assert_eq!(
            err.kind(),
            ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand
        );
    }

    #[test]
    fn unknown_flag_returns_error() {
        let err = Cli::try_parse_from(["cargo-upkeep", "--nope", "detect"]).unwrap_err();
        assert_eq!(err.kind(), ErrorKind::UnknownArgument);
    }

    #[test]
    fn unknown_subcommand_returns_error() {
        let err = Cli::try_parse_from(["cargo-upkeep", "DETECT"]).unwrap_err();
        assert_eq!(err.kind(), ErrorKind::InvalidSubcommand);
    }

    #[test]
    fn init_logging_invalid_level_returns_error() {
        let err = super::init_logging(false, Some("info=bogus")).unwrap_err();
        assert_eq!(err.code(), ErrorCode::Config);
        assert!(err
            .to_string()
            .contains("invalid log level filter: info=bogus"));
    }
}