Island: if the data directory is empty no need to consider backing it up

This commit is contained in:
VakarisZ 2021-10-25 11:13:06 +03:00
parent f6e02e2a6a
commit 97642f45dc
2 changed files with 17 additions and 1 deletions

View File

@ -1,4 +1,5 @@
import logging
import os
import shutil
from pathlib import Path
@ -15,7 +16,7 @@ class IncompatibleDataDirectory(Exception):
def setup_data_dir(data_dir_path: Path) -> None:
logger.info(f"Setting up data directory at {data_dir_path}.")
if data_dir_path.exists() and _data_dir_version_mismatch_exists(data_dir_path):
if _is_data_dir_old(data_dir_path):
logger.info("Version in data directory does not match the Island's version.")
_handle_old_data_directory(data_dir_path)
create_secure_directory(str(data_dir_path))
@ -23,6 +24,14 @@ def setup_data_dir(data_dir_path: Path) -> None:
logger.info("Data directory set up.")
def _is_data_dir_old(data_dir_path: Path) -> bool:
dir_exists = data_dir_path.exists()
if not dir_exists or not os.listdir(data_dir_path):
return False
return _data_dir_version_mismatch_exists(data_dir_path)
def _handle_old_data_directory(data_dir_path: Path) -> None:
should_delete_data_directory = _prompt_user_to_delete_data_directory(data_dir_path)
if should_delete_data_directory:

View File

@ -77,3 +77,10 @@ def test_data_dir_setup_not_needed(temp_data_dir_path, temp_version_file_path):
setup_data_dir(temp_data_dir_path)
assert temp_version_file_path.read_text() == current_version
assert bogus_file_path.is_file()
def test_empty_data_dir(temp_data_dir_path, temp_version_file_path):
temp_data_dir_path.mkdir()
setup_data_dir(temp_data_dir_path)
assert temp_version_file_path.read_text() == current_version