qutebrowser/tests/unit/misc/test_editor.py

155 lines
5.2 KiB
Python
Raw Normal View History

2014-06-19 09:04:37 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2015-01-03 15:51:31 +01:00
# Copyright 2014-2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
2014-05-27 07:43:29 +02: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.editor."""
2014-05-27 07:43:29 +02:00
import os
import os.path
2014-08-26 19:10:14 +02:00
from unittest import mock
2014-05-27 07:43:29 +02:00
from PyQt5.QtCore import QProcess
2015-04-04 18:24:26 +02:00
import pytest
2014-05-27 07:43:29 +02:00
2015-08-19 09:09:09 +02:00
from qutebrowser.misc import editor as editormod
@pytest.fixture(autouse=True)
def patch_things(config_stub, message_mock, monkeypatch, stubs):
message_mock.patch('qutebrowser.misc.editor.message')
monkeypatch.setattr('qutebrowser.misc.editor.guiprocess.QProcess',
stubs.fake_qprocess())
config_stub.data = {
'general': {'editor': [''], 'editor-encoding': 'utf-8'},
'input': {},
}
monkeypatch.setattr('qutebrowser.misc.editor.config', config_stub)
@pytest.yield_fixture
def editor():
ed = editormod.ExternalEditor(0)
ed.editing_finished = mock.Mock()
yield ed
ed._cleanup()
2014-05-27 07:43:29 +02:00
2015-04-04 18:24:26 +02:00
class TestArg:
2014-05-27 11:17:27 +02:00
2014-05-27 13:06:13 +02:00
"""Test argument handling.
Attributes:
editor: The ExternalEditor instance to test.
"""
2014-05-27 11:17:27 +02:00
2015-08-19 09:09:09 +02:00
@pytest.mark.parametrize('args', [[], ['foo', 'bar'], ['foo{}bar']])
def test_start_no_placeholder(self, config_stub, editor, args):
2014-05-27 11:30:57 +02:00
"""Test starting editor without arguments."""
2015-08-19 09:09:09 +02:00
config_stub.data['general']['editor'] = ['bin'] + args
editor.edit("")
editor._proc._proc.start.assert_called_with("bin", args)
def test_placeholder(self, config_stub, editor):
2014-05-27 11:17:27 +02:00
"""Test starting editor with placeholder argument."""
2015-08-19 09:09:09 +02:00
config_stub.data['general']['editor'] = ['bin', 'foo', '{}', 'bar']
editor.edit("")
editor._proc._proc.start.assert_called_with(
"bin", ["foo", editor._filename, "bar"])
2014-05-27 11:30:57 +02:00
2014-05-27 07:43:29 +02:00
class TestFileHandling:
2015-04-05 20:30:31 +02:00
2015-08-19 09:09:09 +02:00
"""Test creation/deletion of tempfile."""
2014-05-27 13:06:13 +02:00
2015-08-19 09:09:09 +02:00
def test_ok(self, editor):
2015-03-31 20:49:29 +02:00
"""Test file handling when closing with an exit status == 0."""
2015-08-19 09:09:09 +02:00
editor.edit("")
filename = editor._filename
2015-04-04 18:24:26 +02:00
assert os.path.exists(filename)
2015-08-19 09:34:44 +02:00
assert os.path.basename(filename).startswith('qutebrowser-editor-')
2015-08-19 09:09:09 +02:00
editor._proc.finished.emit(0, QProcess.NormalExit)
2015-04-04 18:24:26 +02:00
assert not os.path.exists(filename)
2014-05-27 07:43:29 +02:00
def test_error(self, editor):
2015-03-31 20:49:29 +02:00
"""Test file handling when closing with an exit status != 0."""
2015-08-19 09:09:09 +02:00
editor.edit("")
filename = editor._filename
2015-04-04 18:24:26 +02:00
assert os.path.exists(filename)
editor._proc.finished.emit(1, QProcess.NormalExit)
2015-04-04 18:24:26 +02:00
assert not os.path.exists(filename)
2014-05-27 07:43:29 +02:00
def test_crash(self, editor):
2014-05-27 07:43:29 +02:00
"""Test file handling when closing with a crash."""
2015-08-19 09:09:09 +02:00
editor.edit("")
filename = editor._filename
2015-04-04 18:24:26 +02:00
assert os.path.exists(filename)
editor._proc.error.emit(QProcess.Crashed)
2015-08-19 09:09:09 +02:00
editor._proc.finished.emit(0, QProcess.CrashExit)
2015-04-04 18:24:26 +02:00
assert not os.path.exists(filename)
2014-05-27 07:43:29 +02:00
@pytest.mark.posix
2015-08-19 09:34:44 +02:00
def test_unreadable(self, message_mock, editor):
"""Test file handling when closing with an unreadable file."""
editor.edit("")
filename = editor._filename
assert os.path.exists(filename)
os.chmod(filename, 0o077)
editor._proc.finished.emit(0, QProcess.NormalExit)
assert not os.path.exists(filename)
msg = message_mock.getmsg(message_mock.Level.error)
2015-08-19 09:34:44 +02:00
assert msg.text.startswith("Failed to read back edited file: ")
@pytest.mark.posix
2015-08-19 09:34:44 +02:00
def test_unwritable(self, monkeypatch, message_mock, editor, tmpdir):
"""Test file handling when the initial file is not writable."""
tmpdir.chmod(0)
monkeypatch.setattr('qutebrowser.misc.editor.tempfile.tempdir',
str(tmpdir))
editor.edit("")
msg = message_mock.getmsg(message_mock.Level.error)
2015-08-19 09:34:44 +02:00
assert msg.text.startswith("Failed to create initial file: ")
assert editor._proc is None
def test_double_edit(self, editor):
editor.edit("")
with pytest.raises(ValueError):
editor.edit("")
2015-08-19 09:09:09 +02:00
@pytest.mark.parametrize('initial_text, edited_text', [
('', 'Hello'),
('Hello', 'World'),
('Hällö Wörld', 'Überprüfung'),
('\u2603', '\u2601') # Unicode snowman -> cloud
])
def test_modify(editor, initial_text, edited_text):
"""Test if inputs get modified correctly."""
editor.edit(initial_text)
2015-04-05 20:30:31 +02:00
2015-08-19 09:09:09 +02:00
with open(editor._filename, 'r', encoding='utf-8') as f:
assert f.read() == initial_text
2014-05-27 13:06:13 +02:00
2015-08-19 09:09:09 +02:00
with open(editor._filename, 'w', encoding='utf-8') as f:
f.write(edited_text)
2015-08-19 09:09:09 +02:00
editor._proc.finished.emit(0, QProcess.NormalExit)
editor.editing_finished.emit.assert_called_with(edited_text)