Skip to content

Commit

Permalink
Fix bat cache --clear not clearing the --target dir if specified
Browse files Browse the repository at this point in the history
  • Loading branch information
miles170 committed Nov 3, 2022
1 parent 78a67ac commit 5248144
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
18 changes: 12 additions & 6 deletions src/bin/bat/assets.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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::<String>("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<HighlightingAssets> {
Expand Down Expand Up @@ -50,9 +57,8 @@ pub fn assets_from_cache_or_binary(use_custom_assets: bool) -> Result<Highlighti
Ok(custom_assets.unwrap_or_else(HighlightingAssets::from_binary))
}

fn clear_asset(filename: &str, description: &str) {
fn clear_asset(path: PathBuf, description: &str) {
print!("Clearing {} ... ", description);
let path = PROJECT_DIRS.cache_dir().join(filename);
match fs::remove_file(&path) {
Err(err) if err.kind() == io::ErrorKind::NotFound => {
println!("skipped (not present)");
Expand Down
2 changes: 1 addition & 1 deletion src/bin/bat/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
3 changes: 0 additions & 3 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 5248144

Please sign in to comment.