From 665ea6eb636dfe60130fcbbc21d5bd29bbe27250 Mon Sep 17 00:00:00 2001 From: Arttu Tarkiainen Date: Fri, 27 Mar 2020 10:57:14 +0200 Subject: Add unit tests for SimpleMoveFileOperation Task-number: QTIFW-1699 Change-Id: Iac6ba6429d71a31f659914929141b3e04879bf8a Reviewed-by: Katja Marttila --- tests/auto/installer/installer.pro | 3 +- .../simplemovefileoperation.pro | 6 ++ .../tst_simplemovefileoperation.cpp | 116 +++++++++++++++++++++ 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 tests/auto/installer/simplemovefileoperation/simplemovefileoperation.pro create mode 100644 tests/auto/installer/simplemovefileoperation/tst_simplemovefileoperation.cpp diff --git a/tests/auto/installer/installer.pro b/tests/auto/installer/installer.pro index d5a0f7221..65dbad19d 100644 --- a/tests/auto/installer/installer.pro +++ b/tests/auto/installer/installer.pro @@ -26,7 +26,8 @@ SUBDIRS += \ replaceoperation \ brokeninstaller \ cliinterface \ - metadatajob + metadatajob \ + simplemovefileoperation win32 { SUBDIRS += registerfiletypeoperation diff --git a/tests/auto/installer/simplemovefileoperation/simplemovefileoperation.pro b/tests/auto/installer/simplemovefileoperation/simplemovefileoperation.pro new file mode 100644 index 000000000..506def2fd --- /dev/null +++ b/tests/auto/installer/simplemovefileoperation/simplemovefileoperation.pro @@ -0,0 +1,6 @@ +include(../../qttest.pri) + +QT -= gui +QT += testlib + +SOURCES = tst_simplemovefileoperation.cpp diff --git a/tests/auto/installer/simplemovefileoperation/tst_simplemovefileoperation.cpp b/tests/auto/installer/simplemovefileoperation/tst_simplemovefileoperation.cpp new file mode 100644 index 000000000..beeb63e14 --- /dev/null +++ b/tests/auto/installer/simplemovefileoperation/tst_simplemovefileoperation.cpp @@ -0,0 +1,116 @@ +/************************************************************************** +** +** 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 + +using namespace KDUpdater; +using namespace QInstaller; + +class tst_simplemovefileoperation : public QObject +{ + Q_OBJECT + +private slots: + void testMissingArguments() + { + SimpleMoveFileOperation op(nullptr); + + op.backup(); + QVERIFY(op.testOperation()); + QVERIFY(!op.performOperation()); + + QCOMPARE(UpdateOperation::Error(op.error()), UpdateOperation::InvalidArguments); + QCOMPARE(op.errorString(), QString("Invalid arguments in SimpleMoveFile: " + "0 arguments given, exactly 2 arguments expected.")); + + op.setArguments(QStringList() << "" << ""); + QVERIFY(!op.performOperation()); + + QCOMPARE(UpdateOperation::Error(op.error()), UpdateOperation::UserDefinedError); + QCOMPARE(op.errorString(), QString("None of the arguments can be empty: " + "source \"\", target \"\".")); + } + + void testMoveFileWithOverwrite_data() + { + QTest::addColumn("source"); + QTest::addColumn("destination"); + QTest::newRow("relative") << "test1" << "test2"; + QTest::newRow("absolute") + << qApp->applicationDirPath() + QDir::toNativeSeparators("/test") + << generateTemporaryFileName(); + } + + void testMoveFileWithOverwrite() + { + QFETCH(QString, source); + QFETCH(QString, destination); + + // Create dummy original file for move destination + QFile destFile(destination); + QVERIFY(destFile.open(QIODevice::WriteOnly)); + destFile.close(); + + QByteArray testString("Generated by QTest\n"); + QFile testFile(source); + QVERIFY(testFile.open(QIODevice::WriteOnly | QIODevice::Text)); + QTextStream out(&testFile); + out << testString; + testFile.close(); + + QVERIFY(QFileInfo(source).exists()); + QByteArray testFileHash = QInstaller::calculateHash(source, QCryptographicHash::Sha1); + + SimpleMoveFileOperation op(nullptr); + op.setArguments(QStringList() << source << destination); + + QVERIFY2(op.performOperation(), op.errorString().toLatin1()); + + QVERIFY(!QFileInfo(source).exists()); + QByteArray destinationFileHash = QInstaller::calculateHash(destination, QCryptographicHash::Sha1); + QVERIFY(testFileHash == destinationFileHash); + + QVERIFY2(op.undoOperation(), op.errorString().toLatin1()); + + QVERIFY(!QFileInfo(destination).exists()); + QByteArray sourceFileHash = QInstaller::calculateHash(source, QCryptographicHash::Sha1); + QVERIFY(testFileHash == sourceFileHash); + + QVERIFY(QFile(source).remove()); + } +}; + +QTEST_MAIN(tst_simplemovefileoperation) + +#include "tst_simplemovefileoperation.moc" -- cgit v1.2.3