|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "testing/testrunner/coverage_reporting.h" |
| 16 | + |
| 17 | +#include <cerrno> |
| 18 | +#include <cstdlib> |
| 19 | +#include <cstring> |
| 20 | +#include <fstream> |
| 21 | +#include <string> |
| 22 | + |
| 23 | +#include "absl/log/absl_log.h" |
| 24 | +#include "absl/strings/str_cat.h" |
| 25 | +#include "absl/strings/str_format.h" |
| 26 | +#include "absl/strings/str_join.h" |
| 27 | +#include "absl/strings/str_replace.h" |
| 28 | +#include "absl/strings/string_view.h" |
| 29 | +#include "internal/testing.h" |
| 30 | +#include "testing/testrunner/coverage_index.h" |
| 31 | + |
| 32 | +namespace cel::test { |
| 33 | +void CoverageReportingEnvironment::TearDown() { |
| 34 | + CoverageIndex::CoverageReport coverage_report = |
| 35 | + coverage_index_.GetCoverageReport(); |
| 36 | + testing::Test::RecordProperty("CEL Expression", |
| 37 | + coverage_report.cel_expression); |
| 38 | + std::cout << "CEL Expression: " << coverage_report.cel_expression; |
| 39 | + if (coverage_report.nodes == 0) { |
| 40 | + testing::Test::RecordProperty("CEL Coverage", "No coverage stats found"); |
| 41 | + std::cout << "CEL Coverage: " << "No coverage stats found"; |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + // Log Node Coverage results |
| 46 | + double node_coverage = static_cast<double>(coverage_report.covered_nodes) / |
| 47 | + static_cast<double>(coverage_report.nodes) * 100.0; |
| 48 | + std::string node_coverage_string = |
| 49 | + absl::StrFormat("%.2f%% (%d out of %d nodes covered)", node_coverage, |
| 50 | + coverage_report.covered_nodes, coverage_report.nodes); |
| 51 | + testing::Test::RecordProperty("AST Node Coverage", node_coverage_string); |
| 52 | + std::cout << "AST Node Coverage: " << node_coverage_string; |
| 53 | + if (!coverage_report.unencountered_nodes.empty()) { |
| 54 | + testing::Test::RecordProperty( |
| 55 | + "Interesting Unencountered Nodes", |
| 56 | + absl::StrJoin(coverage_report.unencountered_nodes, "\n")); |
| 57 | + std::cout << "Interesting Unencountered Nodes: " |
| 58 | + << absl::StrJoin(coverage_report.unencountered_nodes, "\n"); |
| 59 | + } |
| 60 | + |
| 61 | + // Log Branch Coverage results |
| 62 | + double branch_coverage = 0.0; |
| 63 | + if (coverage_report.branches > 0) { |
| 64 | + branch_coverage = |
| 65 | + static_cast<double>(coverage_report.covered_boolean_outcomes) / |
| 66 | + static_cast<double>(coverage_report.branches) * 100.0; |
| 67 | + } |
| 68 | + std::string branch_coverage_string = absl::StrFormat( |
| 69 | + "%.2f%% (%d out of %d branch outcomes covered)", branch_coverage, |
| 70 | + coverage_report.covered_boolean_outcomes, coverage_report.branches); |
| 71 | + testing::Test::RecordProperty("AST Branch Coverage", branch_coverage_string); |
| 72 | + std::cout << "AST Branch Coverage: " << branch_coverage_string; |
| 73 | + if (!coverage_report.unencountered_branches.empty()) { |
| 74 | + testing::Test::RecordProperty( |
| 75 | + "Interesting Unencountered Branch Paths", |
| 76 | + absl::StrJoin(coverage_report.unencountered_branches, "\n")); |
| 77 | + std::cout << "Interesting Unencountered Branch Paths: " |
| 78 | + << absl::StrJoin(coverage_report.unencountered_branches, |
| 79 | + "\n"); |
| 80 | + } |
| 81 | + if (!coverage_report.dot_graph.empty()) { |
| 82 | + WriteDotGraphToArtifact(coverage_report.dot_graph); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void CoverageReportingEnvironment::WriteDotGraphToArtifact( |
| 87 | + absl::string_view dot_graph) { |
| 88 | + // Save DOT graph to file in TEST_UNDECLARED_OUTPUTS_DIR or default dir |
| 89 | + const char* outputs_dir_env = std::getenv("TEST_UNDECLARED_OUTPUTS_DIR"); |
| 90 | + // For non-Bazel/Blaze users, we write to a subdirectory under the current |
| 91 | + // working directory. |
| 92 | + // NOMUTANTS --cel_artifacts is for non-Bazel/Blaze users only so not |
| 93 | + // needed to test in our case. |
| 94 | + std::string outputs_dir = |
| 95 | + (outputs_dir_env == nullptr) ? "cel_artifacts" : outputs_dir_env; |
| 96 | + std::string coverage_dir = absl::StrCat(outputs_dir, "/cel_test_coverage"); |
| 97 | + // Creates the directory to store CEL test coverage artifacts. |
| 98 | + // The second argument, `0755`, sets the directory's permissions in octal |
| 99 | + // format, which is a standard for file system operations. It grants: |
| 100 | + // - Owner: read, write, and execute permissions (7 = 4+2+1). |
| 101 | + // - Group: read and execute permissions (5 = 4+1). |
| 102 | + // - Others: read and execute permissions (5 = 4+1). |
| 103 | + // This gives the owner full control while allowing other users to access |
| 104 | + // the generated artifacts. |
| 105 | + int mkdir_result = mkdir(coverage_dir.c_str(), 0755); |
| 106 | + // If mkdir fails, it sets the global 'errno' variable to an error code |
| 107 | + // indicating the reason. We check this code to specifically ignore the |
| 108 | + // EEXIST error, which just means the directory already exists (this is not |
| 109 | + // a real failure we need to warn about). |
| 110 | + if (mkdir_result == 0 || errno == EEXIST) { |
| 111 | + std::string graph_path = absl::StrCat(coverage_dir, "/coverage_graph.txt"); |
| 112 | + std::ofstream out(graph_path); |
| 113 | + if (out.is_open()) { |
| 114 | + out << dot_graph; |
| 115 | + out.close(); |
| 116 | + } else { |
| 117 | + ABSL_LOG(WARNING) << "Failed to open file for writing: " << graph_path; |
| 118 | + } |
| 119 | + } else { |
| 120 | + ABSL_LOG(WARNING) << "Failed to create directory: " << coverage_dir |
| 121 | + << " (reason: " << strerror(errno) << ")"; |
| 122 | + } |
| 123 | +} |
| 124 | +} // namespace cel::test |
0 commit comments