| 222 | } // namespace |
| 223 | |
| 224 | int main(int argc, char** argv) { |
| 225 | testing::InitGoogleTest(&argc, argv); |
| 226 | // Create a test context using the factory function returned by the global |
| 227 | // factory function provider which was initialized by the user. |
| 228 | absl::StatusOr<std::unique_ptr<cel::test::CelTestContext>> |
| 229 | cel_test_context_or = cel::test::internal::GetCelTestContextFactory()(); |
| 230 | if (!cel_test_context_or.ok()) { |
| 231 | ABSL_LOG(FATAL) << "Failed to create CEL test context from factory: " |
| 232 | << cel_test_context_or.status(); |
| 233 | } |
| 234 | std::unique_ptr<cel::test::CelTestContext> cel_test_context = |
| 235 | std::move(cel_test_context_or.value()); |
| 236 | |
| 237 | // We manually enable coverage here instead of just setting the |
| 238 | // `enable_coverage` flag on the context. This is intentional and necessary |
| 239 | // for this binary's reporting model. |
| 240 | // |
| 241 | // This binary needs a single coverage report for all tests run. |
| 242 | // We create `coverage_index` here, local to the `main` function, so its |
| 243 | // lifetime spans the entire test run. |
| 244 | // |
| 245 | // We must pass this specific instance to the |
| 246 | // `CoverageReportingEnvironment`, which Google Test calls after all |
| 247 | // dynamically registered tests are finished. |
| 248 | // |
| 249 | // If we just set the `enable_coverage` flag, the `TestRunner`'s |
| 250 | // constructor (as used in our `cc_test` files) would create its own |
| 251 | // internal `CoverageIndex`. That internal index would be destroyed |
| 252 | // with the `TestRunner` and would not populate the `coverage_index` |
| 253 | // instance needed by our global reporter. |
| 254 | // |
| 255 | // This manual approach ensures all tests populate the same `coverage_index` |
| 256 | // (the one local to `main`), which is then ready for the final report. |
| 257 | cel::test::CoverageIndex coverage_index; |
| 258 | |
| 259 | if (absl::GetFlag(FLAGS_collect_coverage)) { |
| 260 | if (cel_test_context->runtime() != nullptr) { |
| 261 | ABSL_CHECK_OK(cel::test::EnableCoverageInRuntime( |
| 262 | const_cast<cel::Runtime&>(*cel_test_context->runtime()), |
| 263 | coverage_index)); |
| 264 | } else if (cel_test_context->cel_expression_builder() != nullptr) { |
| 265 | ABSL_CHECK_OK(cel::test::EnableCoverageInCelExpressionBuilder( |
| 266 | const_cast<CelExpressionBuilder&>( |
| 267 | *cel_test_context->cel_expression_builder()), |
| 268 | coverage_index)); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // Update the context with an expression from flags, if provided. |
| 273 | // This will FATAL if an expression is set by both the factory and flags. |
| 274 | UpdateWithExpressionFromCommandLineFlags(*cel_test_context); |
| 275 | |
| 276 | auto test_runner = std::make_shared<TestRunner>(std::move(cel_test_context)); |
| 277 | ABSL_CHECK_OK(RegisterTests(GetTestSuite(), test_runner)); |
| 278 | |
| 279 | // Make sure the checked expression exists during the entire test run since |
| 280 | // the ast references it during coverage collection at teardown. |
| 281 | absl::StatusOr<cel::expr::CheckedExpr> checked_expr = |
nothing calls this directly
no test coverage detected