From e1ce9146610495811a27cf63ab642deb81a816a5 Mon Sep 17 00:00:00 2001 From: Dima Korolev Date: Thu, 6 Apr 2023 20:41:24 +0800 Subject: [PATCH 1/2] Supported `opa eval --coverage ... | ocov` in addition to `opa test ... --coverage | ocov`. --- ocov.cc | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/ocov.cc b/ocov.cc index e1f427e..c3437e3 100644 --- a/ocov.cc +++ b/ocov.cc @@ -64,11 +64,15 @@ CURRENT_STRUCT(OCOVFile) { CURRENT_FIELD(coverage, Optional); }; -CURRENT_STRUCT(OCOV) { +CURRENT_STRUCT(OPATestInput) { CURRENT_FIELD(files, (std::map)); CURRENT_FIELD(coverage, Optional); }; +CURRENT_STRUCT(OPAEvalInput) { + CURRENT_FIELD(coverage, OPATestInput); +}; + int main(int argc, char** argv) { ParseDFlags(&argc, &argv); if (FLAGS_version || FLAGS_v) { @@ -77,7 +81,19 @@ int main(int argc, char** argv) { } try { - OCOV const ocov = ParseJSON(ReadInput()); + OPATestInput const ocov = [&]() { + std::string const input = ReadInput(); + auto as_test_input = TryParseJSON(input); + if (Exists(as_test_input)) { + return Value(as_test_input); + } + auto as_eval_input = TryParseJSON(input); + if (Exists(as_eval_input)) { + return Value(as_eval_input).coverage; + } + std::cerr << red << bold << "The input should be from `opa test` or from `opa eval`." << reset << std::endl; + std::exit(1); + }(); bool first = true; for (auto const& file : ocov.files) { if (!first) { From 1b9ea36c2f7196422e5f64e4ac3ecbdc05fac097 Mon Sep 17 00:00:00 2001 From: Dima Korolev Date: Thu, 6 Apr 2023 20:47:21 +0800 Subject: [PATCH 2/2] Printing `total {test|eval} coverage`, not just `total test coverage` all the time. --- ocov.cc | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/ocov.cc b/ocov.cc index c3437e3..f1d6a33 100644 --- a/ocov.cc +++ b/ocov.cc @@ -81,19 +81,24 @@ int main(int argc, char** argv) { } try { - OPATestInput const ocov = [&]() { - std::string const input = ReadInput(); - auto as_test_input = TryParseJSON(input); - if (Exists(as_test_input)) { - return Value(as_test_input); - } + std::string const input = ReadInput(); + OPATestInput ocov; + std::string test_or_eval; + auto as_test_input = TryParseJSON(input); + if (Exists(as_test_input)) { + test_or_eval = "test"; + ocov = std::move(Value(as_test_input)); + } else { auto as_eval_input = TryParseJSON(input); if (Exists(as_eval_input)) { - return Value(as_eval_input).coverage; + test_or_eval = "eval"; + ocov = std::move(Value(as_eval_input).coverage); } + } + if (test_or_eval.empty()) { std::cerr << red << bold << "The input should be from `opa test` or from `opa eval`." << reset << std::endl; std::exit(1); - }(); + } bool first = true; for (auto const& file : ocov.files) { if (!first) { @@ -103,8 +108,8 @@ int main(int argc, char** argv) { } std::cout << bold << "# " << blue << file.first << reset << std::endl; if (Exists(file.second.coverage)) { - std::cout << bold << "# " << yellow << "file test coverage: " << Value(file.second.coverage) << '%' << reset - << std::endl; + std::cout << bold << "# " << yellow << "file " << test_or_eval + << " coverage: " << Value(file.second.coverage) << '%' << reset << std::endl; } try { std::string const filename = current::FileSystem::JoinPath(FLAGS_basedir, file.first); @@ -163,8 +168,8 @@ int main(int argc, char** argv) { } if (Exists(ocov.coverage)) { std::cout << std::endl - << bold << "# " << yellow << "total test coverage: " << Value(ocov.coverage) << '%' << reset - << std::endl; + << bold << "# " << yellow << "total " << test_or_eval + << " coverage: " << Value(ocov.coverage) << '%' << reset << std::endl; } return 0; } catch (current::Exception const& e) {