Skip to content

Commit 4538002

Browse files
authored
testrunner: make sure that more redirects/outputs are actually being consumed (cppcheck-opensource#5721)
1 parent 613bbe7 commit 4538002

8 files changed

Lines changed: 50 additions & 14 deletions

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ test/testfunctions.o: test/testfunctions.cpp lib/addoninfo.h lib/check.h lib/che
758758
test/testgarbage.o: test/testgarbage.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h
759759
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testgarbage.cpp
760760

761-
test/testimportproject.o: test/testimportproject.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h test/fixture.h
761+
test/testimportproject.o: test/testimportproject.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h test/fixture.h test/redirect.h
762762
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testimportproject.cpp
763763

764764
test/testincompletestatement.o: test/testincompletestatement.cpp lib/addoninfo.h lib/check.h lib/checkother.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h

test/fixture.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,20 @@ bool TestFixture::prepareTest(const char testname[])
114114
void TestFixture::teardownTest()
115115
{
116116
teardownTestInternal();
117+
118+
// TODO: enable
119+
/*
120+
{
121+
const std::string s = errout.str();
122+
if (!s.empty())
123+
throw std::runtime_error("unconsumed ErrorLogger err: " + s);
124+
}
125+
*/
126+
{
127+
const std::string s = output_str();
128+
if (!s.empty())
129+
throw std::runtime_error("unconsumed ErrorLogger out: " + s);
130+
}
117131
}
118132

119133
std::string TestFixture::getLocationStr(const char * const filename, const unsigned int linenr) const
@@ -381,14 +395,15 @@ std::size_t TestFixture::runTests(const options& args)
381395

382396
void TestFixture::reportOut(const std::string & outmsg, Color /*c*/)
383397
{
384-
output << outmsg << std::endl;
398+
mOutput << outmsg << std::endl;
385399
}
386400

387401
void TestFixture::reportErr(const ErrorMessage &msg)
388402
{
389403
if (msg.severity == Severity::none && msg.id == "logChecker")
390404
return;
391405
const std::string errormessage(msg.toString(mVerbose, mTemplateFormat, mTemplateLocation));
406+
// TODO: remove the unique error handling?
392407
if (errout.str().find(errormessage) == std::string::npos)
393408
errout << errormessage << std::endl;
394409
}

test/fixture.h

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

239-
// TODO: make sure the output has been consumed in the test
239+
std::string output_str() {
240+
std::string s = mOutput.str();
241+
mOutput.str("");
242+
return s;
243+
}
244+
240245
std::ostringstream errout;
241-
std::ostringstream output;
242246

243247
private:
248+
std::ostringstream mOutput;
249+
244250
void reportOut(const std::string &outmsg, Color c = Color::Reset) override;
245251
void reportErr(const ErrorMessage &msg) override;
246252
void run(const std::string &str);

test/testimportproject.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "settings.h"
2121
#include "filesettings.h"
2222
#include "fixture.h"
23+
#include "redirect.h"
2324

2425
#include <list>
2526
#include <map>
@@ -112,6 +113,7 @@ class TestImportProject : public TestFixture {
112113
}
113114

114115
void importCompileCommands1() const {
116+
REDIRECT;
115117
constexpr char json[] = R"([{
116118
"directory": "/tmp",
117119
"command": "gcc -DTEST1 -DTEST2=2 -o /tmp/src.o -c /tmp/src.c",
@@ -125,6 +127,7 @@ class TestImportProject : public TestFixture {
125127
}
126128

127129
void importCompileCommands2() const {
130+
REDIRECT;
128131
// Absolute file path
129132
#ifdef _WIN32
130133
const char json[] = R"([{
@@ -152,6 +155,7 @@ class TestImportProject : public TestFixture {
152155
}
153156

154157
void importCompileCommands3() const {
158+
REDIRECT;
155159
const char json[] = R"([{
156160
"directory": "/tmp/",
157161
"command": "gcc -c src.c",
@@ -165,6 +169,7 @@ class TestImportProject : public TestFixture {
165169
}
166170

167171
void importCompileCommands4() const {
172+
REDIRECT;
168173
constexpr char json[] = R"([{
169174
"directory": "/tmp/",
170175
"command": "gcc -c src.mm",
@@ -177,6 +182,7 @@ class TestImportProject : public TestFixture {
177182
}
178183

179184
void importCompileCommands5() const {
185+
REDIRECT;
180186
constexpr char json[] =
181187
R"([{
182188
"directory": "C:/Users/dan/git/build-test-cppcheck-Desktop_Qt_5_15_0_MSVC2019_64bit-Debug",
@@ -196,6 +202,7 @@ class TestImportProject : public TestFixture {
196202
}
197203

198204
void importCompileCommands6() const {
205+
REDIRECT;
199206
constexpr char json[] =
200207
R"([{
201208
"directory": "C:/Users/dan/git/build-test-cppcheck-Desktop_Qt_5_15_0_MSVC2019_64bit-Debug",
@@ -217,6 +224,7 @@ class TestImportProject : public TestFixture {
217224

218225

219226
void importCompileCommands7() const {
227+
REDIRECT;
220228
// cmake -DFILESDIR="/some/path" ..
221229
constexpr char json[] =
222230
R"([{
@@ -237,6 +245,7 @@ class TestImportProject : public TestFixture {
237245
}
238246

239247
void importCompileCommands8() const {
248+
REDIRECT;
240249
// cmake -DFILESDIR="C:\Program Files\Cppcheck" -G"NMake Makefiles" ..
241250
constexpr char json[] =
242251
R"([{
@@ -250,6 +259,7 @@ class TestImportProject : public TestFixture {
250259
}
251260

252261
void importCompileCommands9() const {
262+
REDIRECT;
253263
// IAR output (https://sourceforge.net/p/cppcheck/discussion/general/thread/608af51e0a/)
254264
constexpr char json[] =
255265
R"([{
@@ -266,6 +276,7 @@ class TestImportProject : public TestFixture {
266276
}
267277

268278
void importCompileCommands10() const { // #10887
279+
REDIRECT;
269280
constexpr char json[] =
270281
R"([{
271282
"file": "/home/danielm/cppcheck/1/test folder/1.c" ,
@@ -285,6 +296,7 @@ class TestImportProject : public TestFixture {
285296
}
286297

287298
void importCompileCommands11() const { // include path order
299+
REDIRECT;
288300
constexpr char json[] =
289301
R"([{
290302
"file": "1.c" ,
@@ -307,6 +319,7 @@ class TestImportProject : public TestFixture {
307319
}
308320

309321
void importCompileCommandsArgumentsSection() const {
322+
REDIRECT;
310323
constexpr char json[] = "[ { \"directory\": \"/tmp/\","
311324
"\"arguments\": [\"gcc\", \"-c\", \"src.c\"],"
312325
"\"file\": \"src.c\" } ]";
@@ -318,15 +331,18 @@ class TestImportProject : public TestFixture {
318331
}
319332

320333
void importCompileCommandsNoCommandSection() const {
334+
REDIRECT;
321335
constexpr char json[] = "[ { \"directory\": \"/tmp/\","
322336
"\"file\": \"src.mm\" } ]";
323337
std::istringstream istr(json);
324338
TestImporter importer;
325339
ASSERT_EQUALS(false, importer.importCompileCommands(istr));
326340
ASSERT_EQUALS(0, importer.fileSettings.size());
341+
ASSERT_EQUALS("cppcheck: error: no 'arguments' or 'command' field found in compilation database entry\n", GET_REDIRECT_OUTPUT);
327342
}
328343

329344
void importCppcheckGuiProject() const {
345+
REDIRECT;
330346
constexpr char xml[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
331347
"<project version=\"1\">\n"
332348
" <root name=\".\"/>\n"

test/testprocessexecutor.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ class TestProcessExecutorBase : public TestFixture {
6767
*/
6868
void check(unsigned int jobs, int files, int result, const std::string &data, const CheckOptions& opt = make_default_obj{}) {
6969
errout.str("");
70-
output.str("");
7170

7271
std::list<FileSettings> fileSettings;
7372

@@ -265,7 +264,7 @@ class TestProcessExecutorBase : public TestFixture {
265264
$.executeCommandCalled = true,
266265
$.exe = exe,
267266
$.args = {"-quiet", "-checks=*,-clang-analyzer-*,-llvm*", file, "--"}*/));
268-
ASSERT_EQUALS("Checking " + file + " ...\n", output.str());
267+
ASSERT_EQUALS("Checking " + file + " ...\n", output_str());
269268
}
270269

271270
// TODO: provide data which actually shows values above 0

test/testsingleexecutor.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ class TestSingleExecutorBase : public TestFixture {
7272

7373
void check(int files, int result, const std::string &data, const CheckOptions& opt = make_default_obj{}) {
7474
errout.str("");
75-
output.str("");
7675

7776
std::list<FileSettings> fileSettings;
7877

@@ -169,7 +168,7 @@ class TestSingleExecutorBase : public TestFixture {
169168
expected += "Checking " + fprefix() + "_" + zpad3(i) + ".cpp ...\n";
170169
expected += std::to_string(i) + "/100 files checked " + std::to_string(i) + "% done\n";
171170
}
172-
ASSERT_EQUALS(expected, output.str());
171+
ASSERT_EQUALS(expected, output_str());
173172
}
174173

175174
void many_files_showtime() {
@@ -259,7 +258,7 @@ class TestSingleExecutorBase : public TestFixture {
259258
$.executeCommandCalled = true,
260259
$.exe = exe,
261260
$.args = {"-quiet", "-checks=*,-clang-analyzer-*,-llvm*", file, "--"}));
262-
ASSERT_EQUALS("Checking " + file + " ...\n", output.str());
261+
ASSERT_EQUALS("Checking " + file + " ...\n", output_str());
263262
}
264263

265264
// TODO: provide data which actually shows values above 0

test/testsuppressions.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ class TestSuppressions : public TestFixture {
200200
unsigned int checkSuppression(std::map<std::string, std::string> &f, const std::string &suppression = emptyString) {
201201
// Clear the error log
202202
errout.str("");
203-
output.str("");
204203

205204
std::list<std::pair<std::string, std::size_t>> files;
206205
for (std::map<std::string, std::string>::const_iterator i = f.cbegin(); i != f.cend(); ++i) {
@@ -210,6 +209,7 @@ class TestSuppressions : public TestFixture {
210209
CppCheck cppCheck(*this, true, nullptr);
211210
Settings& settings = cppCheck.settings();
212211
settings.jobs = 1;
212+
settings.quiet = true;
213213
settings.inlineSuppressions = true;
214214
settings.severity.enable(Severity::information);
215215
if (suppression == "unusedFunction")
@@ -234,13 +234,13 @@ class TestSuppressions : public TestFixture {
234234

235235
unsigned int checkSuppressionThreads(const char code[], const std::string &suppression = emptyString) {
236236
errout.str("");
237-
output.str("");
238237

239238
std::list<std::pair<std::string, std::size_t>> files;
240239
files.emplace_back("test.cpp", strlen(code));
241240

242241
Settings settings;
243242
settings.jobs = 2;
243+
settings.quiet = true;
244244
settings.inlineSuppressions = true;
245245
settings.severity.enable(Severity::information);
246246
if (!suppression.empty()) {
@@ -264,13 +264,13 @@ class TestSuppressions : public TestFixture {
264264
#if !defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
265265
unsigned int checkSuppressionProcesses(const char code[], const std::string &suppression = emptyString) {
266266
errout.str("");
267-
output.str("");
268267

269268
std::list<std::pair<std::string, std::size_t>> files;
270269
files.emplace_back("test.cpp", strlen(code));
271270

272271
Settings settings;
273272
settings.jobs = 2;
273+
settings.quiet = true;
274274
settings.inlineSuppressions = true;
275275
settings.severity.enable(Severity::information);
276276
if (!suppression.empty()) {
@@ -1089,6 +1089,7 @@ class TestSuppressions : public TestFixture {
10891089

10901090
CppCheck cppCheck(*this, false, nullptr); // <- do not "use global suppressions". pretend this is a thread that just checks a file.
10911091
Settings& settings = cppCheck.settings();
1092+
settings.quiet = true;
10921093
settings.nomsg.addSuppressionLine("uninitvar");
10931094
settings.exitCode = 1;
10941095

@@ -1123,6 +1124,7 @@ class TestSuppressions : public TestFixture {
11231124

11241125
CppCheck cppCheck(*this, true, nullptr);
11251126
Settings& settings = cppCheck.settings();
1127+
settings.quiet = true;
11261128
settings.severity.enable(Severity::style);
11271129
settings.inlineSuppressions = true;
11281130
settings.relativePaths = true;

test/testthreadexecutor.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ class TestThreadExecutorBase : public TestFixture {
6767
*/
6868
void check(unsigned int jobs, int files, int result, const std::string &data, const CheckOptions& opt = make_default_obj{}) {
6969
errout.str("");
70-
output.str("");
7170

7271
std::list<FileSettings> fileSettings;
7372

@@ -262,7 +261,7 @@ class TestThreadExecutorBase : public TestFixture {
262261
$.executeCommandCalled = true,
263262
$.exe = exe,
264263
$.args = {"-quiet", "-checks=*,-clang-analyzer-*,-llvm*", file, "--"}));
265-
ASSERT_EQUALS("Checking " + file + " ...\n", output.str());
264+
ASSERT_EQUALS("Checking " + file + " ...\n", output_str());
266265
}
267266

268267
// TODO: provide data which actually shows values above 0

0 commit comments

Comments
 (0)