Skip to content

Commit 465c838

Browse files
authored
[BUG] CLI_Test.Generate_Project_Tests fails on some PCs #347 (#348)
- introduced a synthetic order for problems that were found by KLEE
1 parent 5a25b90 commit 465c838

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

server/src/printers/TestsPrinter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ void TestsPrinter::printFinalCodeAndAlterJson(Tests &tests) {
7474
tests.code.append("\n");
7575
++line_count;
7676
} else {
77-
// anchor for SARIF ala testFilePath,lineThatCallsTestedFunction
77+
// anchor for SARIF
7878
std::string nameAndTestIndex =
7979
line.substr(sarif::PREFIX_FOR_JSON_PATH.size());
80-
int pos = nameAndTestIndex.find(',');
81-
if (pos != -1) {
80+
size_t pos = nameAndTestIndex.find(',');
81+
if (pos != std::string::npos) {
8282
std::string name = nameAndTestIndex.substr(0, pos);
8383
int testIndex = -1;
8484
try {
@@ -245,7 +245,7 @@ void TestsPrinter::printLazyVariables(const Tests::MethodDescription &methodDesc
245245
if (verbose) {
246246
strComment("Construct lazy instantiated variables");
247247
}
248-
for (const auto paramValue : testCase.paramValues) {
248+
for (const auto &paramValue : testCase.paramValues) {
249249
printLazyVariables(paramValue.lazyParams, paramValue.lazyValues);
250250
}
251251
}

server/test/framework/CLI_Tests.cpp

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <protobuf/testgen.grpc.pb.h>
1212

1313
#include <set>
14+
#include <map>
1415

1516
namespace {
1617
using grpc::Channel;
@@ -105,7 +106,7 @@ namespace {
105106
{
106107
std::size_t count{};
107108
for (std::string::size_type pos{};
108-
inout.npos != (pos = inout.find(what.data(), pos, what.length()));
109+
std::string::npos != (pos = inout.find(what.data(), pos, what.length()));
109110
pos += with.length(), ++count) {
110111
inout.replace(pos, what.length(), with.data(), with.length());
111112
}
@@ -120,8 +121,45 @@ namespace {
120121
return content;
121122
}
122123

123-
void compareFiles(const fs::path &golden, const fs::path &real) {
124-
ASSERT_EQ(getNormalizedContent(golden), getNormalizedContent(real));
124+
std::string filterUnstableParams(const std::string &context) {
125+
std::stringstream ins(context);
126+
std::stringstream outs;
127+
std::string line;
128+
while (getline(ins, line)) {
129+
if (line.find(R"x("startLine")x") != std::string::npos) {
130+
outs << "[startLine replacement]" << "\n";
131+
} else if (line.find(R"x( (test)")x") != std::string::npos) {
132+
outs << "[test name replacement]" << "\n";
133+
} else {
134+
outs << line;
135+
}
136+
}
137+
return outs.str();
138+
}
139+
140+
std::map<std::string, std::string> getOrderedRecords(const json &rep) {
141+
json results = rep["runs"][0]["results"];
142+
std::map<std::string, std::string> records;
143+
for (const auto &it : results) {
144+
std::string problemDescription = it["message"]["text"];
145+
146+
std::string srcFile = it["locations"][0]["physicalLocation"]["artifactLocation"]["uri"];
147+
int lineNo = it["locations"][0]["physicalLocation"]["region"]["startLine"];
148+
149+
std::stringstream key;
150+
key << problemDescription << "\n"
151+
<< srcFile << ":" << lineNo;
152+
153+
records.insert(std::pair(key.str(), filterUnstableParams(it.dump(2))));
154+
}
155+
return records;
156+
}
157+
158+
void compareSARIFFiles(const fs::path &golden, const fs::path &real) {
159+
json gjs = json::parse(getNormalizedContent(golden));
160+
json rjs = json::parse(getNormalizedContent(real));
161+
162+
EXPECT_EQ(getOrderedRecords(gjs), getOrderedRecords(rjs));
125163
}
126164

127165
TEST_F(CLI_Test, Generate_Project_Tests) {
@@ -131,8 +169,9 @@ namespace {
131169
buildDirectoryName, "project" });
132170
checkTestDirectory(allProjectTestFiles);
133171

134-
compareFiles( suitePath / "goldenImage" / sarif::SARIF_DIR_NAME / sarif::SARIF_FILE_NAME,
135-
suitePath / sarif::SARIF_DIR_NAME / sarif::SARIF_FILE_NAME);
172+
compareSARIFFiles(
173+
suitePath / "goldenImage" / sarif::SARIF_DIR_NAME /sarif::SARIF_FILE_NAME,
174+
suitePath / sarif::SARIF_DIR_NAME / sarif::SARIF_FILE_NAME);
136175
}
137176

138177
TEST_F(CLI_Test, Generate_File_Tests) {

0 commit comments

Comments
 (0)