qutebrowser/tests/unit/misc/test_lineparser.py

208 lines
7.3 KiB
Python
Raw Normal View History

2015-03-08 16:53:10 +01:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2017-05-09 21:37:03 +02:00
# Copyright 2015-2017 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
2015-03-08 16:53:10 +01:00
#
# 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.misc.lineparser."""
2016-06-09 10:29:16 +02:00
import os
2016-10-21 15:53:58 +02:00
from unittest import mock
import pytest
from qutebrowser.misc import lineparser as lineparsermod
2015-03-08 16:53:10 +01:00
class TestBaseLineParser:
2015-03-08 16:53:10 +01: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
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."""
os_mock = mocker.patch('qutebrowser.misc.lineparser.os')
os_mock.path.exists.return_value = True
2015-03-08 16:53:10 +01:00
lineparser._prepare_save()
assert not os_mock.makedirs.called
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."""
os_mock = mocker.patch('qutebrowser.misc.lineparser.os')
os_mock.path.exists.return_value = False
lineparser._prepare_save()
os_mock.makedirs.assert_called_with(self.CONFDIR, 0o755)
2015-03-08 16:53:10 +01:00
2016-10-21 15:53:58 +02:00
def test_double_open(self, mocker, lineparser):
"""Test if _open refuses reentry."""
mocker.patch('builtins.open', mock.mock_open())
2016-10-21 15:53:58 +02:00
with lineparser._open('r'):
2017-05-23 09:36:00 +02:00
with pytest.raises(IOError, match="Refusing to double-open "
"AppendLineParser."):
2016-10-21 15:53:58 +02:00
with lineparser._open('r'):
pass
def test_binary(self, mocker):
"""Test if _open and _write correctly handle binary files."""
open_mock = mock.mock_open()
mocker.patch('builtins.open', open_mock)
2016-10-21 15:53:58 +02:00
testdata = b'\xf0\xff'
lineparser = lineparsermod.BaseLineParser(
self.CONFDIR, self.FILENAME, binary=True)
with lineparser._open('r') as f:
lineparser._write(f, [testdata])
open_mock.assert_called_once_with(
os.path.join(self.CONFDIR, self.FILENAME), 'rb')
open_mock().write.assert_has_calls([
mock.call(testdata),
mock.call(b'\n')
])
2015-03-08 16:53:10 +01:00
2016-06-08 11:10:21 +02:00
class TestLineParser:
@pytest.fixture
def lineparser(self, tmpdir):
"""Fixture to get a LineParser for tests."""
lp = lineparsermod.LineParser(str(tmpdir), 'file')
lp.save()
return lp
2016-10-21 15:53:58 +02:00
def test_init(self, tmpdir):
"""Test if creating a line parser correctly reads its file."""
(tmpdir / 'file').write('one\ntwo\n')
lineparser = lineparsermod.LineParser(str(tmpdir), 'file')
assert lineparser.data == ['one', 'two']
(tmpdir / 'file').write_binary(b'\xfe\n\xff\n')
lineparser = lineparsermod.LineParser(str(tmpdir), 'file', binary=True)
assert lineparser.data == [b'\xfe', b'\xff']
2016-06-08 11:10:21 +02:00
def test_clear(self, tmpdir, lineparser):
2016-10-21 15:53:58 +02:00
"""Test if clear() empties its file."""
2016-06-08 11:10:21 +02:00
lineparser.data = ['one', 'two']
lineparser.save()
assert (tmpdir / 'file').read() == 'one\ntwo\n'
lineparser.clear()
assert not lineparser.data
assert (tmpdir / 'file').read() == ''
2016-10-21 15:53:58 +02:00
def test_double_open(self, lineparser):
"""Test if save() bails on an already open file."""
with lineparser._open('r'):
with pytest.raises(IOError):
lineparser.save()
def test_prepare_save(self, tmpdir, lineparser):
"""Test if save() bails when _prepare_save() returns False."""
(tmpdir / 'file').write('pristine\n')
lineparser.data = ['changed']
lineparser._prepare_save = lambda: False
lineparser.save()
assert (tmpdir / 'file').read() == 'pristine\n'
2016-06-08 11:10:21 +02:00
class TestAppendLineParser:
2015-03-08 16:53:10 +01:00
BASE_DATA = ['old data 1', 'old data 2']
2015-03-08 16:53:10 +01:00
@pytest.fixture
2016-06-08 11:06:45 +02:00
def lineparser(self, tmpdir):
"""Fixture to get an AppendLineParser for tests."""
2016-06-08 11:06:45 +02:00
lp = lineparsermod.AppendLineParser(str(tmpdir), 'file')
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."""
return '\n'.join(self.BASE_DATA + new_data) + '\n'
2015-03-08 16:53:10 +01:00
2016-06-08 11:06:45 +02:00
def test_save(self, tmpdir, lineparser):
2015-03-08 16:53:10 +01:00
"""Test save()."""
new_data = ['new data 1', 'new data 2']
lineparser.new_data = new_data
lineparser.save()
2016-06-08 11:06:45 +02:00
assert (tmpdir / 'file').read() == self._get_expected(new_data)
2015-03-08 16:53:10 +01:00
2016-06-08 11:06:45 +02:00
def test_clear(self, tmpdir, lineparser):
2016-10-21 15:53:58 +02:00
"""Check if calling clear() empties both pending and persisted data."""
2016-06-08 11:10:21 +02:00
lineparser.new_data = ['one', 'two']
lineparser.save()
assert (tmpdir / 'file').read() == "old data 1\nold data 2\none\ntwo\n"
lineparser.new_data = ['one', 'two']
2016-06-08 10:11:59 +02:00
lineparser.clear()
lineparser.save()
2016-06-08 11:10:21 +02:00
assert not lineparser.new_data
2016-06-08 11:06:45 +02:00
assert (tmpdir / 'file').read() == ""
2016-06-08 10:11:59 +02:00
def test_iter_without_open(self, lineparser):
2015-03-08 16:53:10 +01:00
"""Test __iter__ without having called open()."""
with pytest.raises(ValueError):
iter(lineparser)
2015-03-08 16:53:10 +01:00
def test_iter(self, lineparser):
2015-03-08 16:53:10 +01:00
"""Test __iter__."""
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
def test_iter_not_found(self, mocker):
2015-03-08 16:53:10 +01:00
"""Test __iter__ with no file."""
open_mock = mocker.patch(
'qutebrowser.misc.lineparser.AppendLineParser._open')
2015-03-08 16:53:10 +01:00
open_mock.side_effect = FileNotFoundError
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():
assert list(linep) == new_data
2015-03-08 16:53:10 +01:00
2016-06-08 11:06:45 +02:00
def test_get_recent_none(self, tmpdir):
2015-03-08 16:53:10 +01:00
"""Test get_recent with no data."""
2016-06-08 11:06:45 +02:00
(tmpdir / 'file2').ensure()
linep = lineparsermod.AppendLineParser(str(tmpdir), 'file2')
assert linep.get_recent() == []
2015-03-08 16:53:10 +01:00
def test_get_recent_little(self, lineparser):
2015-03-08 16:53:10 +01:00
"""Test get_recent with little data."""
data = [e + '\n' for e in self.BASE_DATA]
assert lineparser.get_recent() == data
2015-03-08 16:53:10 +01: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)]
lineparser.new_data = new_data
lineparser.save()
data = os.linesep.join(self.BASE_DATA + new_data) + os.linesep
data = [e + '\n' for e in data[-size:].splitlines()]
assert lineparser.get_recent(size) == data