diff options
author | Seth Morton <seth.m.morton@gmail.com> | 2021-12-14 09:18:10 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-14 09:18:10 -0800 |
commit | 0fd9c6d529cfe962b9591fc35ca092187ce7fef5 (patch) | |
tree | f4fba17305131e701f8813be9107889f3b93e996 | |
parent | e76935512110ab03b06ec173273217438a450fb7 (diff) | |
parent | e899cfea82fd5af25901e3af960f5495f8962eec (diff) | |
download | natsort-0fd9c6d529cfe962b9591fc35ca092187ce7fef5.tar.gz |
Merge pull request #143 from SethMMorton/path-splitting-does-not-handle-dot-properly
Path splitting does not handle dot properly
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | natsort/utils.py | 6 | ||||
-rw-r--r-- | tests/test_utils.py | 8 |
3 files changed, 16 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 0077605..159b16c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ Unreleased --- +### Fixed +- Bug where sorting paths fail if one of the paths is '.'. + [8.0.1] - 2021-12-10 --- 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)) |