From 1a61e53daaf8cb4aa1fd85ce151482c88cf9c2ec Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 19 Aug 2015 00:04:01 +0200 Subject: [PATCH] 100% test coverage for misc.checkpyver. --- qutebrowser/misc/checkpyver.py | 9 ++- scripts/dev/check_coverage.py | 1 + tests/unit/misc/test_checkpyver.py | 88 ++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 tests/unit/misc/test_checkpyver.py diff --git a/qutebrowser/misc/checkpyver.py b/qutebrowser/misc/checkpyver.py index cdf1ebbe3..35672d08b 100644 --- a/qutebrowser/misc/checkpyver.py +++ b/qutebrowser/misc/checkpyver.py @@ -25,7 +25,7 @@ import sys try: # Python3 from tkinter import Tk, messagebox -except ImportError: +except ImportError: # pragma: no coverage try: # Python2 # pylint: disable=import-error @@ -47,7 +47,7 @@ def check_python_version(): version_str = '.'.join(map(str, sys.version_info[:3])) text = ("At least Python 3.4 is required to run qutebrowser, but " + version_str + " is installed!\n") - if Tk and '--no-err-windows' not in sys.argv: + if Tk and '--no-err-windows' not in sys.argv: # pragma: no coverage root = Tk() root.withdraw() messagebox.showerror("qutebrowser: Fatal error!", text) @@ -55,3 +55,8 @@ def check_python_version(): sys.stderr.write(text) sys.stderr.flush() sys.exit(1) + + +if __name__ == '__main__': + # Needed for a test which calls this script with python 2. + check_python_version() diff --git a/scripts/dev/check_coverage.py b/scripts/dev/check_coverage.py index 0e1d17eba..64a1abfcd 100644 --- a/scripts/dev/check_coverage.py +++ b/scripts/dev/check_coverage.py @@ -48,6 +48,7 @@ PERFECT_FILES = [ 'qutebrowser/misc/readline.py', 'qutebrowser/misc/split.py', 'qutebrowser/misc/msgbox.py', + 'qutebrowser/misc/checkpyver.py', 'qutebrowser/mainwindow/statusbar/keystring.py', 'qutebrowser/mainwindow/statusbar/percentage.py', diff --git a/tests/unit/misc/test_checkpyver.py b/tests/unit/misc/test_checkpyver.py new file mode 100644 index 000000000..7cc02e7f0 --- /dev/null +++ b/tests/unit/misc/test_checkpyver.py @@ -0,0 +1,88 @@ +# Copyright 2015 Florian Bruhin (The Compiler) +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# 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.misc.checkpyver.""" + +import re +import sys +import subprocess +import unittest.mock + +import pytest + +from qutebrowser.misc import checkpyver + + +TEXT = (r"At least Python 3.4 is required to run qutebrowser, but " + r"\d+\.\d+\.\d+ is installed!\n") + + +def test_python2(): + """Run checkpyver with python 2.""" + try: + proc = subprocess.Popen(['python2', checkpyver.__file__, + '--no-err-windows'], stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + stdout, stderr = proc.communicate() + except FileNotFoundError: + pytest.skip("python2 not found") + assert not stdout + stderr = stderr.decode('utf-8') + assert re.match(TEXT, stderr), stderr + assert proc.returncode == 1 + + +def test_normal(capfd): + checkpyver.check_python_version() + out, err = capfd.readouterr() + assert not out + assert not err + + +def test_patched_no_errwindow(capfd, monkeypatch): + """Test with a patched sys.hexversion and --no-err-windows.""" + monkeypatch.setattr('qutebrowser.misc.checkpyver.sys.argv', + [sys.argv[0], '--no-err-windows']) + monkeypatch.setattr('qutebrowser.misc.checkpyver.sys.hexversion', + 0x03000000) + monkeypatch.setattr('qutebrowser.misc.checkpyver.sys.exit', + lambda status: None) + checkpyver.check_python_version() + stdout, stderr = capfd.readouterr() + assert not stdout + assert re.match(TEXT, stderr), stderr + + +def test_patched_errwindow(capfd, mocker, monkeypatch): + """Test with a patched sys.hexversion and a fake Tk.""" + monkeypatch.setattr('qutebrowser.misc.checkpyver.sys.hexversion', + 0x03000000) + monkeypatch.setattr('qutebrowser.misc.checkpyver.sys.exit', + lambda status: None) + tk_mock = mocker.patch('qutebrowser.misc.checkpyver.Tk', autospec=True) + msgbox_mock = mocker.patch('qutebrowser.misc.checkpyver.messagebox', + autospec=True) + + checkpyver.check_python_version() + stdout, stderr = capfd.readouterr() + assert not stdout + assert not stderr + tk_mock.assert_called_with() + tk_mock().withdraw.assert_called_with() + msgbox_mock.showerror.assert_called_with("qutebrowser: Fatal error!", + unittest.mock.ANY)