Skip to content

Commit ec9dbb3

Browse files
authored
testrunner: make sure all redirected output is being consumed / some cleanups (#5714)
1 parent 55c2b75 commit ec9dbb3

File tree

6 files changed

+36
-83
lines changed

6 files changed

+36
-83
lines changed

test/fixture.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@
3434

3535
#include "xml.h"
3636

37-
std::ostringstream errout;
38-
std::ostringstream output;
39-
4037
/**
4138
* TestRegistry
4239
**/

test/fixture.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,16 @@ class TestFixture : public ErrorLogger {
236236
return SettingsBuilder(*this, std::move(settings));
237237
}
238238

239-
public:
239+
// TODO: make sure the output has been consumed in the test
240+
std::ostringstream errout;
241+
std::ostringstream output;
242+
243+
private:
240244
void reportOut(const std::string &outmsg, Color c = Color::Reset) override;
241245
void reportErr(const ErrorMessage &msg) override;
242246
void run(const std::string &str);
247+
248+
public:
243249
static void printHelp();
244250
const std::string classname;
245251

@@ -248,12 +254,6 @@ class TestFixture : public ErrorLogger {
248254
static std::size_t runTests(const options& args);
249255
};
250256

251-
// TODO: fix these
252-
// NOLINTNEXTLINE(readability-redundant-declaration)
253-
extern std::ostringstream errout;
254-
// NOLINTNEXTLINE(readability-redundant-declaration)
255-
extern std::ostringstream output;
256-
257257
// TODO: most asserts do not actually assert i.e. do not return
258258
#define TEST_CASE( NAME ) do { if (prepareTest(#NAME)) { setVerbose(false); NAME(); teardownTest(); } } while (false)
259259
#define ASSERT( CONDITION ) if (!assert_(__FILE__, __LINE__, (CONDITION))) return

test/redirect.h

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@
1919

2020
#include <iostream>
2121
#include <sstream>
22+
#include <stdexcept>
2223
#include <string>
2324

24-
// NOLINTNEXTLINE(readability-redundant-declaration) - TODO: fix this
25-
extern std::ostringstream errout;
26-
// NOLINTNEXTLINE(readability-redundant-declaration) - TODO: fix this
27-
extern std::ostringstream output;
2825
/**
2926
* @brief Utility class for capturing cout and cerr to ostringstream buffers
3027
* for later use. Uses RAII to stop redirection when the object goes out of
@@ -47,34 +44,35 @@ class RedirectOutputError {
4744
}
4845

4946
/** Revert cout and cerr behaviour */
50-
~RedirectOutputError() {
47+
~RedirectOutputError() noexcept(false) {
5148
std::cout.rdbuf(_oldCout); // restore cout's original streambuf
5249
std::cerr.rdbuf(_oldCerr); // restore cerrs's original streambuf
5350

54-
errout << _err.str();
55-
output << _out.str();
51+
{
52+
const std::string s = _out.str();
53+
if (!s.empty())
54+
throw std::runtime_error("unconsumed stdout: " + s); // cppcheck-suppress exceptThrowInDestructor - FP #11031
55+
}
56+
57+
{
58+
const std::string s = _err.str();
59+
if (!s.empty())
60+
throw std::runtime_error("consumed stderr: " + s);
61+
}
5662
}
5763

58-
/** Return what would be printed to cout. See also clearOutput() */
59-
std::string getOutput() const {
60-
return _out.str();
61-
}
62-
63-
/** Normally called after getOutput() to prevent same text to be returned
64-
twice. */
65-
void clearOutput() {
64+
/** Return what would be printed to cout. */
65+
std::string getOutput() {
66+
std::string s = _out.str();
6667
_out.str("");
68+
return s;
6769
}
6870

69-
/** Return what would be printed to cerr. See also clearErrout() */
70-
std::string getErrout() const {
71-
return _err.str();
72-
}
73-
74-
/** Normally called after getErrout() to prevent same text to be returned
75-
twice. */
76-
void clearErrout() {
71+
/** Return what would be printed to cerr. */
72+
std::string getErrout() {
73+
std::string s = _err.str();
7774
_err.str("");
75+
return s;
7876
}
7977

8078
private:
@@ -112,9 +110,7 @@ class SuppressOutput {
112110

113111
#define REDIRECT RedirectOutputError redir
114112
#define GET_REDIRECT_OUTPUT redir.getOutput()
115-
#define CLEAR_REDIRECT_OUTPUT redir.clearOutput()
116113
#define GET_REDIRECT_ERROUT redir.getErrout()
117-
#define CLEAR_REDIRECT_ERROUT redir.clearErrout()
118114

119115
#define SUPPRESS SuppressOutput supprout
120116

0 commit comments

Comments
 (0)