Skip to content

Commit 89bdb82

Browse files
authored
fix generation for coreutils (#487)
1 parent 9314a3f commit 89bdb82

File tree

5 files changed

+85
-62
lines changed

5 files changed

+85
-62
lines changed

server/src/building/ProjectBuildDatabse.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,18 @@ ProjectBuildDatabase::ProjectBuildDatabase(fs::path _buildCommandsJsonPath,
4040
throw CompilationDatabaseException("Couldn't open link_commands.json or compile_commands.json files");
4141
}
4242

43-
auto linkCommandsJson = JsonUtils::getJsonFromFile(linkCommandsJsonPath);
44-
auto compileCommandsJson = JsonUtils::getJsonFromFile(compileCommandsJsonPath);
45-
46-
initObjects(compileCommandsJson);
47-
initInfo(linkCommandsJson);
48-
filterInstalledFiles();
49-
addLocalSharedLibraries();
50-
fillTargetInfoParents();
51-
createClangCompileCommandsJson();
43+
try {
44+
auto linkCommandsJson = JsonUtils::getJsonFromFile(linkCommandsJsonPath);
45+
auto compileCommandsJson = JsonUtils::getJsonFromFile(compileCommandsJsonPath);
46+
initObjects(compileCommandsJson);
47+
initInfo(linkCommandsJson);
48+
filterInstalledFiles();
49+
addLocalSharedLibraries();
50+
fillTargetInfoParents();
51+
createClangCompileCommandsJson();
52+
} catch (const std::exception &e) {
53+
return;
54+
}
5255
}
5356

5457
ProjectBuildDatabase::ProjectBuildDatabase(utbot::ProjectContext projectContext) : ProjectBuildDatabase(

server/src/commands/Commands.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ namespace Commands {
248248
bool generateForStaticFunctions = true;
249249
bool verbose = false;
250250
int32_t timeoutPerFunction = 30;
251-
int32_t timeoutPerTest = 0;
251+
int32_t timeoutPerTest = 30;
252252
bool noDeterministicSearcher = false;
253253
bool noStubs = false;
254254
};

server/src/coverage/GcovCoverageTool.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,19 @@ CoverageMap GcovCoverageTool::getCoverageInfo() const {
124124
ExecUtils::doWorkWithProgress(
125125
FileSystemUtils::DirectoryIterator(covJsonDirPath), progressWriter,
126126
"Reading coverage files", [&coverageMap](auto const &entry) {
127-
auto jsonPath = entry.path();
128-
auto coverageJson = JsonUtils::getJsonFromFile(jsonPath);
129-
for (const nlohmann::json &jsonFile : coverageJson.at("files")) {
130-
fs::path filePath(std::filesystem::path(jsonFile.at("file")));
131-
if (Paths::isGtest(filePath)) {
132-
continue;
127+
try {
128+
auto jsonPath = entry.path();
129+
auto coverageJson = JsonUtils::getJsonFromFile(jsonPath);
130+
for (const nlohmann::json &jsonFile: coverageJson.at("files")) {
131+
fs::path filePath(std::filesystem::path(jsonFile.at("file")));
132+
if (Paths::isGtest(filePath)) {
133+
continue;
134+
}
135+
setLineNumbers(jsonFile, coverageMap[filePath]);
136+
setFunctionBorders(jsonFile, coverageMap[filePath]);
133137
}
134-
setLineNumbers(jsonFile, coverageMap[filePath]);
135-
setFunctionBorders(jsonFile, coverageMap[filePath]);
138+
} catch (const std::exception &e) {
139+
return;
136140
}
137141
});
138142
return coverageMap;

server/src/coverage/LlvmCoverageTool.cpp

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -153,43 +153,46 @@ Coverage::CoverageMap LlvmCoverageTool::getCoverageInfo() const {
153153
LOG_S(ERROR) << "Can't found coverage.json at " << covJsonPath.string();
154154
throw CoverageGenerationException("Can't found coverage.json at " + covJsonPath.string());
155155
}
156-
LOG_S(INFO) << "Reading coverage.json";
157-
158-
nlohmann::json coverageJson = JsonUtils::getJsonFromFile(covJsonPath);
159-
160-
// Parsing is based on LLVM coverage mapping format
161-
ExecUtils::doWorkWithProgress(
162-
coverageJson.at("data"), progressWriter, "Reading coverage.json",
163-
[&coverageMap](const nlohmann::json &data) {
164-
for (const nlohmann::json &function : data.at("functions")) {
165-
std::string filename = function.at("filenames").at(0);
166-
// no need to show coverage for gtest library
167-
if (Paths::isGtest(filename)) {
168-
continue;
169-
}
170-
for (const nlohmann::json &region : function.at("regions")) {
171-
// In an LLVM coverage mapping format a region is an array with line and
172-
// character position
173-
FileCoverage::SourcePosition startPosition{ region.at(0).get<uint32_t>() - 1,
174-
region.at(1).get<uint32_t>() - 1 };
175-
FileCoverage::SourcePosition endPosition{ region.at(2).get<uint32_t>() - 1,
176-
region.at(3).get<uint32_t>() - 1 };
177-
FileCoverage::SourceRange sourceRange{ startPosition, endPosition };
178-
// The 4th element in LLVM coverage mapping format of a region
179-
if (region.at(4).get<int>() == 0) {
180-
coverageMap[filename].uncoveredRanges.push_back(sourceRange);
181-
} else if (region.at(4).get<int>() >= 1) {
182-
coverageMap[filename].coveredRanges.push_back(sourceRange);
156+
try {
157+
LOG_S(INFO) << "Reading coverage.json";
158+
nlohmann::json coverageJson = JsonUtils::getJsonFromFile(covJsonPath);
159+
160+
// Parsing is based on LLVM coverage mapping format
161+
ExecUtils::doWorkWithProgress(
162+
coverageJson.at("data"), progressWriter, "Reading coverage.json",
163+
[&coverageMap](const nlohmann::json &data) {
164+
for (const nlohmann::json &function: data.at("functions")) {
165+
std::string filename = function.at("filenames").at(0);
166+
// no need to show coverage for gtest library
167+
if (Paths::isGtest(filename)) {
168+
continue;
169+
}
170+
for (const nlohmann::json &region: function.at("regions")) {
171+
// In an LLVM coverage mapping format a region is an array with line and
172+
// character position
173+
FileCoverage::SourcePosition startPosition{region.at(0).get<uint32_t>() - 1,
174+
region.at(1).get<uint32_t>() - 1};
175+
FileCoverage::SourcePosition endPosition{region.at(2).get<uint32_t>() - 1,
176+
region.at(3).get<uint32_t>() - 1};
177+
FileCoverage::SourceRange sourceRange{startPosition, endPosition};
178+
// The 4th element in LLVM coverage mapping format of a region
179+
if (region.at(4).get<int>() == 0) {
180+
coverageMap[filename].uncoveredRanges.push_back(sourceRange);
181+
} else if (region.at(4).get<int>() >= 1) {
182+
coverageMap[filename].coveredRanges.push_back(sourceRange);
183+
}
184+
}
183185
}
184-
}
185-
}
186-
});
186+
});
187187

188-
for (const auto &item : coverageMap) {
189-
countLineCoverage(coverageMap, item.first);
190-
}
188+
for (const auto &item: coverageMap) {
189+
countLineCoverage(coverageMap, item.first);
190+
}
191191

192-
return coverageMap;
192+
return coverageMap;
193+
} catch (const std::exception &e) {
194+
throw CoverageGenerationException("Can't parse coverage.json at " + covJsonPath.string());
195+
}
193196
}
194197

195198
void LlvmCoverageTool::countLineCoverage(Coverage::CoverageMap &coverageMap,
@@ -223,9 +226,18 @@ void LlvmCoverageTool::checkLineForPartial(Coverage::FileCoverage::SourceLine li
223226
}
224227

225228
nlohmann::json LlvmCoverageTool::getTotals() const {
226-
fs::path covJsonPath = Paths::getCoverageJsonPath(projectContext);
227-
nlohmann::json coverageJson = JsonUtils::getJsonFromFile(covJsonPath);
228-
return coverageJson.at("data").back().at("totals");
229+
try {
230+
fs::path covJsonPath = Paths::getCoverageJsonPath(projectContext);
231+
nlohmann::json coverageJson = JsonUtils::getJsonFromFile(covJsonPath);
232+
return coverageJson.at("data").back().at("totals");
233+
} catch (const std::exception &e) {
234+
return {{
235+
"lines", {
236+
{"count", 0},
237+
{"covered", 0},
238+
{"percent", (double) 0.0}
239+
}}};
240+
}
229241
}
230242

231243

server/src/coverage/TestRunner.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,18 @@ testsgen::TestResultObject TestRunner::runTest(const BuildRunCommand &command,
213213
testRes.set_status(testsgen::TEST_DEATH);
214214
return testRes;
215215
}
216-
nlohmann::json gtestResultsJson = JsonUtils::getJsonFromFile(Paths::getGTestResultsJsonPath(projectContext));
217-
if (!google::protobuf::util::TimeUtil::FromString(gtestResultsJson["time"], testRes.mutable_executiontime())) {
218-
LOG_S(WARNING) << "Cannot parse duration of test execution";
219-
}
220-
if (gtestResultsJson["failures"] != 0) {
216+
try {
217+
nlohmann::json gtestResultsJson = JsonUtils::getJsonFromFile(Paths::getGTestResultsJsonPath(projectContext));
218+
if (!google::protobuf::util::TimeUtil::FromString(gtestResultsJson["time"], testRes.mutable_executiontime())) {
219+
LOG_S(WARNING) << "Cannot parse duration of test execution";
220+
}
221+
if (gtestResultsJson["failures"] != 0) {
222+
testRes.set_status(testsgen::TEST_FAILED);
223+
} else {
224+
testRes.set_status(testsgen::TEST_PASSED);
225+
}
226+
} catch (const std::exception &e) {
221227
testRes.set_status(testsgen::TEST_FAILED);
222-
} else {
223-
testRes.set_status(testsgen::TEST_PASSED);
224228
}
225229
return testRes;
226230
}

0 commit comments

Comments
 (0)