Skip to content

Commit 1487675

Browse files
fix: ensure Key is defined in engine.py and use create=True in mock patches for CI compatibility
1 parent 32c046b commit 1487675

2 files changed

Lines changed: 10 additions & 11 deletions

File tree

src/core/engine.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
try:
22
from pynput.keyboard import Key, Listener
3-
except ImportError:
4-
msg = (
5-
"[ERROR] 'pynput' no está instalado. "
6-
"Instálalo con 'pip install pynput'"
7-
)
8-
print(msg)
3+
except (ImportError, Exception):
4+
# En entornos sin GUI (como algunos CI), pynput puede fallar al importar
5+
Key = None
96
Listener = None
107

118

@@ -37,13 +34,14 @@ def on_press(self, key):
3734
print(f"[Captured] {k}")
3835

3936
def on_release(self, key):
40-
if key == Key.esc:
37+
if not Key or key == Key.esc:
4138
# Detener el listener con la tecla ESC
42-
print("[*] Deteniendo captura (ESC presionado)...")
39+
print("[*] Deteniendo captura (ESC o entorno sin soporte)...")
4340
return False
4441

4542
def start(self):
4643
if not Listener:
44+
print("[!] Listener no disponible en este entorno.")
4745
return
4846

4947
msg = f"[*] Iniciando escucha... Logs en: {self.output_file}"

tests/test_core.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
# Mocking Listener and Key from pynput
88
@pytest.fixture
99
def mock_pynput():
10-
with patch('src.core.engine.Listener') as mock_listener, \
11-
patch('src.core.engine.Key') as mock_key:
10+
# Usamos create=True por si pynput no se pudo importar en el entorno de test
11+
with patch('src.core.engine.Listener', create=True) as mock_listener, \
12+
patch('src.core.engine.Key', create=True) as mock_key:
1213
yield mock_listener, mock_key
1314

1415

@@ -43,7 +44,7 @@ def test_engine_on_release_esc(mock_pynput):
4344
assert result is False
4445

4546

46-
@patch('src.core.engine.Listener')
47+
@patch('src.core.engine.Listener', create=True)
4748
def test_engine_start_calls_listener(mock_listener):
4849
"""Verifica que engine.start() inicie el Listener."""
4950
engine = RealEngine()

0 commit comments

Comments
 (0)