From 83d0f24293b3ea1680cc4e6f60eecfed06c8548b Mon Sep 17 00:00:00 2001 From: Katja Marttila Date: Wed, 8 Apr 2020 08:22:05 +0300 Subject: Add unit tests for environmentvariable operation Task-number: QTIFW-1719 Change-Id: I91f91e8704ed409a76302154d013e09afb767707 Reviewed-by: Arttu Tarkiainen --- .../data/repository/A/1.0.2-1meta.7z | Bin 0 -> 882 bytes .../data/repository/Updates.xml | 14 ++ .../environmentvariableoperation.pro | 11 ++ .../environmentvariableoperation/settings.qrc | 6 + .../tst_environmentvariableoperation.cpp | 183 +++++++++++++++++++++ tests/auto/installer/installer.pro | 3 +- tests/environmentvariable/environmentvariable.pro | 15 -- .../environmentvariabletest.cpp | 93 ----------- .../environmentvariable/environmentvariabletest.h | 48 ------ tests/tests.pro | 3 +- 10 files changed, 217 insertions(+), 159 deletions(-) create mode 100644 tests/auto/installer/environmentvariableoperation/data/repository/A/1.0.2-1meta.7z create mode 100644 tests/auto/installer/environmentvariableoperation/data/repository/Updates.xml create mode 100644 tests/auto/installer/environmentvariableoperation/environmentvariableoperation.pro create mode 100644 tests/auto/installer/environmentvariableoperation/settings.qrc create mode 100644 tests/auto/installer/environmentvariableoperation/tst_environmentvariableoperation.cpp delete mode 100644 tests/environmentvariable/environmentvariable.pro delete mode 100644 tests/environmentvariable/environmentvariabletest.cpp delete mode 100644 tests/environmentvariable/environmentvariabletest.h diff --git a/tests/auto/installer/environmentvariableoperation/data/repository/A/1.0.2-1meta.7z b/tests/auto/installer/environmentvariableoperation/data/repository/A/1.0.2-1meta.7z new file mode 100644 index 000000000..26b9ed692 Binary files /dev/null and b/tests/auto/installer/environmentvariableoperation/data/repository/A/1.0.2-1meta.7z differ diff --git a/tests/auto/installer/environmentvariableoperation/data/repository/Updates.xml b/tests/auto/installer/environmentvariableoperation/data/repository/Updates.xml new file mode 100644 index 000000000..6b1856d51 --- /dev/null +++ b/tests/auto/installer/environmentvariableoperation/data/repository/Updates.xml @@ -0,0 +1,14 @@ + + {AnyApplication} + 1.0.0 + false + + A + A + Example component A + 1.0.2-1 + 2015-01-01 + true + + + diff --git a/tests/auto/installer/environmentvariableoperation/environmentvariableoperation.pro b/tests/auto/installer/environmentvariableoperation/environmentvariableoperation.pro new file mode 100644 index 000000000..2d09bc9da --- /dev/null +++ b/tests/auto/installer/environmentvariableoperation/environmentvariableoperation.pro @@ -0,0 +1,11 @@ +include(../../qttest.pri) + +QT -= gui +QT += testlib + +SOURCES = tst_environmentvariableoperation.cpp + +RESOURCES += \ + settings.qrc \ + ..\shared\config.qrc + diff --git a/tests/auto/installer/environmentvariableoperation/settings.qrc b/tests/auto/installer/environmentvariableoperation/settings.qrc new file mode 100644 index 000000000..d030220ab --- /dev/null +++ b/tests/auto/installer/environmentvariableoperation/settings.qrc @@ -0,0 +1,6 @@ + + + data/repository/Updates.xml + data/repository/A/1.0.2-1meta.7z + + diff --git a/tests/auto/installer/environmentvariableoperation/tst_environmentvariableoperation.cpp b/tests/auto/installer/environmentvariableoperation/tst_environmentvariableoperation.cpp new file mode 100644 index 000000000..32cc77043 --- /dev/null +++ b/tests/auto/installer/environmentvariableoperation/tst_environmentvariableoperation.cpp @@ -0,0 +1,183 @@ +/************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Installer Framework. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +**************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +using namespace QInstaller; +using namespace KDUpdater; + +class tst_environmentvariableoperation : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase() + { + m_key = "IFW_TestKey"; + m_value = "IFW_TestValue"; + m_oldValue = "IFW_TestOldValue"; + m_settings = new QSettings("HKEY_CURRENT_USER\\Environment", QSettings::NativeFormat); + } + + void cleanup() + { + m_settings->remove(m_key); + m_settings->remove("IFW_UNIT_TEST_PERSISTENT"); //Added from script + } + + void testPersistentNonSystem() + { + #ifndef Q_OS_WIN + QSKIP("This operation only works on Windows"); + #endif + EnvironmentVariableOperation op(nullptr); + op.setArguments( QStringList() << m_key + << m_value + << QLatin1String("true") + << QLatin1String("false")); + const bool ok = op.performOperation(); + + QVERIFY2(ok, qPrintable(op.errorString())); + + QCOMPARE(m_value, m_settings->value(m_key).toString()); + + QVERIFY(op.undoOperation()); + QVERIFY(m_settings->value(m_key).toString().isEmpty()); + } + + void testNonPersistentNonSystem() + { + EnvironmentVariableOperation op(nullptr); + op.setArguments( QStringList() << m_key + << m_value + << QLatin1String("false") + << QLatin1String("false")); + const bool ok = op.performOperation(); + + QVERIFY2(ok, qPrintable(op.errorString())); + + //Make sure it is not written to env variable + QString comp = QString::fromLocal8Bit(qgetenv(qPrintable(m_key))); + QVERIFY(comp.isEmpty()); + + QCOMPARE(m_value, Environment::instance().value(m_key)); + } + + void testPersistentNonSystemOldValue() + { + #ifndef Q_OS_WIN + QSKIP("This operation only works on Windows"); + #endif + m_settings->setValue(m_key, m_oldValue); + + EnvironmentVariableOperation op(nullptr); + op.setArguments( QStringList() << m_key + << m_value + << QLatin1String("true") + << QLatin1String("false")); + const bool ok = op.performOperation(); + + QVERIFY2(ok, qPrintable(op.errorString())); + + QCOMPARE(m_value, m_settings->value(m_key).toString()); + + QVERIFY(op.undoOperation()); + QCOMPARE(m_settings->value(m_key).toString(), m_oldValue); + m_settings->remove(m_key); + } + + void testNonPersistentNonSystemOldValue() + { + m_settings->setValue(m_key, m_oldValue); + Environment::instance().setTemporaryValue(m_key, m_oldValue); + EnvironmentVariableOperation op(nullptr); + op.setArguments( QStringList() << m_key + << m_value + << QLatin1String("false") + << QLatin1String("false")); + const bool ok = op.performOperation(); + + QVERIFY2(ok, qPrintable(op.errorString())); + + QVERIFY(op.undoOperation()); + QCOMPARE(m_oldValue, Environment::instance().value(m_key)); + } + + void testPerformingFromCLI() + { + QInstaller::init(); //This will eat debug output + PackageManagerCore *core = new PackageManagerCore(BinaryContent::MagicInstallerMarker, QList ()); + core->setAllowedRunningProcesses(QStringList() << QCoreApplication::applicationFilePath()); + QSet repoList; + Repository repo = Repository::fromUserInput(":///data/repository"); + repoList.insert(repo); + core->settings().setDefaultRepositories(repoList); + + QString installDir = QInstaller::generateTemporaryFileName(); + QDir().mkpath(installDir); + core->setValue(scTargetDir, installDir); + core->installDefaultComponentsSilently(); + + QVERIFY(m_settings->value("IFW_UNIT_TEST_LOCAL").toString().isEmpty()); + + // Persistent is in settings in Windows platform only, otherwise it is written to local env. +#ifdef Q_OS_WIN + QCOMPARE(QLatin1String("IFW_UNIT_TEST_PERSISTENT_VALUE"), m_settings->value("IFW_UNIT_TEST_PERSISTENT").toString()); +#else + QCOMPARE(QLatin1String("IFW_UNIT_TEST_PERSISTENT_VALUE"), Environment::instance().value("IFW_UNIT_TEST_PERSISTENT")); +#endif + QCOMPARE(QLatin1String("IFW_UNIT_TEST_VALUE"), Environment::instance().value("IFW_UNIT_TEST_LOCAL")); + + core->commitSessionOperations(); + core->setPackageManager(); + core->uninstallComponentsSilently(QStringList() << "A"); + QVERIFY(m_settings->value("IFW_UNIT_TEST_PERSISTENT").toString().isEmpty()); + QVERIFY(Environment::instance().value("IFW_UNIT_TEST_LOCAL").isEmpty()); + core->deleteLater(); + } + +private: + QSettings *m_settings; + QString m_key; + QString m_value; + QString m_oldValue; +}; + +QTEST_MAIN(tst_environmentvariableoperation) + +#include "tst_environmentvariableoperation.moc" + diff --git a/tests/auto/installer/installer.pro b/tests/auto/installer/installer.pro index 766e8814e..da04efef0 100644 --- a/tests/auto/installer/installer.pro +++ b/tests/auto/installer/installer.pro @@ -32,7 +32,8 @@ SUBDIRS += \ deleteoperation \ copydirectoryoperation \ commandlineupdate \ - moveoperation + moveoperation \ + environmentvariableoperation win32 { SUBDIRS += registerfiletypeoperation diff --git a/tests/environmentvariable/environmentvariable.pro b/tests/environmentvariable/environmentvariable.pro deleted file mode 100644 index d6e7e5043..000000000 --- a/tests/environmentvariable/environmentvariable.pro +++ /dev/null @@ -1,15 +0,0 @@ -TEMPLATE = app -INCLUDEPATH += . .. -TARGET = environmentvariable - -include(../../installerfw.pri) - -QT -= gui -QT += testlib - -CONFIG += console - -HEADERS = environmentvariabletest.h -SOURCES = environmentvariabletest.cpp - -macx:include(../../no_app_bundle.pri) diff --git a/tests/environmentvariable/environmentvariabletest.cpp b/tests/environmentvariable/environmentvariabletest.cpp deleted file mode 100644 index 98663efbf..000000000 --- a/tests/environmentvariable/environmentvariabletest.cpp +++ /dev/null @@ -1,93 +0,0 @@ -/************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the Qt Installer Framework. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -**************************************************************************/ - -#include "environmentvariabletest.h" -#include "environmentvariablesoperation.h" - -#include "init.h" - -#include -#include -#include -#include -#include - -EnvironmentVariableTest::EnvironmentVariableTest() -{ - QInstaller::init(); -} - -void EnvironmentVariableTest::testPersistentNonSystem() -{ -#ifndef Q_OS_WIN - QSKIP("This operation only works on Windows"); -#endif - QString key = QLatin1String("IFW_TestKey"); - QString value = QLatin1String("IFW_TestValue"); - QInstaller::EnvironmentVariableOperation op(nullptr); - op.setArguments( QStringList() << key - << value - << QLatin1String("true") - << QLatin1String("false")); - const bool ok = op.performOperation(); - - QVERIFY2(ok, qPrintable(op.errorString())); - - // Verify now... - QSettings settings(QLatin1String("HKEY_CURRENT_USER\\Environment"), QSettings::NativeFormat); - QVERIFY(value == settings.value(key).toString()); - - // Remove the setting - QEXPECT_FAIL("", "Undo Operation not implemented yet", Continue); - QVERIFY(op.undoOperation()); - - //QVERIFY(settings.value(key).toString().isEmpty()); - settings.remove(key); -} - -void EnvironmentVariableTest::testNonPersistentNonSystem() -{ -#ifndef Q_OS_WIN - QSKIP("This operation only works on Windows"); -#endif - QString key = QLatin1String("IFW_TestKey"); - QString value = QLatin1String("IFW_TestValue"); - QInstaller::EnvironmentVariableOperation op(nullptr); - op.setArguments( QStringList() << key - << value - << QLatin1String("false") - << QLatin1String("false")); - const bool ok = op.performOperation(); - - QVERIFY2(ok, qPrintable(op.errorString())); - - QString comp = QString::fromLocal8Bit(qgetenv(qPrintable(key))); - QCOMPARE(value, comp); -} - -QTEST_MAIN(EnvironmentVariableTest) diff --git a/tests/environmentvariable/environmentvariabletest.h b/tests/environmentvariable/environmentvariabletest.h deleted file mode 100644 index afd5c30f2..000000000 --- a/tests/environmentvariable/environmentvariabletest.h +++ /dev/null @@ -1,48 +0,0 @@ -/************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the Qt Installer Framework. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -**************************************************************************/ - -#ifndef EXTRACTIONARCHIVEOPERATIONTEST_H -#define EXTRACTIONARCHIVEOPERATIONTEST_H - -#include - -#include - -class EnvironmentVariableTest : public QObject -{ - Q_OBJECT - -public: - EnvironmentVariableTest(); - -private Q_SLOTS: - void testPersistentNonSystem(); - void testNonPersistentNonSystem(); -}; - -#endif // EXTRACTARCHIVEOPERATIONTEST_H diff --git a/tests/tests.pro b/tests/tests.pro index 318579000..980779d59 100644 --- a/tests/tests.pro +++ b/tests/tests.pro @@ -2,5 +2,4 @@ TEMPLATE = subdirs SUBDIRS = \ auto \ - downloadspeed \ - environmentvariable + downloadspeed -- cgit v1.2.3