Create cache directory path if it does not exist (#174)

Fixes #167
This commit is contained in:
Danny Mösch 2021-09-12 20:28:22 +02:00 committed by GitHub
commit 1471b1d97b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 15 deletions

View file

@ -10,6 +10,9 @@ use predicates::boolean::PredicateBooleanExt;
use predicates::prelude::predicate::str::{contains, is_empty, similar};
use tempfile::{Builder, TempDir};
// TODO: Should be 'cache::CACHE_DIR_ENV_VAR'. This requires to have a library crate for the logic.
static CACHE_DIR_ENV_VAR: &str = "TEALDEER_CACHE_DIR";
struct TestEnv {
pub cache_dir: TempDir,
pub custom_pages_dir: TempDir,
@ -106,10 +109,7 @@ impl TestEnv {
}
let run = build.run().unwrap();
let mut cmd = run.command();
cmd.env(
"TEALDEER_CACHE_DIR",
self.cache_dir.path().to_str().unwrap(),
);
cmd.env(CACHE_DIR_ENV_VAR, self.cache_dir.path().to_str().unwrap());
cmd.env(
"TEALDEER_CONFIG_DIR",
self.config_dir.path().to_str().unwrap(),
@ -218,6 +218,50 @@ fn test_quiet_old_cache() {
.stderr(contains("The cache hasn't been updated for more than ").not());
}
#[test]
fn test_create_cache_directory_path() {
let testenv = TestEnv::new();
let cache_dir = testenv.cache_dir.path();
let internal_cache_dir = cache_dir.join("internal");
let mut command = testenv.command();
command.env(CACHE_DIR_ENV_VAR, internal_cache_dir.to_str().unwrap());
assert!(!internal_cache_dir.exists());
command
.arg("-u")
.assert()
.success()
.stderr(contains(format!(
"Successfully created cache directory path `{}`.",
internal_cache_dir.to_str().unwrap()
)))
.stderr(contains("Successfully updated cache."));
assert!(internal_cache_dir.is_dir());
}
#[test]
fn test_cache_location_not_a_directory() {
let testenv = TestEnv::new();
let cache_dir = testenv.cache_dir.path();
let internal_file = cache_dir.join("internal");
File::create(&internal_file).unwrap();
let mut command = testenv.command();
command.env(CACHE_DIR_ENV_VAR, internal_file.to_str().unwrap());
command
.arg("-u")
.assert()
.failure()
.stderr(contains(format!(
"Path specified by ${} is not a directory.",
CACHE_DIR_ENV_VAR
)));
}
#[test]
fn test_setup_seed_config() {
let testenv = TestEnv::new();