2014-06-19 09:04:37 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2017-05-09 21:37:03 +02:00
|
|
|
# Copyright 2014-2017 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/>.
|
|
|
|
|
2015-01-24 14:31:58 +01:00
|
|
|
"""Tests for qutebrowser.misc.editor."""
|
2014-05-27 07:43:29 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
import os.path
|
2016-09-15 13:05:53 +02:00
|
|
|
import logging
|
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
|
2016-09-15 13:05:53 +02:00
|
|
|
from qutebrowser.utils import usertypes
|
2015-08-19 09:09:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
2016-09-15 11:56:46 +02:00
|
|
|
def patch_things(config_stub, monkeypatch, stubs):
|
2017-03-01 11:33:41 +01:00
|
|
|
monkeypatch.setattr(editormod.guiprocess, 'QProcess',
|
2015-08-19 09:09:09 +02:00
|
|
|
stubs.fake_qprocess())
|
|
|
|
config_stub.data = {
|
|
|
|
'general': {'editor': [''], 'editor-encoding': 'utf-8'},
|
|
|
|
'input': {},
|
|
|
|
}
|
2017-03-01 11:33:41 +01:00
|
|
|
monkeypatch.setattr(editormod, 'config', config_stub)
|
2015-08-19 09:09:09 +02:00
|
|
|
|
|
|
|
|
2016-08-22 07:40:24 +02:00
|
|
|
@pytest.fixture
|
2016-09-15 13:05:53 +02:00
|
|
|
def editor(caplog):
|
2016-09-14 20:52:32 +02:00
|
|
|
ed = editormod.ExternalEditor()
|
2015-08-19 09:09:09 +02:00
|
|
|
ed.editing_finished = mock.Mock()
|
|
|
|
yield ed
|
2016-09-15 13:05:53 +02:00
|
|
|
with caplog.at_level(logging.ERROR):
|
|
|
|
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
|
|
|
|
2016-01-31 23:15:18 +01:00
|
|
|
@pytest.mark.parametrize('args', [[], ['foo'], ['foo', 'bar']])
|
2015-08-19 09:09:09 +02:00
|
|
|
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(
|
2016-08-07 00:18:22 +02:00
|
|
|
"bin", ["foo", editor._file.name, "bar"])
|
2014-05-27 11:30:57 +02:00
|
|
|
|
2016-02-02 06:53:12 +01:00
|
|
|
def test_placeholder_inline(self, config_stub, editor):
|
|
|
|
"""Test starting editor with placeholder arg inside of another arg."""
|
|
|
|
config_stub.data['general']['editor'] = ['bin', 'foo{}', 'bar']
|
|
|
|
editor.edit("")
|
|
|
|
editor._proc._proc.start.assert_called_with(
|
2016-08-07 00:18:22 +02:00
|
|
|
"bin", ["foo" + editor._file.name, "bar"])
|
2016-02-02 06:53:12 +01:00
|
|
|
|
2014-05-27 07:43:29 +02:00
|
|
|
|
2015-04-08 05:30:02 +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("")
|
2016-08-07 00:18:22 +02:00
|
|
|
filename = editor._file.name
|
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
|
|
|
|
2015-09-11 08:32:16 +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("")
|
2016-08-07 00:18:22 +02:00
|
|
|
filename = editor._file.name
|
2015-04-04 18:24:26 +02:00
|
|
|
assert os.path.exists(filename)
|
2015-09-11 08:32:16 +02:00
|
|
|
|
2015-10-01 14:15:50 +02:00
|
|
|
editor._proc._proc.exitStatus = mock.Mock(
|
2016-03-17 22:10:58 +01:00
|
|
|
return_value=QProcess.CrashExit)
|
2015-09-11 08:32:16 +02:00
|
|
|
editor._proc.finished.emit(1, QProcess.NormalExit)
|
|
|
|
|
2015-10-01 14:15:50 +02:00
|
|
|
assert os.path.exists(filename)
|
|
|
|
|
|
|
|
os.remove(filename)
|
2014-05-27 07:43:29 +02:00
|
|
|
|
2015-09-11 08:32:16 +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("")
|
2016-08-07 00:18:22 +02:00
|
|
|
filename = editor._file.name
|
2015-04-04 18:24:26 +02:00
|
|
|
assert os.path.exists(filename)
|
2015-10-01 14:15:50 +02:00
|
|
|
|
|
|
|
editor._proc._proc.exitStatus = mock.Mock(
|
2016-03-17 22:10:58 +01:00
|
|
|
return_value=QProcess.CrashExit)
|
2015-09-11 08:32:16 +02:00
|
|
|
editor._proc.error.emit(QProcess.Crashed)
|
|
|
|
|
2015-08-19 09:09:09 +02:00
|
|
|
editor._proc.finished.emit(0, QProcess.CrashExit)
|
2015-10-01 14:15:50 +02:00
|
|
|
assert os.path.exists(filename)
|
|
|
|
|
|
|
|
os.remove(filename)
|
2014-05-27 07:43:29 +02:00
|
|
|
|
2016-09-15 13:05:53 +02:00
|
|
|
def test_unreadable(self, message_mock, editor, caplog):
|
2015-08-19 09:34:44 +02:00
|
|
|
"""Test file handling when closing with an unreadable file."""
|
|
|
|
editor.edit("")
|
2016-08-07 00:18:22 +02:00
|
|
|
filename = editor._file.name
|
2015-08-19 09:34:44 +02:00
|
|
|
assert os.path.exists(filename)
|
|
|
|
os.chmod(filename, 0o077)
|
2017-07-03 09:40:39 +02:00
|
|
|
if os.access(filename, os.R_OK):
|
|
|
|
# Docker container or similar
|
|
|
|
pytest.skip("File was still readable")
|
|
|
|
|
2016-09-15 13:05:53 +02:00
|
|
|
with caplog.at_level(logging.ERROR):
|
|
|
|
editor._proc.finished.emit(0, QProcess.NormalExit)
|
2015-08-19 09:34:44 +02:00
|
|
|
assert not os.path.exists(filename)
|
2016-09-15 11:56:46 +02:00
|
|
|
msg = message_mock.getmsg(usertypes.MessageLevel.error)
|
2015-08-19 09:34:44 +02:00
|
|
|
assert msg.text.startswith("Failed to read back edited file: ")
|
|
|
|
|
2016-09-15 13:05:53 +02:00
|
|
|
def test_unwritable(self, monkeypatch, message_mock, editor, tmpdir,
|
|
|
|
caplog):
|
2015-08-19 09:34:44 +02:00
|
|
|
"""Test file handling when the initial file is not writable."""
|
|
|
|
tmpdir.chmod(0)
|
2017-07-03 09:40:39 +02:00
|
|
|
if os.access(str(tmpdir), os.W_OK):
|
|
|
|
# Docker container or similar
|
|
|
|
pytest.skip("File was still writable")
|
|
|
|
|
2017-03-01 11:33:41 +01:00
|
|
|
monkeypatch.setattr(editormod.tempfile, 'tempdir', str(tmpdir))
|
2016-09-15 13:05:53 +02:00
|
|
|
|
|
|
|
with caplog.at_level(logging.ERROR):
|
|
|
|
editor.edit("")
|
|
|
|
|
2016-09-15 11:56:46 +02:00
|
|
|
msg = message_mock.getmsg(usertypes.MessageLevel.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("")
|
|
|
|
|
2014-05-27 11:47:43 +02:00
|
|
|
|
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
|
|
|
|
2016-08-07 00:18:22 +02:00
|
|
|
with open(editor._file.name, 'r', encoding='utf-8') as f:
|
2015-08-19 09:09:09 +02:00
|
|
|
assert f.read() == initial_text
|
2014-05-27 13:06:13 +02:00
|
|
|
|
2016-08-07 00:18:22 +02:00
|
|
|
with open(editor._file.name, 'w', encoding='utf-8') as f:
|
2015-08-19 09:09:09 +02:00
|
|
|
f.write(edited_text)
|
2014-05-27 11:47:43 +02:00
|
|
|
|
2015-08-19 09:09:09 +02:00
|
|
|
editor._proc.finished.emit(0, QProcess.NormalExit)
|
|
|
|
editor.editing_finished.emit.assert_called_with(edited_text)
|