diff --git a/puremagic/main.py b/puremagic/main.py index e2f6419..57f4816 100644 --- a/puremagic/main.py +++ b/puremagic/main.py @@ -270,7 +270,7 @@ def ext_from_filename(filename: os.PathLike | str) -> str: :return: the extension off the end (empty string if it can't find one) """ try: - base, ext = str(filename).lower().rsplit(".", 1) + base, ext = os.path.basename(filename).lower().rsplit(".", 1) except ValueError: return "" ext = f".{ext}" diff --git a/test/test_common_extensions.py b/test/test_common_extensions.py index 0294b2b..f0d2154 100644 --- a/test/test_common_extensions.py +++ b/test/test_common_extensions.py @@ -202,6 +202,16 @@ def test_ext(): assert ext == ".tar.bz2", ext +@pytest.mark.parametrize("path_type", [str, Path]) +@pytest.mark.parametrize( + "filename, expected", + [("README", ""), ("report.PDF", ".pdf"), ("archive.tar.bz2", ".tar.bz2"), (".profile", ".profile")], +) +def test_ext_ignores_parent_directory_dots(path_type, filename, expected): + filename = path_type(os.path.join("directory.with.dots", filename)) + assert puremagic.ext_from_filename(filename) == expected + + def test_cmd_options(): """Test CLI options""" try: