From 2986f7b615ed8d036b04d02a366b7a2fd36690cc Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Fri, 6 Jan 2017 13:32:46 +0100 Subject: [PATCH] add tests for utils.open_file --- tests/unit/utils/test_utils.py | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/unit/utils/test_utils.py b/tests/unit/utils/test_utils.py index 73252ddc4..c64d00e4d 100644 --- a/tests/unit/utils/test_utils.py +++ b/tests/unit/utils/test_utils.py @@ -27,6 +27,7 @@ import logging import functools import collections import socket +import re from PyQt5.QtCore import Qt from PyQt5.QtGui import QColor, QClipboard @@ -984,3 +985,45 @@ def test_random_port(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('localhost', port)) sock.close() + + +class TestOpenFile: + + def test_cmdline_without_argument(self, caplog, config_stub): + config_stub.data = {'general': {'default-open-dispatcher': ''}} + cmdline = '{} -c pass'.format(sys.executable) + utils.open_file('/foo/bar', cmdline) + result = caplog.records[0].message + assert re.match( + r'Opening /foo/bar with \[.*python.*/foo/bar.*\]', result) + + def test_cmdline_with_argument(self, caplog, config_stub): + config_stub.data = {'general': {'default-open-dispatcher': ''}} + cmdline = '{} -c pass {{}} raboof'.format(sys.executable) + utils.open_file('/foo/bar', cmdline) + result = caplog.records[0].message + assert re.match( + r"Opening /foo/bar with \[.*python.*/foo/bar.*'raboof'\]", result) + + def test_setting_override(self, caplog, config_stub): + cmdline = '{} -c pass'.format(sys.executable) + config_stub.data = {'general': {'default-open-dispatcher': cmdline}} + utils.open_file('/foo/bar') + result = caplog.records[0].message + assert re.match( + r"Opening /foo/bar with \[.*python.*/foo/bar.*\]", result) + + def test_system_default_application(self, caplog, config_stub, + monkeypatch): + self.open_url_called = False + config_stub.data = {'general': {'default-open-dispatcher': ''}} + monkeypatch.setattr('PyQt5.QtGui.QDesktopServices.openUrl', + self.mock_open_url) + utils.open_file('/foo/bar') + result = caplog.records[0].message + assert re.match( + r"Opening /foo/bar with the system application", result) + assert self.open_url_called + + def mock_open_url(self, url): + self.open_url_called = True