From 40d376fbcfc0a2599342b0550b0a82d80c2ae7f4 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 30 Nov 2018 16:25:12 +0100 Subject: [PATCH] Move unit tests --- tests/unit/components/test_misccommands.py | 93 ++++++++++++++++++++++ tests/unit/misc/test_utilcmds.py | 68 +--------------- 2 files changed, 94 insertions(+), 67 deletions(-) create mode 100644 tests/unit/components/test_misccommands.py diff --git a/tests/unit/components/test_misccommands.py b/tests/unit/components/test_misccommands.py new file mode 100644 index 000000000..95eb0c6e3 --- /dev/null +++ b/tests/unit/components/test_misccommands.py @@ -0,0 +1,93 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2018 Florian Bruhin (The Compiler) +# +# 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 . + +"""Tests for qutebrowser.components.misccommands.""" + +import signal +import contextlib +import time + +import pytest + +from qutebrowser.api import cmdutils +from qutebrowser.utils import utils +from qutebrowser.components import misccommands + + +@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, match="Forced crash"): + misccommands.debug_crash(typ='exception') + + +@pytest.mark.skipif(utils.is_windows, + 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, match="Segfault failed"): + misccommands.debug_crash(typ='segfault') + time.sleep(0.001) + assert caught + + +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.object(misccommands, 'hunter') + misccommands.debug_trace(1) + 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.object(misccommands, 'hunter') + hunter_mock.trace.side_effect = _mock_exception + with pytest.raises(cmdutils.CommandError, match='Exception: message'): + misccommands.debug_trace() + + +def test_debug_trace_no_hunter(monkeypatch): + """Test that an error is shown if debug_trace is called without hunter.""" + monkeypatch.setattr(misccommands, 'hunter', None) + with pytest.raises(cmdutils.CommandError, match="You need to install " + "'hunter' to use this command!"): + misccommands.debug_trace() diff --git a/tests/unit/misc/test_utilcmds.py b/tests/unit/misc/test_utilcmds.py index b4af06aff..1b71d5ddc 100644 --- a/tests/unit/misc/test_utilcmds.py +++ b/tests/unit/misc/test_utilcmds.py @@ -19,80 +19,14 @@ """Tests for qutebrowser.misc.utilcmds.""" -import contextlib import logging -import signal -import time import pytest from PyQt5.QtCore import QUrl from qutebrowser.misc import utilcmds from qutebrowser.api import cmdutils -from qutebrowser.utils import utils, objreg - - -@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, match="Forced crash"): - utilcmds.debug_crash(typ='exception') - - -@pytest.mark.skipif(utils.is_windows, - 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, match="Segfault failed"): - utilcmds.debug_crash(typ='segfault') - time.sleep(0.001) - assert caught - - -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) - 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(cmdutils.CommandError, match='Exception: message'): - utilcmds.debug_trace() - - -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(cmdutils.CommandError, match="You need to install " - "'hunter' to use this command!"): - utilcmds.debug_trace() +from qutebrowser.utils import objreg def test_repeat_command_initial(mocker, mode_manager):