From 907fa53806965ab023c3bcc15fe006cd5b0d6fa0 Mon Sep 17 00:00:00 2001 From: brightonanc Date: Sun, 3 Sep 2023 13:05:16 -0400 Subject: [PATCH] Fixed broken unit tests, fixed typos, and general cleanup for linting and consistency with main branch Merges needed were: * qutebrowser/keyinput/basekeyparser.py for the new count precaution (commit 51aa7ab) --- qutebrowser/keyinput/basekeyparser.py | 17 ++++++++++------- qutebrowser/keyinput/modeman.py | 6 +++--- qutebrowser/keyinput/modeparsers.py | 4 ++-- tests/unit/keyinput/conftest.py | 3 +-- tests/unit/keyinput/test_basekeyparser.py | 6 +++--- tests/unit/keyinput/test_modeman.py | 18 +++++++++++++----- tests/unit/keyinput/test_modeparsers.py | 10 ++++++---- 7 files changed, 38 insertions(+), 26 deletions(-) diff --git a/qutebrowser/keyinput/basekeyparser.py b/qutebrowser/keyinput/basekeyparser.py index 7b3c7f107..de4c71cbb 100644 --- a/qutebrowser/keyinput/basekeyparser.py +++ b/qutebrowser/keyinput/basekeyparser.py @@ -11,7 +11,7 @@ import traceback from typing import Optional, List from collections.abc import Mapping, MutableMapping, Sequence -from qutebrowser.qt.core import QObject, pyqtSignal, pyqtSlot +from qutebrowser.qt.core import QObject, pyqtSignal from qutebrowser.qt.gui import QKeySequence, QKeyEvent from qutebrowser.config import config @@ -287,7 +287,7 @@ class BaseKeyParser(QObject): return True return False - def handle(self, e: QKeyEvent, *, + def handle(self, e: QKeyEvent, *, # noqa: C901 dry_run: bool = False) -> QKeySequence.SequenceMatch: """Handle a new keypress. @@ -378,7 +378,7 @@ class BaseKeyParser(QObject): "key in the sequence and retry.".format( result.sequence)) # Forward all the leading count keys - while count_keyposs and (0 == count_keyposs[0]): + while count_keyposs and (count_keyposs[0] == 0): self._debug_log("Hit a queued count key ('{}'). " "Forwarding.".format(count[0])) count = count[1:] @@ -405,7 +405,11 @@ class BaseKeyParser(QObject): if dry_run: return result.match_type + self._handle_result(info, result) + return result.match_type + def _handle_result(self, info: keyutils.KeyInfo, result: MatchResult) -> None: + """Handle a final MatchResult from handle().""" # Each of the three following blocks need to emit # self.keystring_updated, either directly (as PartialMatch does) or # indirectly (as ExactMatch and NoMatch do via self.clear_keystring) @@ -414,7 +418,8 @@ class BaseKeyParser(QObject): self._debug_log("Definitive match for '{}'.".format( result.sequence)) try: - count_int = int(self._count) if self._count else None + # note: this 'count' is an int, not a str + count = int(self._count) if self._count else None flag_do_execute = True except ValueError as err: message.error(f"Failed to parse count: {err}", @@ -423,7 +428,7 @@ class BaseKeyParser(QObject): self.clear_partial_keys.emit() self.clear_keystring() if flag_do_execute: - self.execute(result.command, count_int) + self.execute(result.command, count) elif result.match_type == QKeySequence.SequenceMatch.PartialMatch: self._debug_log("No match for '{}' (added {})".format( result.sequence, info)) @@ -436,8 +441,6 @@ class BaseKeyParser(QObject): raise utils.Unreachable("Invalid match value {!r}".format( result.match_type)) - return result.match_type - @config.change_filter('bindings') def _on_config_changed(self) -> None: self._read_config() diff --git a/qutebrowser/keyinput/modeman.py b/qutebrowser/keyinput/modeman.py index b84d3d2c2..6071288e5 100644 --- a/qutebrowser/keyinput/modeman.py +++ b/qutebrowser/keyinput/modeman.py @@ -265,7 +265,7 @@ class ModeManager(QObject): def __repr__(self) -> str: return utils.get_repr(self, mode=self.mode) - def _handle_keypress(self, event: QKeyEvent, *, + def _handle_keypress(self, event: QKeyEvent, *, # noqa: C901 dry_run: bool = False) -> bool: """Handle filtering of KeyPress events. @@ -384,7 +384,7 @@ class ModeManager(QObject): # handle like matching KeyPress keyevent = keyutils.KeyEvent.from_event(event) do_log = (self.mode in self.parsers) and \ - self.parsers[self.mode].get_do_log() + self.parsers[self.mode].get_do_log() if keyevent in self._releaseevents_to_pass: if do_log: log.modes.debug("Passing release event: {}".format(keyevent)) @@ -422,7 +422,7 @@ class ModeManager(QObject): (forward_unbound_keys == 'auto' and is_non_alnum)) @pyqtSlot(usertypes.KeyMode, str) - def forward_partial_match_event(self, mode: usertypes.KeyMode, + def forward_partial_match_event(self, mode: usertypes.KeyMode, # noqa: C901 text: str = None) -> None: """Forward the oldest partial match event for a given mode. diff --git a/qutebrowser/keyinput/modeparsers.py b/qutebrowser/keyinput/modeparsers.py index fb1edd0d9..6cc787ce4 100644 --- a/qutebrowser/keyinput/modeparsers.py +++ b/qutebrowser/keyinput/modeparsers.py @@ -228,7 +228,7 @@ class HintKeyParser(basekeyparser.BaseKeyParser): "empty queue: {}".format(e)) self._partial_match_events.append( keyutils.QueuedKeyEventPair.from_event_press(e)) - self._debug_log("Staring partial timer.") + self._debug_log("Starting partial timer.") self._start_partial_timer() return result elif not had_empty_queue: @@ -278,7 +278,7 @@ class HintKeyParser(basekeyparser.BaseKeyParser): self._debug_log("Text mismatch (this is likely benign): '{}' != " "'{}'".format(text_actual, text)) e = match_event.to_events() - assert 1 == len(e) + assert len(e) == 1 self._handle_hint(e[0]) @pyqtSlot() diff --git a/tests/unit/keyinput/conftest.py b/tests/unit/keyinput/conftest.py index a5e5dbd22..2dffdf4f5 100644 --- a/tests/unit/keyinput/conftest.py +++ b/tests/unit/keyinput/conftest.py @@ -22,8 +22,7 @@ BINDINGS = {'prompt': {'': 'message-info ctrla', '': 'message-info ctrlx'}, 'normal': {'a': 'message-info a', 'ba': 'message-info ba', - 'ccc': 'message-info ccc'} -} + 'ccc': 'message-info ccc'}} MAPPINGS = { 'x': 'a', 'b': 'a', diff --git a/tests/unit/keyinput/test_basekeyparser.py b/tests/unit/keyinput/test_basekeyparser.py index a99e38163..c5c776b44 100644 --- a/tests/unit/keyinput/test_basekeyparser.py +++ b/tests/unit/keyinput/test_basekeyparser.py @@ -419,10 +419,10 @@ class TestHandle: } result = keyparser.handle( - keyutils.KeyInfo(Qt.Key_X, Qt.KeyboardModifier.NoModifier).to_event()) + keyutils.KeyInfo(Qt.Key.Key_X, Qt.KeyboardModifier.NoModifier).to_event()) assert result == QKeySequence.SequenceMatch.PartialMatch result = keyparser.handle( - keyutils.KeyInfo(Qt.Key_2, Qt.KeyboardModifier.NoModifier).to_event()) + keyutils.KeyInfo(Qt.Key.Key_2, Qt.KeyboardModifier.NoModifier).to_event()) # Check that count is not evaluated when an expansive mapping occurs. # This behavior may change in the future. assert result == QKeySequence.SequenceMatch.NoMatch @@ -452,7 +452,7 @@ class TestCount: assert not prompt_keyparser._sequence def test_count_42_invalid(self, handle_text, prompt_keyparser): - # Invalid call with ccx gets ignored + # Invalid call with cce gets ignored handle_text(prompt_keyparser, Qt.Key.Key_4, Qt.Key.Key_2, Qt.Key.Key_C, Qt.Key.Key_C, Qt.Key.Key_E) assert not prompt_keyparser.execute.called diff --git a/tests/unit/keyinput/test_modeman.py b/tests/unit/keyinput/test_modeman.py index 6a83cc530..4d50c67c7 100644 --- a/tests/unit/keyinput/test_modeman.py +++ b/tests/unit/keyinput/test_modeman.py @@ -28,6 +28,9 @@ class FakeKeyparser(QObject): self.allow_forward = True self.forward_widget_name = None + def get_do_log(self): + return True + def handle( self, evt: QKeyEvent, @@ -78,11 +81,14 @@ class FakeKeyparserWithTimeout(QObject): self.forward_widget_name = None self.fake_clear_keystring_called = False + def get_do_log(self): + return True + def handle(self, evt, *, dry_run=False): txt = str(keyutils.KeyInfo.from_event(evt)) - if 'a' == txt: + if txt == 'a': return QKeySequence.SequenceMatch.ExactMatch - elif 'b' == txt: + elif txt == 'b': return QKeySequence.SequenceMatch.PartialMatch else: return QKeySequence.SequenceMatch.NoMatch @@ -147,9 +153,11 @@ def test_partial_keychain_timeout(modeman_with_timeout, config_stub, qtbot, data else: pytest.fail('Unreachable') if behavior in ['timer_active', 'timer_reset']: - # Now simulate a timeout and check the keystring has been cleared. - with qtbot.wait_signal(modeman_with_timeout.keystring_updated) as blocker: - timer.timeout.emit() + # Wait for the timer to timeout and check the keystring has been cleared. + # Only wait for up to 2*timeout, then raise an error. + with qtbot.wait_signal(modeman_with_timeout.keystring_updated, + timeout=2*timeout) as blocker: + pass assert parser.fake_clear_keystring_called parser.fake_clear_keystring_called = False assert blocker.args == [mode, ''] diff --git a/tests/unit/keyinput/test_modeparsers.py b/tests/unit/keyinput/test_modeparsers.py index 8369c7d90..f6130149c 100644 --- a/tests/unit/keyinput/test_modeparsers.py +++ b/tests/unit/keyinput/test_modeparsers.py @@ -259,7 +259,7 @@ class TestHintKeyParser: assert hintmanager.keystr == hint_seq[:len(seq)] else: assert hintmanager.keystr == ''.join( - str(keyutils.KeyInfo(key, Qt.KeyboardModifier.NoModifier)) for key in seq) + str(keyutils.KeyInfo(key, Qt.KeyboardModifier.NoModifier)) for key in seq) @pytest.mark.parametrize('data_sequence', [ ((Qt.Key.Key_A, 'timer_inactive'),), @@ -320,8 +320,10 @@ class TestHintKeyParser: else: pytest.fail('Unreachable') if behavior in ['timer_active', 'timer_reset']: - # Now simulate a timeout and check the keystring has been forwarded. - with qtbot.wait_signal(command_parser.keystring_updated) as blocker: - timer.timeout.emit() + # Wait for the timer to timeout and check the keystring has been forwarded. + # Only wait for up to 2*timeout, then raise an error. + with qtbot.wait_signal(command_parser.keystring_updated, + timeout=2*timeout) as blocker: + pass assert blocker.args == [''] assert hintmanager.keystr == ('b' * len(data_sequence))