File indexing completed on 2025-03-09 04:54:34

0001 /*
0002   SPDX-FileCopyrightText: 2010 Thomas McGuire <thomas.mcguire@kdab.com>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "util.h"
0007 #include <QFile>
0008 #include <QProcess>
0009 #include <QRegularExpression>
0010 #include <QTest>
0011 using namespace MessageViewer;
0012 KMime::Message::Ptr Test::readAndParseMail(const QString &mailFile)
0013 {
0014     QFile file(QStringLiteral(MAIL_DATA_DIR) + QLatin1Char('/') + mailFile);
0015     const bool openFile = file.open(QIODevice::ReadOnly);
0016     Q_ASSERT(openFile);
0017     const QByteArray data = KMime::CRLFtoLF(file.readAll());
0018     Q_ASSERT(!data.isEmpty());
0019     KMime::Message::Ptr msg(new KMime::Message);
0020     msg->setContent(data);
0021     msg->parse();
0022     return msg;
0023 }
0024 
0025 void Test::compareFile(const QString &outFile, const QString &referenceFile)
0026 {
0027     QVERIFY(QFile::exists(outFile));
0028 
0029     const QString htmlFile = outFile + QStringLiteral(".html");
0030     // remove tailing newlines and spaces and make htmlmore uniform (breaks pre tags)
0031     {
0032         QFile f(outFile);
0033         QVERIFY(f.open(QIODevice::ReadOnly));
0034         QString content = QString::fromUtf8(f.readAll());
0035         f.close();
0036         content.replace(QRegularExpression(QStringLiteral("[\t ]+")), QStringLiteral(" "));
0037         content.replace(QRegularExpression(QStringLiteral("[\t ]*\n+[\t ]*")), QStringLiteral("\n"));
0038         content.replace(QRegularExpression(QStringLiteral("([\n\t ])\\1+")), QStringLiteral("\\1"));
0039         content.replace(QRegularExpression(QStringLiteral(">\n+[\t ]*")), QStringLiteral(">"));
0040         content.replace(QRegularExpression(QStringLiteral("[\t ]*\n+[\t ]*<")), QStringLiteral("<"));
0041         content.replace(QLatin1StringView("&nbsp;"), QLatin1StringView("NBSP_ENTITY_PLACEHOLDER")); // xmlling chokes on &nbsp;
0042         QVERIFY(f.open(QIODevice::WriteOnly | QIODevice::Truncate));
0043         f.write(content.toUtf8());
0044         f.close();
0045     }
0046 
0047     // validate xml and pretty-print for comparison
0048     // TODO add proper cmake check for xmllint and diff
0049     QStringList args = QStringList() << QStringLiteral("--format") << QStringLiteral("--encode") << QStringLiteral("UTF8") << QStringLiteral("--output")
0050                                      << htmlFile << outFile;
0051     QCOMPARE(QProcess::execute(QStringLiteral("xmllint"), args), 0);
0052 
0053     // get rid of system dependent or random paths
0054     {
0055         QFile f(htmlFile);
0056         QVERIFY(f.open(QIODevice::ReadOnly));
0057         QString content = QString::fromUtf8(f.readAll());
0058         f.close();
0059         content.replace(QRegularExpression(QStringLiteral("\"file:[^\"]*[/(?:%2F)]([^\"/(?:%2F)]*)\"")), QStringLiteral("\"file:\\1\""));
0060         content.replace(QRegularExpression(QStringLiteral("(file:///tmp/messageviewer)(_[^\"]+)(\\.index\\.[^\"]*)")), QStringLiteral("\\1\\3"));
0061         content.replace(QLatin1StringView("NBSP_ENTITY_PLACEHOLDER"), QLatin1StringView("&nbsp;")); // undo above transformation for xmllint
0062         QVERIFY(f.open(QIODevice::WriteOnly | QIODevice::Truncate));
0063         f.write(content.toUtf8());
0064         f.close();
0065     }
0066 
0067     QProcess proc;
0068 #ifdef _WIN32
0069     args = QStringList() << QStringLiteral("Compare-Object") << QString(QStringLiteral("(Get-Content %1)")).arg(referenceFile)
0070                          << QString(QStringLiteral("(Get-Content %1)")).arg(htmlFile);
0071 
0072     proc.start(QStringLiteral("powershell"), args);
0073     QVERIFY(proc.waitForFinished());
0074 
0075     auto pStdOut = proc.readAllStandardOutput();
0076     if (pStdOut.size()) {
0077         qDebug() << "Files are different, diff output message:\n" << pStdOut.toStdString().c_str();
0078     }
0079 
0080     QCOMPARE(pStdOut.size(), 0);
0081 #else
0082     // compare to reference file
0083     args = QStringList() << QStringLiteral("-u") << referenceFile << htmlFile;
0084 
0085     proc.setProcessChannelMode(QProcess::ForwardedChannels);
0086     proc.start(QStringLiteral("diff"), args);
0087     QVERIFY(proc.waitForFinished());
0088     QCOMPARE(proc.exitCode(), 0);
0089 #endif
0090 }