diff --git a/src/blackmisc/datacache.cpp b/src/blackmisc/datacache.cpp index c15490dd2..33678d53e 100644 --- a/src/blackmisc/datacache.cpp +++ b/src/blackmisc/datacache.cpp @@ -99,10 +99,7 @@ namespace BlackMisc const QString &CDataCache::persistentStore() { - static const QString dir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + - "/org.swift-project/" + - CDirectoryUtils::normalizedApplicationDirectory() + - "/data/cache/core"; + static const QString dir = getCacheRootDirectory() + "/data/cache/core"; return dir; } diff --git a/src/blackmisc/settingscache.cpp b/src/blackmisc/settingscache.cpp index d3dd4b387..81366eb9f 100644 --- a/src/blackmisc/settingscache.cpp +++ b/src/blackmisc/settingscache.cpp @@ -26,10 +26,7 @@ namespace BlackMisc const QString &CSettingsCache::persistentStore() { - static const QString dir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + - "/org.swift-project/" + - CDirectoryUtils::normalizedApplicationDirectory() + - "/settings/core"; + static const QString dir = getCacheRootDirectory() + "/settings/core"; return dir; } diff --git a/src/blackmisc/valuecache.cpp b/src/blackmisc/valuecache.cpp index 8cb475dac..b1e5532af 100644 --- a/src/blackmisc/valuecache.cpp +++ b/src/blackmisc/valuecache.cpp @@ -11,6 +11,7 @@ #include "blackmisc/valuecache.h" #include "blackmisc/atomicfile.h" +#include "blackmisc/directoryutils.h" #include "blackmisc/identifier.h" #include "blackmisc/lockfree.h" #include "blackmisc/logcategory.h" @@ -28,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -46,6 +48,28 @@ namespace BlackMisc bool isSafeToIncrement(const T &value) { return value < std::numeric_limits::max(); } + //! \private + std::pair &> getCacheRootDirectoryMutable() + { + static QString dir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + + "/org.swift-project/" + CDirectoryUtils::normalizedApplicationDirectory(); + static std::atomic frozen { false }; + return { dir, frozen }; + } + + void setMockCacheRootDirectory(const QString &dir) + { + Q_ASSERT_X(! getCacheRootDirectoryMutable().second, Q_FUNC_INFO, "Too late to call this function"); + getCacheRootDirectoryMutable().first = dir; + } + + const QString &CValueCache::getCacheRootDirectory() + { + getCacheRootDirectoryMutable().second = true; + return getCacheRootDirectoryMutable().first; + } + + //////////////////////////////// // CValueCachePacket //////////////////////////////// diff --git a/src/blackmisc/valuecache.h b/src/blackmisc/valuecache.h index c00e268cc..9df45ab49 100644 --- a/src/blackmisc/valuecache.h +++ b/src/blackmisc/valuecache.h @@ -48,6 +48,13 @@ namespace BlackMisc { class CLogCategoryList; + /*! + * Overwrite the default root directory for cache and settings, for testing purposes. + * + * May not be called after any cache or settings objects have been constructed. + */ + BLACKMISC_EXPORT void setMockCacheRootDirectory(const QString &path); + /*! * Value class used for signalling changed values in the cache. */ @@ -280,6 +287,9 @@ namespace BlackMisc //! \threadsafe qint64 getTimestampSync(const QString &key) { return std::get<1>(getValue(key)); } + //! \private + static const QString &getCacheRootDirectory(); + private: friend class Private::CValuePage; struct Element;