Only propegate ValueError if the icon can't be loaded.

This commit is contained in:
Russell Keith-Magee 2024-05-07 13:09:53 +08:00
parent e8be59a026
commit 7368392232
No known key found for this signature in database
GPG key ID: 3D2DAB6A37BB5BC3
4 changed files with 11 additions and 39 deletions

View file

@ -1 +1 @@
If an icon file exists but cannot be loaded, or an application icon cannot be found, Toga will fall back to a default icon.
If an application icon cannot be found, Toga will now fall back to a default icon.

View file

@ -23,6 +23,10 @@ class Icon:
bundle_icon.stem,
ofType=bundle_icon.suffix,
)
# If the icon file doesn't exist, raise the problem as FileNotFoundError
if not Path(path).is_file():
raise FileNotFoundError()
self.path = None
else:
self.path = path

View file

@ -129,8 +129,8 @@ class Icon:
)
self._impl = self.factory.Icon(interface=self, path=full_path)
except (FileNotFoundError, ValueError):
# Icon path couldn't be loaded. If the path is the sentinel for the app
except FileNotFoundError:
# Icon path couldn't be found. If the path is the sentinel for the app
# icon, and this isn't running as a script, fall back to the application
# binary
if path is _APP_ICON:
@ -142,7 +142,7 @@ class Icon:
try:
# Use the application binary's icon
self._impl = self.factory.Icon(interface=self, path=None)
except (FileNotFoundError, ValueError):
except FileNotFoundError:
# Can't find the application binary's icon.
print(
"WARNING: Can't find app icon; falling back to default icon"

View file

@ -135,7 +135,7 @@ def test_create_fallback_missing(monkeypatch, app, capsys):
def test_create_fallback_unloadable(monkeypatch, app, capsys):
"""If a resource exists, but can't be loaded, a fallback icon is used."""
"""If a resource exists, but can't be loaded, an error is raised."""
# Prime the dummy so the app icon cannot be loaded
monkeypatch.setattr(
DummyIcon,
@ -143,16 +143,8 @@ def test_create_fallback_unloadable(monkeypatch, app, capsys):
ValueError("Icon could not be loaded"),
)
icon = toga.Icon("resources/sample")
assert icon._impl is not None
assert icon._impl.interface == toga.Icon.DEFAULT_ICON
# A warning was printed; allow for windows separators
assert (
"WARNING: Can't find icon resources/sample"
in capsys.readouterr().out.replace("\\", "/")
)
with pytest.raises(ValueError):
toga.Icon("resources/sample")
def test_create_fallback_variants(monkeypatch, app, capsys):
@ -223,30 +215,6 @@ def test_create_app_icon_non_script(monkeypatch, app, capsys):
assert capsys.readouterr().out == ""
def test_create_app_icon_unloadable_non_script(monkeypatch, app, capsys):
"""If the icon from binary executable cannot be loaded, the app icon is reset to the default"""
# Prime the dummy so the app icon cannot be loaded
monkeypatch.setattr(
DummyIcon,
"ICON_FAILURE",
ValueError("Icon could not be loaded"),
)
# Patch sys.executable so the test looks like it's running as a packaged binary
monkeypatch.setattr(sys, "executable", "/path/to/App")
# Load the app default icon
icon = toga.Icon(_APP_ICON)
assert isinstance(icon, toga.Icon)
# App icon path reports as `resources/<app_name>`; impl is the default toga icon
assert icon.path == Path("resources/icons")
assert icon._impl.path == Path(TOGA_RESOURCES / "toga.png")
# A warning was printed; allow for windows separators
assert "WARNING: Can't find app icon" in capsys.readouterr().out.replace("\\", "/")
def test_create_app_icon_missing_non_script(monkeypatch, app, capsys):
"""If the icon from binary executable cannot be found, the app icon is reset to the default"""
# Prime the dummy so the app icon cannot be found