Skip to content

Commit

Permalink
Port existing MythSystem tests to the new interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-kristjansson committed Jun 2, 2013
1 parent 3afaff0 commit 2fc542c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 34 deletions.
65 changes: 49 additions & 16 deletions mythtv/libs/libmythbase/mythsystem.cpp
Expand Up @@ -3,6 +3,8 @@

// Qt headers
#include <QStringList>
#include <QByteArray>
#include <QIODevice>
#include <QRegExp>

// MythTV headers
Expand All @@ -14,7 +16,6 @@
#include <iostream>
using namespace std;


class MythSystemLegacyWrapper : public MythSystem
{
public:
Expand Down Expand Up @@ -49,25 +50,25 @@ class MythSystemLegacyWrapper : public MythSystem
Wait(0);
}

virtual uint GetFlags(void) const MOVERRIDE
uint GetFlags(void) const MOVERRIDE
{
return m_flags;
}

/// Returns the starting path of the program
virtual QString GetStartingPath(void) const MOVERRIDE
QString GetStartingPath(void) const MOVERRIDE
{
return m_legacy->GetDirectory();
}

/// Return the CPU Priority of the program
virtual Priority GetCPUPriority(void) const MOVERRIDE
Priority GetCPUPriority(void) const MOVERRIDE
{
return kNormalPriority;
}

/// Return the Disk Priority of the program
virtual Priority GetDiskPriority(void) const MOVERRIDE
Priority GetDiskPriority(void) const MOVERRIDE
{
return kNormalPriority;
}
Expand All @@ -78,7 +79,7 @@ class MythSystemLegacyWrapper : public MythSystem
/// think it is running even though it is not.
/// WARNING The legacy timeout is in seconds not milliseconds,
/// timeout will be rounded.
virtual bool Wait(uint timeout_ms) MOVERRIDE
bool Wait(uint timeout_ms) MOVERRIDE
{
timeout_ms = (timeout_ms >= 1000) ? timeout_ms + 500 :
((timeout_ms == 0) ? 0 : 1000);
Expand All @@ -90,27 +91,59 @@ class MythSystemLegacyWrapper : public MythSystem

/// Returns the standard input stream for the program
/// if the kMSStdIn flag was passed to the constructor.
virtual QIODevice *GetStandardInputStream(void) MOVERRIDE
/// Note: This is not safe!
QIODevice *GetStandardInputStream(void) MOVERRIDE
{
return NULL;
if (!(kMSStdIn & m_flags))
return NULL;

if (!m_legacy->GetBuffer(0)->isOpen() &&
!m_legacy->GetBuffer(0)->open(QIODevice::WriteOnly))
{
return NULL;
}

return m_legacy->GetBuffer(0);
}

/// Returns the standard output stream for the program
/// if the kMSStdOut flag was passed to the constructor.
virtual QIODevice *GetStandardOutputStream(void) MOVERRIDE
QIODevice *GetStandardOutputStream(void) MOVERRIDE
{
return NULL;
if (!(kMSStdOut & m_flags))
return NULL;

Wait(0); // legacy getbuffer is not thread-safe, so wait

if (!m_legacy->GetBuffer(1)->isOpen() &&
!m_legacy->GetBuffer(1)->open(QIODevice::ReadOnly))
{
return NULL;
}

return m_legacy->GetBuffer(1);
}

/// Returns the standard error stream for the program
/// if the kMSStdErr flag was passed to the constructor.
virtual QIODevice *GetStandardErrorStream(void) MOVERRIDE
QIODevice *GetStandardErrorStream(void) MOVERRIDE
{
return NULL;
if (!(kMSStdErr & m_flags))
return NULL;

Wait(0); // legacy getbuffer is not thread-safe, so wait

if (!m_legacy->GetBuffer(2)->isOpen() &&
!m_legacy->GetBuffer(2)->open(QIODevice::ReadOnly))
{
return NULL;
}

return m_legacy->GetBuffer(2);
}

/// Sends the selected signal to the program
virtual void Signal(MythSignal sig) MOVERRIDE
void Signal(MythSignal sig) MOVERRIDE
{
m_legacy->Signal(sig);
}
Expand All @@ -121,7 +154,7 @@ class MythSystemLegacyWrapper : public MythSystem
* Returns -2 if the program has not yet been collected.
* Returns an exit code 0..255 if the program exited with exit code.
*/
virtual int GetExitCode(void) const MOVERRIDE
int GetExitCode(void) const MOVERRIDE
{
// FIXME doesn't actually know why program exited.
// if program returns 142 then we will forever
Expand Down Expand Up @@ -149,7 +182,7 @@ class MythSystemLegacyWrapper : public MythSystem
* limited interface is just telling us if GetExitCode()
* will return -2 or -1, or 0..255, so it is redundant.
*/
virtual MythSignal GetSignal(void) const MOVERRIDE
MythSignal GetSignal(void) const MOVERRIDE
{
return kSignalNone;
/*
Expand Down Expand Up @@ -185,7 +218,7 @@ class MythSystemLegacyWrapper : public MythSystem
}

private:
MythSystemLegacy *m_legacy;
QScopedPointer<MythSystemLegacy> m_legacy;
uint m_flags;
};

Expand Down
3 changes: 3 additions & 0 deletions mythtv/libs/libmythbase/mythsystem.h
Expand Up @@ -115,14 +115,17 @@ class MBASE_PUBLIC MythSystem

/// Returns the standard input stream for the program
/// if the kMSStdIn flag was passed to the constructor.
/// Note: The stream this returns is already open.
virtual QIODevice *GetStandardInputStream(void) = 0;

/// Returns the standard output stream for the program
/// if the kMSStdOut flag was passed to the constructor.
/// Note: The stream this returns is already open.
virtual QIODevice *GetStandardOutputStream(void) = 0;

/// Returns the standard error stream for the program
/// if the kMSStdErr flag was passed to the constructor.
/// Note: The stream this returns is already open.
virtual QIODevice *GetStandardErrorStream(void) = 0;

/// Sends the selected signal to the program
Expand Down
35 changes: 17 additions & 18 deletions mythtv/libs/libmythbase/test/test_mythsystem/test_mythsystem.h
Expand Up @@ -85,16 +85,15 @@ class TestMythSystem: public QObject
m_before = QDateTime();
}

#if 0
void constructed_command_is_run(void)
{
QScopedPointer<MythSystem> cmd(
MythSystem::Create(
QString("echo %1").arg(__FUNCTION__), kMSStdOut));
cmd->Wait();
QVERIFY(QString(cmd->ReadAll()).contains(__FUNCTION__));
QVERIFY(QString(cmd->GetStandardOutputStream()->readAll())
.contains(__FUNCTION__));
}
#endif

void wait_returns_true_on_exit(void)
{
Expand Down Expand Up @@ -133,7 +132,6 @@ class TestMythSystem: public QObject
// TODO kMSProcessEvents -- process events while waiting
// TODO kMSInUi -- the parent is in the UI

#if 0
// kMSStdIn -- allow access to stdin
void stdin_works(void)
{
Expand All @@ -144,15 +142,14 @@ class TestMythSystem: public QObject
QScopedPointer<MythSystem> cmd(
MythSystem::Create(QString("cat - > %1").arg(tempfile.fileName()),
kMSStdIn));
cmd->Write(in);
cmd->GetStandardInputStream()->write(in);
cmd->GetStandardInputStream()->close();
cmd->Wait();
QVERIFY(cmd->GetExitCode() == 0);
QByteArray out = tempfile.readAll();
QVERIFY(QString(out).contains(QString(in)));
}
#endif

#if 0
// kMSStdOut -- allow access to stdout
void stdout_works(void)
{
Expand All @@ -161,11 +158,12 @@ class TestMythSystem: public QObject
kMSStdOut));
cmd->Wait();
QVERIFY(cmd->GetExitCode() == 0);
QVERIFY(QString(cmd->ReadAll()).contains(__FUNCTION__));
QVERIFY(cmd->GetStandardOutputStream());
QVERIFY(cmd->GetStandardOutputStream()->isOpen());
QByteArray ba = cmd->GetStandardOutputStream()->readAll();
QVERIFY(QString(ba).contains(__FUNCTION__));
}
#endif

#if 0
// kMSStdErr -- allow access to stderr
void stderr_works(void)
{
Expand All @@ -174,33 +172,34 @@ class TestMythSystem: public QObject
kMSRunShell | kMSStdErr));
cmd->Wait();
QVERIFY(cmd->GetExitCode() == 0);
QVERIFY(QString(cmd->ReadAllErr()).contains(__FUNCTION__));
QVERIFY(cmd->GetStandardErrorStream());
QVERIFY(cmd->GetStandardErrorStream()->isOpen());
QByteArray ba = cmd->GetStandardErrorStream()->readAll();
QVERIFY(QString(ba).contains(__FUNCTION__));
}
#endif

// TODO kMSBuffered -- buffer the IO channels

#if 0
// kMSRunShell -- run process through shell
void shell_used_when_requested(void)
{
QScopedPointer<MythSystem> cmd(
MythSystem::Create("if [ x != y ] ; then echo X ; else echo Y ; fi",
kMSRunShell | kMSStdOut));
cmd->Wait();
QVERIFY(QString(cmd->ReadAll()).contains("X"));
QVERIFY(QString(cmd->GetStandardOutputStream()->readAll())
.contains("X"));
}
#endif

#if 0
void shell_not_used_when_not_requested(void)
{
QScopedPointer<MythSystem> cmd(
MythSystem::Create("if [ x != y ] ; then echo X ; else echo Y ; fi",
kMSStdOut));
cmd->Wait();
QVERIFY(!QString(cmd->ReadAll()).contains("X"));
QVERIFY(!QString(cmd->GetStandardOutputStream()->readAll())
.contains("X"));
}
#endif

// no need to test kMSNoRunShell, it is a no-op
// TODO delete flag
Expand Down

0 comments on commit 2fc542c

Please sign in to comment.