From bd60bef35f5cb9198db629dfbbf0983d7d422468 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Wed, 7 Jul 2021 11:23:10 +0300 Subject: [PATCH] Change the expand_path method in file_utils.py to throw an error if an empty file path is provided instead of expanding it to current working directory --- monkey/common/utils/file_utils.py | 6 ++++++ .../unit_tests/common/utils/test_common_file_utils.py | 9 ++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/monkey/common/utils/file_utils.py b/monkey/common/utils/file_utils.py index 225fb8732..6110cd020 100644 --- a/monkey/common/utils/file_utils.py +++ b/monkey/common/utils/file_utils.py @@ -1,5 +1,11 @@ import os +class InvalidPath(Exception): + pass + + def expand_path(path: str) -> str: + if not path: + raise InvalidPath("Empty path provided") return os.path.expandvars(os.path.expanduser(path)) diff --git a/monkey/tests/unit_tests/common/utils/test_common_file_utils.py b/monkey/tests/unit_tests/common/utils/test_common_file_utils.py index 226a403b8..b67341cfe 100644 --- a/monkey/tests/unit_tests/common/utils/test_common_file_utils.py +++ b/monkey/tests/unit_tests/common/utils/test_common_file_utils.py @@ -1,6 +1,8 @@ import os -from common.utils.file_utils import expand_path +import pytest + +from common.utils.file_utils import InvalidPath, expand_path def test_expand_user(patched_home_env): @@ -15,3 +17,8 @@ def test_expand_vars(patched_home_env): expected_path = os.path.join(patched_home_env, "test") assert expand_path(input_path) == expected_path + + +def test_expand_path__empty_path_provided(): + with pytest.raises(InvalidPath): + expand_path("")