diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 2572029..a113403 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -30,6 +30,9 @@ jobs: - name: Test (rustls-tls) run: cargo test --features build-binary,lzma,rustls-tls + - name: Test (default features) + run: cargo test + include: - os: ubuntu-latest rust: stable diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a26350..6e4b12d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +## Added + +- Added a `progress-bar` feature to enable the `indicatif` dependency. + ## [v0.9.0](https://github.com/epwalsh/rust-cached-path/releases/tag/v0.9.0) - 2025-08-25 ### Changed diff --git a/Cargo.toml b/Cargo.toml index 89804e7..4df2bb0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,7 +37,7 @@ thiserror = "1.0" flate2 = "1.0" tar = "0.4" zip = "0.6" -indicatif = "0.17.11" +indicatif = { version = "0.17.11", optional = true } env_logger = { version = "0.10", optional = true } structopt = { version = "0.3", optional = true } color-eyre = { version = "0.6", optional = true } @@ -46,10 +46,11 @@ lzma-rs = { version = "0.3", optional = true } [features] default = ["default-tls"] -build-binary = ["env_logger", "structopt", "color-eyre"] +build-binary = ["env_logger", "structopt", "color-eyre", "progress-bar"] rustls-tls = ["reqwest/rustls-tls"] default-tls = ["reqwest/default-tls"] lzma = ["lzma-rs"] +progress-bar = ["indicatif"] [dev-dependencies] httpmock = "0.7" diff --git a/src/cache.rs b/src/cache.rs index f6d93e9..a1e81ea 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -14,7 +14,9 @@ use tempfile::NamedTempFile; use crate::archives::{extract_archive, ArchiveFormat}; use crate::utils::hash_str; -use crate::{meta::Meta, Error, ProgressBar}; +#[cfg(feature = "progress-bar")] +use crate::ProgressBar; +use crate::{meta::Meta, Error}; /// Builder to facilitate creating [`Cache`] objects. #[derive(Debug)] @@ -30,6 +32,7 @@ struct Config { max_backoff: u32, freshness_lifetime: Option, offline: bool, + #[cfg(feature = "progress-bar")] progress_bar: Option, } @@ -44,6 +47,7 @@ impl CacheBuilder { max_backoff: 5000, freshness_lifetime: None, offline: false, + #[cfg(feature = "progress-bar")] progress_bar: Some(ProgressBar::default()), }, } @@ -113,6 +117,7 @@ impl CacheBuilder { /// Set the type of progress bar to use. /// /// The default is `Some(ProgressBar::Full)`. + #[cfg(feature = "progress-bar")] pub fn progress_bar(mut self, progress_bar: Option) -> CacheBuilder { self.config.progress_bar = progress_bar; self @@ -136,6 +141,7 @@ impl CacheBuilder { max_backoff: self.config.max_backoff, freshness_lifetime: self.config.freshness_lifetime, offline: self.config.offline, + #[cfg(feature = "progress-bar")] progress_bar: self.config.progress_bar, }) } @@ -206,6 +212,7 @@ pub struct Cache { /// If set to `true`, no HTTP calls will be made. offline: bool, /// The verbosity level of the progress bar. + #[cfg(feature = "progress-bar")] progress_bar: Option, /// The HTTP client used to fetch remote resources. http_client: Client, @@ -543,6 +550,7 @@ impl Cache { info!("Starting download of {url}"); + #[cfg(feature = "progress-bar")] let bytes = if let Some(progress_bar) = &self.progress_bar { let mut download_wrapper = progress_bar.wrap_download( resource, @@ -555,6 +563,8 @@ impl Cache { } else { response.copy_to(&mut tempfile_write_handle)? }; + #[cfg(not(feature = "progress-bar"))] + let bytes = response.copy_to(&mut tempfile_write_handle)?; info!("Downloaded {bytes} bytes"); debug!("Writing meta file"); diff --git a/src/lib.rs b/src/lib.rs index 2b708b6..5f389cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -96,11 +96,13 @@ pub(crate) mod archives; mod cache; mod error; pub(crate) mod meta; +#[cfg(feature = "progress-bar")] mod progress_bar; pub(crate) mod utils; pub use crate::cache::{Cache, CacheBuilder, Options}; pub use crate::error::Error; +#[cfg(feature = "progress-bar")] pub use crate::progress_bar::ProgressBar; /// Get the cached path to a resource. diff --git a/src/test.rs b/src/test.rs index 5ea6b67..52dbc27 100644 --- a/src/test.rs +++ b/src/test.rs @@ -46,13 +46,30 @@ impl Drop for Fixture<'_> { } } +trait BuilderExt { + fn disable_progress_bar(self) -> Self; +} + +impl BuilderExt for crate::cache::CacheBuilder { + fn disable_progress_bar(self) -> Self { + #[cfg(feature = "progress-bar")] + { + self.progress_bar(None) + } + #[cfg(not(feature = "progress-bar"))] + { + self + } + } +} + #[test] fn test_get_cached_path_local_file() { // Setup cache. let cache_dir = tempdir().unwrap(); let cache = Cache::builder() .dir(cache_dir.path().to_owned()) - .progress_bar(None) + .disable_progress_bar() .build() .unwrap(); @@ -66,7 +83,7 @@ fn test_get_cached_path_non_existant_local_file_fails() { let cache_dir = tempdir().unwrap(); let cache = Cache::builder() .dir(cache_dir.path().to_owned()) - .progress_bar(None) + .disable_progress_bar() .build() .unwrap(); @@ -84,7 +101,7 @@ fn test_cached_path_remote_file() { let cache_dir = tempdir().unwrap(); let cache = Cache::builder() .dir(cache_dir.path().to_owned()) - .progress_bar(None) + .disable_progress_bar() .freshness_lifetime(300) .build() .unwrap(); @@ -127,7 +144,7 @@ fn test_cached_path_remote_file() { // Create a new cache without a freshness lifetime. let cache = Cache::builder() .dir(cache_dir.path().to_owned()) - .progress_bar(None) + .disable_progress_bar() .build() .unwrap(); @@ -173,7 +190,7 @@ fn test_cached_path_remote_file_in_subdir() { let cache_dir = tempdir().unwrap(); let cache = Cache::builder() .dir(cache_dir.path().to_owned()) - .progress_bar(None) + .disable_progress_bar() .build() .unwrap(); @@ -197,7 +214,7 @@ fn assert_extract_archive(filename: &str) { let cache_dir = tempdir().unwrap(); let cache = Cache::builder() .dir(cache_dir.path().to_owned()) - .progress_bar(None) + .disable_progress_bar() .build() .unwrap(); @@ -245,7 +262,7 @@ fn test_extract_in_subdir() { let cache_dir = tempdir().unwrap(); let cache = Cache::builder() .dir(cache_dir.path().to_owned()) - .progress_bar(None) + .disable_progress_bar() .build() .unwrap(); diff --git a/tests/cli.rs b/tests/cli.rs index 6be3ad8..a4d93cf 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -7,6 +7,7 @@ use std::path::PathBuf; use std::process::Command; // Run programs use tempfile::tempdir; +#[cfg(feature = "build-binary")] #[test] fn file_doesnt_exist() -> Result<(), Box> { let mut cmd = Command::new(cargo::cargo_bin("cached-path")); @@ -22,6 +23,7 @@ fn file_doesnt_exist() -> Result<(), Box> { Ok(()) } +#[cfg(feature = "build-binary")] #[test] fn test_remote_file() -> Result<(), Box> { let mut cmd = Command::new(cargo::cargo_bin("cached-path")); @@ -61,6 +63,7 @@ fn test_remote_file() -> Result<(), Box> { Ok(()) } +#[cfg(feature = "build-binary")] #[test] fn test_extract_remote_file() -> Result<(), Box> { let mut cmd = Command::new(cargo::cargo_bin("cached-path")); @@ -86,6 +89,7 @@ fn test_extract_remote_file() -> Result<(), Box> { Ok(()) } +#[cfg(feature = "build-binary")] #[test] fn test_extract_local_file() -> Result<(), Box> { let mut cmd = Command::new(cargo::cargo_bin("cached-path"));