From 09d6f2fdc1ae33a003239e67c9aa99959324ae3b Mon Sep 17 00:00:00 2001 From: David Michaels Date: Mon, 6 May 2024 16:07:24 -0400 Subject: [PATCH] tests for file_utils.normalize_path --- dcicutils/file_utils.py | 16 +++++++-------- pyproject.toml | 2 +- test/test_file_utils.py | 43 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/dcicutils/file_utils.py b/dcicutils/file_utils.py index 02f3847af..60c62f1ac 100644 --- a/dcicutils/file_utils.py +++ b/dcicutils/file_utils.py @@ -79,15 +79,15 @@ def search_for_file(file: str, return None if single is True else [] -def normalize_path(value: Union[str, pathlib.Path], absolute: bool = False, home: Optional[bool] = None) -> str: +def normalize_path(value: Union[str, pathlib.Path], absolute: bool = False, expand_home: Optional[bool] = None) -> str: """ Normalizes the given path value and returns the result; does things like remove redundant consecutive directory separators and redundant parent paths. If the given absolute argument - is True than converts the path to an absolute path. If the given home argument is True and - if the path can reasonably be represented with a home directory indicator (i.e. "~"), then - converts it to such. If the home argument is False and path starts with the home directory - indicator then expands it to the actual (absolute) home path. If the given value is not - actually even a string (or pathlib.Path) then returns an empty string. + is True than converts the path to an absolute path. If the given expand_home argument is False + and if the path can reasonably be represented with a home directory indicator (i.e. "~"), then + converts it to such. If the expand_home argument is True and path starts with the home directory + indicator (i.e. "~") then expands it to the actual (absolute) home path of the caller. If the + given path value is not actually even a string (or pathlib.Path) then returns an empty string. """ if isinstance(value, pathlib.Path): value = str(value) @@ -95,9 +95,9 @@ def normalize_path(value: Union[str, pathlib.Path], absolute: bool = False, home return "" if not (value := value.strip()) or not (value := os.path.normpath(value)): return "" - if home is False: + if expand_home is True: value = os.path.expanduser(value) - elif (home is True) and (os.name == "posix") and value.startswith(home := HOME_DIRECTORY + os.sep): + elif (expand_home is False) and (os.name == "posix") and value.startswith(home := HOME_DIRECTORY + os.sep): value = "~/" + value[len(home):] if absolute is True: value = os.path.abspath(value) diff --git a/pyproject.toml b/pyproject.toml index a9e289f9b..fc4e8d053 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "dcicutils" -version = "8.8.4.1b22" # TODO: To become 8.8.5 +version = "8.8.4.1b23" # TODO: To become 8.8.5 description = "Utility package for interacting with the 4DN Data Portal and other 4DN resources" authors = ["4DN-DCIC Team "] license = "MIT" diff --git a/test/test_file_utils.py b/test/test_file_utils.py index d27477469..d1f618a09 100644 --- a/test/test_file_utils.py +++ b/test/test_file_utils.py @@ -1,7 +1,22 @@ +import importlib +import pytest import os -from dcicutils.file_utils import search_for_file +from dcicutils.file_utils import normalize_path, search_for_file from dcicutils.tmpfile_utils import temporary_directory +HOME_DIRECTORY = "/Some/HomeDirectory" +CURRENT_DIRECTORY = os.path.abspath(os.path.curdir) + + +@pytest.fixture(autouse=True) +def monkey_path_home_directory(monkeypatch): + monkeypatch.setenv("HOME", HOME_DIRECTORY) + file_utils_module = importlib.import_module("dcicutils.file_utils") + save_home_directory = file_utils_module.HOME_DIRECTORY + file_utils_module.HOME_DIRECTORY = HOME_DIRECTORY + yield + file_utils_module = save_home_directory + def test_search_for_file(): @@ -38,3 +53,29 @@ def test_search_for_file(): assert (set(search_for_file(filename, location=topdirname, recursive=True)) == set([filepath, subfilepath])) assert search_for_file(filename, location=topdirname, recursive=False) == [] + + +def test_normalize_path(): + assert os.environ["HOME"] == HOME_DIRECTORY + assert normalize_path(None) == "" + assert normalize_path("") == "" + assert normalize_path(123) == "" + assert normalize_path("///") == "/" + assert normalize_path(".") == "." + assert normalize_path(".//") == "." + assert normalize_path("/") == "/" + assert normalize_path("/.") == "/" + assert normalize_path("./abc") == "abc" + assert normalize_path("./abc", absolute=True) == f"{CURRENT_DIRECTORY}/abc" + assert normalize_path("/abc/def") == "/abc/def" + assert normalize_path("/abc/def/") == "/abc/def" + assert normalize_path("/abc///def") == "/abc/def" + assert normalize_path("///abc///def") == "/abc/def" + assert normalize_path("~///Ghi//Jkl//") == "~/Ghi/Jkl" + assert normalize_path("~///Ghi//Jkl/", expand_home=False, absolute=False) == "~/Ghi/Jkl" + assert normalize_path("~///Ghi//Jkl/", absolute=True) == f"{CURRENT_DIRECTORY}/~/Ghi/Jkl" + assert normalize_path("~///Ghi//Jkl/", expand_home=True, absolute=True) == f"{HOME_DIRECTORY}/Ghi/Jkl" + assert normalize_path("~///Ghi//Jkl/", expand_home=True, absolute=False) == f"{HOME_DIRECTORY}/Ghi/Jkl" + assert normalize_path("~///Ghi//Jkl/", expand_home=False, absolute=True) == f"{CURRENT_DIRECTORY}/~/Ghi/Jkl" + assert normalize_path("~///Ghi//Jkl/", expand_home=True) == f"{HOME_DIRECTORY}/Ghi/Jkl" + assert normalize_path(f"{HOME_DIRECTORY}/Ghi//Jkl/", expand_home=False) == "~/Ghi/Jkl"