Skip to content
Merged
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
21 changes: 12 additions & 9 deletions pyfiglet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,19 @@ def isValidFont(cls, font:str):
if not font.endswith(('.flf', '.tlf')):
return False
f = None
if os.path.isfile(font):
f = open(font, 'rb')
else:
for location in FONT_DIRECTORIES:
full_file = os.path.join(location, font)
if os.path.isfile(full_file):
f = open(full_file, 'rb')
break
try:
if os.path.isfile(font):
f = open(font, 'rb')
else:
f = importlib.resources.files('pyfiglet.fonts').joinpath(font).open('rb')
for location in FONT_DIRECTORIES:
full_file = os.path.join(location, font)
if os.path.isfile(full_file):
f = open(full_file, 'rb')
break
else:
f = importlib.resources.files('pyfiglet.fonts').joinpath(font).open('rb')
except FileNotFoundError:
return False

if zipfile.is_zipfile(f):
# If we have a match, the ZIP file spec says we should just read the first file in the ZIP.
Expand Down
9 changes: 9 additions & 0 deletions pyfiglet/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ def test_font_directories_are_searched(test_font_dir, monkeypatch):
assert "0000000000" in rendered


def test_is_valid_font_missing():
# A .flf/.tlf-named font that does not exist must be reported invalid,
# not raise FileNotFoundError. See #156.
assert pyfiglet.FigletFont.isValidFont("does_not_exist.flf") is False
assert pyfiglet.FigletFont.isValidFont("does_not_exist.tlf") is False
# A real bundled font is still recognized as valid.
assert pyfiglet.FigletFont.isValidFont("standard.flf")


# normalize is just strip with padding
def test_normalize():
command = "pyfiglet -f slant -n 0"
Expand Down
Loading