From bf938b838ec4e616447824d52d7861e5d955ba0c Mon Sep 17 00:00:00 2001 From: Microchip Date: Sat, 26 Sep 2026 14:45:46 -0400 Subject: [PATCH] Update unit tests for testing linux behavior --- src/platform_ops.py | 2 +- src/tests/test_torrent.py | 61 ++++++++++++++++++++++++++++++++++++++- src/torrent.py | 9 ++++-- 3 files changed, 67 insertions(+), 5 deletions(-) diff --git a/src/platform_ops.py b/src/platform_ops.py index e98f3ae..b62daba 100644 --- a/src/platform_ops.py +++ b/src/platform_ops.py @@ -64,7 +64,7 @@ def open_directory(path: str) -> None: raise RuntimeError("xdg-open is required to open directories") subprocess.Popen([opener, path]) -# NOTE: Creates link to windows junction for torrent ? +# NOTE: If windows, creates ntfs junction , else , creates symlink def create_directory_link(link: str, target: str) -> None: if sys.platform == "win32": flags = getattr(subprocess, "CREATE_NO_WINDOW", 0) diff --git a/src/tests/test_torrent.py b/src/tests/test_torrent.py index 921b815..a86d5d6 100644 --- a/src/tests/test_torrent.py +++ b/src/tests/test_torrent.py @@ -45,7 +45,6 @@ def test_ensure_aria2c_returns_windows_path(monkeypatch): assert torrent.ensure_aria2c() == expected - def test_ensure_aria2c_installs_when_not_found(monkeypatch): monkeypatch.setattr( torrent, @@ -53,6 +52,12 @@ def test_ensure_aria2c_installs_when_not_found(monkeypatch): Mock(return_value=None), ) + monkeypatch.setattr( + torrent.sys, + "platform", + "win32", + ) + installer = Mock( return_value="/fake/aria2c.exe" ) @@ -73,6 +78,60 @@ def test_ensure_aria2c_installs_when_not_found(monkeypatch): log_fn=torrent.log, ) +def test_ensure_aria2c_linux_returns_installed_binary(monkeypatch): + monkeypatch.setattr( + torrent.sys, + "platform", + "linux", + ) + + monkeypatch.setattr( + torrent, + "find_aria2c", + Mock(return_value="/usr/bin/aria2c"), + ) + + installer = Mock() + + monkeypatch.setattr( + torrent, + "install_aria2c_windows", + installer, + ) + + result = torrent.ensure_aria2c() + + assert result == "/usr/bin/aria2c" + installer.assert_not_called() + +def test_ensure_aria2c_linux_raises_when_not_installed(monkeypatch): + monkeypatch.setattr( + torrent.sys, + "platform", + "linux", + ) + + monkeypatch.setattr( + torrent, + "find_aria2c", + Mock(return_value=None), + ) + + installer = Mock() + + monkeypatch.setattr( + torrent, + "install_aria2c_windows", + installer, + ) + + with pytest.raises( + RuntimeError, + match="aria2c required but not installed", + ): + torrent.ensure_aria2c() + + installer.assert_not_called() def test_ensure_aria2c_passes_custom_logger(monkeypatch): monkeypatch.setattr( diff --git a/src/torrent.py b/src/torrent.py index 8d0a48f..1e119ab 100644 --- a/src/torrent.py +++ b/src/torrent.py @@ -6,6 +6,7 @@ import re import subprocess import threading import urllib.request +import sys from functools import cache from .app_log import log @@ -33,12 +34,14 @@ def ensure_aria2c(log_fn=log) -> str: if ARIA2C_PATH: return ARIA2C_PATH - - - return install_aria2c_windows( APP_DATA_DIR=APP_DATA_DIR, + + if sys.platform.startswith == "win": + return install_aria2c_windows( APP_DATA_DIR=APP_DATA_DIR, ALLOWED_DOWNLOAD_HOSTS=ALLOWED_DOWNLOAD_HOSTS, log_fn=log_fn) + raise RuntimeError("aria2c required but not installed") + def _bdecode(buf: bytes, pos: int = 0): """Minimal bencode decoder → (value, next_pos). Strings stay bytes."""