Skip to content

Commit 205d3b3

Browse files
committed
FIXED! Tests works, and LSBLK is replaced w/Python native tooling1
Please enter the commit message for your changes. Lines starting with '' will be ignored, and an empty message aborts the commit. On branch less-deps-feature-win-tweaks Your branch is ahead of 'origin/less-deps-feature-win-tweaks' by 1 commit. (use "git push" to publish your local commits) Changes to be committed: modified: tests/test_get_usb_info_and_detect_windows_fixes.py
1 parent 732856d commit 205d3b3

1 file changed

Lines changed: 23 additions & 31 deletions

File tree

tests/test_get_usb_info_and_detect_windows_fixes.py

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,33 @@
1313
from lufus.drives.get_usb_info import get_usb_info
1414
import lufus.writing.windows.detect as dw_module
1515
from lufus.writing.windows.detect import _label_is_windows, _read_iso_label, is_windows_iso
16+
from unittest.mock import MagicMock
17+
import os as real_os
1618

1719

1820
def _fake_partitions(mount, device):
1921
return lambda all=False: [SimpleNamespace(mountpoint=mount, device=device)]
2022

2123

22-
def _fake_check_output(size="1000000000", label="MY_USB"):
23-
def impl(cmd, **kwargs):
24-
if "SIZE" in cmd:
25-
return size + "\n"
26-
return label + "\n"
24+
def _mock_os_stat_and_pyudev(monkeypatch, size="1000000000", label="MY_USB"):
25+
# Mock os.stat safely
26+
os_stat_orig = gui_module.os.stat
27+
def mock_os_stat(p):
28+
if str(p).startswith("/dev/"):
29+
m = MagicMock()
30+
m.st_rdev = 1234
31+
return m
32+
return os_stat_orig(p)
33+
monkeypatch.setattr(gui_module.os, "stat", mock_os_stat)
2734

28-
return impl
35+
# Mock pyudev
36+
mock_context = MagicMock()
37+
mock_device = MagicMock()
38+
# size in 512-byte sectors
39+
mock_device.attributes = {"size": str(int(size) // 512)}
40+
mock_device.get.return_value = label
41+
monkeypatch.setattr(gui_module.pyudev, "Context", lambda: mock_context)
42+
monkeypatch.setattr(gui_module.pyudev.Devices, "from_device_number", lambda ctx, type, num: mock_device)
2943

3044

3145
class Testget_usb_infoNormalisedMountPath:
@@ -36,14 +50,14 @@ class Testget_usb_infoNormalisedMountPath:
3650

3751
def test_trailing_slash_is_stripped(self, monkeypatch):
3852
monkeypatch.setattr(gui_module.psutil, "disk_partitions", _fake_partitions("/media/u/USB/", "/dev/sdb1"))
39-
monkeypatch.setattr(gui_module.subprocess, "check_output", _fake_check_output())
53+
_mock_os_stat_and_pyudev(monkeypatch)
4054
result = get_usb_info("/media/u/USB/")
4155
assert result["mount_path"] == "/media/u/USB"
4256

4357
def test_normalised_path_matches_normpath(self, monkeypatch, tmp_path):
4458
mount = str(tmp_path)
4559
monkeypatch.setattr(gui_module.psutil, "disk_partitions", _fake_partitions(mount, "/dev/sdc1"))
46-
monkeypatch.setattr(gui_module.subprocess, "check_output", _fake_check_output())
60+
_mock_os_stat_and_pyudev(monkeypatch)
4761
result = get_usb_info(mount)
4862
import os
4963

@@ -67,28 +81,6 @@ def fake_dp(all=False):
6781
assert calls.get("all") is True
6882

6983

70-
class Testget_usb_infoTimeoutExpired:
71-
"""TimeoutExpired was previously swallowed by the broad Exception handler
72-
with a generic message. It must now be caught explicitly.
73-
"""
74-
75-
def test_returns_empty_dict_on_timeout(self, monkeypatch):
76-
monkeypatch.setattr(gui_module.psutil, "disk_partitions", _fake_partitions("/media/u/USB", "/dev/sdb1"))
77-
78-
def raise_timeout(*args, **kwargs):
79-
raise subprocess.TimeoutExpired(cmd="lsblk", timeout=5)
80-
81-
monkeypatch.setattr(gui_module.subprocess, "check_output", raise_timeout)
82-
result = get_usb_info("/media/u/USB")
83-
assert result is None
84-
85-
def test_timeout_handler_is_explicit(self):
86-
import inspect
87-
88-
src = inspect.getsource(get_usb_info)
89-
assert "TimeoutExpired" in src
90-
91-
9284
class Testget_usb_infoForElse:
9385
"""When no partition matches the mount path, get_usb_info must return {}."""
9486

@@ -100,7 +92,7 @@ def test_returns_empty_when_no_match(self, monkeypatch):
10092

10193
class TestLabelIsWindowsDeadBranch:
10294
"""'or label.startswith("WINDOWS")' was dead code — every "WINDOWS…"
103-
string already starts with "WIN". The redundant check must be gone.
95+
string already starts with "WIN". The redundant check must be gone. Idk why I don't like that, but eh, it works...
10496
"""
10597

10698
def test_windows_prefix_still_detected(self):

0 commit comments

Comments
 (0)