diff options
author | Seth Morton <seth.m.morton@gmail.com> | 2021-12-14 09:01:09 -0800 |
---|---|---|
committer | Seth Morton <seth.m.morton@gmail.com> | 2021-12-14 09:03:14 -0800 |
commit | e1eb6fb7bc922f436d2e2c066d57a822e4ba6dc1 (patch) | |
tree | bf13f45f10830a7cf4599ab10384992d018ac665 | |
parent | e76935512110ab03b06ec173273217438a450fb7 (diff) | |
download | natsort-e1eb6fb7bc922f436d2e2c066d57a822e4ba6dc1.tar.gz |
Add handling for '.' when splitting paths
pathlib.Path(".").parts returns an empty tuple. This is unexpected, and
caused a tuple unpacking statement to fail. The solution is to catch the
ValueError from tuple unpacking and manually construct the return
values.
-rw-r--r-- | natsort/utils.py | 6 | ||||
-rw-r--r-- | tests/test_utils.py | 8 |
2 files changed, 13 insertions, 1 deletions
diff --git a/natsort/utils.py b/natsort/utils.py index c9448b4..8d56b06 100644 --- a/natsort/utils.py +++ b/natsort/utils.py @@ -887,7 +887,11 @@ def path_splitter( s = PurePath(s) # Split the path into parts. - *path_parts, base = s.parts + try: + *path_parts, base = s.parts + except ValueError: + path_parts = [] + base = str(s) # Now, split off the file extensions until we reach a decimal number at # the beginning of the suffix or there are no more extensions. diff --git a/tests/test_utils.py b/tests/test_utils.py index 38df303..bb229b9 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -141,6 +141,14 @@ def test_path_splitter_splits_path_string_by_sep_example() -> None: assert tuple(utils.path_splitter(pathlib.Path(given))) == tuple(expected) +@pytest.mark.parametrize("given", [".", "./", "./././", ".\\"]) +def test_path_splitter_handles_dot_properly(given: str) -> None: + # https://github.com/SethMMorton/natsort/issues/142 + expected = (os.path.normpath(given),) + assert tuple(utils.path_splitter(given)) == expected + assert tuple(utils.path_splitter(pathlib.Path(given))) == expected + + @given(lists(sampled_from(string.ascii_letters), min_size=2).filter(all)) def test_path_splitter_splits_path_string_by_sep(x: List[str]) -> None: z = str(pathlib.Path(*x)) |