Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions testing/testrunner/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ cc_test(
"//runtime:standard_runtime_builder_factory",
"@com_google_absl//absl/status:status_matchers",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
"@com_google_cel_spec//proto/cel/expr:syntax_cc_proto",
"@com_google_protobuf//:protobuf",
Expand Down
35 changes: 35 additions & 0 deletions testing/testrunner/coverage_index.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

#include "testing/testrunner/coverage_index.h"

#include <algorithm>
#include <cstdint>
#include <fstream>
#include <iterator>
#include <map>
#include <string>
#include <vector>

Expand Down Expand Up @@ -170,6 +174,13 @@ void TraverseAndCalculateCoverage(
}
}

int32_t GetLineNumber(const cel::expr::SourceInfo& source_info,
int32_t offset) {
auto line_it = std::upper_bound(source_info.line_offsets().begin(),
source_info.line_offsets().end(), offset);
return std::distance(source_info.line_offsets().begin(), line_it) + 1;
}

} // namespace

void CoverageIndex::RecordCoverage(int64_t node_id, const cel::Value& value) {
Expand Down Expand Up @@ -211,6 +222,30 @@ CoverageIndex::CoverageReport CoverageIndex::GetCoverageReport() const {
return report;
}

void CoverageIndex::WriteLCOV(absl::string_view path) {
std::ofstream file(std::string(path).c_str());
if (!file.is_open()) {
return;
}

// Maps instrumented line numbers to whether they are covered.
std::map<int, bool> lines;
const auto& positions = checked_expr_.source_info().positions();
for (const auto& [node_id, stats] : node_coverage_stats_) {
auto it = positions.find(node_id);
if (it == positions.end()) continue;
int line_num = GetLineNumber(checked_expr_.source_info(), it->second);
bool& covered = lines[line_num];
covered = covered || stats.covered;
}

file << "SF:" << checked_expr_.source_info().location() << "\n";
for (auto& [line_num, covered] : lines) {
file << "DA:" << line_num << "," << (covered ? 1 : 0) << "\n";
}
file << "end_of_record\n";
}

InstrumentationFactory InstrumentationFactoryForCoverage(
CoverageIndex& coverage_index) {
return [&](const cel::Ast& ast) -> Instrumentation {
Expand Down
3 changes: 3 additions & 0 deletions testing/testrunner/coverage_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class CoverageIndex {
// Returns a coverage report for the given checked expression.
CoverageReport GetCoverageReport() const;

// Writes the coverage in LCOV format to the given path.
void WriteLCOV(absl::string_view path);

private:
absl::flat_hash_map<int64_t, NodeCoverageStats> node_coverage_stats_;
NavigableProtoAst navigable_ast_;
Expand Down
67 changes: 67 additions & 0 deletions testing/testrunner/coverage_index_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
// limitations under the License.
#include "testing/testrunner/coverage_index.h"

#include <fstream>
#include <memory>
#include <sstream>
#include <string>
#include <utility>

#include "cel/expr/syntax.pb.h"
#include "absl/status/status_matchers.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "checker/type_checker_builder.h"
#include "checker/validation_result.h"
Expand Down Expand Up @@ -89,5 +93,68 @@ TEST(CoverageIndexTest, RecordCoverageWithErrorDoesNotCrash) {
EXPECT_TRUE(result.IsError());
}

TEST(CoverageIndexTest, WriteLCOV) {
ASSERT_OK_AND_ASSIGN(
std::unique_ptr<cel::CompilerBuilder> compiler_builder,
cel::NewCompilerBuilder(cel::internal::GetTestingDescriptorPool()));
ASSERT_THAT(compiler_builder->AddLibrary(cel::StandardCompilerLibrary()),
IsOk());
ASSERT_THAT(compiler_builder->GetCheckerBuilder().AddVariable(
cel::MakeVariableDecl("x", cel::BoolType())),
IsOk());
ASSERT_OK_AND_ASSIGN(std::unique_ptr<cel::Compiler> compiler,
std::move(compiler_builder)->Build());
const absl::string_view kSrc = R"(x ?
true :
false
)";
ASSERT_OK_AND_ASSIGN(cel::ValidationResult validation_result,
compiler->Compile(kSrc));
CheckedExpr checked_expr;
ASSERT_THAT(cel::AstToCheckedExpr(*validation_result.GetAst(), &checked_expr),
IsOk());
checked_expr.mutable_source_info()->set_location("test.cel");

CoverageIndex coverage_index;
coverage_index.Init(checked_expr);

ASSERT_OK_AND_ASSIGN(std::unique_ptr<const cel::Runtime> runtime,
CreateTestRuntime());
ASSERT_THAT(EnableCoverageInRuntime(*const_cast<cel::Runtime*>(runtime.get()),
coverage_index),
IsOk());
ASSERT_OK_AND_ASSIGN(std::unique_ptr<cel::Ast> ast,
cel::CreateAstFromCheckedExpr(checked_expr));
ASSERT_OK_AND_ASSIGN(auto program, runtime->CreateProgram(std::move(ast)));

cel::Activation activation;
activation.InsertOrAssignValue("x", cel::BoolValue(true));
google::protobuf::Arena arena;
ASSERT_OK_AND_ASSIGN(cel::Value result,
program->Evaluate(&arena, activation));
EXPECT_TRUE(result.GetBool().NativeValue());

std::string temp_file = absl::StrCat(testing::TempDir(), "/coverage.lcov");
coverage_index.WriteLCOV(temp_file);

std::ifstream f(temp_file);
std::stringstream buffer;
buffer << f.rdbuf();
std::string content = buffer.str();

// Verify content.
// We expect "test.cel" to be the source file.
EXPECT_THAT(content, testing::HasSubstr("SF:test.cel"));
// Line 1 (x ?) should be covered.
EXPECT_THAT(content, testing::HasSubstr("DA:1,1"));
// Line 2 (true) should be covered.
EXPECT_THAT(content, testing::HasSubstr("DA:2,1"));
// Line 3 (false) should be uncovered.
EXPECT_THAT(content, testing::HasSubstr("DA:3,0"));
// Line 4 (empty) should not be instrumented.
EXPECT_THAT(content, testing::Not(testing::HasSubstr("DA:4,")));
EXPECT_THAT(content, testing::HasSubstr("end_of_record"));
}

} // namespace
} // namespace cel::test