on master help sign in
feat(config): added recognition of $BAT_CONFIG_DIR
commitbf78288e9ec28cfbec7610de19c20bbe04f33f98 authorBill Risher <bill@billrisher.com> dateThu 12 Aug 2021, 17:08 (5 years ago) parentf8498b2
3 files changed+39−11
CHANGELOG.md+1 src/bin/bat/directories.rs+19−11 tests/integration_tests.rs+19
CHANGELOG.md+1
@@ -2,6 +2,7 @@
22
33## Features
44
5+- $BAT_CONFIG_DIR is now a recognized environment variable and has precedent over $XDG_CONFIG_HOME. (@billrisher)
56
67## Bugfixes
78
src/bin/bat/directories.rs+19−11
@@ -4,8 +4,9 @@ use std::path::{Path, PathBuf};
44use lazy_static::lazy_static;
55
66/// Wrapper for 'dirs' that treats MacOS more like Linux, by following the XDG specification.
7−/// This means that the `XDG_CACHE_HOME` and `XDG_CONFIG_HOME` environment variables are
8−/// checked first. The fallback directories are `~/.cache/bat` and `~/.config/bat`, respectively.
7+/// The `XDG_CACHE_HOME` environment variable is checked first. `BAT_CONFIG_DIR`
8+/// is then checked before the `XDG_CONFIG_HOME` environment variable.
9+/// The fallback directories are `~/.cache/bat` and `~/.config/bat`, respectively.
910pub struct BatProjectDirs {
1011 cache_dir: PathBuf,
1112 config_dir: PathBuf,
@@ -14,17 +15,24 @@ pub struct BatProjectDirs {
1415impl BatProjectDirs {
1516 fn new() -> Option<BatProjectDirs> {
1617 let cache_dir = BatProjectDirs::get_cache_dir()?;
18+
19+ // Checks whether or not $BAT_CONFIG_DIR exists. If it doesn't, set our config dir
20+ // to our system's default configuration home.
21+ let config_dir = if let Some(config_dir_op) = env::var_os("BAT_CONFIG_DIR")
22+ .map(PathBuf::from) {
23+ config_dir_op
24+ } else {
25+ #[cfg(target_os = "macos")]
26+ let config_dir_op = env::var_os("XDG_CONFIG_HOME")
27+ .map(PathBuf::from)
28+ .filter(|p| p.is_absolute())
29+ .or_else(|| dirs_next::home_dir().map(|d| d.join(".config")));
1730
18− #[cfg(target_os = "macos")]
19− let config_dir_op = env::var_os("XDG_CONFIG_HOME")
20− .map(PathBuf::from)
21− .filter(|p| p.is_absolute())
22− .or_else(|| dirs_next::home_dir().map(|d| d.join(".config")));
23−
24− #[cfg(not(target_os = "macos"))]
25− let config_dir_op = dirs_next::config_dir();
31+ #[cfg(not(target_os = "macos"))]
32+ let config_dir_op = dirs_next::config_dir();
2633
27− let config_dir = config_dir_op.map(|d| d.join("bat"))?;
34+ config_dir_op.map(|d| d.join("bat"))?
35+ };
2836
2937 Some(BatProjectDirs {
3038 cache_dir,
tests/integration_tests.rs+19
@@ -747,6 +747,25 @@ fn config_location_when_generating() {
747747 assert!(tmp_config_path.exists());
748748}
749749
750+#[test]
751+fn config_location_from_bat_config_dir_variable() {
752+ let bat_conf_dir = tempdir().expect("can't create temporary directory.").path().join("bat_conf_dir/");
753+
754+ // Create file at BAT_CONFIG_DIR
755+ bat_with_config()
756+ .env("BAT_CONFIG_DIR", bat_conf_dir.to_str().unwrap())
757+ .arg("--generate-config-file")
758+ .assert()
759+ .success()
760+ .stdout(
761+ predicate::str::is_match("Success! Config file written to .*config\n")
762+ .unwrap()
763+ );
764+
765+ // Ensure generated config file exists.
766+ assert!(bat_conf_dir.join("config").exists());
767+}
768+
750769#[test]
751770fn config_read_arguments_from_file() {
752771 bat_with_config()