From 978013e750b6cc9cd693b5b3527f4e580118faf6 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 2 Jul 2017 17:18:58 +0200 Subject: [PATCH] Fix CommandParser and don't use a generator --- qutebrowser/commands/runners.py | 6 +++++- qutebrowser/config/configtypes.py | 2 +- tests/unit/commands/test_runners.py | 24 +++++++++++------------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/qutebrowser/commands/runners.py b/qutebrowser/commands/runners.py index 5c2815c4e..1b81c37c6 100644 --- a/qutebrowser/commands/runners.py +++ b/qutebrowser/commands/runners.py @@ -118,7 +118,7 @@ class CommandParser: new_cmd += ' ' return new_cmd - def parse_all(self, text, aliases=True, *args, **kwargs): + def _parse_all_gen(self, text, aliases=True, *args, **kwargs): """Split a command on ;; and parse all parts. If the first command in the commandline is a non-split one, it only @@ -153,6 +153,10 @@ class CommandParser: for sub in sub_texts: yield self.parse(sub, *args, **kwargs) + def parse_all(self, *args, **kwargs): + """Wrapper over parse_all.""" + return list(self._parse_all_gen(*args, **kwargs)) + def parse(self, text, *, fallback=False, keep=False): """Split the commandline text into command and arguments. diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index a39da0979..484f5abcf 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -747,7 +747,7 @@ class Command(BaseType): from qutebrowser.commands import runners, cmdexc parser = runners.CommandParser() try: - list(parser.parse_all(value)) + parser.parse_all(value) except cmdexc.Error as e: raise configexc.ValidationError(value, str(e)) finally: diff --git a/tests/unit/commands/test_runners.py b/tests/unit/commands/test_runners.py index 720fa5209..82d121c40 100644 --- a/tests/unit/commands/test_runners.py +++ b/tests/unit/commands/test_runners.py @@ -24,9 +24,7 @@ import pytest from qutebrowser.commands import runners, cmdexc -class TestCommandRunner: - - """Tests for CommandRunner.""" +class TestCommandParser: def test_parse_all(self, cmdline_test): """Test parsing of commands. @@ -36,22 +34,22 @@ class TestCommandRunner: Args: cmdline_test: A pytest fixture which provides testcases. """ - cr = runners.CommandRunner(0) + parser = runners.CommandParser() if cmdline_test.valid: - list(cr.parse_all(cmdline_test.cmd, aliases=False)) + parser.parse_all(cmdline_test.cmd, aliases=False) else: with pytest.raises(cmdexc.NoSuchCommandError): - list(cr.parse_all(cmdline_test.cmd, aliases=False)) + parser.parse_all(cmdline_test.cmd, aliases=False) def test_parse_all_with_alias(self, cmdline_test, config_stub): config_stub.data = {'aliases': {'alias_name': cmdline_test.cmd}} - cr = runners.CommandRunner(0) + parser = runners.CommandParser() if cmdline_test.valid: - assert len(list(cr.parse_all("alias_name"))) > 0 + assert len(parser.parse_all("alias_name")) > 0 else: with pytest.raises(cmdexc.NoSuchCommandError): - list(cr.parse_all("alias_name")) + parser.parse_all("alias_name") @pytest.mark.parametrize('command', ['', ' ']) def test_parse_empty_with_alias(self, command): @@ -60,15 +58,15 @@ class TestCommandRunner: See https://github.com/qutebrowser/qutebrowser/issues/1690 and https://github.com/qutebrowser/qutebrowser/issues/1773 """ - cr = runners.CommandRunner(0) + parser = runners.CommandParser() with pytest.raises(cmdexc.NoSuchCommandError): - list(cr.parse_all(command)) + parser.parse_all(command) def test_partial_parsing(self): """Test partial parsing with a runner where it's enabled. The same with it being disabled is tested by test_parse_all. """ - cr = runners.CommandRunner(0, partial_match=True) - result = cr.parse('message-i') + parser = runners.CommandParser(partial_match=True) + result = parser.parse('message-i') assert result.cmd.name == 'message-info'