From 558134f6c8a9c3601c48863a45f5c3bf7f125972 Mon Sep 17 00:00:00 2001 From: Ricky Date: Thu, 1 Nov 2018 15:48:56 +0000 Subject: [PATCH] Changed to unwrap methods, added integration tests --- src/config.rs | 16 +++++----------- tests/examples/bat.conf | 1 + tests/integration_tests.rs | 31 +++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 tests/examples/bat.conf diff --git a/src/config.rs b/src/config.rs index 8701e068..340e532a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,17 +9,11 @@ use dirs::PROJECT_DIRS; use util::transpose; pub fn config_file() -> PathBuf { - match env::var("BAT_CONFIG_PATH") { - Ok(env_path) => { - let env_path_buf = PathBuf::from(env_path); - if env_path_buf.is_file() { - return env_path_buf; - } else { - return PROJECT_DIRS.config_dir().join("config"); - } - } - Err(_) => PROJECT_DIRS.config_dir().join("config"), - } + env::var("BAT_CONFIG_PATH") + .ok() + .map(PathBuf::from) + .filter(|config_path| config_path.is_file()) + .unwrap_or(PROJECT_DIRS.config_dir().join("config")) } pub fn get_args_from_config_file() -> Result, shell_words::ParseError> { diff --git a/tests/examples/bat.conf b/tests/examples/bat.conf new file mode 100644 index 00000000..96a04342 --- /dev/null +++ b/tests/examples/bat.conf @@ -0,0 +1 @@ +--paging=always \ No newline at end of file diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 28f30ddd..15dc872b 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -322,3 +322,34 @@ fn pager_disable() { .success() .stdout("hello world\n"); } + +fn bat_config() -> Command { + let mut cmd = Command::main_binary().unwrap(); + cmd.current_dir("tests/examples"); + cmd.env_remove("BAT_PAGER"); + cmd.env_remove("BAT_CONFIG_PATH"); + cmd +} + +#[test] +fn config_location_test() { + bat_config() + .env_remove("BAT_CONFIG_PATH") + .env("BAT_CONFIG_PATH", "bat.conf") + .arg("--config-file") + .assert() + .success() + .stdout("bat.conf\n"); +} + +#[test] +fn config_read_paging_test() { + bat_config() + .env_remove("BAT_CONFIG_PATH") + .env("BAT_CONFIG_PATH", "bat.conf") + .env("BAT_PAGER", "echo testing-config-file") + .arg("test.txt") + .assert() + .success() + .stdout("testing-config-file\n"); +}