Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supported opa eval --coverage ... | ocov in addition to opa test ... --coverage | ocov. #2

Merged
merged 2 commits into from
Apr 6, 2023
Merged
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
33 changes: 27 additions & 6 deletions ocov.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,15 @@ CURRENT_STRUCT(OCOVFile) {
CURRENT_FIELD(coverage, Optional<double>);
};

CURRENT_STRUCT(OCOV) {
CURRENT_STRUCT(OPATestInput) {
CURRENT_FIELD(files, (std::map<std::string, OCOVFile>));
CURRENT_FIELD(coverage, Optional<double>);
};

CURRENT_STRUCT(OPAEvalInput) {
CURRENT_FIELD(coverage, OPATestInput);
};

int main(int argc, char** argv) {
ParseDFlags(&argc, &argv);
if (FLAGS_version || FLAGS_v) {
Expand All @@ -77,7 +81,24 @@ int main(int argc, char** argv) {
}

try {
OCOV const ocov = ParseJSON<OCOV, JSONFormat::Minimalistic>(ReadInput());
std::string const input = ReadInput();
OPATestInput ocov;
std::string test_or_eval;
auto as_test_input = TryParseJSON<OPATestInput, JSONFormat::Minimalistic>(input);
if (Exists(as_test_input)) {
test_or_eval = "test";
ocov = std::move(Value(as_test_input));
} else {
auto as_eval_input = TryParseJSON<OPAEvalInput, JSONFormat::Minimalistic>(input);
if (Exists(as_eval_input)) {
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) {
Expand All @@ -87,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);
Expand Down Expand Up @@ -147,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) {
Expand Down