From 2406687ea0153b9c3c054f466e4c7a2013944542 Mon Sep 17 00:00:00 2001 From: Victor Chang Date: Mon, 13 Jan 2025 19:06:13 -0800 Subject: [PATCH] Prompt when input directory is empty Signed-off-by: Victor Chang --- pyproject.toml | 2 +- src/holoscan_cli/common/argparse_types.py | 4 ++++ tests/unit/common/test_argparse_types.py | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index fbd76b8..160c8f0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -56,11 +56,11 @@ classifiers = [ "Topic :: Software Development :: Libraries :: Python Modules", "Typing :: Typed", ] -version = "0.0.0" # [tool.poetry.build] # script = "build.py" # generate-setup-file = false +version = "0.0.0" [tool.poetry.dependencies] python = ">=3.9,<3.13" diff --git a/src/holoscan_cli/common/argparse_types.py b/src/holoscan_cli/common/argparse_types.py index f81398f..af4eed3 100644 --- a/src/holoscan_cli/common/argparse_types.py +++ b/src/holoscan_cli/common/argparse_types.py @@ -84,6 +84,10 @@ def valid_existing_path(path: str) -> Path: path = os.path.expanduser(path) file_path = Path(path).absolute() if file_path.exists(): + if os.path.isdir(file_path): + if os.listdir(path): + return file_path + raise argparse.ArgumentTypeError(f"No such file/folder: '{file_path}'") return file_path raise argparse.ArgumentTypeError(f"No such file/folder: '{file_path}'") diff --git a/tests/unit/common/test_argparse_types.py b/tests/unit/common/test_argparse_types.py index 21c95d6..53a019c 100644 --- a/tests/unit/common/test_argparse_types.py +++ b/tests/unit/common/test_argparse_types.py @@ -93,6 +93,20 @@ def test_existing_path_not_exists(self, monkeypatch): with pytest.raises(argparse.ArgumentTypeError): valid_existing_path("this/is/some/path") + def test_empty_directory(self, monkeypatch): + monkeypatch.setattr(pathlib.Path, "exists", lambda x: True) + monkeypatch.setattr(os.path, "isdir", lambda x: True) + monkeypatch.setattr(os, "listdir", lambda x: []) + with pytest.raises(argparse.ArgumentTypeError): + valid_existing_path("this/is/some/path") + + def test_non_empty_directory(self, monkeypatch): + monkeypatch.setattr(pathlib.Path, "exists", lambda x: True) + monkeypatch.setattr(os.path, "isdir", lambda x: True) + monkeypatch.setattr(os, "listdir", lambda x: ["/a"]) + result = valid_existing_path("this/is/some/path") + assert type(result) is PosixPath + class TestValidPlatforms: @pytest.mark.parametrize(