-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocov.cc
180 lines (165 loc) · 6.41 KB
/
ocov.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <unistd.h>
#include <stdio.h>
#include "current/bricks/dflags/dflags.h"
#include "current/bricks/file/file.h"
#include "current/typesystem/serialization/json.h"
#include "current/blocks/xterm/vt100.h"
#include "current_build.h"
DEFINE_bool(version, false, "Dump version details.");
DEFINE_bool(v, false, "Dump version details.");
DEFINE_string(input, "", "The input file to read the coverage JSON from instead of the standard input.");
DEFINE_string(basedir, "", "The base directory to prefix the covered files paths with.");
using namespace current::vt100;
inline void DumpVersion() {
using namespace current::build;
std::cout << bold << "OCOV" << reset << ", built " << green << kBuildDateTime << reset << std::endl;
std::cout << "Branch " << magenta << kGitBranch << reset << ", commit " << blue << kGitCommit << reset << std::endl;
#ifndef NDEBUG
std::cout << red << bold << "Warning" << reset << ": unoptimized build";
#if defined(CURRENT_POSIX) || defined(CURRENT_APPLE)
std::cout << ", run `" << cyan << "NDEBUG=1 make clean all" << reset << '`';
#endif
std::cout << '.' << std::endl;
#endif // NDEBUG
}
inline void SynopsisAndExit(std::string const& error = "") {
if (!error.empty()) {
std::cerr << red << bold << "Error: " << reset << error << std::endl;
}
std::cerr << bold << blue << "Synopsis" << reset << ": " << cyan << "opa test ... --coverage | ocov"
<< reset << ", or " << cyan << "ocov --input coverage.json" << reset << '.' << std::endl;
std::exit(-1);
}
inline std::string ReadInput() {
if (FLAGS_input.empty()) {
if (isatty(fileno(stdin))) {
SynopsisAndExit("The input is a TTY, not a pipe.");
} else {
return std::string(std::istreambuf_iterator<char>(std::cin), std::istreambuf_iterator<char>());
}
} else {
return current::FileSystem::ReadFileAsString(FLAGS_input);
}
return "";
}
CURRENT_STRUCT(OCOVRow) { CURRENT_FIELD(row, uint32_t, 0u); };
CURRENT_STRUCT(OCOVBeginEnd) {
CURRENT_FIELD(start, OCOVRow);
CURRENT_FIELD(end, OCOVRow);
};
CURRENT_STRUCT(OCOVFile) {
CURRENT_FIELD(covered, Optional<std::vector<OCOVBeginEnd>>);
CURRENT_FIELD(not_covered, Optional<std::vector<OCOVBeginEnd>>);
CURRENT_FIELD(coverage, Optional<double>);
};
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) {
DumpVersion();
return 0;
}
try {
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) {
std::cout << std::endl;
} else {
first = false;
}
std::cout << bold << "# " << blue << file.first << reset << std::endl;
if (Exists(file.second.coverage)) {
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);
enum class Type : uint8_t { Covered = 0, NotCovered = 1 };
struct RowMarker final {
std::pair<uint32_t, bool> key; // { row, false = start, true = end }.
Type type;
explicit RowMarker(uint32_t row = 0u, bool start = false, Type type = Type::Covered)
: key(row, !start), type(type) {}
bool operator<(RowMarker const& rhs) const { return key < rhs.key; }
};
std::vector<RowMarker> markers;
if (Exists(file.second.covered)) {
for (auto const& e : Value(file.second.covered)) {
markers.emplace_back(e.start.row, true, Type::Covered);
markers.emplace_back(e.end.row, false, Type::Covered);
}
}
if (Exists(file.second.not_covered)) {
for (auto const& e : Value(file.second.not_covered)) {
markers.emplace_back(e.start.row, true, Type::NotCovered);
markers.emplace_back(e.end.row, false, Type::NotCovered);
}
}
std::sort(std::begin(markers), std::end(markers));
std::pair<uint32_t, bool> key(0u, false);
size_t index = 0u;
bool status[2] = {false, false};
current::FileSystem::ReadFileByLines(filename, [&key, &index, &markers, &status](std::string const& loc) {
++key.first;
key.second = false;
while (index < markers.size() && markers[index].key <= key) {
status[static_cast<uint8_t>(markers[index].type)] = !markers[index].key.second;
++index;
}
bool need_reset = false;
if (status[0] && !status[1]) {
std::cout << green;
need_reset = true;
} else if (status[1] && !status[0]) {
std::cout << red;
need_reset = true;
} else if (status[0] && status[1]) {
std::cout << magenta;
need_reset = true;
}
std::cout << loc;
if (need_reset) {
std::cout << reset;
}
std::cout << std::endl;
});
} catch (current::FileException const& e) {
std::cout << bold << red << "# This file can not be read." << reset << std::endl;
}
}
if (Exists(ocov.coverage)) {
std::cout << std::endl
<< bold << "# " << yellow << "total " << test_or_eval
<< " coverage: " << Value(ocov.coverage) << '%' << reset << std::endl;
}
return 0;
} catch (current::Exception const& e) {
std::cerr << red << bold << "Exception" << reset << ": " << e.Caller() << std::endl;
std::cerr << e.OriginalDescription() << std::endl;
return -1;
}
}