mirror of
https://github.com/Llewellynvdm/starship.git
synced 2025-02-13 17:39:06 +00:00
test: enable logging in tests (#2151)
* test: enable logging in tests * fix clippy
This commit is contained in:
parent
0faa05628e
commit
fb0381f7e0
@ -17,8 +17,8 @@ pub struct StarshipLogger {
|
|||||||
log_level: Level,
|
log_level: Level,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StarshipLogger {
|
impl Default for StarshipLogger {
|
||||||
fn new() -> Self {
|
fn default() -> Self {
|
||||||
let log_dir = env::var_os("STARSHIP_CACHE")
|
let log_dir = env::var_os("STARSHIP_CACHE")
|
||||||
.map(PathBuf::from)
|
.map(PathBuf::from)
|
||||||
.unwrap_or_else(|| {
|
.unwrap_or_else(|| {
|
||||||
@ -56,6 +56,19 @@ impl StarshipLogger {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl StarshipLogger {
|
||||||
|
/// Override the minimum log level
|
||||||
|
pub fn set_log_level(&mut self, level: log::Level) {
|
||||||
|
self.log_level = level;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Override the log level path
|
||||||
|
/// This won't change anything if a log file was already opened
|
||||||
|
pub fn set_log_file_path(&mut self, path: PathBuf) {
|
||||||
|
self.log_file_path = path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl log::Log for StarshipLogger {
|
impl log::Log for StarshipLogger {
|
||||||
fn enabled(&self, metadata: &Metadata) -> bool {
|
fn enabled(&self, metadata: &Metadata) -> bool {
|
||||||
metadata.level() <= self.log_level
|
metadata.level() <= self.log_level
|
||||||
@ -119,6 +132,6 @@ impl log::Log for StarshipLogger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn init() {
|
pub fn init() {
|
||||||
log::set_boxed_logger(Box::new(StarshipLogger::new())).unwrap();
|
log::set_boxed_logger(Box::new(StarshipLogger::default())).unwrap();
|
||||||
log::set_max_level(LevelFilter::Trace);
|
log::set_max_level(LevelFilter::Trace);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
use crate::config::StarshipConfig;
|
use crate::config::StarshipConfig;
|
||||||
use crate::context::{Context, Shell};
|
use crate::context::{Context, Shell};
|
||||||
|
use crate::logger::StarshipLogger;
|
||||||
|
use log::{Level, LevelFilter};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
@ -12,6 +14,21 @@ static FIXTURE_DIR: Lazy<PathBuf> =
|
|||||||
static GIT_FIXTURE: Lazy<PathBuf> = Lazy::new(|| FIXTURE_DIR.join("git-repo.bundle"));
|
static GIT_FIXTURE: Lazy<PathBuf> = Lazy::new(|| FIXTURE_DIR.join("git-repo.bundle"));
|
||||||
static HG_FIXTURE: Lazy<PathBuf> = Lazy::new(|| FIXTURE_DIR.join("hg-repo.bundle"));
|
static HG_FIXTURE: Lazy<PathBuf> = Lazy::new(|| FIXTURE_DIR.join("hg-repo.bundle"));
|
||||||
|
|
||||||
|
static LOGGER: Lazy<()> = Lazy::new(|| {
|
||||||
|
let mut logger = StarshipLogger::default();
|
||||||
|
|
||||||
|
// Don't log to files during tests
|
||||||
|
let nul = if cfg!(windows) { "nul" } else { "/dev/null" };
|
||||||
|
let nul = PathBuf::from(nul);
|
||||||
|
|
||||||
|
// Maxmimum log level
|
||||||
|
log::set_max_level(LevelFilter::Trace);
|
||||||
|
logger.set_log_level(Level::Trace);
|
||||||
|
logger.set_log_file_path(nul);
|
||||||
|
|
||||||
|
log::set_boxed_logger(Box::new(logger)).unwrap();
|
||||||
|
});
|
||||||
|
|
||||||
/// Render a specific starship module by name
|
/// Render a specific starship module by name
|
||||||
pub struct ModuleRenderer<'a> {
|
pub struct ModuleRenderer<'a> {
|
||||||
name: &'a str,
|
name: &'a str,
|
||||||
@ -21,6 +38,9 @@ pub struct ModuleRenderer<'a> {
|
|||||||
impl<'a> ModuleRenderer<'a> {
|
impl<'a> ModuleRenderer<'a> {
|
||||||
/// Creates a new ModuleRenderer
|
/// Creates a new ModuleRenderer
|
||||||
pub fn new(name: &'a str) -> Self {
|
pub fn new(name: &'a str) -> Self {
|
||||||
|
// Start logger
|
||||||
|
Lazy::force(&LOGGER);
|
||||||
|
|
||||||
let mut context = Context::new_with_dir(clap::ArgMatches::default(), PathBuf::new());
|
let mut context = Context::new_with_dir(clap::ArgMatches::default(), PathBuf::new());
|
||||||
context.shell = Shell::Unknown;
|
context.shell = Shell::Unknown;
|
||||||
context.config = StarshipConfig { config: None };
|
context.config = StarshipConfig { config: None };
|
||||||
|
Loading…
x
Reference in New Issue
Block a user