[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 "CommandList.h"
|
||||||
|
|
||||||
|
#include "utils/Logger.h"
|
||||||
|
|
||||||
#include <QVariantList>
|
#include <QVariantList>
|
||||||
|
|
||||||
class CommandList::Private
|
static QStringList get_variant_stringlist(const QVariantList& l)
|
||||||
{
|
{
|
||||||
public:
|
QStringList retl;
|
||||||
Private( const QVariantList& l );
|
unsigned int c = 0;
|
||||||
~Private();
|
for ( const auto& v : l )
|
||||||
} ;
|
|
||||||
|
|
||||||
CommandList::Private::Private(const QVariantList& 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
|
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
@ -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 );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user