From 70b8585e9505cc6a9815c6c811684c756863cbf4 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 17 Sep 2017 20:44:08 +0200 Subject: [PATCH] Move qtutils.unset_organization to standarddir --- qutebrowser/utils/qtutils.py | 18 ------------------ qutebrowser/utils/standarddir.py | 23 +++++++++++++++++++++-- tests/unit/utils/test_qtutils.py | 15 --------------- tests/unit/utils/test_standarddir.py | 22 ++++++++++++++++++++++ 4 files changed, 43 insertions(+), 35 deletions(-) diff --git a/qutebrowser/utils/qtutils.py b/qutebrowser/utils/qtutils.py index 6778c30ee..ad02c9a45 100644 --- a/qutebrowser/utils/qtutils.py +++ b/qutebrowser/utils/qtutils.py @@ -35,7 +35,6 @@ import contextlib from PyQt5.QtCore import (qVersion, QEventLoop, QDataStream, QByteArray, QIODevice, QSaveFile, QT_VERSION_STR) -from PyQt5.QtWidgets import QApplication try: from PyQt5.QtWebKit import qWebKitVersion except ImportError: # pragma: no cover @@ -242,23 +241,6 @@ def savefile_open(filename, binary=False, encoding='utf-8'): raise QtOSError(f, msg="Commit failed!") -@contextlib.contextmanager -def unset_organization(): - """Temporarily unset QApplication.organizationName(). - - This is primarily needed in config.py. - """ - qapp = QApplication.instance() - if qapp is not None: - orgname = qapp.organizationName() - qapp.setOrganizationName(None) - try: - yield - finally: - if qapp is not None: - qapp.setOrganizationName(orgname) - - class PyQIODevice(io.BufferedIOBase): """Wrapper for a QIODevice which provides a python interface. diff --git a/qutebrowser/utils/standarddir.py b/qutebrowser/utils/standarddir.py index a4f4dbd7b..468fba51e 100644 --- a/qutebrowser/utils/standarddir.py +++ b/qutebrowser/utils/standarddir.py @@ -23,10 +23,12 @@ import os import sys import shutil import os.path +import contextlib from PyQt5.QtCore import QStandardPaths +from PyQt5.QtWidgets import QApplication -from qutebrowser.utils import log, qtutils, debug, usertypes, message +from qutebrowser.utils import log, debug, usertypes, message # The cached locations _locations = {} @@ -45,6 +47,23 @@ class EmptyValueError(Exception): """Error raised when QStandardPaths returns an empty value.""" +@contextlib.contextmanager +def _unset_organization(): + """Temporarily unset QApplication.organizationName(). + + This is primarily needed in config.py. + """ + qapp = QApplication.instance() + if qapp is not None: + orgname = qapp.organizationName() + qapp.setOrganizationName(None) + try: + yield + finally: + if qapp is not None: + qapp.setOrganizationName(orgname) + + def _init_config(args): """Initialize the location for configs.""" typ = QStandardPaths.ConfigLocation @@ -204,7 +223,7 @@ def _writable_location(typ): # FIXME old Qt getattr(QStandardPaths, 'AppDataLocation', object())], typ_str - with qtutils.unset_organization(): + with _unset_organization(): path = QStandardPaths.writableLocation(typ) log.misc.debug("writable location for {}: {}".format(typ_str, path)) diff --git a/tests/unit/utils/test_qtutils.py b/tests/unit/utils/test_qtutils.py index ff8b81c9a..a87b9c745 100644 --- a/tests/unit/utils/test_qtutils.py +++ b/tests/unit/utils/test_qtutils.py @@ -530,21 +530,6 @@ class TestSavefileOpen: assert data == b'foo\nbar\nbaz' -@pytest.mark.parametrize('orgname, expected', [(None, ''), ('test', 'test')]) -def test_unset_organization(qapp, orgname, expected): - """Test unset_organization. - - Args: - orgname: The organizationName to set initially. - expected: The organizationName which is expected when reading back. - """ - qapp.setOrganizationName(orgname) - assert qapp.organizationName() == expected # sanity check - with qtutils.unset_organization(): - assert qapp.organizationName() == '' - assert qapp.organizationName() == expected - - if test_file is not None and sys.platform != 'darwin': # If we were able to import Python's test_file module, we run some code # here which defines unittest TestCases to run the python tests over diff --git a/tests/unit/utils/test_standarddir.py b/tests/unit/utils/test_standarddir.py index 011e86b42..b828926e9 100644 --- a/tests/unit/utils/test_standarddir.py +++ b/tests/unit/utils/test_standarddir.py @@ -56,6 +56,28 @@ def clear_standarddir_cache_and_patch(qapp, monkeypatch): monkeypatch.setattr(standarddir, '_locations', {}) +@pytest.mark.parametrize('orgname, expected', [(None, ''), ('test', 'test')]) +def test_unset_organization(qapp, orgname, expected): + """Test unset_organization. + + Args: + orgname: The organizationName to set initially. + expected: The organizationName which is expected when reading back. + """ + qapp.setOrganizationName(orgname) + assert qapp.organizationName() == expected # sanity check + with standarddir._unset_organization(): + assert qapp.organizationName() == '' + assert qapp.organizationName() == expected + + +def test_unset_organization_no_qapp(monkeypatch): + """Without a QApplication, _unset_organization should do nothing.""" + monkeypatch.setattr(standarddir.QApplication, 'instance', lambda: None) + with standarddir._unset_organization(): + pass + + def test_fake_mac_config(tmpdir, monkeypatch): """Test standardir.config on a fake Mac.""" monkeypatch.setattr(sys, 'platform', 'darwin')