From 8b0144f6a35dbf2afff4332e26e6484b6b917460 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 27 May 2014 07:43:29 +0200 Subject: [PATCH] Add editor tests --- qutebrowser/test/utils/test_editor.py | 120 ++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 qutebrowser/test/utils/test_editor.py diff --git a/qutebrowser/test/utils/test_editor.py b/qutebrowser/test/utils/test_editor.py new file mode 100644 index 000000000..78ea9bdea --- /dev/null +++ b/qutebrowser/test/utils/test_editor.py @@ -0,0 +1,120 @@ +# Copyright 2014 Florian Bruhin (The Compiler) +# +# 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 . + +"""Tests for qutebrowser.utils.editor.""" + +import os +import os.path +import logging +import unittest +from unittest import TestCase +from unittest.mock import Mock + +from PyQt5.QtCore import QProcess + +import qutebrowser.utils.editor as editor + + +class ConfigStub: + + """Stub for editor.config.""" + + def __init__(self, editor): + self.editor = editor + + def get(self, sect, opt): + if sect == 'general' and opt == 'editor': + return self.editor + else: + raise ValueError("Invalid option {} -> {}".format(sect, opt)) + + +class FakeQProcess: + + finished = Mock() + error = Mock() + + NormalExit = QProcess.NormalExit + CrashExit = QProcess.CrashExit + + FailedToStart = QProcess.FailedToStart + Crashed = QProcess.Crashed + Timedout = QProcess.Timedout + WriteError = QProcess.WriteError + ReadError = QProcess.ReadError + UnknownError = QProcess.UnknownError + + def __init__(self, parent): + self.executable = None + self.args = None + + def start(self, executable, args): + self.executable = executable + self.args = args + self.on_started() + + def on_started(self): + pass + + +def setUpModule(): + editor.message = Mock() + editor.logger = Mock() + editor.QProcess = FakeQProcess + + +class FileHandlingTests(TestCase): + + """Test creation/deletion of tempfile.""" + + def setUp(self): + self.editor = editor.ExternalEditor() + self.editor.editing_finished = Mock() + editor.config = ConfigStub([""]) + + def test_file_handling_closed_ok(self): + """Test file handling when closing with an exitstatus == 0.""" + self.editor.edit("") + filename = self.editor.filename + self.assertTrue(os.path.exists(filename)) + self.editor.on_proc_closed(0, QProcess.NormalExit) + self.assertFalse(os.path.exists(filename)) + + def test_file_handling_closed_error(self): + """Test file handling when closing with an exitstatus != 0.""" + self.editor.edit("") + filename = self.editor.filename + self.assertTrue(os.path.exists(filename)) + self.editor.on_proc_closed(1, QProcess.NormalExit) + self.assertFalse(os.path.exists(filename)) + + def test_file_handling_closed_crash(self): + """Test file handling when closing with a crash.""" + self.editor.edit("") + filename = self.editor.filename + self.assertTrue(os.path.exists(filename)) + self.editor.on_proc_error(QProcess.Crashed) + self.editor.on_proc_closed(0, QProcess.CrashExit) + self.assertFalse(os.path.exists(filename)) + + + #self.editor.proc.start.assert_called_with("") + #self.assertTrue(self.editor.proc. + + +if __name__ == '__main__': + unittest.main()