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
wilder
Ahmad Samir 6 years ago committed by David Faure
parent d1edad3cc4
commit 7c96cf22f1
  1. 11
      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) {

Loading…
Cancel
Save