From c641f5dec68df2db2cc1fdafd99eb0cd4c057f15 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 29 Jan 2018 21:08:42 +0100 Subject: [PATCH] [libcalamares] Implement object-style command line - handle command: and timeout: entries - test for setting the values --- src/libcalamares/utils/CommandList.cpp | 26 ++++++++++++++++++++++++++ src/libcalamares/utils/CommandList.h | 12 ++++++++++++ src/modules/shellprocess/Tests.cpp | 9 ++++++++- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/libcalamares/utils/CommandList.cpp b/src/libcalamares/utils/CommandList.cpp index 543c6a5ad..64b88a387 100644 --- a/src/libcalamares/utils/CommandList.cpp +++ b/src/libcalamares/utils/CommandList.cpp @@ -21,6 +21,7 @@ #include "GlobalStorage.h" #include "JobQueue.h" +#include "utils/CalamaresUtils.h" #include "utils/CalamaresUtilsSystem.h" #include "utils/Logger.h" @@ -29,6 +30,17 @@ namespace CalamaresUtils { +static CommandLine get_variant_object( const QVariantMap& m ) +{ + QString command = CalamaresUtils::getString( m, "command" ); + int timeout = CalamaresUtils::getInteger( m, "timeout", -1 ); + + if ( !command.isEmpty() ) + return CommandLine( command, timeout ); + cDebug() << "WARNING Bad CommandLine element" << m; + return CommandLine(); +} + static CommandList_t get_variant_stringlist( const QVariantList& l, int timeout ) { CommandList_t retl; @@ -37,6 +49,13 @@ static CommandList_t get_variant_stringlist( const QVariantList& l, int timeout { if ( v.type() == QVariant::String ) retl.append( CommandLine( v.toString(), timeout ) ); + else if ( v.type() == QVariant::Map ) + { + auto c( get_variant_object( v.toMap() ) ); + if ( c.isValid() ) + retl.append( c ); + // Otherwise warning is already given + } else cDebug() << "WARNING Bad CommandList element" << c << v.type() << v; ++c; @@ -63,6 +82,13 @@ CommandList::CommandList::CommandList( const QVariant& v, bool doChroot, int tim } else if ( v.type() == QVariant::String ) append( v.toString() ); + else if ( v.type() == QVariant::Map ) + { + auto c( get_variant_object( v.toMap() ) ); + if ( c.isValid() ) + append( c ); + // Otherwise warning is already given + } else cDebug() << "WARNING: CommandList does not understand variant" << v.type(); } diff --git a/src/libcalamares/utils/CommandList.h b/src/libcalamares/utils/CommandList.h index e80a1b742..58d2e2721 100644 --- a/src/libcalamares/utils/CommandList.h +++ b/src/libcalamares/utils/CommandList.h @@ -33,6 +33,12 @@ namespace CalamaresUtils */ struct CommandLine : public QPair< QString, int > { + /// An invalid command line + CommandLine() + : QPair< QString, int >( QString(), -1 ) + { + } + CommandLine( const QString& s ) : QPair< QString, int >( s, 10 ) { @@ -52,6 +58,11 @@ struct CommandLine : public QPair< QString, int > { return second; } + + bool isValid() const + { + return !first.isEmpty(); + } } ; /** @brief Abbreviation, used internally. */ @@ -82,6 +93,7 @@ public: using CommandList_t::cbegin; using CommandList_t::cend; using CommandList_t::const_iterator; + using CommandList_t::at; protected: using CommandList_t::append; diff --git a/src/modules/shellprocess/Tests.cpp b/src/modules/shellprocess/Tests.cpp index a1198a190..9792f4e7c 100644 --- a/src/modules/shellprocess/Tests.cpp +++ b/src/modules/shellprocess/Tests.cpp @@ -102,6 +102,8 @@ script: "ls /tmp" CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); QVERIFY( !cl.isEmpty() ); QCOMPARE( cl.count(), 1 ); + QCOMPARE( cl.at(0).timeout(), 10 ); + QCOMPARE( cl.at(0).command(), QStringLiteral( "ls /tmp" ) ); // Not a string doc = YAML::Load( R"(--- @@ -125,6 +127,8 @@ script: CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); QVERIFY( !cl.isEmpty() ); QCOMPARE( cl.count(), 1 ); + QCOMPARE( cl.at(0).timeout(), 20 ); + QCOMPARE( cl.at(0).command(), QStringLiteral( "ls /tmp" ) ); } void ShellProcessTests::testProcessListFromObject() @@ -132,11 +136,14 @@ void ShellProcessTests::testProcessListFromObject() YAML::Node doc = YAML::Load( R"(--- script: - command: "ls /tmp" - timeout: 20 + timeout: 12 - "-/bin/false" )" ); CommandList cl( CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); QVERIFY( !cl.isEmpty() ); QCOMPARE( cl.count(), 2 ); + QCOMPARE( cl.at(0).timeout(), 12 ); + QCOMPARE( cl.at(0).command(), QStringLiteral( "ls /tmp" ) ); + QCOMPARE( cl.at(1).timeout(), 10 ); // default }