|
5 | 5 | #include "TestsPrinter.h" |
6 | 6 |
|
7 | 7 | #include "Paths.h" |
| 8 | +#include "sarif/FileSarif.h" |
8 | 9 | #include "utils/ArgumentsUtils.h" |
9 | 10 | #include "utils/Copyright.h" |
| 11 | +#include "utils/JsonUtils.h" |
10 | 12 | #include "visitors/ParametrizedAssertsVisitor.h" |
11 | 13 | #include "visitors/VerboseAssertsParamVisitor.h" |
12 | 14 | #include "visitors/VerboseAssertsReturnValueVisitor.h" |
13 | 15 | #include "visitors/VerboseParameterVisitor.h" |
14 | 16 | #include "utils/KleeUtils.h" |
15 | 17 |
|
| 18 | + |
| 19 | +using json = nlohmann::json; |
16 | 20 | using printer::TestsPrinter; |
17 | 21 |
|
18 | 22 | TestsPrinter::TestsPrinter(const types::TypesHandler *typesHandler, utbot::Language srcLanguage) : Printer(srcLanguage) , typesHandler(typesHandler) { |
@@ -58,7 +62,47 @@ void TestsPrinter::joinToFinalCode(Tests &tests, const fs::path& generatedHeader |
58 | 62 | tests.regressionMethodsNumber = printSuiteAndReturnMethodsCount(Tests::DEFAULT_SUITE_NAME, tests.methods); |
59 | 63 | tests.errorMethodsNumber = printSuiteAndReturnMethodsCount(Tests::ERROR_SUITE_NAME, tests.methods); |
60 | 64 | ss << RB(); |
61 | | - tests.code = ss.str(); |
| 65 | + printFinalCodeAndAlterJson(tests); |
| 66 | +} |
| 67 | + |
| 68 | +void TestsPrinter::printFinalCodeAndAlterJson(Tests &tests) { |
| 69 | + int line_count = 0; |
| 70 | + string line; |
| 71 | + while (getline(ss, line)) { |
| 72 | + if (line.rfind(sarif::FileSarif::prefix_for_json_path, 0) == 0) { |
| 73 | + fs::path json_path = line.substr(sarif::FileSarif::prefix_for_json_path.size()); |
| 74 | + if (!exists(json_path)) { |
| 75 | + LOG_S(ERROR) << "Json from klee not found"; |
| 76 | + continue; |
| 77 | + } |
| 78 | + json location = R"( |
| 79 | + { |
| 80 | + "location" : { |
| 81 | + "physicalLocation" : { |
| 82 | + "artifactLocation" : { |
| 83 | + "uri" : {} |
| 84 | + }, |
| 85 | + "region" : { |
| 86 | + "startLine" : {} |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + )"_json; |
| 92 | + location.at("location").at("physicalLocation").at("artifactLocation").at("uri") = |
| 93 | + tests.testSourceFilePath; |
| 94 | + location.at("location").at("physicalLocation").at("region").at("startLine") = line_count; |
| 95 | + json sarifResultFromFile = JsonUtils::getJsonFromFile(json_path); |
| 96 | + auto &arr = sarifResultFromFile.at("codeFlows").at(0).at("threadFlows").at(0). |
| 97 | + at("locations"); |
| 98 | + arr.insert(arr.begin(), location); |
| 99 | + JsonUtils::writeJsonToFile(json_path, sarifResultFromFile); |
| 100 | + } else { |
| 101 | + tests.code.append(line); |
| 102 | + tests.code.append("\n"); |
| 103 | + line_count++; |
| 104 | + } |
| 105 | + } |
62 | 106 | } |
63 | 107 |
|
64 | 108 | std::uint32_t TestsPrinter::printSuiteAndReturnMethodsCount(const string &suiteName, const Tests::MethodsMap &methods) { |
@@ -510,11 +554,19 @@ void TestsPrinter::parametrizedAsserts(const Tests::MethodDescription &methodDes |
510 | 554 | const std::optional<LineInfo::PredicateInfo>& predicateInfo) { |
511 | 555 | auto visitor = visitor::ParametrizedAssertsVisitor(typesHandler, this, predicateInfo, testCase.isError()); |
512 | 556 | visitor.visit(methodDescription, testCase); |
| 557 | + printJsonPathFromKlee(testCase); |
513 | 558 | globalParamsAsserts(methodDescription, testCase); |
514 | 559 | classAsserts(methodDescription, testCase); |
515 | 560 | changeableParamsAsserts(methodDescription, testCase); |
516 | 561 | } |
517 | 562 |
|
| 563 | +void TestsPrinter::printJsonPathFromKlee(const Tests::MethodTestCase &testCase) { |
| 564 | + if (!testCase.errorDescriptionInJson) { |
| 565 | + return; |
| 566 | + } |
| 567 | + ss << sarif::FileSarif::prefix_for_json_path << testCase.errorDescriptionInJson.value().string() << NL; |
| 568 | +} |
| 569 | + |
518 | 570 | std::vector<string> TestsPrinter::methodParametersListParametrized(const Tests::MethodDescription &methodDescription, |
519 | 571 | const Tests::MethodTestCase &testCase) { |
520 | 572 | std::vector<string> args; |
|
0 commit comments