2016-01-04 07:12:39 +01:00
|
|
|
# Copyright 2014-2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
2014-10-02 06:22:06 +02:00
|
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""Tests for qutebrowser.utils.jinja."""
|
|
|
|
|
2015-09-25 18:51:07 +02:00
|
|
|
import os
|
2014-10-14 08:18:06 +02:00
|
|
|
import os.path
|
2014-10-02 06:22:06 +02:00
|
|
|
|
2015-04-09 00:46:48 +02:00
|
|
|
import pytest
|
2016-03-25 23:57:29 +01:00
|
|
|
import logging
|
2015-08-12 06:25:05 +02:00
|
|
|
import jinja2
|
2015-09-25 14:31:04 +02:00
|
|
|
from PyQt5.QtCore import QUrl
|
2015-04-09 00:46:48 +02:00
|
|
|
|
2016-03-25 23:57:29 +01:00
|
|
|
from qutebrowser.utils import utils, jinja
|
2014-10-02 06:22:06 +02:00
|
|
|
|
|
|
|
|
2015-04-09 00:46:48 +02:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def patch_read_file(monkeypatch):
|
2015-04-09 06:53:21 +02:00
|
|
|
"""pytest fixture to patch utils.read_file."""
|
2016-03-25 23:57:29 +01:00
|
|
|
real_read_file = utils.read_file
|
2016-03-26 00:24:54 +01:00
|
|
|
|
2015-04-09 00:46:48 +02:00
|
|
|
def _read_file(path):
|
2015-04-09 06:53:21 +02:00
|
|
|
"""A read_file which returns a simple template if the path is right."""
|
2015-04-09 00:46:48 +02:00
|
|
|
if path == os.path.join('html', 'test.html'):
|
|
|
|
return """Hello {{var}}"""
|
2016-09-15 16:01:56 +02:00
|
|
|
elif path == os.path.join('html', 'resource_url.html'):
|
|
|
|
return """{{ resource_url('utils/testfile', False) }}"""
|
|
|
|
elif path == os.path.join('html', 'resource_url_qute.html'):
|
|
|
|
return """{{ resource_url('utils/testfile', True) }}"""
|
2016-03-25 23:57:29 +01:00
|
|
|
elif path == os.path.join('html', 'undef.html'):
|
|
|
|
return """{{ does_not_exist() }}"""
|
|
|
|
elif path == os.path.join('html', 'undef_error.html'):
|
|
|
|
return real_read_file(path)
|
2015-04-09 00:46:48 +02:00
|
|
|
else:
|
2015-08-12 06:25:05 +02:00
|
|
|
raise IOError("Invalid path {}!".format(path))
|
2015-04-09 00:46:48 +02:00
|
|
|
|
|
|
|
monkeypatch.setattr('qutebrowser.utils.jinja.utils.read_file', _read_file)
|
2014-10-02 06:22:06 +02:00
|
|
|
|
|
|
|
|
2015-04-09 00:46:48 +02:00
|
|
|
def test_simple_template():
|
2015-04-09 00:38:57 +02:00
|
|
|
"""Test with a simple template."""
|
2016-03-25 14:29:30 +01:00
|
|
|
data = jinja.render('test.html', var='World')
|
2015-04-09 00:38:57 +02:00
|
|
|
assert data == "Hello World"
|
|
|
|
|
|
|
|
|
2015-09-25 14:31:04 +02:00
|
|
|
def test_resource_url():
|
|
|
|
"""Test resource_url() which can be used from templates."""
|
2016-09-15 16:01:56 +02:00
|
|
|
data = jinja.render('resource_url.html')
|
2015-09-25 14:31:04 +02:00
|
|
|
print(data)
|
|
|
|
url = QUrl(data)
|
|
|
|
assert url.isValid()
|
|
|
|
assert url.scheme() == 'file'
|
2015-09-25 18:51:07 +02:00
|
|
|
|
|
|
|
path = url.path()
|
|
|
|
|
|
|
|
if os.name == "nt":
|
2015-10-17 18:43:14 +02:00
|
|
|
path = path.lstrip('/')
|
|
|
|
path = path.replace('/', os.sep)
|
2015-09-25 18:51:07 +02:00
|
|
|
|
|
|
|
with open(path, 'r', encoding='utf-8') as f:
|
2015-09-25 14:31:04 +02:00
|
|
|
assert f.read().splitlines()[0] == "Hello World!"
|
|
|
|
|
|
|
|
|
2016-09-15 16:01:56 +02:00
|
|
|
def test_resource_url_qutescheme():
|
|
|
|
"""Test resource_url() which can be used from templates."""
|
|
|
|
data = jinja.render('resource_url_qute.html')
|
|
|
|
print(data)
|
|
|
|
url = QUrl(data)
|
|
|
|
assert url == QUrl('qute://resource/utils/testfile')
|
|
|
|
|
|
|
|
|
2015-08-12 06:25:05 +02:00
|
|
|
def test_not_found():
|
|
|
|
"""Test with a template which does not exist."""
|
|
|
|
with pytest.raises(jinja2.TemplateNotFound) as excinfo:
|
2016-03-25 14:29:30 +01:00
|
|
|
jinja.render('does_not_exist.html')
|
2015-08-12 06:25:05 +02:00
|
|
|
assert str(excinfo.value) == 'does_not_exist.html'
|
|
|
|
|
|
|
|
|
2015-04-09 00:46:48 +02:00
|
|
|
def test_utf8():
|
2015-04-09 00:38:57 +02:00
|
|
|
"""Test rendering with an UTF8 template.
|
|
|
|
|
|
|
|
This was an attempt to get a failing test case for #127 but it seems
|
|
|
|
the issue is elsewhere.
|
|
|
|
|
|
|
|
https://github.com/The-Compiler/qutebrowser/issues/127
|
|
|
|
"""
|
2016-03-25 14:29:30 +01:00
|
|
|
data = jinja.render('test.html', var='\u2603')
|
2015-04-09 00:38:57 +02:00
|
|
|
assert data == "Hello \u2603"
|
2015-08-12 06:25:05 +02:00
|
|
|
|
|
|
|
|
2016-03-25 23:57:29 +01:00
|
|
|
def test_undefined_function(caplog):
|
|
|
|
"""Make sure we don't crash if an undefined function is called."""
|
|
|
|
with caplog.at_level(logging.ERROR):
|
|
|
|
data = jinja.render('undef.html')
|
|
|
|
assert 'There was an error while rendering undef.html' in data
|
|
|
|
assert "'does_not_exist' is undefined" in data
|
|
|
|
assert data.startswith('<!DOCTYPE html>')
|
|
|
|
|
|
|
|
assert len(caplog.records) == 1
|
|
|
|
assert caplog.records[0].msg == "UndefinedError while rendering undef.html"
|
|
|
|
|
|
|
|
|
2015-08-12 06:25:05 +02:00
|
|
|
@pytest.mark.parametrize('name, expected', [
|
|
|
|
(None, False),
|
|
|
|
('foo', False),
|
|
|
|
('foo.html', True),
|
|
|
|
('foo.htm', True),
|
|
|
|
('foo.xml', True),
|
|
|
|
('blah/bar/foo.html', True),
|
|
|
|
('foo.bar.html', True),
|
|
|
|
])
|
|
|
|
def test_autoescape(name, expected):
|
|
|
|
assert jinja._guess_autoescape(name) == expected
|