From 001bf31623c02ba8249dd066777d014d546eb7f9 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Fri, 9 Apr 2021 11:31:07 +0200 Subject: qbs-config: Add convenience command for adding a profile in one go Task-number: QTCREATORBUG-25463 Change-Id: I2d18ea5f7d53efc0762d34cf4845b49533ca6fbf Reviewed-by: Ivan Komissarov --- src/app/config/configcommand.h | 2 +- src/app/config/configcommandexecutor.cpp | 12 ++++++ src/app/config/configcommandlineparser.cpp | 31 ++++++++++++--- tests/auto/blackbox/tst_blackbox.cpp | 61 ++++++++++++++++++++++++++++++ tests/auto/blackbox/tst_blackbox.h | 2 + 5 files changed, 101 insertions(+), 7 deletions(-) diff --git a/src/app/config/configcommand.h b/src/app/config/configcommand.h index 1574e3745..f8e2a8ae9 100644 --- a/src/app/config/configcommand.h +++ b/src/app/config/configcommand.h @@ -47,7 +47,7 @@ class ConfigCommand { public: - enum Command { CfgSet, CfgUnset, CfgList, CfgExport, CfgImport, CfgNone }; + enum Command { CfgSet, CfgUnset, CfgList, CfgExport, CfgImport, CfgAddProfile, CfgNone }; ConfigCommand() : command(CfgNone) {} diff --git a/src/app/config/configcommandexecutor.cpp b/src/app/config/configcommandexecutor.cpp index 0a6883aa7..a01058b11 100644 --- a/src/app/config/configcommandexecutor.cpp +++ b/src/app/config/configcommandexecutor.cpp @@ -42,6 +42,7 @@ #include "../shared/logging/consolelogger.h" #include +#include #include #include @@ -73,6 +74,17 @@ void ConfigCommandExecutor::execute(const ConfigCommand &command) for (const QString &varName : command.varNames) m_settings->remove(varName); break; + case ConfigCommand::CfgAddProfile: { + Profile profile(command.varValue, m_settings); + profile.removeProfile(); + Q_ASSERT(command.varNames.size() % 2 == 0); + for (int i = 0; i < command.varNames.size(); i += 2) { + const QString &key = command.varNames.at(i); + const QString &rawValue = command.varNames.at(i + 1); + profile.setValue(key, representationToSettingsValue(rawValue)); + } + break; + } case ConfigCommand::CfgExport: exportSettings(command.fileName); break; diff --git a/src/app/config/configcommandlineparser.cpp b/src/app/config/configcommandlineparser.cpp index 2f2d1610e..8652a58bc 100644 --- a/src/app/config/configcommandlineparser.cpp +++ b/src/app/config/configcommandlineparser.cpp @@ -71,6 +71,8 @@ void ConfigCommandLineParser::parse(const QStringList &commandLine) setCommand(ConfigCommand::CfgExport); else if (arg == QLatin1String("import")) setCommand(ConfigCommand::CfgImport); + else if (arg == QLatin1String("add-profile")) + setCommand(ConfigCommand::CfgAddProfile); else if (arg == QLatin1String("settings-dir")) assignOptionArgument(arg, m_settingsDir); else if (arg == QLatin1String("user")) @@ -113,6 +115,22 @@ void ConfigCommandLineParser::parse(const QStringList &commandLine) case ConfigCommand::CfgList: m_command.varNames = m_commandLine; break; + case ConfigCommand::CfgAddProfile: + if (m_commandLine.empty()) + throw Error(Tr::tr("Profile name missing.")); + m_command.varValue = m_commandLine.takeFirst(); + if (m_command.varValue.isEmpty()) + throw Error(Tr::tr("Profile name must not be empty.")); + m_command.varNames = m_commandLine; + if (m_command.varNames.isEmpty()) + throw Error(Tr::tr("Profile properties must be provided.")); + if (m_command.varNames.size() % 2 != 0) + throw Error(Tr::tr("Profile properties must be key/value pairs.")); + for (int i = 0; i < m_command.varNames.size(); ++i) { + if (m_command.varNames.at(i).isEmpty()) + throw Error(Tr::tr("Property names must not be empty.")); + } + break; default: break; } @@ -140,12 +158,13 @@ void ConfigCommandLineParser::printUsage() const " qbs config [--settings-dir " "\n" "Options:\n" - " --list [ ...] list keys under key or all keys\n" - " --user consider only user-level settings\n" - " --system consider only system-level settings\n" - " --unset remove key with given name\n" - " --import import settings from given file\n" - " --export export settings to given file\n"); + " --list [ ...] list keys under key or all keys\n" + " --user consider only user-level settings\n" + " --system consider only system-level settings\n" + " --unset remove key with given name\n" + " --add-profile ... add profile with the given name and properties\n" + " --import import settings from given file\n" + " --export export settings to given file\n"); } void ConfigCommandLineParser::assignOptionArgument(const QString &option, QString &argument) diff --git a/tests/auto/blackbox/tst_blackbox.cpp b/tests/auto/blackbox/tst_blackbox.cpp index 1b9fa0b15..7d75ab91f 100644 --- a/tests/auto/blackbox/tst_blackbox.cpp +++ b/tests/auto/blackbox/tst_blackbox.cpp @@ -5958,6 +5958,67 @@ void TestBlackbox::qbsConfig() } } +void TestBlackbox::qbsConfigAddProfile() +{ + QbsRunParameters params("config"); + QTemporaryDir settingsDir1; + QTemporaryDir settingsDir2; + QVERIFY(settingsDir1.isValid()); + QVERIFY(settingsDir2.isValid()); + const QStringList settingsDir1Args = QStringList{"--settings-dir", settingsDir1.path()}; + const QStringList settingsDir2Args = QStringList{"--settings-dir", settingsDir2.path()}; + + QFETCH(QStringList, args); + QFETCH(QString, errorMsg); + + // Step 1: Run --add-profile. + params.arguments = settingsDir1Args; + params.arguments << "--add-profile"; + params.arguments << args; + params.expectFailure = !errorMsg.isEmpty(); + QCOMPARE(runQbs(params) == 0, !params.expectFailure); + if (params.expectFailure) { + QVERIFY(QString::fromLocal8Bit(m_qbsStderr).contains(errorMsg)); + return; + } + params.expectFailure = false; + params.arguments = settingsDir1Args; + params.arguments << "--list"; + QCOMPARE(runQbs(params), 0); + const QByteArray output1 = m_qbsStdout; + + // Step 2: Set properties manually. + for (int i = 1; i < args.size(); i += 2) { + params.arguments = settingsDir2Args; + params.arguments << ("profiles." + args.first() + '.' + args.at(i)) << args.at(i + 1); + QCOMPARE(runQbs(params), 0); + } + params.arguments = settingsDir2Args; + params.arguments << "--list"; + QCOMPARE(runQbs(params), 0); + const QByteArray output2 = m_qbsStdout; + + // Step3: Compare results. + QCOMPARE(output1, output2); +} + +void TestBlackbox::qbsConfigAddProfile_data() +{ + QTest::addColumn("args"); + QTest::addColumn("errorMsg"); + QTest::newRow("no arguments") << QStringList() << QString("Profile name missing"); + QTest::newRow("empty name") << QStringList{"", "p", "v"} + << QString("Profile name must not be empty"); + QTest::newRow("no properties") << QStringList("p") + << QString("Profile properties must be provided"); + QTest::newRow("one property") << QStringList{"p", "p", "v"} << QString(); + QTest::newRow("two properties") << QStringList{"p", "p1", "v1", "p2", "v2"} << QString(); + QTest::newRow("missing value") << QStringList{"p", "p"} + << QString("Profile properties must be key/value pairs"); + QTest::newRow("missing values") << QStringList{"p", "p1", "v1", "p2"} + << QString("Profile properties must be key/value pairs"); +} + static QJsonObject getNextSessionPacket(QProcess &session, QByteArray &data) { int totalSize = -1; diff --git a/tests/auto/blackbox/tst_blackbox.h b/tests/auto/blackbox/tst_blackbox.h index 8cc21378a..942cb6b47 100644 --- a/tests/auto/blackbox/tst_blackbox.h +++ b/tests/auto/blackbox/tst_blackbox.h @@ -258,6 +258,8 @@ private slots: void protobufLibraryInstall(); void pseudoMultiplexing(); void qbsConfig(); + void qbsConfigAddProfile(); + void qbsConfigAddProfile_data(); void qbsSession(); void qbsVersion(); void qtBug51237(); -- cgit v1.2.3