diff options
author | Maximilian Goldstein <max.goldstein@qt.io> | 2020-03-18 09:19:31 +0100 |
---|---|---|
committer | Maximilian Goldstein <max.goldstein@qt.io> | 2020-03-25 06:15:13 +0100 |
commit | 4faf242d1a18e206ddd9c567649a9dddbf6217df (patch) | |
tree | b7e5c81f06123d0d67196609bfd13378224b8b9f | |
parent | 2b8042182d77241ffe7e6b4edf4e727315f93147 (diff) |
qmlformat: Add option for alternative line endings
Allows user to decide between native (default), macos (\r), unix (\n) or windows (\r\n)
line endings.
Fixes: QTBUG-82258
Change-Id: Ie1eb365085815cbbebbf0d026c6f72f0ef2acb9d
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r-- | tests/auto/qml/qmlformat/tst_qmlformat.cpp | 44 | ||||
-rw-r--r-- | tools/qmlformat/main.cpp | 35 |
2 files changed, 69 insertions, 10 deletions
diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp index 47255d7745..bc48e07ae4 100644 --- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp +++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp @@ -27,8 +27,11 @@ ****************************************************************************/ #include <QtTest/QtTest> +#include <QDir> +#include <QFile> #include <QProcess> #include <QString> +#include <QTemporaryDir> #include <util.h> @@ -43,6 +46,7 @@ private Q_SLOTS: void testFormatNoSort(); void testAnnotations(); void testAnnotationsNoSort(); + void testLineEndings(); void testReadOnlyProps(); @@ -53,7 +57,7 @@ private Q_SLOTS: private: QString readTestFile(const QString &path); - QString runQmlformat(const QString &fileToFormat, bool sortImports, bool shouldSucceed); + QString runQmlformat(const QString &fileToFormat, bool sortImports, bool shouldSucceed, const QString &newlineFormat = "native"); QString m_qmlformatPath; QStringList m_excludedDirs; @@ -200,6 +204,23 @@ void TestQmlformat::testReadOnlyProps() QCOMPARE(runQmlformat(testFile("readOnlyProps.qml"), false, true), readTestFile("readOnlyProps.formatted.qml")); } +void TestQmlformat::testLineEndings() +{ + // macos + const QString macosContents = runQmlformat(testFile("Example1.formatted.qml"), false, true, "macos"); + QVERIFY(!macosContents.contains("\n")); + QVERIFY(macosContents.contains("\r")); + + // windows + const QString windowsContents = runQmlformat(testFile("Example1.formatted.qml"), false, true, "windows"); + QVERIFY(windowsContents.contains("\r\n")); + + // unix + const QString unixContents = runQmlformat(testFile("Example1.formatted.qml"), false, true, "unix"); + QVERIFY(unixContents.contains("\n")); + QVERIFY(!unixContents.contains("\r")); +} + #if !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled void TestQmlformat::testExample_data() { @@ -228,15 +249,22 @@ void TestQmlformat::testExample() } #endif -QString TestQmlformat::runQmlformat(const QString &fileToFormat, bool sortImports, bool shouldSucceed) +QString TestQmlformat::runQmlformat(const QString &fileToFormat, bool sortImports, bool shouldSucceed, const QString &newlineFormat) { + // Copy test file to temporary location + QTemporaryDir tempDir; + const QString tempFile = tempDir.path() + QDir::separator() + "to_format.qml"; + QFile::copy(fileToFormat, tempFile); + QStringList args; - args << fileToFormat; + args << "-i"; + args << tempFile; if (!sortImports) args << "-n"; - QString output; + args << "-l" << newlineFormat; + auto verify = [&]() { QProcess process; process.start(m_qmlformatPath, args); @@ -246,11 +274,15 @@ QString TestQmlformat::runQmlformat(const QString &fileToFormat, bool sortImport QCOMPARE(process.exitCode(), 0); else QVERIFY(process.exitCode() != 0); - output = process.readAllStandardOutput(); }; verify(); - return output; + QFile temp(tempFile); + + temp.open(QIODevice::ReadOnly); + QString formatted = QString::fromUtf8(temp.readAll()); + + return formatted; } QTEST_MAIN(TestQmlformat) diff --git a/tools/qmlformat/main.cpp b/tools/qmlformat/main.cpp index da58ffd5d0..3e71110cf9 100644 --- a/tools/qmlformat/main.cpp +++ b/tools/qmlformat/main.cpp @@ -43,7 +43,7 @@ #include "dumpastvisitor.h" #include "restructureastvisitor.h" -bool parseFile(const QString& filename, bool inplace, bool verbose, bool sortImports, bool force) +bool parseFile(const QString& filename, bool inplace, bool verbose, bool sortImports, bool force, const QString& newline) { QFile file(filename); @@ -128,11 +128,27 @@ bool parseFile(const QString& filename, bool inplace, bool verbose, bool sortImp } } + + const bool native = newline == "native"; + + if (!native) { + if (newline == "macos") { + dumpCode = dumpCode.replace("\n","\r"); + } else if (newline == "windows") { + dumpCode = dumpCode.replace("\n", "\r\n"); + } else if (newline == "unix") { + // Nothing needs to be done for unix line-endings + } else { + qWarning().noquote() << "Unknown line ending type" << newline; + return false; + } + } + if (inplace) { if (verbose) qWarning().noquote() << "Writing to file" << filename; - if (!file.open(QIODevice::Text | QIODevice::WriteOnly)) + if (!file.open(native ? QIODevice::WriteOnly | QIODevice::Text : QIODevice::WriteOnly)) { qWarning().noquote() << "Failed to open" << filename << "for writing"; return false; @@ -141,7 +157,9 @@ bool parseFile(const QString& filename, bool inplace, bool verbose, bool sortImp file.write(dumpCode.toUtf8()); file.close(); } else { - QTextStream(stdout) << dumpCode; + QFile out; + out.open(stdout, QIODevice::WriteOnly); + out.write(dumpCode.toUtf8()); } return true; @@ -172,6 +190,10 @@ int main(int argc, char *argv[]) parser.addOption(QCommandLineOption({"f", "force"}, QStringLiteral("Continue even if an error has occurred."))); + parser.addOption(QCommandLineOption({"l", "newline"}, + QStringLiteral("Override the new line format to use (native macos unix windows)."), + "newline", "native")); + parser.addPositionalArgument("filenames", "files to be processed by qmlformat"); parser.process(app); @@ -181,8 +203,13 @@ int main(int argc, char *argv[]) if (positionalArguments.isEmpty()) parser.showHelp(-1); + if (!parser.isSet("inplace") && parser.value("newline") != "native") { + qWarning() << "Error: The -l option can only be used with -i"; + return -1; + } + for (const QString& file: parser.positionalArguments()) { - if (!parseFile(file, parser.isSet("inplace"), parser.isSet("verbose"), !parser.isSet("no-sort"), parser.isSet("force"))) + if (!parseFile(file, parser.isSet("inplace"), parser.isSet("verbose"), !parser.isSet("no-sort"), parser.isSet("force"), parser.value("newline"))) success = false; } #endif |