File indexing completed on 2025-01-19 04:46:48

0001 /*
0002   SPDX-FileCopyrightText: 2010 Volker Krause <vkrause@kde.org>
0003   SPDX-FileCopyrightText: 2016 Sandro Knauß <sknauss@kde.org>
0004 
0005   SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include <QFile>
0009 #include <QProcess>
0010 #include <QRegularExpression>
0011 #include <QTest>
0012 
0013 static void compareFile(const QString &outFile, const QString &referenceFile)
0014 {
0015     QVERIFY(QFile::exists(outFile));
0016 
0017     const QString htmlFile = outFile + QStringLiteral(".html");
0018     // remove tailing newlines and spaces and make htmlmore uniform (breaks pre tags)
0019     {
0020         QFile f(outFile);
0021         QVERIFY(f.open(QIODevice::ReadOnly));
0022         QString content = QString::fromUtf8(f.readAll());
0023         f.close();
0024         content.replace(QRegularExpression(QStringLiteral("[\t ]+")), QStringLiteral(" "));
0025         content.replace(QRegularExpression(QStringLiteral("[\t ]*\n+[\t ]*")), QStringLiteral("\n"));
0026         content.replace(QRegularExpression(QStringLiteral("([\n\t ])\\1+")), QStringLiteral("\\1"));
0027         content.replace(QRegularExpression(QStringLiteral(">\n+[\t ]*")), QStringLiteral(">"));
0028         content.replace(QRegularExpression(QStringLiteral("[\t ]*\n+[\t ]*<")), QStringLiteral("<"));
0029         content.replace(QLatin1StringView("&nbsp;"), QLatin1StringView("NBSP_ENTITY_PLACEHOLDER")); // xmlling chokes on &nbsp;
0030         QVERIFY(f.open(QIODevice::WriteOnly | QIODevice::Truncate));
0031         f.write(content.toUtf8());
0032         f.close();
0033     }
0034 
0035     // validate xml and pretty-print for comparison
0036     // TODO add proper cmake check for xmllint and diff
0037     QStringList args = QStringList() << QStringLiteral("--format") << QStringLiteral("--encode") << QStringLiteral("UTF8") << QStringLiteral("--output")
0038                                      << htmlFile << outFile;
0039     QCOMPARE(QProcess::execute(QStringLiteral("xmllint"), args), 0);
0040 
0041     // get rid of system dependent or random paths
0042     {
0043         QFile f(htmlFile);
0044         QVERIFY(f.open(QIODevice::ReadOnly));
0045         QString content = QString::fromUtf8(f.readAll());
0046         f.close();
0047         content.replace(QRegularExpression(QStringLiteral("\"file:[^\"]*[/(?:%2F)]([^\"/(?:%2F)]*)\"")), QStringLiteral("\"file:\\1\""));
0048         content.replace(QLatin1StringView("NBSP_ENTITY_PLACEHOLDER"), QLatin1StringView("&nbsp;")); // undo above transformation for xmllint
0049         content.replace(QRegularExpression(QStringLiteral("/bodypart/\\d+/")), QStringLiteral("/bodypart/0/"));
0050         QVERIFY(f.open(QIODevice::WriteOnly | QIODevice::Truncate));
0051         f.write(content.toUtf8());
0052         f.close();
0053     }
0054 
0055     // compare to reference file
0056     args = QStringList() << QStringLiteral("-u") << referenceFile << htmlFile;
0057     QProcess proc;
0058     proc.setProcessChannelMode(QProcess::ForwardedChannels);
0059     proc.start(QStringLiteral("diff"), args);
0060     QVERIFY(proc.waitForFinished());
0061 
0062     QCOMPARE(proc.exitCode(), 0);
0063 }