From c38b88497a833e482e6892b72c8f52adec6de857 Mon Sep 17 00:00:00 2001 From: Jenkins CI Date: Wed, 18 Dec 2013 00:45:18 +0000 Subject: Initial import from the monolithic kdelibs. This is the beginning of revision history for this module. If you want to look at revision history older than this, please refer to the techbase wiki for how to use Git history grafting. At the time of writing, this wiki is located here: http://community.kde.org/Frameworks/GitOldHistory If you have already performed the grafting and you don't see any history beyond this commit, try running "git log" with the "--follow" argument. Branched from the monolithic repo, kdelibs frameworks branch, at commit 162066dbbecde401a7347a1cff4fe72a9c919f58 --- tier1/kconfig/autotests/test_kconf_update.cpp | 553 ++++++++++++++++++++++++++ 1 file changed, 553 insertions(+) create mode 100644 tier1/kconfig/autotests/test_kconf_update.cpp (limited to 'tier1/kconfig/autotests/test_kconf_update.cpp') diff --git a/tier1/kconfig/autotests/test_kconf_update.cpp b/tier1/kconfig/autotests/test_kconf_update.cpp new file mode 100644 index 00000000..8fae1c74 --- /dev/null +++ b/tier1/kconfig/autotests/test_kconf_update.cpp @@ -0,0 +1,553 @@ +/* This file is part of the KDE libraries + Copyright 2010 Canonical Ltd + Author: Aurélien Gâteau + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License (LGPL) as published by the Free Software Foundation; + either version 2 of the License, or (at your option) any later + version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ +#include "test_kconf_update.h" + +// Qt +#include +#include +#include +#include +#include +#include +#include "config-kconf.h" + +#include + +// QT5 TODO QTEST_GUILESS_MAIN(TestKConfUpdate) +QTEST_MAIN(TestKConfUpdate) + +static void writeFile(const QString &path, const QString &content) +{ + QFile file(path); + bool ok = file.open(QIODevice::WriteOnly); + Q_UNUSED(ok) // silence warnings + Q_ASSERT(ok); + file.write(content.toUtf8()); +} + +static QString readFile(const QString &path) +{ + QFile file(path); + bool ok = file.open(QIODevice::ReadOnly); + Q_UNUSED(ok) // silence warnings + Q_ASSERT(ok); + return QString::fromUtf8(file.readAll()); +} + +static QTemporaryFile* writeUpdFile(const QString &content) +{ + QTemporaryFile* file = new QTemporaryFile(QDir::tempPath() + QLatin1String("/test_kconf_update_XXXXXX.upd")); + bool ok = file->open(); + Q_UNUSED(ok) // silence warnings + Q_ASSERT(ok); + file->write(content.toUtf8()); + file->flush(); + return file; +} + +static void runKConfUpdate(const QString &updPath) +{ + QString exePath = KCONF_UPDATE_BINARY_DIR "/kconf_update"; + QVERIFY(QFile::exists(exePath)); + QProcess::execute(exePath, QStringList() << "--debug" << updPath); +} + +void TestKConfUpdate::test_data() +{ + QTest::addColumn("updContent"); + QTest::addColumn("oldConfName"); + QTest::addColumn("oldConfContent"); + QTest::addColumn("newConfName"); + QTest::addColumn("expectedNewConfContent"); + QTest::addColumn("expectedOldConfContent"); + + QTest::newRow("moveKeysSameFile") + << + "File=testrc\n" + "Group=group\n" + "Key=old,new\n" + "Options=overwrite\n" + << + "testrc" + << + "[group]\n" + "old=value\n" + << + "testrc" + << + "[$Version]\n" + "update_info=%1\n" + "\n" + "[group]\n" + "new=value\n" + << + "" + ; + QTest::newRow("moveKeysOtherFile") + << + "File=oldrc,newrc\n" + "Group=group1,group2\n" + "Key=old,new\n" + "Options=overwrite\n" + << + "oldrc" + << + "[group1]\n" + "old=value\n" + "[stay]\n" + "foo=bar\n" + << + "newrc" + << + "[$Version]\n" + "update_info=%1\n" + "\n" + "[group2]\n" + "new=value\n" + << + "[$Version]\n" + "update_info=%1\n" + "\n" + "[stay]\n" + "foo=bar\n" + ; + QTest::newRow("allKeys") + << + "File=testrc\n" + "Group=group1,group2\n" + "AllKeys\n" + << + "testrc" + << + "[group1]\n" + "key1=value1\n" + "key2=value2\n" + "\n" + "[stay]\n" + "foo=bar\n" + << + "testrc" + << + "[$Version]\n" + "update_info=%1\n" + "\n" + "[group2]\n" + "key1=value1\n" + "key2=value2\n" + "\n" + "[stay]\n" + "foo=bar\n" + << + "" + ; + QTest::newRow("allKeysSubGroup") + << + "File=testrc\n" + "Group=[group][sub1],[group][sub2]\n" + "AllKeys\n" + << + "testrc" + << + "[group][sub1]\n" + "key1=value1\n" + "key2=value2\n" + "\n" + "[group][sub1][subsub]\n" + "key3=value3\n" + "key4=value4\n" + "\n" + "[stay]\n" + "foo=bar\n" + << + "testrc" + << + "[$Version]\n" + "update_info=%1\n" + "\n" + "[group][sub2]\n" + "key1=value1\n" + "key2=value2\n" + "\n" + "[group][sub2][subsub]\n" + "key3=value3\n" + "key4=value4\n" + "\n" + "[stay]\n" + "foo=bar\n" + << + "" + ; + QTest::newRow("removeGroup") + << + "File=testrc\n" + "RemoveGroup=remove\n" + << + "testrc" + << + "[keep]\n" + "key=value\n" + "" + "[remove]\n" + "key=value\n" + << + "testrc" + << + "[$Version]\n" + "update_info=%1\n" + "\n" + "[keep]\n" + "key=value\n" + << + "" + ; + QTest::newRow("moveKeysSameFileDontExist") + << + "File=testrc\n" + "Group=group,group2\n" + "Key=key1\n" + "Key=key2\n" + << + "testrc" + << + "[group]\n" + "key1=value1\n" + "key3=value3\n" + << + "testrc" + << + "[$Version]\n" + "update_info=%1\n" + "\n" + "[group]\n" + "key3=value3\n" + "\n" + "[group2]\n" + "key1=value1\n" + << + "" + ; +} + +void TestKConfUpdate::test() +{ + QFETCH(QString, updContent); + QFETCH(QString, oldConfName); + QFETCH(QString, oldConfContent); + QFETCH(QString, newConfName); + QFETCH(QString, expectedNewConfContent); + QFETCH(QString, expectedOldConfContent); + + // Prepend the Id= field to the upd content + updContent = QString("Id=%1\n").arg(QTest::currentDataTag()) + updContent; + + QString oldConfPath = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QLatin1Char('/') + oldConfName; + QString newConfPath = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QLatin1Char('/') + newConfName; + + QFile::remove(oldConfPath); + QFile::remove(newConfPath); + + writeFile(oldConfPath, oldConfContent); + QSharedPointer updFile(writeUpdFile(updContent)); + runKConfUpdate(updFile->fileName()); + + QString updateInfo = QString("%1:%2") + .arg(updFile->fileName().section('/', -1)) + .arg(QTest::currentDataTag()); + + QString newConfContentAfter = readFile(newConfPath); + expectedNewConfContent = expectedNewConfContent.arg(updateInfo); + QCOMPARE(newConfContentAfter, expectedNewConfContent); + + if (oldConfName != newConfName) { + QString oldConfContentAfter = readFile(oldConfPath); + expectedOldConfContent = expectedOldConfContent.arg(updateInfo); + QCOMPARE(oldConfContentAfter, expectedOldConfContent); + } +} + +void TestKConfUpdate::testScript_data() +{ + QTest::addColumn("updContent"); + QTest::addColumn("updScript"); + QTest::addColumn("oldConfContent"); + QTest::addColumn("expectedNewConfContent"); + + QTest::newRow("delete-key") + << + "File=testrc\n" + "Group=group\n" + "Script=test.sh,sh\n" + << + "echo '# DELETE deprecated'\n" + << + "[group]\n" + "deprecated=foo\n" + "valid=bar\n" + << + "[$Version]\n" + "update_info=%1\n" + "\n" + "[group]\n" + "valid=bar\n" + ; + + QTest::newRow("delete-key2") + << + "File=testrc\n" + "Script=test.sh,sh\n" + << + "echo '# DELETE [group]deprecated'\n" + "echo '# DELETE [group][sub]deprecated2'\n" + << + "[group]\n" + "deprecated=foo\n" + "valid=bar\n" + "\n" + "[group][sub]\n" + "deprecated2=foo\n" + "valid2=bar\n" + << + "[$Version]\n" + "update_info=%1\n" + "\n" + "[group]\n" + "valid=bar\n" + "\n" + "[group][sub]\n" + "valid2=bar\n" + ; + + QTest::newRow("delete-group") + << + "File=testrc\n" + "Script=test.sh,sh\n" + << + "echo '# DELETEGROUP [group1]'\n" + "echo '# DELETEGROUP [group2][sub]'\n" + << + "[group1]\n" + "key=value\n" + "\n" + "[group2]\n" + "valid=bar\n" + "\n" + "[group2][sub]\n" + "key=value\n" + << + "[$Version]\n" + "update_info=%1\n" + "\n" + "[group2]\n" + "valid=bar\n" + ; + + QTest::newRow("delete-group2") + << + "File=testrc\n" + "Group=group\n" + "Script=test.sh,sh\n" + << + "echo '# DELETEGROUP'\n" + << + "[group]\n" + "key=value\n" + "\n" + "[group2]\n" + "valid=bar\n" + << + "[$Version]\n" + "update_info=%1\n" + "\n" + "[group2]\n" + "valid=bar\n" + ; + + QTest::newRow("new-key") + << + "File=testrc\n" + "Script=test.sh,sh\n" + << + "echo '[group]'\n" + "echo 'new=value'\n" + << + "[group]\n" + "valid=bar\n" + << + "[$Version]\n" + "update_info=%1\n" + "\n" + "[group]\n" + "new=value\n" + "valid=bar\n" + ; + + QTest::newRow("modify-key-no-overwrite") + << + "File=testrc\n" + "Script=test.sh,sh\n" + << + "echo '[group]'\n" + "echo 'existing=new'\n" + << + "[group]\n" + "existing=old\n" + << + "[$Version]\n" + "update_info=%1\n" + "\n" + "[group]\n" + "existing=old\n" + ; + + QTest::newRow("modify-key-overwrite") + << + "File=testrc\n" + "Options=overwrite\n" + "Script=test.sh,sh\n" + << + "echo '[group]'\n" + "echo 'existing=new'\n" + << + "[group]\n" + "existing=old\n" + << + "[$Version]\n" + "update_info=%1\n" + "\n" + "[group]\n" + "existing=new\n" + ; + + QTest::newRow("new-key-in-subgroup") + << + "File=testrc\n" + "Script=test.sh,sh\n" + << + "echo '[group][sub]'\n" + "echo 'new=value2'\n" + << + "[group][sub]\n" + "existing=foo\n" + << + "[$Version]\n" + "update_info=%1\n" + "\n" + "[group][sub]\n" + "existing=foo\n" + "new=value2\n" + ; + + QTest::newRow("new-key-in-subgroup2") + << + "File=testrc\n" + "Script=test.sh,sh\n" + << + "echo '[group][sub]'\n" + "echo 'new=value3'\n" + << + "[group][sub]\n" + "existing=foo\n" + << + "[$Version]\n" + "update_info=%1\n" + "\n" + "[group][sub]\n" + "existing=foo\n" + "new=value3\n" + ; + + QTest::newRow("filter") + << + "File=testrc\n" + "Script=test.sh,sh\n" + << + "echo '# DELETE [group]changed'\n" + "sed s/value/VALUE/\n" + << + "[group]\n" + "changed=value\n" + "unchanged=value\n" + << + "[$Version]\n" + "update_info=%1\n" + "\n" + "[group]\n" + "changed=VALUE\n" + "unchanged=value\n" + ; + + QTest::newRow("filter-subgroup") + << + "File=testrc\n" + "Script=test.sh,sh\n" + << + "echo '# DELETE [group][sub]changed'\n" + "sed s/value/VALUE/\n" + << + "[group]\n" + "unchanged=value\n" + "\n" + "[group][sub]\n" + "changed=value\n" + "unchanged=value\n" + << + "[$Version]\n" + "update_info=%1\n" + "\n" + "[group]\n" + "unchanged=value\n" + "\n" + "[group][sub]\n" + "changed=VALUE\n" + "unchanged=value\n" + ; +} + +void TestKConfUpdate::testScript() +{ + QFETCH(QString, updContent); + QFETCH(QString, updScript); + QFETCH(QString, oldConfContent); + QFETCH(QString, expectedNewConfContent); + + // Prepend the Id= field to the upd content + updContent = QString("Id=%1\n").arg(QTest::currentDataTag()) + updContent; + + QSharedPointer updFile(writeUpdFile(updContent)); + + const QString scriptDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/kconf_update"; + QVERIFY(QDir().mkpath(scriptDir)); + QString scriptPath = scriptDir + "/test.sh"; + writeFile(scriptPath, updScript); + + QString confPath = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QLatin1Char('/') + "testrc"; + writeFile(confPath, oldConfContent); + + runKConfUpdate(updFile->fileName()); + + QString updateInfo = QString("%1:%2") + .arg(updFile->fileName().section('/', -1)) + .arg(QTest::currentDataTag()); + QString newConfContent = readFile(confPath); + expectedNewConfContent = expectedNewConfContent.arg(updateInfo); + QCOMPARE(newConfContent, expectedNewConfContent); +} + -- cgit v1.2.1