Improve file handling in test of QSettings.

- Avoid duplication of slashes and use static variable for the
  const part in settingsPath().
- Do not run expensive cleanup twice in init()/cleanup() as was
  before by moving the code into a separate cleanupTestData()
  function called from cleanup() and initTestCase().
- Use QDir::removeRecursively() (which should be able to deal
  with readonly files, etc after 26bcc0565f )
  instead of system calls or the special removePath() function for
  CE/RT.
- Switch QStandardPaths into test mode.

Change-Id: Idcde2d17020eae1ea43e448266e3940c06f174ef
Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com>
This commit is contained in:
Friedemann Kleint 2015-10-21 14:18:01 +02:00
parent 2a1d3f330d
commit a3abf9d76b

View File

@ -89,12 +89,11 @@ class tst_QSettings : public QObject
Q_OBJECT
public:
tst_QSettings() : m_canWriteNativeSystemSettings(canWriteNativeSystemSettings()) {}
tst_QSettings();
public slots:
void initTestCase();
void init();
void cleanup();
void cleanup() { cleanupTestFiles(); }
private slots:
void getSetCheck();
void ctor_data();
@ -165,6 +164,8 @@ private slots:
void bom();
private:
void cleanupTestFiles();
const bool m_canWriteNativeSystemSettings;
};
@ -180,39 +181,16 @@ void tst_QSettings::getSetCheck()
QCOMPARE(true, obj1.fallbacksEnabled());
}
#if defined(Q_OS_WINCE) || defined(Q_OS_WINRT)
static void removePath(const QString& _path)
{
QString path = _path;
QDir dir(path);
if (!dir.exists())
return;
QStringList entries = dir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot);
foreach(QString name, entries) {
QString absolute = path + name;
if (QFileInfo(absolute).isDir())
removePath(absolute+"\\");
else
QFile::remove(absolute);
}
dir.cdUp();
if (path[path.size()-1] == '\\')
path = path.left(path.size()-1);
dir.rmdir(path.mid(path.lastIndexOf('\\')+1));
}
#endif
static QString settingsPath(const char *path = "")
static QString settingsPath(const char *path = Q_NULLPTR)
{
// Temporary path for files that are specified explicitly in the constructor.
#ifndef Q_OS_WINRT
QString tempPath = QDir::tempPath();
static const QString tempPath = QDir::tempPath() + QLatin1String("/tst_QSettings");
#else
QString tempPath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
static const QString tempPath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)
+ QLatin1String("/tst_QSettings");
#endif
if (tempPath.endsWith("/"))
tempPath.truncate(tempPath.size() - 1);
return QDir::toNativeSeparators(tempPath + "/tst_QSettings/" + QLatin1String(path));
return path && *path ? tempPath + QLatin1Char('/') + QLatin1String(path) : tempPath;
}
static bool readCustom1File(QIODevice &device, QSettings::SettingsMap &map)
@ -282,6 +260,12 @@ static void populateWithFormats()
QTest::newRow("custom2") << QSettings::CustomFormat2;
}
tst_QSettings::tst_QSettings()
: m_canWriteNativeSystemSettings(canWriteNativeSystemSettings())
{
QStandardPaths::setTestModeEnabled(true);
}
void tst_QSettings::initTestCase()
{
if (!m_canWriteNativeSystemSettings)
@ -294,13 +278,19 @@ void tst_QSettings::initTestCase()
);
QVERIFY(custom1 == QSettings::CustomFormat1);
QVERIFY(custom2 == QSettings::CustomFormat2);
cleanupTestFiles();
}
void tst_QSettings::init()
void tst_QSettings::cleanupTestFiles()
{
QSettings::setSystemIniPath(settingsPath("__system__"));
QSettings::setUserIniPath(settingsPath("__user__"));
QDir settingsDir(settingsPath());
if (settingsDir.exists())
QVERIFY(settingsDir.removeRecursively());
#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
QSettings("HKEY_CURRENT_USER\\Software\\software.org", QSettings::NativeFormat).clear();
QSettings("HKEY_CURRENT_USER\\Software\\other.software.org", QSettings::NativeFormat).clear();
@ -316,17 +306,7 @@ void tst_QSettings::init()
QSettings("HKEY_LOCAL_MACHINE\\Software\\bat", QSettings::NativeFormat).clear();
QSettings("HKEY_LOCAL_MACHINE\\Software\\baz", QSettings::NativeFormat).clear();
}
if (QDir(settingsPath()).exists()) {
#if defined(Q_OS_WINCE)
removePath(settingsPath());
#else
if (QSysInfo::windowsVersion() & QSysInfo::WV_NT_based)
system(QString("rmdir /Q /S %1").arg(settingsPath()).toLatin1());
else
system(QString("deltree /Y %1").arg(settingsPath()).toLatin1());
#endif
}
#elif defined(Q_OS_DARWIN)
#elif defined(Q_OS_DARWIN) || defined(Q_OS_WINRT)
QSettings(QSettings::UserScope, "software.org", "KillerAPP").clear();
QSettings(QSettings::SystemScope, "software.org", "KillerAPP").clear();
QSettings(QSettings::UserScope, "other.software.org", "KillerAPP").clear();
@ -337,31 +317,16 @@ void tst_QSettings::init()
QSettings(QSettings::SystemScope, "other.software.org").clear();
#endif
#if !defined(Q_OS_WIN)
system(QString("chmod -R u+rw %1 2> /dev/null").arg(settingsPath()).toLatin1());
system(QString("rm -fr %1 2> /dev/null").arg(settingsPath()).toLatin1());
#endif
const QString foo(QLatin1String("foo"));
#if defined(Q_OS_WINRT)
QSettings(QSettings::UserScope, "software.org", "KillerAPP").clear();
QSettings(QSettings::SystemScope, "software.org", "KillerAPP").clear();
QSettings(QSettings::UserScope, "other.software.org", "KillerAPP").clear();
QSettings(QSettings::SystemScope, "other.software.org", "KillerAPP").clear();
QSettings(QSettings::UserScope, "software.org").clear();
QSettings(QSettings::SystemScope, "software.org").clear();
QSettings(QSettings::UserScope, "other.software.org").clear();
QSettings(QSettings::SystemScope, "other.software.org").clear();
QSettings("foo", QSettings::NativeFormat).clear();
removePath(settingsPath());
QFile::remove(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/foo");
QSettings(foo, QSettings::NativeFormat).clear();
QFile fooFile(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + QLatin1Char('/') + foo);
#else
QFile::remove("foo");
QFile fooFile(foo);
#endif
}
void tst_QSettings::cleanup()
{
init();
if (fooFile.exists())
QVERIFY2(fooFile.remove(), qPrintable(fooFile.errorString()));
}
/*