diff options
author | Sami Shalayel <sami.shalayel@qt.io> | 2024-04-12 10:43:29 +0200 |
---|---|---|
committer | Sami Shalayel <sami.shalayel@qt.io> | 2024-04-18 11:39:16 +0200 |
commit | f1051cb48243ab27a44ec4f30259fa38116c2783 (patch) | |
tree | 2bf3e6322ac95506dde288ffc0be1cfc944aac62 /tests/auto/qml | |
parent | 1bef3c4d0db67bd9dcfc7289b6322649af980c22 (diff) |
qmllint: enable environment variables like QML_IMPORT_PATH via -E
Add a -E commandline option for qmllint to enable using environment
variables. For now, this only allows using QML_IMPORT_PATH and
QML2_IMPORT_PATH to pass import paths to qmllint.
Task-number: QTBUG-114969
Change-Id: Ie6baf9c8035703a456ba1e7e575ba424f89d287a
Reviewed-by: Semih Yavuz <semih.yavuz@qt.io>
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tests/auto/qml')
-rw-r--r-- | tests/auto/qml/qmllint/tst_qmllint.cpp | 75 |
1 files changed, 70 insertions, 5 deletions
diff --git a/tests/auto/qml/qmllint/tst_qmllint.cpp b/tests/auto/qml/qmllint/tst_qmllint.cpp index dd5e57b707..4e69fc2e9e 100644 --- a/tests/auto/qml/qmllint/tst_qmllint.cpp +++ b/tests/auto/qml/qmllint/tst_qmllint.cpp @@ -43,6 +43,11 @@ public: Flags flags = {}; }; + struct Environment : public QList<QPair<QString, QString>> + { + using QList<QPair<QString, QString>>::QList; + }; + private Q_SLOTS: void initTestCase() override; @@ -100,6 +105,10 @@ private Q_SLOTS: void valueTypesFromString(); void ignoreSettingsNotCommandLineOptions(); + + void environment_data(); + void environment(); + #if QT_CONFIG(library) void testPlugin(); void quickPlugin(); @@ -116,10 +125,12 @@ private: QString runQmllint(const QString &fileToLint, std::function<void(QProcess &)> handleResult, const QStringList &extraArgs = QStringList(), bool ignoreSettings = true, - bool addImportDirs = true, bool absolutePath = true); + bool addImportDirs = true, bool absolutePath = true, + const Environment &env = {}); QString runQmllint(const QString &fileToLint, bool shouldSucceed, const QStringList &extraArgs = QStringList(), bool ignoreSettings = true, - bool addImportDirs = true, bool absolutePath = true); + bool addImportDirs = true, bool absolutePath = true, + const Environment &env = {}); void callQmllint(const QString &fileToLint, bool shouldSucceed, QJsonArray *warnings = nullptr, QStringList importDirs = {}, QStringList qmltypesFiles = {}, QStringList resources = {}, @@ -1419,7 +1430,7 @@ void TestQmllint::compilerWarnings() QString TestQmllint::runQmllint(const QString &fileToLint, std::function<void(QProcess &)> handleResult, const QStringList &extraArgs, bool ignoreSettings, - bool addImportDirs, bool absolutePath) + bool addImportDirs, bool absolutePath, const Environment &env) { auto qmlImportDir = QLibraryInfo::path(QLibraryInfo::QmlImportsPath); QStringList args; @@ -1445,6 +1456,11 @@ QString TestQmllint::runQmllint(const QString &fileToLint, QString errors; auto verify = [&](bool isSilent) { QProcess process; + QProcessEnvironment processEnv = QProcessEnvironment::systemEnvironment(); + for (const auto &entry : env) + processEnv.insert(entry.first, entry.second); + + process.setProcessEnvironment(processEnv); process.setWorkingDirectory(QFileInfo(absoluteFilePath).absolutePath()); process.start(m_qmllintPath, args); handleResult(process); @@ -1487,7 +1503,7 @@ QString TestQmllint::runQmllint(const QString &fileToLint, QString TestQmllint::runQmllint(const QString &fileToLint, bool shouldSucceed, const QStringList &extraArgs, bool ignoreSettings, - bool addImportDirs, bool absolutePath) + bool addImportDirs, bool absolutePath, const Environment &env) { return runQmllint( fileToLint, @@ -1500,7 +1516,7 @@ QString TestQmllint::runQmllint(const QString &fileToLint, bool shouldSucceed, else QVERIFY(process.exitCode() != 0); }, - extraArgs, ignoreSettings, addImportDirs, absolutePath); + extraArgs, ignoreSettings, addImportDirs, absolutePath, env); } void TestQmllint::callQmllint(const QString &fileToLint, bool shouldSucceed, QJsonArray *warnings, @@ -2140,6 +2156,55 @@ void TestQmllint::quickPlugin() } }); runTest("pluginQuick_propertyChangesInvalidTarget.qml", Result {}); // we don't care about the specific warnings } + +void TestQmllint::environment_data() +{ + QTest::addColumn<QString>("file"); + QTest::addColumn<bool>("shouldSucceed"); + QTest::addColumn<QStringList>("extraArgs"); + QTest::addColumn<Environment>("env"); + QTest::addColumn<QString>("expectedWarning"); + + const QString fileThatNeedsImportPath = testFile(u"NeedImportPath.qml"_s); + const QString importPath = testFile(u"ImportPath"_s); + const QString invalidImportPath = testFile(u"ImportPathThatDoesNotExist"_s); + const QString noWarningExpected; + + QTest::addRow("missing-import-dir") + << fileThatNeedsImportPath << false << QStringList{} + << Environment{ { u"QML_IMPORT_PATH"_s, importPath } } << noWarningExpected; + + QTest::addRow("import-dir-via-arg") + << fileThatNeedsImportPath << true << QStringList{ u"-I"_s, importPath } + << Environment{ { u"QML_IMPORT_PATH"_s, invalidImportPath } } << noWarningExpected; + + QTest::addRow("import-dir-via-env") + << fileThatNeedsImportPath << true << QStringList{ u"-E"_s } + << Environment{ { u"QML_IMPORT_PATH"_s, importPath } } + << u"Using import directories passed from environment variable \"QML_IMPORT_PATH\": \"%1\"."_s + .arg(importPath); + + QTest::addRow("import-dir-via-env2") + << fileThatNeedsImportPath << true << QStringList{ u"-E"_s } + << Environment{ { u"QML2_IMPORT_PATH"_s, importPath } } + << u"Using import directories passed from the deprecated environment variable \"QML2_IMPORT_PATH\": \"%1\"."_s + .arg(importPath); +} + +void TestQmllint::environment() +{ + QFETCH(QString, file); + QFETCH(bool, shouldSucceed); + QFETCH(QStringList, extraArgs); + QFETCH(Environment, env); + QFETCH(QString, expectedWarning); + + const QString output = runQmllint(file, shouldSucceed, extraArgs, false, true, false, env); + if (!expectedWarning.isEmpty()) { + QVERIFY(output.contains(expectedWarning)); + } +} + #endif void TestQmllint::ignoreSettingsNotCommandLineOptions() |