File indexing completed on 2024-05-12 05:50:09

0001 /*
0002     SPDX-FileCopyrightText: 2016 Vladyslav Batyrenko <mvlabat@gmail.com>
0003 
0004     SPDX-License-Identifier: BSD-2-Clause
0005 */
0006 
0007 #include "abstractaddtest.h"
0008 #include "archiveentry.h"
0009 #include "jobs.h"
0010 #include "testhelper.h"
0011 
0012 #include <QTest>
0013 
0014 using namespace Kerfuffle;
0015 
0016 class AddTest : public AbstractAddTest
0017 {
0018     Q_OBJECT
0019 
0020 public:
0021     AddTest()
0022         : AbstractAddTest()
0023     {
0024     }
0025 
0026 private Q_SLOTS:
0027     void testAdding_data();
0028     void testAdding();
0029 };
0030 
0031 QTEST_GUILESS_MAIN(AddTest)
0032 
0033 void AddTest::testAdding_data()
0034 {
0035     QTest::addColumn<QString>("archiveName");
0036     QTest::addColumn<Plugin *>("plugin");
0037     QTest::addColumn<QVector<Archive::Entry *>>("targetEntries");
0038     QTest::addColumn<Archive::Entry *>("destination");
0039     QTest::addColumn<QStringList>("expectedNewPaths");
0040     QTest::addColumn<uint>("numberOfEntries");
0041 
0042     setupRows(QStringLiteral("without destination"),
0043               QStringLiteral("test"),
0044               QVector<Archive::Entry *>{new Archive::Entry(this, QStringLiteral("textfile1.txt")), new Archive::Entry(this, QStringLiteral("textfile2.txt"))},
0045               new Archive::Entry(this),
0046               QStringList{QStringLiteral("textfile1.txt"), QStringLiteral("textfile2.txt")},
0047               15);
0048 
0049     setupRows(QStringLiteral("with destination, files"),
0050               QStringLiteral("test"),
0051               QVector<Archive::Entry *>{new Archive::Entry(this, QStringLiteral("textfile1.txt")), new Archive::Entry(this, QStringLiteral("textfile2.txt"))},
0052               new Archive::Entry(this, QStringLiteral("empty_dir/")),
0053               QStringList{QStringLiteral("empty_dir/textfile1.txt"), QStringLiteral("empty_dir/textfile2.txt")},
0054               15);
0055 
0056     setupRows(QStringLiteral("with destination, directory"),
0057               QStringLiteral("test"),
0058               QVector<Archive::Entry *>{
0059                   new Archive::Entry(this, QStringLiteral("testdir/")),
0060               },
0061               new Archive::Entry(this, QStringLiteral("empty_dir/")),
0062               QStringList{QStringLiteral("empty_dir/testdir/testfile1.txt"), QStringLiteral("empty_dir/testdir/testfile2.txt")},
0063               16);
0064 
0065     setupRows(QStringLiteral("without destination, directory 2"),
0066               QStringLiteral("test"),
0067               QVector<Archive::Entry *>{
0068                   new Archive::Entry(this, QStringLiteral("testdir2/")),
0069               },
0070               new Archive::Entry(this),
0071               QStringList{QStringLiteral("testdir2/testdir/testfile1.txt"), QStringLiteral("testdir2/testdir/testfile2.txt")},
0072               17);
0073 
0074     setupRows(QStringLiteral("with destination, directory 2"),
0075               QStringLiteral("test"),
0076               QVector<Archive::Entry *>{
0077                   new Archive::Entry(this, QStringLiteral("testdir2/")),
0078               },
0079               new Archive::Entry(this, QStringLiteral("empty_dir/")),
0080               QStringList{QStringLiteral("empty_dir/testdir2/testdir/testfile1.txt"), QStringLiteral("empty_dir/testdir2/testdir/testfile2.txt")},
0081               17);
0082 
0083     setupRows(QStringLiteral("overwriting an existing entry"),
0084               QStringLiteral("test"),
0085               QVector<Archive::Entry *>{
0086                   new Archive::Entry(this, QStringLiteral("a.txt")),
0087               },
0088               new Archive::Entry(this),
0089               QStringList(),
0090               13);
0091 }
0092 
0093 void AddTest::testAdding()
0094 {
0095     QTemporaryDir temporaryDir;
0096 
0097     QFETCH(QString, archiveName);
0098     const QString archivePath = temporaryDir.path() + QLatin1Char('/') + archiveName;
0099     QVERIFY(QFile::copy(QFINDTESTDATA(QStringLiteral("data/") + archiveName), archivePath));
0100 
0101     QFETCH(Plugin *, plugin);
0102     QVERIFY(plugin);
0103 
0104     auto loadJob = Archive::load(archivePath, plugin);
0105     QVERIFY(loadJob);
0106     loadJob->setAutoDelete(false);
0107 
0108     TestHelper::startAndWaitForResult(loadJob);
0109     auto archive = loadJob->archive();
0110     QVERIFY(archive);
0111 
0112     if (!archive->isValid()) {
0113         QSKIP("Could not find a plugin to handle the archive. Skipping test.", SkipSingle);
0114     }
0115 
0116     QFETCH(QVector<Archive::Entry *>, targetEntries);
0117     QFETCH(Archive::Entry *, destination);
0118     QFETCH(QStringList, expectedNewPaths);
0119 
0120     // Retrieve current paths in the archive.
0121     QStringList oldPaths = getEntryPaths(archive);
0122 
0123     // Check that the expected paths (after the AddJob) are not in the archive.
0124     for (const auto &expectedPath : std::as_const(expectedNewPaths)) {
0125         QVERIFY(!oldPaths.contains(expectedPath));
0126     }
0127 
0128     CompressionOptions options;
0129     options.setGlobalWorkDir(QFINDTESTDATA("data"));
0130     AddJob *addJob = archive->addFiles(targetEntries, destination, options);
0131     TestHelper::startAndWaitForResult(addJob);
0132 
0133     // Retrieve the resulting paths.
0134     QStringList newPaths = getEntryPaths(archive);
0135 
0136     // Check that the expected paths are now in the archive.
0137     for (const auto &path : std::as_const(expectedNewPaths)) {
0138         QVERIFY(newPaths.contains(path));
0139     }
0140 
0141     QFETCH(uint, numberOfEntries);
0142     QCOMPARE(archive->numberOfEntries(), numberOfEntries);
0143 
0144     loadJob->deleteLater();
0145     archive->deleteLater();
0146 }
0147 
0148 #include "addtest.moc"