|  | 
|  | 1 | +#include <utils/FileSystemUtils.h> | 
|  | 2 | + | 
|  | 3 | +#include "CoverageAndResultsStatisticsPrinter.h" | 
|  | 4 | +#include "utils/StringUtils.h" | 
|  | 5 | +#include "utils/CollectionUtils.h" | 
|  | 6 | +#include "Paths.h" | 
|  | 7 | + | 
|  | 8 | +void printer::CoverageAndResultsStatisticsPrinter::write(const utbot::ProjectContext &projectContext, | 
|  | 9 | +                                                         const Coverage::TestResultMap &testsResultMap, | 
|  | 10 | +                                                         const Coverage::CoverageMap &coverageMap) { | 
|  | 11 | +    for (auto const &[testPath, testsResult]: testsResultMap) { | 
|  | 12 | +        fs::path sourcePath = Paths::testPathToSourcePath(projectContext, testPath); | 
|  | 13 | +        Coverage::FileCoverage fileCoverage = CollectionUtils::getOrDefault(coverageMap, | 
|  | 14 | +                                                                            sourcePath, | 
|  | 15 | +                                                                            Coverage::FileCoverage()); | 
|  | 16 | +        insert({sourcePath, FileCoverageAndResultsStatistics(testsResult, fileCoverage)}); | 
|  | 17 | +    } | 
|  | 18 | +    std::vector<std::string> metricNames = { | 
|  | 19 | +            "filename", "executionTime (ms)", | 
|  | 20 | +            "totalTestsNumber", "passedTestsNumber", "failedTestsNumber", "deathTestsNumber", "interruptedTestsNumber", | 
|  | 21 | +            "totalLinesNumber", "coveredLinesNumber", "lineCoverageRatio (%)" | 
|  | 22 | +    }; | 
|  | 23 | +    std::string header = StringUtils::joinWith(metricNames, ","); | 
|  | 24 | +    std::stringstream ss; | 
|  | 25 | +    ss << header << '\n'; | 
|  | 26 | +    for (auto const &[sourcePath, statistics]: *this) { | 
|  | 27 | +        ss << fs::relative(sourcePath, projectContext.projectPath) << ","; | 
|  | 28 | +        ss << statistics.totalExecutionTimeMs << ','; | 
|  | 29 | +        ss << statistics.totalTestsNum << ","; | 
|  | 30 | +        ss << CollectionUtils::getOrDefault(statistics.testsWithStatusNum, testsgen::TestStatus::TEST_PASSED, 0u) | 
|  | 31 | +           << ','; | 
|  | 32 | +        ss << CollectionUtils::getOrDefault(statistics.testsWithStatusNum, testsgen::TestStatus::TEST_FAILED, 0u) | 
|  | 33 | +           << ','; | 
|  | 34 | +        ss << CollectionUtils::getOrDefault(statistics.testsWithStatusNum, testsgen::TestStatus::TEST_DEATH, 0u) << ','; | 
|  | 35 | +        ss << CollectionUtils::getOrDefault(statistics.testsWithStatusNum, testsgen::TestStatus::TEST_INTERRUPTED, 0u) | 
|  | 36 | +           << ','; | 
|  | 37 | + | 
|  | 38 | +        uint32_t coveredLinesNum = | 
|  | 39 | +                statistics.coverage.fullCoverageLines.size() + statistics.coverage.partialCoverageLines.size(); | 
|  | 40 | +        uint32_t totalLinesNum = coveredLinesNum + statistics.coverage.noCoverageLines.size(); | 
|  | 41 | +        double lineCoverageRatio = 0; | 
|  | 42 | +        if (totalLinesNum != 0) { | 
|  | 43 | +            lineCoverageRatio = 100.0 * coveredLinesNum / totalLinesNum; | 
|  | 44 | +        } | 
|  | 45 | +        ss << totalLinesNum << ',' << coveredLinesNum << ','; | 
|  | 46 | +        ss << std::fixed << lineCoverageRatio << '\n'; | 
|  | 47 | +    } | 
|  | 48 | +    fs::path resultsFilePath = resultsDirectory / (TimeUtils::getDate() + "-stats.csv"); | 
|  | 49 | +    FileSystemUtils::writeToFile(resultsFilePath, ss.str()); | 
|  | 50 | +    LOG_S(INFO) << StringUtils::stringFormat("See statistics info here: %s", resultsFilePath); | 
|  | 51 | +} | 
|  | 52 | + | 
|  | 53 | +printer::FileCoverageAndResultsStatistics::FileCoverageAndResultsStatistics( | 
|  | 54 | +        const Coverage::FileTestsResult &testsResult, | 
|  | 55 | +        Coverage::FileCoverage fileCoverage) { | 
|  | 56 | +    coverage = std::move(fileCoverage); | 
|  | 57 | +    totalTestsNum = 0; | 
|  | 58 | +    totalExecutionTimeMs = 0; | 
|  | 59 | +    for (const auto &[_, testResult]: testsResult) { | 
|  | 60 | +        totalTestsNum++; | 
|  | 61 | +        testsWithStatusNum[testResult.status()]++; | 
|  | 62 | +        totalExecutionTimeMs += testResult.executiontimems(); | 
|  | 63 | +    } | 
|  | 64 | +} | 
0 commit comments