Permalink
Browse files

Fix pdfjs version parsing for never versions

  • Loading branch information...
The-Compiler committed Jun 7, 2016
1 parent 64fc1a3 commit e8123bb68ac019deab7ff32223ae3214323dd31c
Showing with 20 additions and 5 deletions.
  1. +4 −2 qutebrowser/utils/version.py
  2. +16 −3 tests/unit/utils/test_version.py
@@ -197,12 +197,14 @@ def _pdfjs_version():
return 'no'
else:
pdfjs_file = pdfjs_file.decode('utf-8')
- version_re = re.compile(r"^PDFJS\.version = '([^']+)';$", re.MULTILINE)
+ version_re = re.compile(
+ r"^(PDFJS\.version|var pdfjsVersion) = '([^']+)';$", re.MULTILINE)
+
match = version_re.search(pdfjs_file)
if not match:
pdfjs_version = 'unknown'
else:
- pdfjs_version = match.group(1)
+ pdfjs_version = match.group(2)
if file_path is None:
file_path = 'bundled'
return '{} ({})'.format(pdfjs_version, file_path)
@@ -543,25 +543,38 @@ def test_unknown(self, monkeypatch):
lambda path: (b'foobar', None))
assert version._pdfjs_version() == 'unknown (bundled)'
- def test_known(self, monkeypatch):
+ @pytest.mark.parametrize('varname', [
+ 'PDFJS.version', # older versions
+ 'var pdfjsVersion', # newer versions
+ ])
+ def test_known(self, monkeypatch, varname):
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';
+ VARNAME = '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')
+ """.replace('VARNAME', varname)).strip().encode('utf-8')
monkeypatch.setattr(
'qutebrowser.utils.version.pdfjs.get_pdfjs_res_and_path',
lambda path: (pdfjs_code, '/foo/bar/pdf.js'))
assert version._pdfjs_version() == '1.2.109 (/foo/bar/pdf.js)'
+ def test_real_file(self):
+ """Test against the real file if pdfjs was found."""
+ try:
+ pdfjs.get_pdfjs_res_and_path('build/pdf.js')
+ except pdfjs.PDFJSNotFound:
+ pytest.skip("No pdfjs found")
+ ver = version._pdfjs_version()
+ assert ver.split()[0] not in ['no', 'unknown'], ver
+
class FakeQSslSocket:

0 comments on commit e8123bb

Please sign in to comment.