Using pipes instead of QProcess for pymod tests, enable pipes on Windows (needs testing)

This commit is contained in:
Matthias Koefferlein 2018-07-04 20:26:34 +02:00
parent 642fb4270b
commit ad7a9836b0
3 changed files with 103 additions and 79 deletions

View File

@ -31,38 +31,30 @@
#define _STRINGIFY(s) #s #define _STRINGIFY(s) #s
#include "tlUnitTest.h" #include "tlUnitTest.h"
#include "tlStream.h"
#if defined(HAVE_QT) int run_pymodtest (tl::TestBase *_this, const std::string &fn)
# include <QProcess>
# include <QProcessEnvironment>
#endif
int run_pymodtest (tl::TestBase * /*_this*/, const std::string &fn)
{ {
#if defined(HAVE_QT) static std::string pypath;
QProcess process; pypath = "PYTHONPATH=";
process.setProcessChannelMode (QProcess::MergedChannels); pypath += STRINGIFY (PYTHONPATH);
putenv ((char *) pypath.c_str ());
QStringList args;
std::string fp (tl::testsrc ()); std::string fp (tl::testsrc ());
fp += "/testdata/pymod/"; fp += "/testdata/pymod/";
fp += fn; fp += fn;
args << tl::to_qstring (fp);
QProcessEnvironment env = QProcessEnvironment::systemEnvironment (); std::string text;
env.insert("PYTHONPATH", STRINGIFY (PYTHONPATH)); {
process.setProcessEnvironment(env); tl::InputPipe pipe (std::string (STRINGIFY (PYTHON)) + " " + fp + " 2>&1");
tl::InputStream is (pipe);
text = is.read_all ();
}
process.start (tl::to_qstring (STRINGIFY (PYTHON)), args); tl::info << text;
process.waitForFinished (-1); EXPECT_EQ (text.find ("OK") != std::string::npos, true);
tl::info << process.readAll ().constData (); return 0;
return process.exitCode ();
#else
return 1; // TODO: provide a Qt-less version
#endif
} }
#define PYMODTEST(n, file) \ #define PYMODTEST(n, file) \

View File

@ -861,7 +861,91 @@ OutputZLibFile::write (const char *b, size_t n)
} }
} }
#ifndef _WIN32 // not available on Windows #if defined(_WIN32)
// ---------------------------------------------------------------
// InputPipe delegate implementation
InputPipe::InputPipe (const std::string &path)
: m_file (NULL)
{
std::wstring wpath = tl::to_wstring (path);
m_source = path;
m_file = _wpopen (wpath.c_str (), "r");
if (m_file == NULL) {
throw FilePOpenErrorException (m_source, errno);
}
}
InputPipe::~InputPipe ()
{
close ();
}
void
InputPipe::close ()
{
if (m_file != NULL) {
fclose (m_file);
m_file = NULL;
}
}
size_t
InputPipe::read (char *b, size_t n)
{
tl_assert (m_file != NULL);
size_t ret = fread (b, 1, n, m_file);
if (ret < n) {
if (ferror (m_file)) {
throw FilePReadErrorException (m_source, ferror (m_file));
}
}
return ret;
}
void
InputPipe::reset ()
{
throw tl::Exception (tl::to_string (tr ("'reset' is not supported on pipeline input files")));
}
// ---------------------------------------------------------------
// OutputPipe delegate implementation
OutputPipe::OutputPipe (const std::string &path)
: m_file (NULL)
{
std::wstring wpath = tl::to_wstring (path);
m_source = path;
m_file = _wpopen (wpath.c_str (), "w");
if (m_file == NULL) {
throw FilePOpenErrorException (m_source, errno);
}
}
OutputPipe::~OutputPipe ()
{
if (m_file != NULL) {
fclose (m_file);
m_file = NULL;
}
}
void
OutputPipe::write (const char *b, size_t n)
{
tl_assert (m_file != NULL);
size_t ret = fwrite (b, 1, n, m_file);
if (ret < n) {
if (ferror (m_file)) {
throw FilePWriteErrorException (m_source, ferror (m_file));
}
}
}
#else
// --------------------------------------------------------------- // ---------------------------------------------------------------
// InputPipe delegate implementation // InputPipe delegate implementation
@ -943,58 +1027,6 @@ OutputPipe::write (const char *b, size_t n)
} }
} }
#else
// ---------------------------------------------------------------
// InputPipe delegate implementation
InputPipe::InputPipe (const std::string & /*path*/)
: m_file (NULL)
{
// TODO: emulate?
}
InputPipe::~InputPipe ()
{
}
size_t
InputPipe::read (char * /*b*/, size_t /*n*/)
{
throw tl::Exception (tl::to_string (tr ("pipeline input files not available on Windows")));
}
void
InputPipe::reset ()
{
throw tl::Exception (tl::to_string (tr ("pipeline input files not available on Windows")));
}
void
InputPipe::close ()
{
throw tl::Exception (tl::to_string (tr ("pipeline input files not available on Windows")));
}
// ---------------------------------------------------------------
// OutputPipe delegate implementation
OutputPipe::OutputPipe (const std::string & /*path*/)
: m_file (NULL)
{
// TODO: emulate?
}
OutputPipe::~OutputPipe ()
{
}
void
OutputPipe::write (const char * /*b*/, size_t /*n*/)
{
throw tl::Exception (tl::to_string (tr ("pipeline input files not available on Windows")));
}
#endif #endif
// --------------------------------------------------------------- // ---------------------------------------------------------------

View File

@ -171,7 +171,7 @@ private:
* *
* Implements the reader for a zlib stream * Implements the reader for a zlib stream
*/ */
class InputZLibFile class TL_PUBLIC InputZLibFile
: public InputStreamBase : public InputStreamBase
{ {
public: public:
@ -224,7 +224,7 @@ private:
* *
* Implements the reader for ordinary files. * Implements the reader for ordinary files.
*/ */
class InputFile class TL_PUBLIC InputFile
: public InputStreamBase : public InputStreamBase
{ {
public: public:
@ -272,7 +272,7 @@ private:
* *
* Implements the reader for pipe streams * Implements the reader for pipe streams
*/ */
class InputPipe class TL_PUBLIC InputPipe
: public InputStreamBase : public InputStreamBase
{ {
public: public: