From 904a12c11e8c39ae044b81f54d0bb3b4fb9059b9 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Tue, 9 Oct 2018 23:40:28 +0200 Subject: [PATCH] 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. --- src/tl/tl/tlStream.cc | 10 +++++-- src/tl/tl/tlStream.h | 6 ++++ src/tl/unit_tests/tlStreamTests.cc | 44 ++++++++++++++++++++++++++++++ src/tl/unit_tests/tlVector.cc | 0 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/tl/unit_tests/tlStreamTests.cc delete mode 100644 src/tl/unit_tests/tlVector.cc diff --git a/src/tl/tl/tlStream.cc b/src/tl/tl/tlStream.cc index 11d7807b8..60758c7ed 100644 --- a/src/tl/tl/tlStream.cc +++ b/src/tl/tl/tlStream.cc @@ -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 diff --git a/src/tl/tl/tlStream.h b/src/tl/tl/tlStream.h index a81924c7a..db1db4012 100644 --- a/src/tl/tl/tlStream.h +++ b/src/tl/tl/tlStream.h @@ -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 &); diff --git a/src/tl/unit_tests/tlStreamTests.cc b/src/tl/unit_tests/tlStreamTests.cc new file mode 100644 index 000000000..4a96013d1 --- /dev/null +++ b/src/tl/unit_tests/tlStreamTests.cc @@ -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); +} diff --git a/src/tl/unit_tests/tlVector.cc b/src/tl/unit_tests/tlVector.cc deleted file mode 100644 index e69de29bb..000000000