-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathCoverageAndResultsGenerator.cpp
More file actions
114 lines (101 loc) · 4.52 KB
/
CoverageAndResultsGenerator.cpp
File metadata and controls
114 lines (101 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "CoverageAndResultsGenerator.h"
#include "TimeExecStatistics.h"
#include "exceptions/CoverageGenerationException.h"
#include "utils/FileSystemUtils.h"
#include "utils/StringUtils.h"
#include "utils/stats/TestsExecutionStats.h"
#include "loguru.h"
using grpc::Status;
using grpc::StatusCode;
CoverageAndResultsGenerator::CoverageAndResultsGenerator(
testsgen::CoverageAndResultsRequest const *coverageAndResultsRequest,
CoverageAndResultsWriter *coverageAndResultsWriter)
: TestRunner(utbot::ProjectContext(coverageAndResultsRequest->projectcontext()),
coverageAndResultsRequest->testfilter().testfilepath(),
coverageAndResultsRequest->testfilter().testsuite(),
coverageAndResultsRequest->testfilter().testname(),
coverageAndResultsRequest->testfilter().functionname(),
coverageAndResultsWriter),
coverageAndResultsWriter(coverageAndResultsWriter) {
}
grpc::Status CoverageAndResultsGenerator::generate(bool withCoverage,
testsgen::SettingsContext &settingsContext) {
auto context = utbot::SettingsContext(settingsContext);
return generate(withCoverage, context);
}
grpc::Status CoverageAndResultsGenerator::generate(bool withCoverage,
utbot::SettingsContext &settingsContext) {
MEASURE_FUNCTION_EXECUTION_TIME
try {
init(withCoverage);
runTests(withCoverage, settingsContext.timeoutPerTest);
if (withCoverage) {
collectCoverage();
StatsUtils::TestsExecutionStatsFileMap testsExecutionStats(projectContext, testResultMap, coverageMap);
printer::CSVPrinter printer = testsExecutionStats.toCSV();
FileSystemUtils::writeToFile(Paths::getExecutionStatsCSVPath(projectContext), printer.getStream().str());
LOG_S(INFO) << StringUtils::stringFormat("See execution stats here: %s",
Paths::getExecutionStatsCSVPath(projectContext));
}
} catch (CoverageGenerationException &e) {
showErrors();
return Status(StatusCode::FAILED_PRECONDITION, e.what());
} catch (ExecutionProcessException &e) {
exceptions.emplace_back(e);
showErrors();
return Status(StatusCode::FAILED_PRECONDITION, e.what());
} catch (CancellationException &e) {
return Status::CANCELLED;
}
showErrors();
return Status::OK;
}
void CoverageAndResultsGenerator::showErrors() const {
std::optional<fs::path> errorMessage;
if (hasExceptions()) {
auto log = StringUtils::joinWith(exceptions, "\n");
fs::path logFilePath =
LogUtils::writeLog(log, projectContext.projectName, "Collection_coverage");
auto message =
StringUtils::stringFormat("%d actions failed during building and running tests. "
"See more info here: "
"%s",
exceptions.size(), logFilePath);
LOG_S(WARNING) << message;
errorMessage = message;
}
coverageAndResultsWriter->writeResponse(projectContext, testResultMap, coverageMap, totals, errorMessage);
}
Coverage::CoverageMap const &CoverageAndResultsGenerator::getCoverageMap() {
return coverageMap;
}
nlohmann::json const &CoverageAndResultsGenerator::getTotals() {
return totals;
}
void CoverageAndResultsGenerator::collectCoverage() {
MEASURE_FUNCTION_EXECUTION_TIME
if (testsToLaunch.empty()) {
return;
}
std::vector<ShellExecTask> coverageCommands = coverageTool->getCoverageCommands(
CollectionUtils::filterToVector(testsToLaunch, [this](const UnitTest &testToLaunch) {
return testResultMap[testToLaunch.testFilePath][testToLaunch.testname].status() !=
testsgen::TEST_INTERRUPTED;
}));
if (coverageCommands.empty()) {
return;
}
ExecUtils::doWorkWithProgress(
coverageCommands, coverageAndResultsWriter, "Collecting coverage",
[this](ShellExecTask &task) {
auto [out, status, path] = task.run();
if (status != 0) {
exceptions.emplace_back(
StringUtils::stringFormat("Command: %s\nOutput: %s", task.toString(), out),
path.value());
}
});
LOG_S(DEBUG) << "All coverage commands were executed";
coverageMap = coverageTool->getCoverageInfo();
totals = coverageTool->getTotals();
}