From 7c96cf22f148d47317a21b1c73de84708c6ffab1 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Sat, 25 Jul 2020 09:43:29 +0000 Subject: Update sGlobalFileName when QStandardPaths TestMode is toggled When running unit tests, usually QStandardPaths TestMode is enabled so as not to mess up the config files in the home dir of the dev running the unit tests; sGlobalFileName, a global static, was defined only once, which meant that if KSharedConfig::openConfig() is called before the unit test has had a chance to call QStandardPaths::setTestModeEnabled(true), then sGlobalFileName will go on referring to the wrong kdeglobals file (as can be seen in dfaure's debugging of the issue at [1]). Change the code so as to detect QStandardPaths TestMode status and change sGlobalFileName as needed. All unit tests still pass. [1] https://invent.kde.org/frameworks/kio/-/merge_requests/77#note_74124 --- src/core/kconfig.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/core/kconfig.cpp') diff --git a/src/core/kconfig.cpp b/src/core/kconfig.cpp index 0b742a36..d9232fa5 100644 --- a/src/core/kconfig.cpp +++ b/src/core/kconfig.cpp @@ -39,6 +39,9 @@ bool KConfigPrivate::mappingsRegistered = false; +// For caching purposes +static bool s_wasTestModeEnabled = false; + Q_GLOBAL_STATIC(QStringList, s_globalFiles) // For caching purposes. static QBasicMutex s_globalFilesMutex; Q_GLOBAL_STATIC_WITH_ARGS(QString, sGlobalFileName, (QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QLatin1String("/kdeglobals"))) @@ -56,6 +59,14 @@ KConfigPrivate::KConfigPrivate(KConfig::OpenFlags flags, bFileImmutable(false), bForceGlobal(false), bSuppressGlobal(false), configState(KConfigBase::NoAccess) { + const bool isTestMode = QStandardPaths::isTestModeEnabled(); + // If sGlobalFileName was initialised and testMode has been toggled, + // sGlobalFileName may need to be updated to point to the correct kdeglobals file + if (sGlobalFileName.exists() && s_wasTestModeEnabled != isTestMode) { + s_wasTestModeEnabled = isTestMode; + *sGlobalFileName = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QLatin1String("/kdeglobals"); + } + static QBasicAtomicInt use_etc_kderc = Q_BASIC_ATOMIC_INITIALIZER(-1); #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0) if (use_etc_kderc.load() < 0) { -- cgit v1.2.1