|
14 | 14 |
|
15 | 15 | // This binary is a test runner for CEL tests. It is used to run CEL tests |
16 | 16 | // written in the CEL test suite format. |
| 17 | +#include <cerrno> |
| 18 | +#include <cstdlib> |
| 19 | +#include <cstring> |
17 | 20 | #include <fstream> |
18 | 21 | #include <functional> |
19 | 22 | #include <ios> |
|
31 | 34 | #include "absl/strings/str_cat.h" |
32 | 35 | #include "absl/strings/str_format.h" |
33 | 36 | #include "absl/strings/str_join.h" |
| 37 | +#include "absl/strings/string_view.h" |
34 | 38 | #include "eval/public/cel_expression.h" |
35 | 39 | #include "internal/testing.h" |
36 | 40 | #include "runtime/runtime.h" |
@@ -110,9 +114,54 @@ class CoverageReportingEnvironment : public testing::Environment { |
110 | 114 | << absl::StrJoin(coverage_report.unencountered_branches, |
111 | 115 | "\n"); |
112 | 116 | } |
| 117 | + |
| 118 | + if (!coverage_report.dot_graph.empty()) { |
| 119 | + WriteDotGraphToArtifact(coverage_report.dot_graph); |
| 120 | + } |
113 | 121 | } |
114 | 122 |
|
115 | 123 | private: |
| 124 | + void WriteDotGraphToArtifact(absl::string_view dot_graph) { |
| 125 | + // Save DOT graph to file in TEST_UNDECLARED_OUTPUTS_DIR or default dir |
| 126 | + const char* outputs_dir_env = std::getenv("TEST_UNDECLARED_OUTPUTS_DIR"); |
| 127 | + |
| 128 | + // For non-Bazel/Blaze users, we write to a subdirectory under the current |
| 129 | + // working directory. |
| 130 | + std::string outputs_dir = |
| 131 | + (outputs_dir_env != nullptr) ? outputs_dir_env : "cel_artifacts"; |
| 132 | + |
| 133 | + std::string coverage_dir = absl::StrCat(outputs_dir, "/cel_test_coverage"); |
| 134 | + |
| 135 | + // Creates the directory to store CEL test coverage artifacts. |
| 136 | + // The second argument, `0755`, sets the directory's permissions in octal |
| 137 | + // format, which is a standard for file system operations. It grants: |
| 138 | + // - Owner: read, write, and execute permissions (7 = 4+2+1). |
| 139 | + // - Group: read and execute permissions (5 = 4+1). |
| 140 | + // - Others: read and execute permissions (5 = 4+1). |
| 141 | + // This gives the owner full control while allowing other users to access |
| 142 | + // the generated artifacts. |
| 143 | + int mkdir_result = mkdir(coverage_dir.c_str(), 0755); |
| 144 | + |
| 145 | + // If mkdir fails, it sets the global 'errno' variable to an error code |
| 146 | + // indicating the reason. We check this code to specifically ignore the |
| 147 | + // EEXIST error, which just means the directory already exists (this is not |
| 148 | + // a real failure we need to warn about). |
| 149 | + if (mkdir_result != 0 && errno != EEXIST) { |
| 150 | + ABSL_LOG(WARNING) << "Failed to create directory: " << coverage_dir |
| 151 | + << " (reason: " << strerror(errno) << ")"; |
| 152 | + } else { |
| 153 | + std::string graph_path = |
| 154 | + absl::StrCat(coverage_dir, "/coverage_graph.txt"); |
| 155 | + std::ofstream out(graph_path); |
| 156 | + if (out.is_open()) { |
| 157 | + out << dot_graph; |
| 158 | + out.close(); |
| 159 | + } else { |
| 160 | + ABSL_LOG(WARNING) << "Failed to open file for writing: " << graph_path; |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
116 | 165 | cel::test::CoverageIndex& coverage_index_; |
117 | 166 | }; |
118 | 167 |
|
|
0 commit comments