Added a way to get the exit code from InputPipe

The method is to call wait() on the pipe object
which returns the exit code.

Some tests have been added for this.
This commit is contained in:
Matthias Koefferlein 2018-10-09 23:40:28 +02:00
parent 7df9242efb
commit 904a12c11e
4 changed files with 58 additions and 2 deletions

View File

@ -966,11 +966,17 @@ InputPipe::~InputPipe ()
void
InputPipe::close ()
{
wait ();
}
int InputPipe::wait ()
{
int ret = 0;
if (m_file != NULL) {
pclose (m_file);
// TODO: pclose delivers the exit code - we should indicate it as return value of close.
ret = pclose (m_file);
m_file = NULL;
}
return ret;
}
size_t

View File

@ -326,6 +326,7 @@ public:
/**
* @brief Closes the pipe
* This method will wait for the child process to terminate.
*/
virtual void close ();
@ -352,6 +353,11 @@ public:
return std::string ();
}
/**
* @brief Closes the pipe and returns the exit code of the child process
*/
int wait ();
private:
// No copying
InputPipe (const InputPipe &);

View File

@ -0,0 +1,44 @@
/*
KLayout Layout Viewer
Copyright (C) 2006-2018 Matthias Koefferlein
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "tlStream.h"
#include "tlUnitTest.h"
TEST(InputPipe1)
{
tl::InputPipe pipe ("echo HELLOWORLD");
tl::InputStream str (pipe);
tl::TextInputStream tstr (str);
EXPECT_EQ (tstr.get_line (), "HELLOWORLD");
EXPECT_EQ (pipe.wait (), 0);
}
TEST(InputPipe2)
{
tl::InputPipe pipe ("thiscommanddoesnotexistithink 2>&1");
tl::InputStream str (pipe);
tl::TextInputStream tstr (str);
tstr.get_line ();
int ret = pipe.wait ();
tl::info << "Process exit code: " << ret;
EXPECT_NE (ret, 0);
}