File indexing completed on 2024-12-22 05:05:25

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "testimportfile.h"
0008 #include "abstractimportexportjob.h"
0009 #include "archivestorage.h"
0010 #include "compareimportfile.h"
0011 #include "compareloggingfile.h"
0012 #include "utils.h"
0013 
0014 #include <QSignalSpy>
0015 #include <QTest>
0016 #define REMOVE_TEMPORARY_DIR 1
0017 
0018 TestImportFile::TestImportFile(const QString &archivePath, QObject *parent)
0019     : QObject(parent)
0020     , mArchivePath(archivePath + QStringLiteral("archive.zip"))
0021 {
0022     mArchiveStorage = new ArchiveStorage(mArchivePath, this);
0023     const bool archiveOpened = mArchiveStorage->openArchive(false);
0024     if (!archiveOpened) {
0025         qDebug() << " mArchivePath can't be opened :" << mArchivePath;
0026     }
0027     Q_ASSERT(archiveOpened);
0028 }
0029 
0030 TestImportFile::~TestImportFile() = default;
0031 
0032 void TestImportFile::start()
0033 {
0034     // Don't use setTestModeEnabled otherwise we can set env
0035     // QStandardPaths::setTestModeEnabled(true);
0036     // TODO define extract path
0037     QVERIFY(!mExtractPath.isEmpty());
0038 
0039     qputenv("XDG_DATA_HOME", QByteArray(mExtractPath.toLatin1() + "/share"));
0040     qputenv("XDG_CONFIG_HOME", QByteArray(mExtractPath.toLatin1() + "/config"));
0041 
0042     const int version = Utils::archiveVersion(mArchiveStorage->archive());
0043     QVERIFY(version <= Utils::currentArchiveVersion());
0044     mAbstractImportExportJob->setArchiveVersion(version);
0045 
0046     QSignalSpy finish(mAbstractImportExportJob, &AbstractImportExportJob::jobFinished);
0047     QSignalSpy error(mAbstractImportExportJob, &AbstractImportExportJob::error);
0048     mAbstractImportExportJob->start();
0049     QVERIFY(finish.wait());
0050     delete mArchiveStorage;
0051     mArchiveStorage = nullptr;
0052 
0053     CompareImportFile compareExportFile;
0054     compareExportFile.setArchiveFilePath(mArchivePath);
0055     compareExportFile.setListFilePath(mPathConfig);
0056     compareExportFile.setInstallPath(mExtractPath);
0057     compareExportFile.setExcludePath(mExcludePath);
0058     compareExportFile.compareFile();
0059 
0060     CompareLoggingFile file;
0061     file.setLoggingFilePath(mLoggingFilePath);
0062     file.setListFilePath(mPathConfig);
0063     file.compare();
0064 
0065     if (!error.isEmpty()) {
0066         qDebug() << error.at(0);
0067     }
0068     QCOMPARE(error.count(), 0);
0069 
0070 #ifdef REMOVE_TEMPORARY_DIR
0071     QVERIFY(QDir(mExtractPath).removeRecursively());
0072 #endif
0073 }
0074 
0075 AbstractImportExportJob *TestImportFile::abstractImportExportJob() const
0076 {
0077     return mAbstractImportExportJob;
0078 }
0079 
0080 void TestImportFile::setAbstractImportExportJob(AbstractImportExportJob *abstractImportExportJob)
0081 {
0082     mAbstractImportExportJob = abstractImportExportJob;
0083 }
0084 
0085 ArchiveStorage *TestImportFile::archiveStorage() const
0086 {
0087     return mArchiveStorage;
0088 }
0089 
0090 void TestImportFile::setArchiveStorage(ArchiveStorage *archiveStorage)
0091 {
0092     mArchiveStorage = archiveStorage;
0093 }
0094 
0095 QString TestImportFile::pathConfig() const
0096 {
0097     return mPathConfig;
0098 }
0099 
0100 void TestImportFile::setPathConfig(const QString &pathConfig)
0101 {
0102     mPathConfig = pathConfig;
0103 }
0104 
0105 void TestImportFile::setExtractPath(const QString &extractPath)
0106 {
0107     mExtractPath = extractPath;
0108 }
0109 
0110 QString TestImportFile::excludePath() const
0111 {
0112     return mExcludePath;
0113 }
0114 
0115 void TestImportFile::setExcludePath(const QString &excludePath)
0116 {
0117     mExcludePath = excludePath;
0118 }
0119 
0120 QString TestImportFile::loggingFilePath() const
0121 {
0122     return mLoggingFilePath;
0123 }
0124 
0125 void TestImportFile::setLoggingFilePath(const QString &loggingFilePath)
0126 {
0127     mLoggingFilePath = loggingFilePath;
0128 }
0129 
0130 QString TestImportFile::extractPath() const
0131 {
0132     return mExtractPath;
0133 }
0134 
0135 #include "moc_testimportfile.cpp"