| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#if CHECK_SIMTEXTH |
| 5 |
#include "../shared/simtexth.h" |
| 6 |
#endif |
| 7 |
|
| 8 |
#include <QtCore/QByteArray> |
| 9 |
#include <QtCore/QDebug> |
| 10 |
#include <QtCore/QDir> |
| 11 |
#include <QtCore/QFile> |
| 12 |
#include <QtCore/QElapsedTimer> |
| 13 |
#include <QtCore/private/qconfig_p.h> |
| 14 |
#include <QtCore/QSet> |
| 15 |
#include <QtCore/QSysInfo> |
| 16 |
|
| 17 |
#include <QtTest/QtTest> |
| 18 |
#include <QtTools/private/qttools-config_p.h> |
| 19 |
|
| 20 |
#include <algorithm> |
| 21 |
#include <limits> |
| 22 |
|
| 23 |
using namespace Qt::Literals::StringLiterals; |
| 24 |
|
| 25 |
static constexpr int TIMEOUT = 120000; |
| 26 |
|
| 27 |
class tst_lupdate : public QObject |
| 28 |
{ |
| 29 |
Q_OBJECT |
| 30 |
public: |
| 31 |
tst_lupdate(); |
| 32 |
|
| 33 |
private slots: |
| 34 |
void cleanupTestCase(); |
| 35 |
void good_data(); |
| 36 |
void good(); |
| 37 |
#if CHECK_SIMTEXTH |
| 38 |
void simtexth(); |
| 39 |
void simtexth_data(); |
| 40 |
#endif |
| 41 |
|
| 42 |
private: |
| 43 |
QString m_cmdLupdate; |
| 44 |
QString m_basePath; |
| 45 |
QElapsedTimer m_timer; |
| 46 |
qint64 m_maxElapsed = -1; |
| 47 |
|
| 48 |
static void doCompare(QList<QStringView> actual, const QString &expectedFn, bool err); |
| 49 |
void doCompare(const QString &actualFn, const QString &expectedFn, bool err); |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
tst_lupdate::tst_lupdate() |
| 54 |
{ |
| 55 |
m_timer.start(); |
| 56 |
QString binPath = QLibraryInfo::path(QLibraryInfo::BinariesPath); |
| 57 |
m_cmdLupdate = binPath + "/lupdate"_L1; |
| 58 |
m_basePath = QFINDTESTDATA("testdata/"); |
| 59 |
} |
| 60 |
|
| 61 |
void tst_lupdate::cleanupTestCase() |
| 62 |
{ |
| 63 |
if (m_maxElapsed > 0) |
| 64 |
qInfo().noquote().nospace() << "max elapsed: " << m_maxElapsed << "ms"; |
| 65 |
} |
| 66 |
|
| 67 |
static bool prepareMatch(QStringView expect, QString *tmpl, qsizetype *require, qsizetype *accept) |
| 68 |
{ |
| 69 |
if (expect.startsWith(QLatin1Char('\\'))) { |
| 70 |
*tmpl = expect.sliced(1).toString(); |
| 71 |
*require = *accept = 1; |
| 72 |
} else if (expect.startsWith(u'?')) { |
| 73 |
*tmpl = expect.sliced(1).toString(); |
| 74 |
*require = 0; |
| 75 |
*accept = 1; |
| 76 |
} else if (expect.startsWith(u'*')) { |
| 77 |
*tmpl = expect.sliced(1).toString(); |
| 78 |
*require = 0; |
| 79 |
*accept = std::numeric_limits<qsizetype>::max(); |
| 80 |
} else if (expect.startsWith(u'+')) { |
| 81 |
*tmpl = expect.sliced(1).toString(); |
| 82 |
*require = 1; |
| 83 |
*accept = std::numeric_limits<qsizetype>::max(); |
| 84 |
} else if (expect.startsWith(u'{')) { |
| 85 |
const auto brc = expect.indexOf(u'}', 1); |
| 86 |
if (brc < 0) |
| 87 |
return false; |
| 88 |
*tmpl = expect.sliced(brc + 1).toString(); |
| 89 |
auto sub = expect.sliced(1, brc - 1); |
| 90 |
const auto com = sub.indexOf(u','); |
| 91 |
bool ok{}; |
| 92 |
if (com < 0) { |
| 93 |
*require = *accept = sub.toInt(&ok); |
| 94 |
return ok; |
| 95 |
} |
| 96 |
*require = sub.left(com).toInt(); |
| 97 |
*accept = sub.sliced(com + 1).toInt(&ok); |
| 98 |
if (!ok) |
| 99 |
*accept = std::numeric_limits<qsizetype>::max(); |
| 100 |
return *accept >= *require; |
| 101 |
} else { |
| 102 |
*tmpl = expect.toString(); |
| 103 |
*require = *accept = 1; |
| 104 |
} |
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
static bool isStashMessage(QStringView v) |
| 109 |
{ |
| 110 |
return v.startsWith("Info: creating stash file "_L1); |
| 111 |
} |
| 112 |
|
| 113 |
void tst_lupdate::doCompare(QList<QStringView> actual, const QString &expectedFn, bool err) |
| 114 |
{ |
| 115 |
QFile file(expectedFn); |
| 116 |
QVERIFY2(file.open(QIODevice::ReadOnly | QIODevice::Text), qPrintable(expectedFn)); |
| 117 |
const QString expectedS = QString::fromUtf8(file.readAll()); |
| 118 |
auto expected = QStringView{expectedS}.split(u'\n'); |
| 119 |
|
| 120 |
actual.erase(std::remove_if(actual.begin(), actual.end(), isStashMessage), actual.end()); |
| 121 |
|
| 122 |
qsizetype ei = 0, ai = 0, em = expected.size(), am = actual.size(); |
| 123 |
qsizetype oei = 0, oai = 0, oem = em, oam = am; |
| 124 |
qsizetype require = 0, accept = 0; |
| 125 |
QString tmpl; |
| 126 |
forever { |
| 127 |
if (!accept) { |
| 128 |
oei = ei, oai = ai; |
| 129 |
if (ei == em) { |
| 130 |
if (ai == am) |
| 131 |
return; |
| 132 |
break; |
| 133 |
} |
| 134 |
if (!prepareMatch(expected.at(ei++), &tmpl, &require, &accept)) |
| 135 |
QFAIL(qPrintable(QString("Malformed expected %1 at %3:%2"_L1) |
| 136 |
.arg(err ? "output"_L1 : "result"_L1).arg(ei).arg(expectedFn))); |
| 137 |
} |
| 138 |
if (ai == am) { |
| 139 |
if (require <= 0) { |
| 140 |
accept = 0; |
| 141 |
continue; |
| 142 |
} |
| 143 |
break; |
| 144 |
} |
| 145 |
if (err |
| 146 |
? !QRegularExpression(QRegularExpression::anchoredPattern(tmpl)).matchView(actual.at(ai)).hasMatch() |
| 147 |
: (actual.at(ai) != tmpl)) { |
| 148 |
if (require <= 0) { |
| 149 |
accept = 0; |
| 150 |
continue; |
| 151 |
} |
| 152 |
ei--; |
| 153 |
require = accept = 0; |
| 154 |
forever { |
| 155 |
if (!accept) { |
| 156 |
oem = em, oam = am; |
| 157 |
if (ei == em) |
| 158 |
break; |
| 159 |
if (!prepareMatch(expected.at(--em), &tmpl, &require, &accept)) |
| 160 |
QFAIL(qPrintable(QString("Malformed expected %1 at %3:%2"_L1) |
| 161 |
.arg(err ? "output"_L1 : "result"_L1) |
| 162 |
.arg(em + 1).arg(expectedFn))); |
| 163 |
} |
| 164 |
if (ai == am || (err |
| 165 |
? !QRegularExpression(QRegularExpression::anchoredPattern(tmpl)).matchView(actual.at(am - 1)).hasMatch() |
| 166 |
: (actual.at(am - 1) != tmpl))) { |
| 167 |
if (require <= 0) { |
| 168 |
accept = 0; |
| 169 |
continue; |
| 170 |
} |
| 171 |
break; |
| 172 |
} |
| 173 |
accept--; |
| 174 |
require--; |
| 175 |
am--; |
| 176 |
} |
| 177 |
break; |
| 178 |
} |
| 179 |
accept--; |
| 180 |
require--; |
| 181 |
ai++; |
| 182 |
} |
| 183 |
QString diff; |
| 184 |
for (qsizetype j = qMax(qsizetype(0), oai - 3); j < oai; j++) { |
| 185 |
diff += actual.at(j); |
| 186 |
diff += u'\n'; |
| 187 |
} |
| 188 |
diff += "<<<<<<< got\n"_L1; |
| 189 |
for (qsizetype j = oai; j < oam; j++) { |
| 190 |
diff += actual.at(j); |
| 191 |
diff += u'\n'; |
| 192 |
if (j >= oai + 5) { |
| 193 |
diff += "...\n"_L1; |
| 194 |
break; |
| 195 |
} |
| 196 |
} |
| 197 |
diff += "=========\n"_L1; |
| 198 |
for (qsizetype j = oei; j < oem; j++) { |
| 199 |
diff += expected.at(j); |
| 200 |
diff += u'\n'; |
| 201 |
if (j >= oei + 5) { |
| 202 |
diff += "...\n"_L1; |
| 203 |
break; |
| 204 |
} |
| 205 |
} |
| 206 |
diff += ">>>>>>> expected\n"_L1; |
| 207 |
for (qsizetype j = oam; j < qMin(oam + 3, actual.size()); j++) { |
| 208 |
diff += actual.at(j); |
| 209 |
diff += u'\n'; |
| 210 |
} |
| 211 |
QFAIL(qPrintable((err ? "Output for "_L1 : "Result for "_L1) |
| 212 |
+ expectedFn + " does not meet expectations:\n"_L1 + diff)); |
| 213 |
} |
| 214 |
|
| 215 |
void tst_lupdate::doCompare(const QString &actualFn, const QString &expectedFn, bool err) |
| 216 |
{ |
| 217 |
QFile afile(actualFn); |
| 218 |
QVERIFY2(afile.open(QIODevice::ReadOnly | QIODevice::Text), qPrintable(actualFn)); |
| 219 |
const QString actual = QString::fromUtf8(afile.readAll()); |
| 220 |
|
| 221 |
doCompare(QStringView{actual}.split(u'\n'), expectedFn, err); |
| 222 |
} |
| 223 |
|
| 224 |
void tst_lupdate::good_data() |
| 225 |
{ |
| 226 |
QTest::addColumn<QString>("directory"); |
| 227 |
|
| 228 |
QDir parsingDir(m_basePath + "good"_L1); |
| 229 |
QStringList dirs = parsingDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name); |
| 230 |
|
| 231 |
#ifndef Q_OS_WIN |
| 232 |
dirs.removeAll("backslashes"_L1); |
| 233 |
#endif |
| 234 |
#ifndef Q_OS_MACOS |
| 235 |
dirs.removeAll("parseobjc"_L1); |
| 236 |
#endif |
| 237 |
|
| 238 |
for (const QString &dir : dirs) { |
| 239 |
QTest::newRow(dir.toLocal8Bit()) << dir; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
static QByteArray msgStartFailed(const QProcess &process) |
| 244 |
{ |
| 245 |
const QString result = u'"' + process.program() + u' ' + process.arguments().join(u' ') |
| 246 |
+ "\": "_L1 + process.errorString(); |
| 247 |
return result.toLocal8Bit(); |
| 248 |
} |
| 249 |
|
| 250 |
static QByteArray msgTimeout(const QProcess &process) |
| 251 |
{ |
| 252 |
const QString result = u'"' + process.program() + u' ' + process.arguments().join(u' ') |
| 253 |
+ "\" timed out: "_L1 + process.errorString(); |
| 254 |
return result.toLocal8Bit(); |
| 255 |
} |
| 256 |
|
| 257 |
static QByteArray msgCrashed(const QProcess &process, const QByteArray &output) |
| 258 |
{ |
| 259 |
const QString result = u'"' + process.program() + u' ' + process.arguments().join(u' ') |
| 260 |
+ "\" crashed\n"_L1; |
| 261 |
return result.toLocal8Bit() + output; |
| 262 |
} |
| 263 |
|
| 264 |
static QByteArray msgExitCode(const QProcess &process, const QByteArray &output) |
| 265 |
{ |
| 266 |
const QString result = u'"' + process.program() + u' ' + process.arguments().join(u' ') |
| 267 |
+ "\" exited with code "_L1 + QString::number(process.exitCode()) |
| 268 |
+ u'\n'; |
| 269 |
return result.toLocal8Bit() + output; |
| 270 |
} |
| 271 |
|
| 272 |
void tst_lupdate::good() |
| 273 |
{ |
| 274 |
QFETCH(QString, directory); |
| 275 |
|
| 276 |
QString dir = m_basePath + "good/"_L1 + directory; |
| 277 |
QString workDir = dir; |
| 278 |
QStringList generatedtsfiles("project.ts"_L1); |
| 279 |
QStringList lupdateArguments; |
| 280 |
|
| 281 |
QFile file(dir + "/lupdatecmd"_L1); |
| 282 |
if (file.exists()) { |
| 283 |
QVERIFY2(file.open(QIODevice::ReadOnly | QIODevice::Text), qPrintable(file.fileName())); |
| 284 |
while (!file.atEnd()) { |
| 285 |
QByteArray cmdstring = file.readLine().simplified(); |
| 286 |
if (cmdstring.startsWith(u'#')) |
| 287 |
continue; |
| 288 |
if (cmdstring.startsWith("lupdate"_L1)) { |
| 289 |
for (auto argument : cmdstring.sliced(8).simplified().split(' ')) |
| 290 |
lupdateArguments += argument; |
| 291 |
break; |
| 292 |
} |
| 293 |
if (cmdstring.startsWith("TRANSLATION:"_L1)) { |
| 294 |
cmdstring.remove(0, 12); |
| 295 |
generatedtsfiles.clear(); |
| 296 |
const auto parts = cmdstring.split(u' '); |
| 297 |
for (const QByteArray &s : parts) |
| 298 |
if (!s.isEmpty()) |
| 299 |
generatedtsfiles << QLatin1StringView(s); |
| 300 |
} else if (cmdstring.startsWith("cd "_L1)) { |
| 301 |
cmdstring.remove(0, 3); |
| 302 |
workDir = QDir::cleanPath(dir + u'/' + QLatin1StringView(cmdstring)); |
| 303 |
} |
| 304 |
} |
| 305 |
file.close(); |
| 306 |
} |
| 307 |
|
| 308 |
for (const QString &ts : std::as_const(generatedtsfiles)) { |
| 309 |
QString genTs = workDir + u'/' + ts; |
| 310 |
QFile::remove(genTs); |
| 311 |
QString beforetsfile = dir + u'/' + ts + ".before"_L1; |
| 312 |
if (QFile::exists(beforetsfile)) |
| 313 |
QVERIFY2(QFile::copy(beforetsfile, genTs), qPrintable(beforetsfile)); |
| 314 |
} |
| 315 |
|
| 316 |
file.setFileName(workDir + "/.qmake.cache"_L1); |
| 317 |
QVERIFY(file.open(QIODevice::WriteOnly)); |
| 318 |
file.close(); |
| 319 |
|
| 320 |
if (lupdateArguments.isEmpty()) { |
| 321 |
// Automatically pass "project.pro" or "-project project.json". |
| 322 |
if (QFile::exists(dir + u"/project.json"_s)) { |
| 323 |
lupdateArguments << u"-project"_s << u"project.json"_s; |
| 324 |
} else { |
| 325 |
lupdateArguments.append("project.pro"_L1); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
lupdateArguments.prepend("-silent"_L1); |
| 330 |
|
| 331 |
QProcess proc; |
| 332 |
proc.setWorkingDirectory(workDir); |
| 333 |
proc.setProcessChannelMode(QProcess::MergedChannels); |
| 334 |
const auto startTime = m_timer.elapsed(); |
| 335 |
proc.start(m_cmdLupdate, lupdateArguments, QIODevice::ReadWrite | QIODevice::Text); |
| 336 |
QVERIFY2(proc.waitForStarted(), msgStartFailed(proc).constData()); |
| 337 |
if (!proc.waitForFinished(TIMEOUT)) { |
| 338 |
const auto message = msgTimeout(proc); |
| 339 |
proc.kill(); |
| 340 |
proc.waitForFinished(50); |
| 341 |
QFAIL(message.constData()); |
| 342 |
} |
| 343 |
const auto elapsed = m_timer.elapsed() - startTime; |
| 344 |
if (elapsed > m_maxElapsed) |
| 345 |
m_maxElapsed = elapsed; |
| 346 |
|
| 347 |
const QByteArray output = proc.readAll(); |
| 348 |
QVERIFY2(proc.exitStatus() == QProcess::NormalExit, msgCrashed(proc, output).constData()); |
| 349 |
QVERIFY2(proc.exitCode() == 0, msgExitCode(proc, output).constData()); |
| 350 |
|
| 351 |
qInfo().noquote().nospace() << elapsed << "ms"; |
| 352 |
|
| 353 |
// If the file expectedoutput.txt exists, compare the |
| 354 |
// console output with the content of that file |
| 355 |
QFile outfile(dir + "/expectedoutput.txt"_L1); |
| 356 |
if (outfile.exists()) { |
| 357 |
const QString errslist = QString::fromLocal8Bit(output); |
| 358 |
doCompare(QStringView{errslist}.split(u'\n'), outfile.fileName(), true); |
| 359 |
if (QTest::currentTestFailed()) |
| 360 |
return; |
| 361 |
} |
| 362 |
|
| 363 |
for (const QString &ts : std::as_const(generatedtsfiles)) { |
| 364 |
doCompare(workDir + u'/' + ts, |
| 365 |
dir + u'/' + ts + ".result"_L1, false); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
#if CHECK_SIMTEXTH |
| 370 |
void tst_lupdate::simtexth() |
| 371 |
{ |
| 372 |
QFETCH(QString, one); |
| 373 |
QFETCH(QString, two); |
| 374 |
QFETCH(int, expected); |
| 375 |
|
| 376 |
int measured = getSimilarityScore(one, two.toLatin1()); |
| 377 |
QCOMPARE(measured, expected); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
void tst_lupdate::simtexth_data() |
| 382 |
{ |
| 383 |
using namespace QTest; |
| 384 |
|
| 385 |
addColumn<QString>("one"); |
| 386 |
addColumn<QString>("two"); |
| 387 |
addColumn<int>("expected"); |
| 388 |
|
| 389 |
newRow("00") << "" << "" << 1024; |
| 390 |
newRow("01") << "a" << "a" << 1024; |
| 391 |
newRow("02") << "ab" << "ab" << 1024; |
| 392 |
newRow("03") << "abc" << "abc" << 1024; |
| 393 |
newRow("04") << "abcd" << "abcd" << 1024; |
| 394 |
} |
| 395 |
#endif |
| 396 |
|
| 397 |
QTEST_MAIN(tst_lupdate) |
| 398 |
#include "tst_lupdate.moc" |