Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion puremagic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
10 changes: 10 additions & 0 deletions test/test_common_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down