Skip to content

Commit

Permalink
Add ability to ignore packages
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Nov 18, 2023
1 parent 917304a commit 8757125
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ octocrab = "0.32"
once_cell = "1.18"
regex = "1.10"
remain = "0.2"
serde = "1.0"
serde_json = "1.0"
tempfile = "3.8"
termcolor = "1.3"
tokio = "1.34"
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ irrecoverable errors occurred, 1 if unmaintained packages were found, and 2 if a
error occurred.
```

## Ignoring packages

If a workspace's `Cargo.toml` file includes a `workspace.metadata.unmaintained.ignore` array, all packages named therein will be ignored. Example:

```toml
[package.metadata.unmaintained]
ignore = ["proc-macro-error"]
```

## Known problems

Repositories whose urls change across versions may be incorrectly reported as unmaintained.
Expand Down
24 changes: 23 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use once_cell::sync::Lazy;
use regex::Regex;
use std::{
cell::RefCell,
collections::HashMap,
collections::{HashMap, HashSet},
env::{args, var},
ffi::OsStr,
fs::{read_to_string, remove_dir_all, File},
Expand Down Expand Up @@ -417,10 +417,16 @@ fn unmaintained() -> Result<bool> {
fn filter_packages(metadata: &Metadata) -> Result<Vec<&Package>> {
let mut packages = Vec::new();

let ignored_packages = ignored_packages(metadata)?;

// smoelius: If a project relies on multiple versions of a package, check only the latest one.
let metadata_latest_version_map = build_metadata_latest_version_map(metadata);

for pkg in &metadata.packages {
if ignored_packages.contains(&pkg.name) {
continue;
}

#[allow(clippy::panic)]
let version = metadata_latest_version_map
.get(&pkg.name)
Expand Down Expand Up @@ -457,6 +463,22 @@ fn filter_packages(metadata: &Metadata) -> Result<Vec<&Package>> {
Ok(packages)
}

#[derive(serde::Deserialize)]
struct UnmaintainedMetadata {
ignored: Option<Vec<String>>,
}

pub fn ignored_packages(metadata: &Metadata) -> Result<HashSet<String>> {
let serde_json::Value::Object(object) = &metadata.workspace_metadata else {
return Ok(HashSet::default());
};
let Some(value) = object.get("unmaintained") else {
return Ok(HashSet::default());
};
let metadata = serde_json::value::from_value::<UnmaintainedMetadata>(value.clone())?;
Ok(metadata.ignored.unwrap_or_default().into_iter().collect())
}

fn build_metadata_latest_version_map(metadata: &Metadata) -> HashMap<String, Version> {
let mut map: HashMap<String, Version> = HashMap::new();

Expand Down
47 changes: 47 additions & 0 deletions tests/ignored.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#![cfg_attr(dylint_lib = "general", allow(crate_wide_allow))]
#![cfg_attr(dylint_lib = "try_io_result", allow(try_io_result))]

use anyhow::{ensure, Result};
use snapbox::cmd::cargo_bin;
use std::{fs::OpenOptions, io::Write, path::Path, process::Command};
use tempfile::tempdir;

#[test]
fn ignored() -> Result<()> {
let tempdir = tempdir()?;

let status = Command::new("cargo")
.args(["init", "--name=test-package"])
.current_dir(&tempdir)
.status()?;
ensure!(status.success());

let mut manifest = OpenOptions::new()
.append(true)
.open(tempdir.path().join("Cargo.toml"))?;
writeln!(manifest, r#"lz4-compress = "*""#)?;

let status = cargo_unmaintained(tempdir.path()).status()?;
ensure!(!status.success());

writeln!(
manifest,
r#"
[workspace.metadata.unmaintained]
ignored = ["lz4-compress"]
"#
)?;

let status = cargo_unmaintained(tempdir.path()).status()?;
ensure!(status.success());

Ok(())
}

fn cargo_unmaintained(dir: &Path) -> Command {
let mut command = Command::new(cargo_bin("cargo-unmaintained"));
command
.args(["unmaintained", "--fail-fast"])
.current_dir(dir);
command
}

0 comments on commit 8757125

Please sign in to comment.