From 83c7b8396ac65dc418275bcfa2cfc5206bac8fe5 Mon Sep 17 00:00:00 2001 From: Miles Liu Date: Thu, 3 Nov 2022 10:30:27 +0800 Subject: [PATCH] Fix `bat cache --clear` not clearing the `--target` dir if specified --- CHANGELOG.md | 2 ++ src/bin/bat/assets.rs | 18 ++++++++++++------ src/bin/bat/main.rs | 2 +- tests/integration_tests.rs | 3 --- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c37d935547..957373bb9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ ## Bugfixes +- Fix `bat cache --clear` not clearing the `--target` dir if specified. See #2393 (@miles170) + ## Other - Various bash completion improvements, see #2310 (@scop) diff --git a/src/bin/bat/assets.rs b/src/bin/bat/assets.rs index 951a574b56..8f66a50d3e 100644 --- a/src/bin/bat/assets.rs +++ b/src/bin/bat/assets.rs @@ -1,6 +1,8 @@ use std::borrow::Cow; use std::fs; use std::io; +use std::path::Path; +use std::path::PathBuf; use clap::crate_version; @@ -18,10 +20,15 @@ pub fn cache_dir() -> Cow<'static, str> { PROJECT_DIRS.cache_dir().to_string_lossy() } -pub fn clear_assets() { - clear_asset("themes.bin", "theme set cache"); - clear_asset("syntaxes.bin", "syntax set cache"); - clear_asset("metadata.yaml", "metadata file"); +pub fn clear_assets(matches: &clap::ArgMatches) { + let target_dir = matches + .get_one::("target") + .map(Path::new) + .unwrap_or_else(|| PROJECT_DIRS.cache_dir()); + + clear_asset(target_dir.join("themes.bin"), "theme set cache"); + clear_asset(target_dir.join("syntaxes.bin"), "syntax set cache"); + clear_asset(target_dir.join("metadata.yaml"), "metadata file"); } pub fn assets_from_cache_or_binary(use_custom_assets: bool) -> Result { @@ -50,9 +57,8 @@ pub fn assets_from_cache_or_binary(use_custom_assets: bool) -> Result { println!("skipped (not present)"); diff --git a/src/bin/bat/main.rs b/src/bin/bat/main.rs index b4e5d9b7ce..b39769c472 100644 --- a/src/bin/bat/main.rs +++ b/src/bin/bat/main.rs @@ -67,7 +67,7 @@ fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> { #[cfg(not(feature = "build-assets"))] println!("bat has been built without the 'build-assets' feature. The 'cache --build' option is not available."); } else if matches.get_flag("clear") { - clear_assets(); + clear_assets(matches); } Ok(()) diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index a1c79e0287..0991dda481 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -892,11 +892,8 @@ fn config_read_arguments_from_file() { .stdout(predicate::eq("dummy-pager-from-config\n").normalize()); } -// Ignore this test for now as `bat cache --clear` only targets the default cache dir. -// `bat cache --clear` must clear the `--target` dir for this test to pass. #[cfg(unix)] #[test] -#[ignore] fn cache_clear() { let src_dir = "cache_source"; let tmp_dir = tempdir().expect("can create temporary directory");