Skip to content

Commit 4e54005

Browse files
committed
fix(themes): ensure tests cannot hit real loading directory
1 parent 53b54dd commit 4e54005

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

crates/atuin-client/src/theme.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use strum_macros;
22
use std::path::PathBuf;
3-
use std::error::Error;
3+
use std::error;
4+
use std::io::{Error, ErrorKind};
45
use config::{
56
Config, File as ConfigFile, FileFormat,
67
};
@@ -176,18 +177,27 @@ pub struct ThemeManager {
176177

177178
// Theme-loading logic
178179
impl ThemeManager {
179-
pub fn new(debug: Option<bool>) -> Self {
180+
pub fn new(debug: Option<bool>, theme_dir: Option<String>) -> Self {
180181
Self {
181182
loaded_themes: HashMap::new(),
182183
debug: debug.unwrap_or(false),
183-
override_theme_dir: std::env::var("ATUIN_THEME_DIR").ok(),
184+
override_theme_dir: match theme_dir {
185+
Some(theme_dir) => Some(theme_dir),
186+
None => std::env::var("ATUIN_THEME_DIR").ok()
187+
},
184188
}
185189
}
186190

187191
// Try to load a theme from a `{name}.toml` file in the theme directory. If an override is set
188192
// for the theme dir (via ATUIN_THEME_DIR env) we should load the theme from there
189-
pub fn load_theme_from_file(&mut self, name: &str) -> Result<&Theme, Box<dyn Error>> {
193+
pub fn load_theme_from_file(&mut self, name: &str) -> Result<&Theme, Box<dyn error::Error>> {
190194
let mut theme_file = if let Some(p) = &self.override_theme_dir {
195+
if p.is_empty() {
196+
return Err(Box::new(Error::new(
197+
ErrorKind::NotFound,
198+
"Empty theme directory override and could not find theme elsewhere"
199+
)));
200+
}
191201
PathBuf::from(p)
192202
} else {
193203
let config_dir = atuin_common::utils::config_dir();
@@ -230,7 +240,7 @@ impl ThemeManager {
230240
None => match self.load_theme_from_file(name) {
231241
Ok(theme) => theme,
232242
Err(err) => {
233-
print!["Could not load theme {}: {}", name, err];
243+
println!["Could not load theme {}: {}", name, err];
234244
built_ins.get("").unwrap()
235245
}
236246
}
@@ -244,8 +254,8 @@ mod theme_tests {
244254

245255
#[test]
246256
fn load_theme() {
247-
let mut manager = ThemeManager::new(Some(false));
257+
let mut manager = ThemeManager::new(Some(false), Some("".to_string()));
248258
let theme = manager.load_theme("autumn");
249-
assert_eq!(theme.as_style(Meaning::Annotation).foreground_color, Some(Color::DarkGrey));
259+
assert_eq!(theme.as_style(Meaning::Guidance).foreground_color, from_named("brown").ok());
250260
}
251261
}

crates/atuin/src/command/client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl Cmd {
9494
.unwrap();
9595

9696
let settings = Settings::new().wrap_err("could not load client settings")?;
97-
let theme_manager = theme::ThemeManager::new(settings.theme.debug);
97+
let theme_manager = theme::ThemeManager::new(settings.theme.debug, None);
9898
let res = runtime.block_on(self.run_inner(settings, theme_manager));
9999

100100
runtime.shutdown_timeout(std::time::Duration::from_millis(50));

0 commit comments

Comments
 (0)