Merge branch 'more-tests' of https://github.com/HolySmoke86/qutebrowser into HolySmoke86-more-tests

This commit is contained in:
Florian Bruhin 2016-10-30 23:24:15 +01:00
commit b4872192c3
8 changed files with 431 additions and 114 deletions

View File

@ -92,6 +92,12 @@ class BaseLineParser(QObject):
Args:
mode: The mode to use ('a'/'r'/'w')
Raises:
IOError: if the file is already open
Yields:
a file object for the config file
"""
assert self._configfile is not None
if self._opened:

View File

@ -20,7 +20,8 @@
"""Misc. utility commands exposed to the user."""
import functools
import types
import os
import signal
import traceback
try:
@ -142,10 +143,7 @@ def debug_crash(typ='exception'):
typ: either 'exception' or 'segfault'.
"""
if typ == 'segfault':
# From python's Lib/test/crashers/bogus_code_obj.py
co = types.CodeType(0, 0, 0, 0, 0, b'\x04\x71\x00\x00', (), (), (),
'', '', 1, b'')
exec(co)
os.kill(os.getpid(), signal.SIGSEGV)
raise Exception("Segfault failed (wat.)")
else:
raise Exception("Forced crash")
@ -173,12 +171,15 @@ def debug_console():
try:
con_widget = objreg.get('debug-console')
except KeyError:
log.misc.debug('initializing debug console')
con_widget = consolewidget.ConsoleWidget()
objreg.register('debug-console', con_widget)
if con_widget.isVisible():
log.misc.debug('hiding debug console')
con_widget.hide()
else:
log.misc.debug('showing debug console')
con_widget.show()

View File

@ -45,20 +45,6 @@ Feature: Various utility commands.
When I run :set-cmd-text foo
Then the error "Invalid command text 'foo'." should be shown
## :message-*
Scenario: :message-error
When I run :message-error "Hello World"
Then the error "Hello World" should be shown
Scenario: :message-info
When I run :message-info "Hello World"
Then the message "Hello World" should be shown
Scenario: :message-warning
When I run :message-warning "Hello World"
Then the warning "Hello World" should be shown
## :jseval
Scenario: :jseval
@ -243,16 +229,6 @@ Feature: Various utility commands.
And I run :view-source
Then the error "Already viewing source!" should be shown
# :debug-console
@no_xvfb
Scenario: :debug-console smoke test
When I run :debug-console
And I wait for "Focus object changed: <qutebrowser.misc.consolewidget.ConsoleLineEdit *>" in the log
And I run :debug-console
And I wait for "Focus object changed: *" in the log
Then no crash should happen
# :help
Scenario: :help without topic
@ -496,31 +472,6 @@ Feature: Various utility commands.
Then qute://log?level=error should be loaded
And the page should contain the plaintext "No messages to show."
Scenario: Using :debug-log-capacity
When I run :debug-log-capacity 100
And I run :message-info oldstuff
And I run :repeat 20 message-info otherstuff
And I run :message-info newstuff
And I open qute:log
Then the page should contain the plaintext "newstuff"
And the page should not contain the plaintext "oldstuff"
Scenario: Using :debug-log-capacity with negative capacity
When I run :debug-log-capacity -1
Then the error "Can't set a negative log capacity!" should be shown
# :debug-log-level / :debug-log-filter
# Other :debug-log-{level,filter} features are tested in
# unit/utils/test_log.py as using them would break end2end tests.
Scenario: Using debug-log-level with invalid level
When I run :debug-log-level hello
Then the error "level: Invalid value hello - expected one of: vdebug, debug, info, warning, error, critical" should be shown
Scenario: Using debug-log-filter with invalid filter
When I run :debug-log-filter blah
Then the error "filters: Invalid value blah - expected one of: statusbar, *" should be shown
## https://github.com/The-Compiler/qutebrowser/issues/1523
Scenario: Completing a single option argument
@ -561,51 +512,6 @@ Feature: Various utility commands.
And I set general -> private-browsing to false
Then the page should contain the plaintext "Local storage status: not working"
Scenario: :repeat-command
Given I open data/scroll/simple.html
And I run :tab-only
When I run :scroll down
And I run :repeat-command
And I run :scroll up
Then the page should be scrolled vertically
Scenario: :repeat-command with count
Given I open data/scroll/simple.html
And I run :tab-only
When I run :scroll down with count 3
And I wait until the scroll position changed
And I run :scroll up
And I wait until the scroll position changed
And I run :repeat-command with count 2
And I wait until the scroll position changed to 0/0
Then the page should not be scrolled
Scenario: :repeat-command with not-normal command inbetween
Given I open data/scroll/simple.html
And I run :tab-only
When I run :scroll down with count 3
And I wait until the scroll position changed
And I run :scroll up
And I wait until the scroll position changed
And I run :prompt-accept
And I run :repeat-command with count 2
And I wait until the scroll position changed to 0/0
Then the page should not be scrolled
And the error "prompt-accept: This command is only allowed in prompt/yesno mode, not normal." should be shown
@qtwebengine_createWindow
Scenario: :repeat-command with mode-switching command
Given I open data/hints/link_blank.html
And I run :tab-only
When I hint with args "all"
And I run :leave-mode
And I run :repeat-command
And I run :follow-hint a
And I wait until data/hello.txt is loaded
Then the following tabs should be open:
- data/hints/link_blank.html
- data/hello.txt (active)
Scenario: Using 0 as count
When I run :scroll down with count 0
Then the error "scroll: A zero count is not allowed for this command!" should be shown
@ -723,13 +629,3 @@ Feature: Various utility commands.
And I run :command-accept
And I set general -> private-browsing to false
Then the message "blah" should be shown
## :run-with-count
Scenario: :run-with-count
When I run :run-with-count 2 scroll down
Then "command called: scroll ['down'] (count=2)" should be logged
Scenario: :run-with-count with count
When I run :run-with-count 2 scroll down with count 3
Then "command called: scroll ['down'] (count=6)" should be logged

View File

@ -0,0 +1,22 @@
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
import pytest_bdd as bdd
bdd.scenarios('utilcmds.feature')

View File

@ -0,0 +1,165 @@
Feature: Miscellaneous utility commands exposed to the user.
Background:
Given I open data/scroll/simple.html
And I run :tab-only
## :later
Scenario: :later before
When I run :later 500 scroll down
Then the page should not be scrolled
# wait for scroll to execture so we don't ruin our future
And the page should be scrolled vertically
Scenario: :later after
When I run :later 500 scroll down
And I wait 0.6s
Then the page should be scrolled vertically
# for some reason, argparser gives us the error instead, see #2046
@xfail
Scenario: :later with negative delay
When I run :later -1 scroll down
Then the error "I can't run something in the past!" should be shown
Scenario: :later with humongous delay
When I run :later 36893488147419103232 scroll down
Then the error "Numeric argument is too large for internal int representation." should be shown
## :repeat
Scenario: :repeat simple
When I run :repeat 5 scroll-px 10 0
And I wait until the scroll position changed to 50/0
# Then already covered by above And
Scenario: :repeat zero times
When I run :repeat 0 scroll-px 10 0
And I wait 0.01s
Then the page should not be scrolled
## :run-with-count
Scenario: :run-with-count
When I run :run-with-count 2 scroll down
Then "command called: scroll ['down'] (count=2)" should be logged
Scenario: :run-with-count with count
When I run :run-with-count 2 scroll down with count 3
Then "command called: scroll ['down'] (count=6)" should be logged
## :message-*
Scenario: :message-error
When I run :message-error "Hello World"
Then the error "Hello World" should be shown
Scenario: :message-info
When I run :message-info "Hello World"
Then the message "Hello World" should be shown
Scenario: :message-warning
When I run :message-warning "Hello World"
Then the warning "Hello World" should be shown
# argparser again
@xfail
Scenario: :repeat negative times
When I run :repeat -4 scroll-px 10 0
Then the error "A negative count doesn't make sense." should be shown
And the page should not be scrolled
## :debug-all-objects
Scenario: :debug-all-objects
When I run :debug-all-objects
Then "*Qt widgets - *Qt objects - *" should be logged
## :debug-cache-stats
Scenario: :debug-cache-stats
When I run :debug-cache-stats
Then "config: CacheInfo(*)" should be logged
And "style: CacheInfo(*)" should be logged
## :debug-console
@no_xvfb
Scenario: :debug-console smoke test
When I run :debug-console
And I wait for "Focus object changed: <qutebrowser.misc.consolewidget.ConsoleLineEdit *>" in the log
And I run :debug-console
And I wait for "Focus object changed: *" in the log
Then "initializing debug console" should be logged
And "showing debug console" should be logged
And "hiding debug console" should be logged
And no crash should happen
## :repeat-command
Scenario: :repeat-command
When I run :scroll down
And I run :repeat-command
And I run :scroll up
Then the page should be scrolled vertically
Scenario: :repeat-command with count
When I run :scroll down with count 3
And I wait until the scroll position changed
And I run :scroll up
And I wait until the scroll position changed
And I run :repeat-command with count 2
And I wait until the scroll position changed to 0/0
Then the page should not be scrolled
Scenario: :repeat-command with not-normal command inbetween
When I run :scroll down with count 3
And I wait until the scroll position changed
And I run :scroll up
And I wait until the scroll position changed
And I run :prompt-accept
And I run :repeat-command with count 2
And I wait until the scroll position changed to 0/0
Then the page should not be scrolled
And the error "prompt-accept: This command is only allowed in prompt/yesno mode, not normal." should be shown
@qtwebengine_createWindow
Scenario: :repeat-command with mode-switching command
When I open data/hints/link_blank.html
And I run :tab-only
And I hint with args "all"
And I run :leave-mode
And I run :repeat-command
And I run :follow-hint a
And I wait until data/hello.txt is loaded
Then the following tabs should be open:
- data/hints/link_blank.html
- data/hello.txt (active)
## :debug-log-capacity
Scenario: Using :debug-log-capacity
When I run :debug-log-capacity 100
And I run :message-info oldstuff
And I run :repeat 20 message-info otherstuff
And I run :message-info newstuff
And I open qute:log
Then the page should contain the plaintext "newstuff"
And the page should not contain the plaintext "oldstuff"
Scenario: Using :debug-log-capacity with negative capacity
When I run :debug-log-capacity -1
Then the error "Can't set a negative log capacity!" should be shown
## :debug-log-level / :debug-log-filter
# Other :debug-log-{level,filter} features are tested in
# unit/utils/test_log.py as using them would break end2end tests.
Scenario: Using debug-log-level with invalid level
When I run :debug-log-level hello
Then the error "level: Invalid value hello - expected one of: vdebug, debug, info, warning, error, critical" should be shown
Scenario: Using debug-log-filter with invalid filter
When I run :debug-log-filter blah
Then the error "filters: Invalid value blah - expected one of: statusbar, *" should be shown

View File

@ -157,7 +157,7 @@ def pattern_match(*, pattern, value):
True on a match, False otherwise.
"""
re_pattern = '.*'.join(re.escape(part) for part in pattern.split('*'))
return re.fullmatch(re_pattern, value) is not None
return re.fullmatch(re_pattern, value, flags=re.DOTALL) is not None
def abs_datapath():

View File

@ -21,14 +21,13 @@
import os
import pytest
from unittest import mock
from qutebrowser.misc import lineparser as lineparsermod
class TestBaseLineParser:
"""Tests for BaseLineParser."""
CONFDIR = "this really doesn't matter"
FILENAME = "and neither does this"
@ -53,6 +52,45 @@ class TestBaseLineParser:
lineparser._prepare_save()
os_mock.makedirs.assert_called_with(self.CONFDIR, 0o755)
def test_prepare_save_no_config(self, mocker):
"""Test if _prepare_save doesn't create a None config dir."""
os_mock = mocker.patch('qutebrowser.misc.lineparser.os')
os_mock.path.exists.return_value = True
lineparser = lineparsermod.BaseLineParser(None, self.FILENAME)
assert not lineparser._prepare_save()
assert not os_mock.makedirs.called
def test_double_open(self, mocker, lineparser):
"""Test if _open refuses reentry."""
mocker.patch('builtins.open', mock.mock_open())
with lineparser._open('r'):
with pytest.raises(IOError) as excinf:
with lineparser._open('r'):
pass
assert str(excinf.value) == 'Refusing to double-open AppendLineParser.'
def test_binary(self, mocker):
"""Test if _open and _write correctly handle binary files."""
open_mock = mock.mock_open()
mocker.patch('builtins.open', open_mock)
testdata = b'\xf0\xff'
lineparser = lineparsermod.BaseLineParser(
self.CONFDIR, self.FILENAME, binary=True)
with lineparser._open('r') as f:
lineparser._write(f, [testdata])
open_mock.assert_called_once_with(
os.path.join(self.CONFDIR, self.FILENAME), 'rb')
open_mock().write.assert_has_calls([
mock.call(testdata),
mock.call(b'\n')
])
class TestLineParser:
@ -63,7 +101,18 @@ class TestLineParser:
lp.save()
return lp
def test_init(self, tmpdir):
"""Test if creating a line parser correctly reads its file."""
(tmpdir / 'file').write('one\ntwo\n')
lineparser = lineparsermod.LineParser(str(tmpdir), 'file')
assert lineparser.data == ['one', 'two']
(tmpdir / 'file').write_binary(b'\xfe\n\xff\n')
lineparser = lineparsermod.LineParser(str(tmpdir), 'file', binary=True)
assert lineparser.data == [b'\xfe', b'\xff']
def test_clear(self, tmpdir, lineparser):
"""Test if clear() empties its file."""
lineparser.data = ['one', 'two']
lineparser.save()
assert (tmpdir / 'file').read() == 'one\ntwo\n'
@ -71,11 +120,23 @@ class TestLineParser:
assert not lineparser.data
assert (tmpdir / 'file').read() == ''
def test_double_open(self, lineparser):
"""Test if save() bails on an already open file."""
with lineparser._open('r'):
with pytest.raises(IOError):
lineparser.save()
def test_prepare_save(self, tmpdir, lineparser):
"""Test if save() bails when _prepare_save() returns False."""
(tmpdir / 'file').write('pristine\n')
lineparser.data = ['changed']
lineparser._prepare_save = lambda: False
lineparser.save()
assert (tmpdir / 'file').read() == 'pristine\n'
class TestAppendLineParser:
"""Tests for AppendLineParser."""
BASE_DATA = ['old data 1', 'old data 2']
@pytest.fixture
@ -97,7 +158,17 @@ class TestAppendLineParser:
lineparser.save()
assert (tmpdir / 'file').read() == self._get_expected(new_data)
def test_save_without_configdir(self, tmpdir, lineparser):
"""Test save() failing because no configdir was set."""
new_data = ['new data 1', 'new data 2']
lineparser.new_data = new_data
lineparser._configdir = None
assert not lineparser.save()
# make sure new data is still there
assert lineparser.new_data == new_data
def test_clear(self, tmpdir, lineparser):
"""Check if calling clear() empties both pending and persisted data."""
lineparser.new_data = ['one', 'two']
lineparser.save()
assert (tmpdir / 'file').read() == "old data 1\nold data 2\none\ntwo\n"
@ -108,6 +179,14 @@ class TestAppendLineParser:
assert not lineparser.new_data
assert (tmpdir / 'file').read() == ""
def test_clear_without_configdir(self, tmpdir, lineparser):
"""Test clear() failing because no configdir was set."""
new_data = ['new data 1', 'new data 2']
lineparser.new_data = new_data
lineparser._configdir = None
assert not lineparser.clear()
assert lineparser.new_data == new_data
def test_iter_without_open(self, lineparser):
"""Test __iter__ without having called open()."""
with pytest.raises(ValueError):

View File

@ -0,0 +1,148 @@
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Tests for qutebrowser.misc.utilcmds."""
import contextlib
import logging
import os
import pytest
import signal
import time
from qutebrowser.misc import utilcmds
from qutebrowser.commands import cmdexc
@contextlib.contextmanager
def _trapped_segv(handler):
"""Temporarily install given signal handler for SIGSEGV."""
old_handler = signal.signal(signal.SIGSEGV, handler)
yield
signal.signal(signal.SIGSEGV, old_handler)
def test_debug_crash_exception():
"""Verify that debug_crash crashes as intended."""
with pytest.raises(Exception) as excinfo:
utilcmds.debug_crash(typ='exception')
assert str(excinfo.value) == 'Forced crash'
@pytest.mark.skipif(os.name == 'nt',
reason="current CPython/win can't recover from SIGSEGV")
def test_debug_crash_segfault():
"""Verify that debug_crash crashes as intended."""
caught = False
def _handler(num, frame):
"""Temporary handler for segfault."""
nonlocal caught
caught = num == signal.SIGSEGV
with _trapped_segv(_handler):
# since we handle the segfault, execution will continue and run into
# the "Segfault failed (wat.)" Exception
with pytest.raises(Exception) as excinfo:
utilcmds.debug_crash(typ='segfault')
time.sleep(0.001)
assert caught
assert 'Segfault failed' in str(excinfo.value)
def test_debug_trace(mocker):
"""Check if hunter.trace is properly called."""
# but only if hunter is available
pytest.importorskip('hunter')
hunter_mock = mocker.patch('qutebrowser.misc.utilcmds.hunter')
utilcmds.debug_trace(1)
assert hunter_mock.trace.assert_called_with(1)
def test_debug_trace_exception(mocker):
"""Check that exceptions thrown by hunter.trace are handled."""
def _mock_exception():
"""Side effect for testing debug_trace's reraise."""
raise Exception('message')
hunter_mock = mocker.patch('qutebrowser.misc.utilcmds.hunter')
hunter_mock.trace.side_effect = _mock_exception
with pytest.raises(cmdexc.CommandError) as excinfo:
utilcmds.debug_trace()
assert str(excinfo.value) == 'Exception: message'
def test_debug_trace_no_hunter(monkeypatch):
"""Test that an error is shown if debug_trace is called without hunter."""
monkeypatch.setattr(utilcmds, 'hunter', None)
with pytest.raises(cmdexc.CommandError) as excinfo:
utilcmds.debug_trace()
assert str(excinfo.value) == "You need to install 'hunter' to use this " \
"command!"
def test_repeat_command_initial(mocker, mode_manager):
"""Test repeat_command first-time behavior.
If :repeat-command is called initially, it should err, because there's
nothing to repeat.
"""
objreg_mock = mocker.patch('qutebrowser.misc.utilcmds.objreg')
objreg_mock.get.return_value = mode_manager
with pytest.raises(cmdexc.CommandError) as excinfo:
utilcmds.repeat_command(win_id=0)
assert str(excinfo.value) == "You didn't do anything yet."
def test_debug_log_level(mocker):
"""Test interactive log level changing."""
formatter_mock = mocker.patch(
'qutebrowser.misc.utilcmds.log.change_console_formatter')
handler_mock = mocker.patch(
'qutebrowser.misc.utilcmds.log.console_handler')
utilcmds.debug_log_level(level='debug')
formatter_mock.assert_called_with(logging.DEBUG)
handler_mock.setLevel.assert_called_with(logging.DEBUG)
class FakeWindow:
"""Mock class for window_only."""
def __init__(self, deleted=False):
self.closed = False
self.deleted = deleted
def close(self):
"""Flag as closed."""
self.closed = True
def test_window_only(mocker, monkeypatch):
"""Verify that window_only doesn't close the current or deleted windows."""
test_windows = {0: FakeWindow(), 1: FakeWindow(True), 2: FakeWindow()}
winreg_mock = mocker.patch('qutebrowser.misc.utilcmds.objreg')
winreg_mock.window_registry = test_windows
sip_mock = mocker.patch('qutebrowser.misc.utilcmds.sip')
sip_mock.isdeleted.side_effect = lambda window: window.deleted
utilcmds.window_only(current_win_id=0)
assert not test_windows[0].closed
assert not test_windows[1].closed
assert test_windows[2].closed