aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAhmad Samir <a.samirh78@gmail.com>2020-07-25 09:43:29 +0000
committerDavid Faure <faure@kde.org>2020-07-25 09:43:29 +0000
commit7c96cf22f148d47317a21b1c73de84708c6ffab1 (patch)
tree12eda35a326f673dfdcec555235b39222cb15609 /src
parentd1edad3cc4de378e0065933f3bbdce692678a381 (diff)
downloadkconfig-7c96cf22f148d47317a21b1c73de84708c6ffab1.tar.gz
kconfig-7c96cf22f148d47317a21b1c73de84708c6ffab1.tar.bz2
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
Diffstat (limited to 'src')
-rw-r--r--src/core/kconfig.cpp11
1 files changed, 11 insertions, 0 deletions
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) {