🐛 | Fix SonarQube dead code and improve coverage
All checks were successful
SonarQube Code Quality Scan / SonarQube Scan (push) Successful in 1m43s
All checks were successful
SonarQube Code Quality Scan / SonarQube Scan (push) Successful in 1m43s
This commit is contained in:
@@ -116,7 +116,7 @@ class DiscordRPC:
|
|||||||
log.warning("Discord not running – RPC unavailable")
|
log.warning("Discord not running – RPC unavailable")
|
||||||
self._connected = False
|
self._connected = False
|
||||||
return False
|
return False
|
||||||
except (OSError, RuntimeError, DiscordNotFound) as exc:
|
except (OSError, RuntimeError) as exc:
|
||||||
log.debug("Discord RPC connect failed: %s", exc)
|
log.debug("Discord RPC connect failed: %s", exc)
|
||||||
self._connected = False
|
self._connected = False
|
||||||
return False
|
return False
|
||||||
|
|||||||
2
gui.py
2
gui.py
@@ -168,7 +168,7 @@ def _load_thumbnail(video_id: str, thumb_url: str,
|
|||||||
nam = _get_thumbnail_nam()
|
nam = _get_thumbnail_nam()
|
||||||
reply = nam.get(QNetworkRequest(QUrl(thumb_url)))
|
reply = nam.get(QNetworkRequest(QUrl(thumb_url)))
|
||||||
|
|
||||||
def _on_downloaded(reply=reply, nam=nam, cache_path=cache_path,
|
def _on_downloaded(reply=reply, cache_path=cache_path,
|
||||||
art_label=art_label, size=size,
|
art_label=art_label, size=size,
|
||||||
style_pass=style_pass, style_fail=style_fail,
|
style_pass=style_pass, style_fail=style_fail,
|
||||||
video_id=video_id):
|
video_id=video_id):
|
||||||
|
|||||||
@@ -161,11 +161,11 @@ class TestDiscordRPC:
|
|||||||
# If we get here without exception the test passes
|
# If we get here without exception the test passes
|
||||||
|
|
||||||
@mock.patch("discord_rpc.Presence")
|
@mock.patch("discord_rpc.Presence")
|
||||||
def test_connect_failure_graceful(self, mock_presence_cls):
|
def test_connect_discord_not_found(self, mock_presence_cls):
|
||||||
"""When Discord is not running, connect failure is handled gracefully."""
|
"""When Discord is not running, DiscordNotFound is caught (covers lines 116-118)."""
|
||||||
from discord_rpc import DiscordRPC
|
from discord_rpc import DiscordRPC, DiscordNotFound
|
||||||
mock_instance = mock.MagicMock()
|
mock_instance = mock.MagicMock()
|
||||||
mock_instance.connect.side_effect = Exception("Discord not found")
|
mock_instance.connect.side_effect = DiscordNotFound()
|
||||||
mock_presence_cls.return_value = mock_instance
|
mock_presence_cls.return_value = mock_instance
|
||||||
|
|
||||||
drpc = DiscordRPC(client_id="test")
|
drpc = DiscordRPC(client_id="test")
|
||||||
@@ -231,3 +231,49 @@ class TestDiscordRPC:
|
|||||||
# Should not crash
|
# Should not crash
|
||||||
drpc._send_presence()
|
drpc._send_presence()
|
||||||
assert True
|
assert True
|
||||||
|
|
||||||
|
def test_connect_oserror_handled(self):
|
||||||
|
"""_connect handles OSError gracefully (covers lines 119-122)."""
|
||||||
|
with mock.patch("discord_rpc.Presence") as mock_presence_cls:
|
||||||
|
mock_instance = mock.MagicMock()
|
||||||
|
mock_instance.connect.side_effect = OSError("Connection refused")
|
||||||
|
mock_presence_cls.return_value = mock_instance
|
||||||
|
|
||||||
|
from discord_rpc import DiscordRPC
|
||||||
|
drpc = DiscordRPC(client_id="test")
|
||||||
|
result = drpc._connect()
|
||||||
|
assert result is False
|
||||||
|
assert drpc._connected is False
|
||||||
|
|
||||||
|
def test_connect_runtimeerror_handled(self):
|
||||||
|
"""_connect handles RuntimeError gracefully (covers lines 119-122)."""
|
||||||
|
with mock.patch("discord_rpc.Presence") as mock_presence_cls:
|
||||||
|
mock_instance = mock.MagicMock()
|
||||||
|
mock_instance.connect.side_effect = RuntimeError("pipe error")
|
||||||
|
mock_presence_cls.return_value = mock_instance
|
||||||
|
|
||||||
|
from discord_rpc import DiscordRPC
|
||||||
|
drpc = DiscordRPC(client_id="test")
|
||||||
|
result = drpc._connect()
|
||||||
|
assert result is False
|
||||||
|
assert drpc._connected is False
|
||||||
|
|
||||||
|
def test_run_reconnects_on_failure(self):
|
||||||
|
"""_run loop retries connection when _connect fails (covers lines 174-175)."""
|
||||||
|
with mock.patch("discord_rpc.Presence") as mock_presence_cls:
|
||||||
|
mock_instance = mock.MagicMock()
|
||||||
|
mock_instance.connect.side_effect = OSError("no discord")
|
||||||
|
mock_presence_cls.return_value = mock_instance
|
||||||
|
|
||||||
|
from discord_rpc import DiscordRPC
|
||||||
|
drpc = DiscordRPC(client_id="test")
|
||||||
|
# Start _run in a background thread so we can signal stop
|
||||||
|
# from the main thread while _run blocks on wait().
|
||||||
|
import threading
|
||||||
|
t = threading.Thread(target=drpc._run, daemon=True)
|
||||||
|
t.start()
|
||||||
|
time.sleep(0.1) # let _connect fail and enter wait(10)
|
||||||
|
drpc._stop_event.set() # unblock wait() so _run can exit
|
||||||
|
t.join(timeout=2)
|
||||||
|
assert drpc._connected is False
|
||||||
|
assert drpc._rpc is None
|
||||||
|
|||||||
Reference in New Issue
Block a user