[shellprocess] Improve CommandList

- Also allow a single string instead of a list
 - Add count() method to CommandList
 - Drop over-engineering, add more logging
 - Expand tests with some more examples
This commit is contained in:
Adriaan de Groot 2018-01-10 11:01:39 -05:00
parent 5f8fb655c4
commit b7fb24837a
4 changed files with 83 additions and 36 deletions

View File

@ -18,53 +18,50 @@
#include "CommandList.h" #include "CommandList.h"
#include "utils/Logger.h"
#include <QVariantList> #include <QVariantList>
class CommandList::Private static QStringList get_variant_stringlist(const QVariantList& l)
{
public:
Private( const QVariantList& l );
~Private();
} ;
CommandList::Private::Private(const QVariantList& l)
{ {
QStringList retl;
unsigned int c = 0;
for ( const auto& v : l )
{
if ( v.type() == QVariant::String )
retl.append( v.toString() );
else
cDebug() << "WARNING Bad CommandList element" << c << v.type() << v;
++c;
}
return retl;
} }
CommandList::Private::~Private()
{
}
CommandList::CommandList() CommandList::CommandList()
: m_d( nullptr )
{ {
} }
CommandList::CommandList::CommandList(const QVariant& v) CommandList::CommandList::CommandList(const QVariant& v)
: CommandList() : CommandList()
{ {
if ( ( v.type() == QVariant::List ) ) if ( v.type() == QVariant::List )
{ {
const auto v_list = v.toList(); const auto v_list = v.toList();
if ( v_list.count() ) if ( v_list.count() )
{ {
m_d = new Private( v_list ); append( get_variant_stringlist( v_list ) );
} }
else
cDebug() << "WARNING: Empty CommandList";
} }
else if ( v.type() == QVariant::String )
{
append( v.toString() );
}
else
cDebug() << "WARNING: CommandList does not understand variant" << v.type();
} }
CommandList::~CommandList() CommandList::~CommandList()
{ {
delete m_d;
m_d = nullptr; // TODO: UniquePtr
}
bool CommandList::isEmpty() const
{
if ( !m_d )
return true;
return false; // FIXME: actually count things
} }

View File

@ -19,21 +19,18 @@
#ifndef COMMANDLIST_H #ifndef COMMANDLIST_H
#define COMMANDLIST_H #define COMMANDLIST_H
#include <QStringList>
#include <QVariant> #include <QVariant>
class CommandList class CommandList : protected QStringList
{ {
class Private;
public: public:
CommandList(); CommandList();
CommandList(const QVariant& v); CommandList(const QVariant& v);
~CommandList(); ~CommandList();
bool isEmpty() const; using QStringList::isEmpty;
using QStringList::count;
private:
Private *m_d;
} ; } ;
#endif // COMMANDLIST_H #endif // COMMANDLIST_H

View File

@ -44,7 +44,7 @@ ShellProcessTests::initTestCase()
} }
void void
ShellProcessTests::testProcessList() ShellProcessTests::testProcessListSampleConfig()
{ {
YAML::Node doc; YAML::Node doc;
@ -62,4 +62,52 @@ ShellProcessTests::testProcessList()
CommandList cl( CommandList cl(
CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
QVERIFY( !cl.isEmpty() ); QVERIFY( !cl.isEmpty() );
QCOMPARE( cl.count(), 2 );
}
void ShellProcessTests::testProcessListFromList()
{
YAML::Node doc = YAML::Load( R"(---
script:
- "ls /tmp"
- "ls /nonexistent"
- "/bin/false"
)" );
CommandList cl(
CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
QVERIFY( !cl.isEmpty() );
QCOMPARE( cl.count(), 3 );
// Contains 1 bad element
doc = YAML::Load( R"(---
script:
- "ls /tmp"
- false
- "ls /nonexistent"
)" );
CommandList cl1(
CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
QVERIFY( !cl1.isEmpty() );
QCOMPARE( cl1.count(), 2 ); // One element ignored
}
void ShellProcessTests::testProcessListFromString()
{
YAML::Node doc = YAML::Load( R"(---
script: "ls /tmp"
)" );
CommandList cl(
CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
QVERIFY( !cl.isEmpty() );
QCOMPARE( cl.count(), 1 );
// Not a string
doc = YAML::Load( R"(---
script: false
)" );
CommandList cl1(
CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
QVERIFY( cl1.isEmpty() );
QCOMPARE( cl1.count(), 0 );
} }

View File

@ -30,7 +30,12 @@ public:
private Q_SLOTS: private Q_SLOTS:
void initTestCase(); void initTestCase();
void testProcessList(); // Check the sample config file is processed correctly
void testProcessListSampleConfig();
// Create from a YAML list
void testProcessListFromList();
// Create from a simple YAML string
void testProcessListFromString();
}; };
#endif #endif