2015-03-08 16:53:10 +01:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
|
|
|
# Copyright 2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2015-03-08 23:15:35 +01:00
|
|
|
# pylint: disable=protected-access
|
|
|
|
|
2015-03-08 16:53:10 +01:00
|
|
|
"""Tests for qutebrowser.misc.lineparser."""
|
|
|
|
|
|
|
|
import io
|
|
|
|
import os
|
|
|
|
|
2015-05-10 15:53:24 +02:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from qutebrowser.misc import lineparser as lineparsermod
|
2015-03-08 16:53:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
class LineParserWrapper:
|
|
|
|
|
|
|
|
"""A wrapper over lineparser.BaseLineParser to make it testable."""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self._data = None
|
|
|
|
self._test_save_prepared = False
|
|
|
|
|
|
|
|
def _open(self, mode):
|
2015-03-08 23:15:35 +01:00
|
|
|
"""Override _open to use StringIO/BytesIO instead of a real file."""
|
2015-03-08 16:53:10 +01:00
|
|
|
if mode not in 'rwa':
|
|
|
|
raise ValueError("Unknown mode {!r}!".format(mode))
|
|
|
|
if self._test_save_prepared:
|
|
|
|
self._test_save_prepared = False
|
|
|
|
elif mode != 'r':
|
|
|
|
raise ValueError("Doing unprepared save!")
|
|
|
|
|
|
|
|
if mode in 'ar' and self._data is not None:
|
|
|
|
prev_val = self._data
|
|
|
|
else:
|
|
|
|
prev_val = None
|
|
|
|
|
2015-03-08 23:15:35 +01:00
|
|
|
if self._binary: # pylint: disable=no-member
|
2015-03-08 16:53:10 +01:00
|
|
|
fobj = io.BytesIO(prev_val)
|
|
|
|
else:
|
|
|
|
fobj = io.StringIO(prev_val)
|
|
|
|
|
|
|
|
if mode == 'a':
|
|
|
|
fobj.seek(0, os.SEEK_END)
|
|
|
|
return fobj
|
|
|
|
|
|
|
|
def _write(self, fp, data):
|
2015-03-08 23:15:35 +01:00
|
|
|
"""Extend _write to get the data after writing it."""
|
2015-03-08 16:53:10 +01:00
|
|
|
super()._write(fp, data)
|
|
|
|
self._data = fp.getvalue()
|
|
|
|
|
|
|
|
def _prepare_save(self):
|
2015-03-08 23:15:35 +01:00
|
|
|
"""Keep track if _prepare_save has been called."""
|
2015-03-08 16:53:10 +01:00
|
|
|
self._test_save_prepared = True
|
2015-05-16 22:12:27 +02:00
|
|
|
return True
|
2015-03-08 16:53:10 +01:00
|
|
|
|
|
|
|
|
2015-05-27 07:50:56 +02:00
|
|
|
class AppendLineParserTestable(LineParserWrapper,
|
2015-05-10 15:53:24 +02:00
|
|
|
lineparsermod.AppendLineParser):
|
2015-03-08 23:15:35 +01:00
|
|
|
|
|
|
|
"""Wrapper over AppendLineParser to make it testable."""
|
|
|
|
|
2015-03-08 16:53:10 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-05-27 07:50:56 +02:00
|
|
|
class LineParserTestable(LineParserWrapper, lineparsermod.LineParser):
|
2015-03-08 23:15:35 +01:00
|
|
|
|
|
|
|
"""Wrapper over LineParser to make it testable."""
|
|
|
|
|
2015-03-08 16:53:10 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-05-27 07:50:56 +02:00
|
|
|
class LimitLineParserTestable(LineParserWrapper,
|
2015-05-10 15:53:24 +02:00
|
|
|
lineparsermod.LimitLineParser):
|
2015-03-08 23:15:35 +01:00
|
|
|
|
|
|
|
"""Wrapper over LimitLineParser to make it testable."""
|
|
|
|
|
2015-03-08 16:53:10 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-05-10 15:53:24 +02:00
|
|
|
class TestBaseLineParser:
|
2015-03-08 16:53:10 +01:00
|
|
|
|
|
|
|
"""Tests for BaseLineParser."""
|
|
|
|
|
2015-05-10 15:53:24 +02:00
|
|
|
CONFDIR = "this really doesn't matter"
|
|
|
|
FILENAME = "and neither does this"
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def lineparser(self):
|
|
|
|
"""Fixture providing a BaseLineParser."""
|
|
|
|
return lineparsermod.BaseLineParser(self.CONFDIR, self.FILENAME)
|
2015-03-08 16:53:10 +01:00
|
|
|
|
2015-05-10 15:53:24 +02:00
|
|
|
def test_prepare_save_existing(self, mocker, lineparser):
|
2015-03-08 16:53:10 +01:00
|
|
|
"""Test if _prepare_save does what it's supposed to do."""
|
2015-07-06 18:35:58 +02:00
|
|
|
os_mock = mocker.patch('qutebrowser.misc.lineparser.os')
|
|
|
|
os_mock.path.exists.return_value = True
|
2015-03-08 16:53:10 +01:00
|
|
|
|
2015-05-10 15:53:24 +02:00
|
|
|
lineparser._prepare_save()
|
2015-07-06 18:35:58 +02:00
|
|
|
assert not os_mock.makedirs.called
|
2015-05-10 15:53:24 +02:00
|
|
|
|
|
|
|
def test_prepare_save_missing(self, mocker, lineparser):
|
2015-03-08 16:53:10 +01:00
|
|
|
"""Test if _prepare_save does what it's supposed to do."""
|
2015-07-06 18:35:58 +02:00
|
|
|
os_mock = mocker.patch('qutebrowser.misc.lineparser.os')
|
|
|
|
os_mock.path.exists.return_value = False
|
2015-05-10 15:53:24 +02:00
|
|
|
|
|
|
|
lineparser._prepare_save()
|
2015-07-06 18:35:58 +02:00
|
|
|
os_mock.makedirs.assert_called_with(self.CONFDIR, 0o755)
|
2015-03-08 16:53:10 +01:00
|
|
|
|
|
|
|
|
2015-05-10 15:53:24 +02:00
|
|
|
class TestAppendLineParser:
|
2015-03-08 16:53:10 +01:00
|
|
|
|
|
|
|
"""Tests for AppendLineParser."""
|
|
|
|
|
2015-05-10 15:53:24 +02:00
|
|
|
BASE_DATA = ['old data 1', 'old data 2']
|
2015-03-08 16:53:10 +01:00
|
|
|
|
2015-05-10 15:53:24 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def lineparser(self):
|
|
|
|
"""Fixture to get an AppendLineParser for tests."""
|
2015-05-27 07:50:56 +02:00
|
|
|
lp = AppendLineParserTestable('this really', 'does not matter')
|
2015-05-10 15:53:24 +02:00
|
|
|
lp.new_data = self.BASE_DATA
|
|
|
|
lp.save()
|
|
|
|
return lp
|
|
|
|
|
|
|
|
def _get_expected(self, new_data):
|
2015-03-08 23:15:35 +01:00
|
|
|
"""Get the expected data with newlines."""
|
2015-05-10 15:53:24 +02:00
|
|
|
return '\n'.join(self.BASE_DATA + new_data) + '\n'
|
2015-03-08 16:53:10 +01:00
|
|
|
|
2015-05-10 15:53:24 +02:00
|
|
|
def test_save(self, lineparser):
|
2015-03-08 16:53:10 +01:00
|
|
|
"""Test save()."""
|
2015-05-10 15:53:24 +02:00
|
|
|
new_data = ['new data 1', 'new data 2']
|
|
|
|
lineparser.new_data = new_data
|
|
|
|
lineparser.save()
|
|
|
|
assert lineparser._data == self._get_expected(new_data)
|
2015-03-08 16:53:10 +01:00
|
|
|
|
2015-05-10 15:53:24 +02:00
|
|
|
def test_iter_without_open(self, lineparser):
|
2015-03-08 16:53:10 +01:00
|
|
|
"""Test __iter__ without having called open()."""
|
2015-05-10 15:53:24 +02:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
iter(lineparser)
|
2015-03-08 16:53:10 +01:00
|
|
|
|
2015-05-10 15:53:24 +02:00
|
|
|
def test_iter(self, lineparser):
|
2015-03-08 16:53:10 +01:00
|
|
|
"""Test __iter__."""
|
2015-05-10 15:53:24 +02:00
|
|
|
new_data = ['new data 1', 'new data 2']
|
|
|
|
lineparser.new_data = new_data
|
|
|
|
with lineparser.open():
|
|
|
|
assert list(lineparser) == self.BASE_DATA + new_data
|
2015-03-08 16:53:10 +01:00
|
|
|
|
2015-05-10 15:53:24 +02:00
|
|
|
def test_iter_not_found(self, mocker):
|
2015-03-08 16:53:10 +01:00
|
|
|
"""Test __iter__ with no file."""
|
2015-05-10 15:53:24 +02:00
|
|
|
open_mock = mocker.patch(
|
|
|
|
'qutebrowser.misc.lineparser.AppendLineParser._open')
|
2015-03-08 16:53:10 +01:00
|
|
|
open_mock.side_effect = FileNotFoundError
|
2015-05-10 15:53:24 +02:00
|
|
|
new_data = ['new data 1', 'new data 2']
|
|
|
|
linep = lineparsermod.AppendLineParser('foo', 'bar')
|
|
|
|
linep.new_data = new_data
|
2015-03-08 16:53:10 +01:00
|
|
|
with linep.open():
|
2015-05-10 15:53:24 +02:00
|
|
|
assert list(linep) == new_data
|
2015-03-08 16:53:10 +01:00
|
|
|
|
|
|
|
def test_get_recent_none(self):
|
|
|
|
"""Test get_recent with no data."""
|
2015-05-27 07:50:56 +02:00
|
|
|
linep = AppendLineParserTestable('this really', 'does not matter')
|
2015-05-10 15:53:24 +02:00
|
|
|
assert linep.get_recent() == []
|
2015-03-08 16:53:10 +01:00
|
|
|
|
2015-05-10 15:53:24 +02:00
|
|
|
def test_get_recent_little(self, lineparser):
|
2015-03-08 16:53:10 +01:00
|
|
|
"""Test get_recent with little data."""
|
2015-05-10 15:53:24 +02:00
|
|
|
data = [e + '\n' for e in self.BASE_DATA]
|
|
|
|
assert lineparser.get_recent() == data
|
2015-03-08 16:53:10 +01:00
|
|
|
|
2015-05-10 15:53:24 +02:00
|
|
|
def test_get_recent_much(self, lineparser):
|
2015-03-08 16:53:10 +01:00
|
|
|
"""Test get_recent with much data."""
|
|
|
|
size = 64
|
|
|
|
new_data = ['new data {}'.format(i) for i in range(size)]
|
2015-05-10 15:53:24 +02:00
|
|
|
lineparser.new_data = new_data
|
|
|
|
lineparser.save()
|
|
|
|
data = '\n'.join(self.BASE_DATA + new_data)
|
2015-03-08 16:53:10 +01:00
|
|
|
data = [e + '\n' for e in data[-(size - 1):].splitlines()]
|
2015-05-10 15:53:24 +02:00
|
|
|
assert lineparser.get_recent(size) == data
|