Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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"
Expand Down
12 changes: 11 additions & 1 deletion src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -30,6 +32,7 @@ struct Config {
max_backoff: u32,
freshness_lifetime: Option<u64>,
offline: bool,
#[cfg(feature = "progress-bar")]
progress_bar: Option<ProgressBar>,
}

Expand All @@ -44,6 +47,7 @@ impl CacheBuilder {
max_backoff: 5000,
freshness_lifetime: None,
offline: false,
#[cfg(feature = "progress-bar")]
progress_bar: Some(ProgressBar::default()),
},
}
Expand Down Expand Up @@ -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<ProgressBar>) -> CacheBuilder {
self.config.progress_bar = progress_bar;
self
Expand All @@ -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,
})
}
Expand Down Expand Up @@ -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<ProgressBar>,
/// The HTTP client used to fetch remote resources.
http_client: Client,
Expand Down Expand Up @@ -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,
Expand All @@ -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");
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
31 changes: 24 additions & 7 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand All @@ -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();
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand All @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
4 changes: 4 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
let mut cmd = Command::new(cargo::cargo_bin("cached-path"));
Expand All @@ -22,6 +23,7 @@ fn file_doesnt_exist() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

#[cfg(feature = "build-binary")]
#[test]
fn test_remote_file() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::new(cargo::cargo_bin("cached-path"));
Expand Down Expand Up @@ -61,6 +63,7 @@ fn test_remote_file() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

#[cfg(feature = "build-binary")]
#[test]
fn test_extract_remote_file() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::new(cargo::cargo_bin("cached-path"));
Expand All @@ -86,6 +89,7 @@ fn test_extract_remote_file() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

#[cfg(feature = "build-binary")]
#[test]
fn test_extract_local_file() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::new(cargo::cargo_bin("cached-path"));
Expand Down
Loading