2015-05-08 21:10:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-19 11:23:05 +00:00
|
|
|
** Copyright (C) 2016 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sergio Martins <sergio.martins@kdab.com>
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2015-05-08 21:10:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the plugins of the Qt Toolkit.
|
|
|
|
**
|
2016-01-19 11:23:05 +00:00
|
|
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
2015-05-08 21:10:43 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-19 11:23:05 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2015-05-08 21:10:43 +00:00
|
|
|
**
|
2016-01-19 11:23:05 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2015-05-08 21:10:43 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <QtTest/QtTest>
|
|
|
|
#include <QProcess>
|
|
|
|
#include <QString>
|
|
|
|
|
2019-08-16 11:11:53 +00:00
|
|
|
#include <util.h>
|
|
|
|
|
|
|
|
class TestQmllint: public QQmlDataTest
|
2015-05-08 21:10:43 +00:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
2019-08-16 11:11:53 +00:00
|
|
|
void initTestCase() override;
|
2019-09-19 08:42:51 +00:00
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
void testUnqualified();
|
|
|
|
void testUnqualified_data();
|
2019-09-19 08:42:51 +00:00
|
|
|
|
|
|
|
void cleanQmlCode_data();
|
|
|
|
void cleanQmlCode();
|
|
|
|
|
|
|
|
void dirtyQmlCode_data();
|
|
|
|
void dirtyQmlCode();
|
|
|
|
|
2019-07-23 14:10:24 +00:00
|
|
|
void testUnqualifiedNoSpuriousParentWarning();
|
2019-08-16 09:31:51 +00:00
|
|
|
|
2015-05-08 21:10:43 +00:00
|
|
|
private:
|
2019-08-21 09:43:42 +00:00
|
|
|
QString runQmllint(const QString &fileToLint, bool shouldSucceed);
|
|
|
|
|
2015-05-08 21:10:43 +00:00
|
|
|
QString m_qmllintPath;
|
|
|
|
};
|
|
|
|
|
|
|
|
void TestQmllint::initTestCase()
|
|
|
|
{
|
2019-08-16 11:11:53 +00:00
|
|
|
QQmlDataTest::initTestCase();
|
2015-05-08 21:10:43 +00:00
|
|
|
m_qmllintPath = QLibraryInfo::location(QLibraryInfo::BinariesPath) + QLatin1String("/qmllint");
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
m_qmllintPath += QLatin1String(".exe");
|
|
|
|
#endif
|
|
|
|
if (!QFileInfo(m_qmllintPath).exists()) {
|
|
|
|
QString message = QStringLiteral("qmllint executable not found (looked for %0)").arg(m_qmllintPath);
|
|
|
|
QFAIL(qPrintable(message));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
void TestQmllint::testUnqualified()
|
|
|
|
{
|
|
|
|
QFETCH(QString, filename);
|
|
|
|
QFETCH(QString, warningMessage);
|
|
|
|
QFETCH(int, warningLine);
|
|
|
|
QFETCH(int, warningColumn);
|
|
|
|
|
2019-08-21 09:43:42 +00:00
|
|
|
const QString output = runQmllint(filename, false);
|
2019-06-14 12:21:25 +00:00
|
|
|
QVERIFY(output.contains(QString::asprintf("Warning: unqualified access at %d:%d", warningLine, warningColumn)));
|
|
|
|
QVERIFY(output.contains(warningMessage));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestQmllint::testUnqualified_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QString>("filename");
|
|
|
|
QTest::addColumn<QString>("warningMessage");
|
|
|
|
QTest::addColumn<int>("warningLine");
|
|
|
|
QTest::addColumn<int>("warningColumn");
|
|
|
|
|
|
|
|
// check for false positive due to and warning about with statement
|
|
|
|
QTest::newRow("WithStatement") << QStringLiteral("WithStatement.qml") << QStringLiteral("with statements are strongly discouraged") << 10 << 25;
|
|
|
|
// id from nowhere (as with setContextProperty)
|
|
|
|
QTest::newRow("IdFromOuterSpaceDirect") << QStringLiteral("IdFromOuterSpace.qml") << "alien.x" << 4 << 8;
|
|
|
|
QTest::newRow("IdFromOuterSpaceAccess") << QStringLiteral("IdFromOuterSpace.qml") << "console.log(alien)" << 7 << 21;
|
|
|
|
// access property of root object
|
|
|
|
QTest::newRow("FromRootDirect") << QStringLiteral("FromRoot.qml") << QStringLiteral("x: root.unqualified") << 9 << 16; // new property
|
|
|
|
QTest::newRow("FromRootAccess") << QStringLiteral("FromRoot.qml") << QStringLiteral("property int check: root.x") << 13 << 33; // builtin property
|
|
|
|
// access injected name from signal
|
2019-07-24 13:43:49 +00:00
|
|
|
QTest::newRow("SignalHandler1") << QStringLiteral("SignalHandler.qml") << QStringLiteral("onDoubleClicked: function(mouse) {...") << 5 << 21;
|
|
|
|
QTest::newRow("SignalHandler2") << QStringLiteral("SignalHandler.qml") << QStringLiteral("onPositionChanged: function(mouse) {...") << 10 << 21;
|
|
|
|
QTest::newRow("SignalHandlerShort1") << QStringLiteral("SignalHandler.qml") << QStringLiteral("onClicked: (mouse) => {...") << 8 << 29;
|
|
|
|
QTest::newRow("SignalHandlerShort2") << QStringLiteral("SignalHandler.qml") << QStringLiteral("onPressAndHold: (mouse) => {...") << 12 << 34;
|
2019-08-16 09:24:24 +00:00
|
|
|
// access catch identifier outside catch block
|
|
|
|
QTest::newRow("CatchStatement") << QStringLiteral("CatchStatement.qml") << QStringLiteral("err") << 6 << 21;
|
2019-06-14 12:21:25 +00:00
|
|
|
|
2019-09-19 08:42:51 +00:00
|
|
|
QTest::newRow("NonSpuriousParent") << QStringLiteral("nonSpuriousParentWarning.qml") << QStringLiteral("property int x: <id>.parent.x") << 6 << 25;
|
2019-07-23 14:10:24 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 08:42:51 +00:00
|
|
|
void TestQmllint::testUnqualifiedNoSpuriousParentWarning()
|
2019-08-16 09:24:24 +00:00
|
|
|
{
|
2019-11-08 13:48:32 +00:00
|
|
|
const QString unknownNotFound = runQmllint("spuriousParentWarning.qml", false);
|
2019-09-19 08:42:51 +00:00
|
|
|
QVERIFY(unknownNotFound.contains(
|
|
|
|
QStringLiteral("warning: Unknown was not found. Did you add all import paths?")));
|
2019-08-16 09:24:24 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 08:42:51 +00:00
|
|
|
void TestQmllint::dirtyQmlCode_data()
|
2019-08-16 09:31:51 +00:00
|
|
|
{
|
2019-09-19 08:42:51 +00:00
|
|
|
QTest::addColumn<QString>("filename");
|
|
|
|
QTest::addColumn<QString>("warningMessage");
|
|
|
|
QTest::addColumn<QString>("notContained");
|
|
|
|
|
|
|
|
QTest::newRow("Invalid_syntax_QML")
|
|
|
|
<< QStringLiteral("failure1.qml")
|
|
|
|
<< QStringLiteral("failure1.qml:4 : Expected token `:'")
|
|
|
|
<< QString();
|
|
|
|
QTest::newRow("Invalid_syntax_JS")
|
|
|
|
<< QStringLiteral("failure1.js")
|
|
|
|
<< QStringLiteral("failure1.js:4 : Expected token `;'")
|
|
|
|
<< QString();
|
2019-11-11 17:18:04 +00:00
|
|
|
QTest::newRow("AutomatchedSignalHandler")
|
|
|
|
<< QStringLiteral("AutomatchedSignalHandler.qml")
|
|
|
|
<< QString("Warning: unqualified access at 12:36")
|
|
|
|
<< QStringLiteral("no matching signal found");
|
2019-11-08 13:48:32 +00:00
|
|
|
QTest::newRow("MemberNotFound")
|
|
|
|
<< QStringLiteral("memberNotFound.qml")
|
|
|
|
<< QString("Warning: Property \"foo\" not found on type \"QtObject\" at 6:31")
|
|
|
|
<< QString();
|
|
|
|
QTest::newRow("UnknownJavascriptMethd")
|
|
|
|
<< QStringLiteral("unknownJavascriptMethod.qml")
|
|
|
|
<< QString("Warning: Property \"foo2\" not found on type \"Methods\" at 5:25")
|
|
|
|
<< QString();
|
2019-11-11 16:35:09 +00:00
|
|
|
QTest::newRow("badAlias")
|
|
|
|
<< QStringLiteral("badAlias.qml")
|
|
|
|
<< QString("Warning: unqualified access at 4:27")
|
|
|
|
<< QString();
|
|
|
|
QTest::newRow("badAliasProperty")
|
|
|
|
<< QStringLiteral("badAliasProperty.qml")
|
|
|
|
<< QString("Warning: Property \"nowhere\" not found on type \"QtObject\" at 5:32")
|
|
|
|
<< QString();
|
2019-11-14 14:59:56 +00:00
|
|
|
QTest::newRow("badParent")
|
|
|
|
<< QStringLiteral("badParent.qml")
|
|
|
|
<< QString("Warning: Property \"rrr\" not found on type \"Item\" at 5:34")
|
|
|
|
<< QString();
|
|
|
|
QTest::newRow("parentIsComponent")
|
|
|
|
<< QStringLiteral("parentIsComponent.qml")
|
|
|
|
<< QString("Warning: Property \"progress\" not found on type \"QQuickItem\" at 7:39")
|
|
|
|
<< QString();
|
2019-11-15 15:59:46 +00:00
|
|
|
QTest::newRow("badTypeAssertion")
|
|
|
|
<< QStringLiteral("badTypeAssertion.qml")
|
|
|
|
<< QString("Warning: Property \"rrr\" not found on type \"Item\" at 5:39")
|
|
|
|
<< QString();
|
2019-08-16 09:31:51 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 08:42:51 +00:00
|
|
|
void TestQmllint::dirtyQmlCode()
|
2019-08-16 15:03:30 +00:00
|
|
|
{
|
2019-09-19 08:42:51 +00:00
|
|
|
QFETCH(QString, filename);
|
|
|
|
QFETCH(QString, warningMessage);
|
|
|
|
QFETCH(QString, notContained);
|
2019-08-16 15:03:30 +00:00
|
|
|
|
2019-09-19 08:42:51 +00:00
|
|
|
const QString output = runQmllint(filename, false);
|
|
|
|
QVERIFY(output.contains(warningMessage));
|
|
|
|
if (!notContained.isEmpty())
|
|
|
|
QVERIFY(!output.contains(notContained));
|
2019-08-16 14:44:06 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 08:42:51 +00:00
|
|
|
void TestQmllint::cleanQmlCode_data()
|
2019-08-16 14:15:22 +00:00
|
|
|
{
|
2019-09-19 08:42:51 +00:00
|
|
|
QTest::addColumn<QString>("filename");
|
|
|
|
QTest::newRow("Simple_QML") << QStringLiteral("Simple.qml");
|
|
|
|
QTest::newRow("QML_importing_JS") << QStringLiteral("importing_js.qml");
|
|
|
|
QTest::newRow("JS_with_pragma_and_import") << QStringLiteral("QTBUG-45916.js");
|
|
|
|
QTest::newRow("uiQml") << QStringLiteral("FormUser.qml");
|
|
|
|
QTest::newRow("methodInScope") << QStringLiteral("MethodInScope.qml");
|
|
|
|
QTest::newRow("importWithPrefix") << QStringLiteral("ImportWithPrefix.qml");
|
|
|
|
QTest::newRow("catchIdentifier") << QStringLiteral("catchIdentifierNoWarning.qml");
|
2019-09-13 09:34:08 +00:00
|
|
|
QTest::newRow("qmldirAndQmltypes") << QStringLiteral("qmldirAndQmltypes.qml");
|
2019-09-17 15:13:18 +00:00
|
|
|
QTest::newRow("forLoop") << QStringLiteral("forLoop.qml");
|
2019-10-15 08:39:40 +00:00
|
|
|
QTest::newRow("esmodule") << QStringLiteral("esmodule.mjs");
|
2019-11-08 17:43:31 +00:00
|
|
|
QTest::newRow("methodsInJavascript") << QStringLiteral("javascriptMethods.qml");
|
2019-11-11 16:35:09 +00:00
|
|
|
QTest::newRow("goodAlias") << QStringLiteral("goodAlias.qml");
|
2019-11-14 14:59:56 +00:00
|
|
|
QTest::newRow("goodParent") << QStringLiteral("goodParent.qml");
|
2019-11-15 15:59:46 +00:00
|
|
|
QTest::newRow("goodTypeAssertion") << QStringLiteral("goodTypeAssertion.qml");
|
2019-08-16 14:15:22 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 08:42:51 +00:00
|
|
|
void TestQmllint::cleanQmlCode()
|
2015-05-08 21:10:43 +00:00
|
|
|
{
|
|
|
|
QFETCH(QString, filename);
|
2019-09-19 08:42:51 +00:00
|
|
|
const QString warnings = runQmllint(filename, true);
|
|
|
|
QVERIFY(warnings.isEmpty());
|
2015-05-08 21:10:43 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 09:43:42 +00:00
|
|
|
QString TestQmllint::runQmllint(const QString &fileToLint, bool shouldSucceed)
|
|
|
|
{
|
|
|
|
auto qmlImportDir = QLibraryInfo::location(QLibraryInfo::Qml2ImportsPath);
|
|
|
|
QStringList args;
|
|
|
|
args << QStringLiteral("-U") << testFile(fileToLint)
|
2019-09-19 08:42:51 +00:00
|
|
|
<< QStringLiteral("-I") << qmlImportDir
|
2019-09-13 09:34:08 +00:00
|
|
|
<< QStringLiteral("-I") << dataDirectory()
|
2019-09-19 08:42:51 +00:00
|
|
|
<< QStringLiteral("--silent");
|
|
|
|
QString errors;
|
|
|
|
auto verify = [&](bool isSilent) {
|
|
|
|
QProcess process;
|
|
|
|
process.start(m_qmllintPath, args);
|
2019-08-21 09:43:42 +00:00
|
|
|
QVERIFY(process.waitForFinished());
|
|
|
|
QCOMPARE(process.exitStatus(), QProcess::NormalExit);
|
|
|
|
if (shouldSucceed)
|
|
|
|
QCOMPARE(process.exitCode(), 0);
|
|
|
|
else
|
|
|
|
QVERIFY(process.exitCode() != 0);
|
2019-09-19 08:42:51 +00:00
|
|
|
errors = process.readAllStandardError();
|
|
|
|
|
|
|
|
if (isSilent)
|
|
|
|
QVERIFY(errors.isEmpty());
|
|
|
|
};
|
|
|
|
verify(true);
|
|
|
|
args.removeLast();
|
|
|
|
verify(false);
|
|
|
|
return errors;
|
2019-08-21 09:43:42 +00:00
|
|
|
}
|
|
|
|
|
2015-05-08 21:10:43 +00:00
|
|
|
QTEST_MAIN(TestQmllint)
|
2019-08-16 11:11:53 +00:00
|
|
|
#include "tst_qmllint.moc"
|