diff --git a/src/holoscan_cli/common/argparse_types.py b/src/holoscan_cli/common/argparse_types.py index 5de293c..276595e 100644 --- a/src/holoscan_cli/common/argparse_types.py +++ b/src/holoscan_cli/common/argparse_types.py @@ -84,10 +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(file_path): + if file_path.is_dir(): + if any(os.scandir(file_path)): return file_path - raise argparse.ArgumentTypeError(f"No such file/folder: '{file_path}'") + raise argparse.ArgumentTypeError(f"Directory is empty: '{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 53a019c..5fcb879 100644 --- a/tests/unit/common/test_argparse_types.py +++ b/tests/unit/common/test_argparse_types.py @@ -95,15 +95,15 @@ def test_existing_path_not_exists(self, monkeypatch): 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: []) + monkeypatch.setattr(pathlib.Path, "is_dir", lambda x: True) + monkeypatch.setattr(os, "scandir", 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"]) + monkeypatch.setattr(pathlib.Path, "is_dir", lambda x: True) + monkeypatch.setattr(os, "scandir", lambda x: ["file"]) result = valid_existing_path("this/is/some/path") assert type(result) is PosixPath