refs #695 Allow to set a mock root directory for caches, for unit tests.

This commit is contained in:
Mathew Sutcliffe
2016-07-28 22:00:27 +01:00
parent 9339991644
commit 61f3655b2c
4 changed files with 36 additions and 8 deletions

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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 <QList>
#include <QMetaMethod>
#include <QMutexLocker>
#include <QStandardPaths>
#include <QThread>
#include <Qt>
#include <algorithm>
@@ -46,6 +48,28 @@ namespace BlackMisc
bool isSafeToIncrement(const T &value) { return value < std::numeric_limits<T>::max(); }
//! \private
std::pair<QString &, std::atomic<bool> &> getCacheRootDirectoryMutable()
{
static QString dir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) +
"/org.swift-project/" + CDirectoryUtils::normalizedApplicationDirectory();
static std::atomic<bool> 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
////////////////////////////////

View File

@@ -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;