From 03739a3686c60e8b9379dd124a628d4fc71c7df5 Mon Sep 17 00:00:00 2001 From: Pete Gadomski Date: Fri, 31 Jan 2025 09:59:22 -0700 Subject: [PATCH] feat: lint and format scripts (#615) - Closes #544 --- .github/workflows/ci.yml | 6 ++---- CONTRIBUTING.md | 1 + crates/cli/src/lib.rs | 2 +- crates/core/src/error.rs | 2 +- crates/core/src/item.rs | 2 +- crates/core/src/item_collection.rs | 2 +- crates/pgstac/src/lib.rs | 7 ++++--- scripts/format | 6 ++++++ scripts/lint | 6 ++++++ 9 files changed, 23 insertions(+), 11 deletions(-) create mode 100755 scripts/format create mode 100755 scripts/lint diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b2e7aa69..7d5d20d3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -58,10 +58,8 @@ jobs: run: | wget https://github.com/duckdb/duckdb/releases/download/v1.1.3/libduckdb-linux-amd64.zip unzip libduckdb-linux-amd64.zip -d /opt/duckdb - - name: Fmt - run: cargo fmt - - name: Clippy - run: cargo clippy --workspace --all-features + - name: Lint + run: scripts/lint - name: Build # need to build first to get the executable for CLI tests run: cargo build --all-features - name: Test diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 45a2bcb5..4f215047 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -13,6 +13,7 @@ Draft pull requests with a failing test to demonstrate a bug are much appreciate Please open a [pull request](https://docs.github.com/en/pull-requests) with your changes -- make sure to include unit tests. Please follow standard git commit formatting (subject line 50 characters max, wrap the body at 72 characters). +Run `scripts/lint` to make sure your changes are nice, and use `scripts/format` to fix things that can be fixed. We use [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/). Your commits do not have to but if you'd like to format them this way, we would be grateful. diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs index bddcf200..484190d7 100644 --- a/crates/cli/src/lib.rs +++ b/crates/cli/src/lib.rs @@ -365,7 +365,7 @@ impl Stacrs { } else { serde_json::to_writer_pretty(std::io::stdout(), &value)?; } - println!(""); + println!(); } else { return Err(anyhow!("invalid output format: {}", format)); } diff --git a/crates/core/src/error.rs b/crates/core/src/error.rs index 6f8fad23..bdc426ac 100644 --- a/crates/core/src/error.rs +++ b/crates/core/src/error.rs @@ -102,7 +102,7 @@ pub enum Error { Parquet(#[from] parquet::errors::ParquetError), /// [reqwest::Error] - #[cfg(any(feature = "reqwest"))] + #[cfg(feature = "reqwest")] #[error(transparent)] Reqwest(#[from] reqwest::Error), diff --git a/crates/core/src/item.rs b/crates/core/src/item.rs index f5bba6d9..d3d971d0 100644 --- a/crates/core/src/item.rs +++ b/crates/core/src/item.rs @@ -738,7 +738,7 @@ mod tests { ])))) .unwrap(); assert!(item - .intersects(&crate::geo::bbox(&vec![-106.0, 41.0, -105.0, 42.0]).unwrap()) + .intersects(&crate::geo::bbox(&[-106.0, 41.0, -105.0, 42.0]).unwrap()) .unwrap()); } diff --git a/crates/core/src/item_collection.rs b/crates/core/src/item_collection.rs index cf35f93f..195fb573 100644 --- a/crates/core/src/item_collection.rs +++ b/crates/core/src/item_collection.rs @@ -136,7 +136,7 @@ mod tests { #[test] fn item_collection_from_iter() { let items = vec![Item::new("a"), Item::new("b")]; - let _ = ItemCollection::from_iter(items.into_iter()); + let _ = ItemCollection::from_iter(items); } #[test] diff --git a/crates/pgstac/src/lib.rs b/crates/pgstac/src/lib.rs index 1078c091..3b066e2c 100644 --- a/crates/pgstac/src/lib.rs +++ b/crates/pgstac/src/lib.rs @@ -334,12 +334,13 @@ pub(crate) mod tests { use stac_api::{Fields, Filter, Search, Sortby}; use std::{ ops::Deref, - sync::{atomic::AtomicU16, Mutex}, + sync::{atomic::AtomicU16, LazyLock}, }; + use tokio::sync::Mutex; use tokio_postgres::{Client, Config, NoTls}; use tokio_test as _; - static MUTEX: Mutex<()> = Mutex::new(()); + static MUTEX: LazyLock> = LazyLock::new(|| Mutex::new(())); struct TestClient { client: Client, @@ -359,7 +360,7 @@ pub(crate) mod tests { let dbname = format!("pgstac_test_{}", id); let config = config(); { - let _mutex = MUTEX.lock().unwrap(); + let _mutex = MUTEX.lock().await; let (client, connection) = config.connect(NoTls).await.unwrap(); let _handle = tokio::spawn(async move { connection.await.unwrap() }); let _ = client diff --git a/scripts/format b/scripts/format new file mode 100755 index 00000000..a67e2a3e --- /dev/null +++ b/scripts/format @@ -0,0 +1,6 @@ +#!/usr/bin/env sh + +set -e + +cargo fmt +cargo clippy --workspace --all-features --fix diff --git a/scripts/lint b/scripts/lint new file mode 100755 index 00000000..12d72e51 --- /dev/null +++ b/scripts/lint @@ -0,0 +1,6 @@ +#!/usr/bin/env sh + +set -e + +cargo fmt --check +cargo clippy --workspace --all-features -- -D warnings