diff --git a/qutebrowser/utils/version.py b/qutebrowser/utils/version.py index 7e5a185da..e96be6131 100644 --- a/qutebrowser/utils/version.py +++ b/qutebrowser/utils/version.py @@ -19,6 +19,7 @@ """Utilities to show various version informations.""" +import re import sys import glob import os.path @@ -34,6 +35,7 @@ from PyQt5.QtWidgets import QApplication import qutebrowser from qutebrowser.utils import log, utils +from qutebrowser.browser import pdfjs GPL_BOILERPLATE = """ @@ -183,6 +185,25 @@ def _os_info(): return lines +def _pdfjs_version(): + """Get the pdf.js version. + + Return: + A string with the version number. + """ + try: + pdfjs_file = pdfjs.get_pdfjs_res('build/pdf.js').decode('utf-8') + except pdfjs.PDFJSNotFound: + return 'no' + else: + version_re = re.compile(r"^PDFJS\.version = '([^']+)';$", re.MULTILINE) + match = version_re.search(pdfjs_file) + if not match: + return 'unknown' + else: + return match.group(1) + + def version(short=False): """Return a string with various version informations. @@ -211,6 +232,7 @@ def version(short=False): lines += _module_versions() lines += [ + 'pdf.js: {}'.format(_pdfjs_version()), 'Webkit: {}'.format(qWebKitVersion()), 'Harfbuzz: {}'.format(os.environ.get('QT_HARFBUZZ', 'system')), 'SSL: {}'.format(QSslSocket.sslLibraryVersionString()), diff --git a/tests/unit/utils/test_version.py b/tests/unit/utils/test_version.py index 3ad45ce34..f8c1a88e3 100644 --- a/tests/unit/utils/test_version.py +++ b/tests/unit/utils/test_version.py @@ -34,6 +34,7 @@ import pytest import qutebrowser from qutebrowser.utils import version +from qutebrowser.browser import pdfjs class GitStrSubprocessFake: @@ -529,6 +530,39 @@ class TestOsInfo: version._os_info() +class TestPDFJSVersion: + + """Tests for _pdfjs_version.""" + + def test_not_found(self, mocker): + mocker.patch('qutebrowser.utils.version.pdfjs.get_pdfjs_res', + side_effect=pdfjs.PDFJSNotFound) + assert version._pdfjs_version() == 'no' + + def test_unknown(self, monkeypatch): + monkeypatch.setattr('qutebrowser.utils.version.pdfjs.get_pdfjs_res', + lambda path: b'foobar') + assert version._pdfjs_version() == 'unknown' + + def test_known(self, monkeypatch): + pdfjs_code = textwrap.dedent(""" + // Initializing PDFJS global object (if still undefined) + if (typeof PDFJS === 'undefined') { + (typeof window !== 'undefined' ? window : this).PDFJS = {}; + } + + PDFJS.version = '1.2.109'; + PDFJS.build = '875588d'; + + (function pdfjsWrapper() { + // Use strict in our context only - users might not want it + 'use strict'; + """).strip().encode('utf-8') + monkeypatch.setattr('qutebrowser.utils.version.pdfjs.get_pdfjs_res', + lambda path: pdfjs_code) + assert version._pdfjs_version() == '1.2.109' + + class FakeQSslSocket: """Fake for the QSslSocket Qt class. @@ -571,6 +605,7 @@ def test_version_output(git_commit, harfbuzz, frozen, short, stubs, 'qVersion': lambda: 'QT RUNTIME VERSION', 'PYQT_VERSION_STR': 'PYQT VERSION', '_module_versions': lambda: ['MODULE VERSION 1', 'MODULE VERSION 2'], + '_pdfjs_version': lambda: 'PDFJS VERSION', 'qWebKitVersion': lambda: 'WEBKIT VERSION', 'QSslSocket': FakeQSslSocket('SSL VERSION'), 'platform.platform': lambda: 'PLATFORM', @@ -613,6 +648,7 @@ def test_version_output(git_commit, harfbuzz, frozen, short, stubs, Desktop: DESKTOP MODULE VERSION 1 MODULE VERSION 2 + pdf.js: PDFJS VERSION Webkit: WEBKIT VERSION Harfbuzz: {harfbuzz} SSL: SSL VERSION