[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:
parent
5f8fb655c4
commit
b7fb24837a
@ -18,53 +18,50 @@
|
||||
|
||||
#include "CommandList.h"
|
||||
|
||||
#include "utils/Logger.h"
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
CommandList::Private::~Private()
|
||||
{
|
||||
return retl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
CommandList::CommandList()
|
||||
: m_d( nullptr )
|
||||
{
|
||||
}
|
||||
|
||||
CommandList::CommandList::CommandList(const QVariant& v)
|
||||
: CommandList()
|
||||
{
|
||||
if ( ( v.type() == QVariant::List ) )
|
||||
if ( v.type() == QVariant::List )
|
||||
{
|
||||
const auto v_list = v.toList();
|
||||
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()
|
||||
{
|
||||
delete m_d;
|
||||
m_d = nullptr; // TODO: UniquePtr
|
||||
}
|
||||
|
||||
bool CommandList::isEmpty() const
|
||||
{
|
||||
if ( !m_d )
|
||||
return true;
|
||||
|
||||
return false; // FIXME: actually count things
|
||||
}
|
||||
|
@ -19,21 +19,18 @@
|
||||
#ifndef COMMANDLIST_H
|
||||
#define COMMANDLIST_H
|
||||
|
||||
#include <QStringList>
|
||||
#include <QVariant>
|
||||
|
||||
class CommandList
|
||||
class CommandList : protected QStringList
|
||||
{
|
||||
class Private;
|
||||
|
||||
public:
|
||||
CommandList();
|
||||
CommandList(const QVariant& v);
|
||||
~CommandList();
|
||||
|
||||
bool isEmpty() const;
|
||||
|
||||
private:
|
||||
Private *m_d;
|
||||
using QStringList::isEmpty;
|
||||
using QStringList::count;
|
||||
} ;
|
||||
|
||||
#endif // COMMANDLIST_H
|
||||
|
@ -44,7 +44,7 @@ ShellProcessTests::initTestCase()
|
||||
}
|
||||
|
||||
void
|
||||
ShellProcessTests::testProcessList()
|
||||
ShellProcessTests::testProcessListSampleConfig()
|
||||
{
|
||||
YAML::Node doc;
|
||||
|
||||
@ -62,4 +62,52 @@ ShellProcessTests::testProcessList()
|
||||
CommandList cl(
|
||||
CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
|
||||
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 );
|
||||
|
||||
}
|
||||
|
@ -30,7 +30,12 @@ public:
|
||||
|
||||
private Q_SLOTS:
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user