summaryrefslogtreecommitdiff
path: root/tests/unit/misc/test_lineparser.py
blob: 20f20809d735c2648c93db399a39157578e6fc3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# SPDX-FileCopyrightText: Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later

"""Tests for qutebrowser.misc.lineparser."""

import pathlib
from unittest import mock

import pytest

from qutebrowser.misc import lineparser as lineparsermod


class TestBaseLineParser:

    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)

    def test_prepare_save_missing(self, mocker, lineparser):
        """Test if _prepare_save does what it's supposed to do."""
        os_mock = mocker.patch('qutebrowser.misc.lineparser.os')
        lineparser._prepare_save()
        os_mock.makedirs.assert_called_with(self.CONFDIR, 0o755, exist_ok=True)

    def test_double_open(self, mocker, lineparser):
        """Test if _open refuses reentry."""
        mocker.patch('builtins.open', mock.mock_open())

        with lineparser._open('r'):
            with pytest.raises(OSError,
                               match="Refusing to double-open LineParser."):
                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)

        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(
            str(pathlib.Path(self.CONFDIR) / self.FILENAME), 'rb')

        open_mock().write.assert_has_calls([
            mock.call(testdata),
            mock.call(b'\n')
        ])


class TestLineParser:

    @pytest.fixture
    def lineparser(self, tmp_path):
        """Fixture to get a LineParser for tests."""
        lp = lineparsermod.LineParser(str(tmp_path), 'file')
        lp.save()
        return lp

    def test_init(self, tmp_path):
        """Test if creating a line parser correctly reads its file."""
        (tmp_path / 'file').write_text('one\ntwo\n')
        lineparser = lineparsermod.LineParser(str(tmp_path), 'file')
        assert lineparser.data == ['one', 'two']

        (tmp_path / 'file').write_bytes(b'\xfe\n\xff\n')
        lineparser = lineparsermod.LineParser(str(tmp_path), 'file',
                                              binary=True)
        assert lineparser.data == [b'\xfe', b'\xff']

    def test_clear(self, tmp_path, lineparser):
        """Test if clear() empties its file."""
        lineparser.data = ['one', 'two']
        lineparser.save()
        assert (tmp_path / 'file').read_text() == 'one\ntwo\n'
        lineparser.clear()
        assert not lineparser.data
        assert (tmp_path / 'file').read_text() == ''

    def test_double_open(self, lineparser):
        """Test if save() bails on an already open file."""
        with lineparser._open('r'):
            with pytest.raises(OSError,
                               match="Refusing to double-open LineParser."):
                lineparser.save()

    def test_prepare_save(self, tmp_path, lineparser):
        """Test if save() bails when _prepare_save() returns False."""
        (tmp_path / 'file').write_text('pristine\n')
        lineparser.data = ['changed']
        lineparser._prepare_save = lambda: False
        lineparser.save()
        assert (tmp_path / 'file').read_text() == 'pristine\n'