[libcalamares] Implement object-style command line

- handle command: and timeout: entries
 - test for setting the values
main
Adriaan de Groot 7 years ago
parent 72bac332be
commit c641f5dec6

@ -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();
}

@ -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;

@ -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
}

Loading…
Cancel
Save