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

0001 /*
0002     SPDX-FileCopyrightText: 2016 Elvis Angelaccio <elvis.angelaccio@kde.org>
0003 
0004     SPDX-License-Identifier: BSD-2-Clause
0005 */
0006 
0007 #include "batchextract.h"
0008 
0009 #include <QDirIterator>
0010 #include <QTest>
0011 
0012 class BatchExtractTest : public QObject
0013 {
0014     Q_OBJECT
0015 
0016 private Q_SLOTS:
0017     void initTestCase();
0018     void testBatchExtraction_data();
0019     void testBatchExtraction();
0020 
0021 private:
0022     QString m_expectedWorkingDir;
0023 };
0024 
0025 QTEST_MAIN(BatchExtractTest)
0026 
0027 void BatchExtractTest::initTestCase()
0028 {
0029     // #395939: after each extraction, the cwd must be the one we started from.
0030     m_expectedWorkingDir = QDir::currentPath();
0031 }
0032 
0033 void BatchExtractTest::testBatchExtraction_data()
0034 {
0035     QTest::addColumn<QString>("archivePath");
0036     QTest::addColumn<bool>("autoSubfolder");
0037     // Expected numbers of entries (files + folders) in the temporary extraction folder.
0038     // This is the number of entries in the archive (+ 1, if the autosubfolder is expected).
0039     QTest::addColumn<int>("expectedExtractedEntriesCount");
0040 
0041     QTest::newRow("extract the whole simple%archive.tar.gz (bug #365798)") << QFINDTESTDATA("data/simple%archive.tar.gz") << true << 5;
0042 
0043     QTest::newRow("single-folder, no autosubfolder") << QFINDTESTDATA("../kerfuffle/data/one_toplevel_folder.zip") << false << 9;
0044 
0045     QTest::newRow("single-folder, autosubfolder") << QFINDTESTDATA("../kerfuffle/data/one_toplevel_folder.zip") << true << 9;
0046 
0047     QTest::newRow("non single-folder, no autosubfolder") << QFINDTESTDATA("../kerfuffle/data/simplearchive.tar.gz") << false << 4;
0048 
0049     QTest::newRow("non single-folder, autosubfolder") << QFINDTESTDATA("../kerfuffle/data/simplearchive.tar.gz") << true << 5;
0050 
0051     QTest::newRow("single-file, no autosubfolder") << QFINDTESTDATA("data/test.txt.gz") << false << 1;
0052 
0053     QTest::newRow("single-file, autosubfolder") << QFINDTESTDATA("data/test.txt.gz") << true << 1;
0054 }
0055 
0056 void BatchExtractTest::testBatchExtraction()
0057 {
0058     auto batchJob = new BatchExtract(this);
0059 
0060     QFETCH(QString, archivePath);
0061     batchJob->addInput(QUrl::fromUserInput(archivePath));
0062 
0063     QFETCH(bool, autoSubfolder);
0064     batchJob->setAutoSubfolder(autoSubfolder);
0065 
0066     QTemporaryDir destDir;
0067     if (!destDir.isValid()) {
0068         QSKIP("Could not create a temporary directory for extraction. Skipping test.", SkipSingle);
0069     }
0070 
0071     batchJob->setDestinationFolder(destDir.path());
0072 
0073     QEventLoop eventLoop(this);
0074     connect(batchJob, &KJob::result, &eventLoop, &QEventLoop::quit);
0075     batchJob->start();
0076     eventLoop.exec(); // krazy:exclude=crashy
0077 
0078     QFETCH(int, expectedExtractedEntriesCount);
0079     int extractedEntriesCount = 0;
0080 
0081     QDirIterator dirIt(destDir.path(), QDir::AllEntries | QDir::Hidden | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
0082     while (dirIt.hasNext()) {
0083         extractedEntriesCount++;
0084         dirIt.next();
0085     }
0086 
0087     QCOMPARE(extractedEntriesCount, expectedExtractedEntriesCount);
0088     QCOMPARE(QDir::currentPath(), m_expectedWorkingDir);
0089 }
0090 
0091 #include "batchextracttest.moc"