-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
coverage.rs
240 lines (202 loc) · 5.92 KB
/
coverage.rs
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use crate::colors;
use crate::inspector::DenoInspector;
use crate::inspector::InspectorSession;
use deno_core::error::AnyError;
use deno_core::serde_json;
use deno_core::serde_json::json;
use deno_core::url::Url;
use serde::Deserialize;
pub struct CoverageCollector {
session: Box<InspectorSession>,
}
impl CoverageCollector {
pub fn new(inspector_ptr: *mut DenoInspector) -> Self {
let session = InspectorSession::new(inspector_ptr);
Self { session }
}
pub async fn start_collecting(&mut self) -> Result<(), AnyError> {
self.session.post_message("Debugger.enable", None).await?;
self.session.post_message("Profiler.enable", None).await?;
self
.session
.post_message(
"Profiler.startPreciseCoverage",
Some(json!({"callCount": true, "detailed": true})),
)
.await?;
Ok(())
}
pub async fn collect(&mut self) -> Result<Vec<Coverage>, AnyError> {
let result = self
.session
.post_message("Profiler.takePreciseCoverage", None)
.await?;
let take_coverage_result: TakePreciseCoverageResult =
serde_json::from_value(result)?;
let mut coverages: Vec<Coverage> = Vec::new();
for script_coverage in take_coverage_result.result {
let result = self
.session
.post_message(
"Debugger.getScriptSource",
Some(json!({
"scriptId": script_coverage.script_id,
})),
)
.await?;
let get_script_source_result: GetScriptSourceResult =
serde_json::from_value(result)?;
coverages.push(Coverage {
script_coverage,
script_source: get_script_source_result.script_source,
})
}
Ok(coverages)
}
pub async fn stop_collecting(&mut self) -> Result<(), AnyError> {
self
.session
.post_message("Profiler.stopPreciseCoverage", None)
.await?;
self.session.post_message("Profiler.disable", None).await?;
self.session.post_message("Debugger.disable", None).await?;
Ok(())
}
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CoverageRange {
pub start_offset: usize,
pub end_offset: usize,
pub count: usize,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FunctionCoverage {
pub function_name: String,
pub ranges: Vec<CoverageRange>,
pub is_block_coverage: bool,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ScriptCoverage {
pub script_id: String,
pub url: String,
pub functions: Vec<FunctionCoverage>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Coverage {
pub script_coverage: ScriptCoverage,
pub script_source: String,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct TakePreciseCoverageResult {
result: Vec<ScriptCoverage>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetScriptSourceResult {
pub script_source: String,
pub bytecode: Option<String>,
}
pub struct PrettyCoverageReporter {
quiet: bool,
}
// TODO(caspervonb) add support for lcov output (see geninfo(1) for format spec).
impl PrettyCoverageReporter {
pub fn new(quiet: bool) -> PrettyCoverageReporter {
PrettyCoverageReporter { quiet }
}
pub fn visit_coverage(&mut self, coverage: &Coverage) {
let lines = coverage.script_source.lines().collect::<Vec<_>>();
let mut covered_lines: Vec<usize> = Vec::new();
let mut uncovered_lines: Vec<usize> = Vec::new();
let mut line_start_offset = 0;
for (index, line) in lines.iter().enumerate() {
let line_end_offset = line_start_offset + line.len();
let mut count = 0;
for function in &coverage.script_coverage.functions {
for range in &function.ranges {
if range.start_offset <= line_start_offset
&& range.end_offset >= line_end_offset
{
if range.count == 0 {
count = 0;
break;
}
count += range.count;
}
}
line_start_offset = line_end_offset;
}
if count > 0 {
covered_lines.push(index);
} else {
uncovered_lines.push(index);
}
}
if !self.quiet {
print!("cover {} ... ", coverage.script_coverage.url);
let line_coverage_ratio = covered_lines.len() as f32 / lines.len() as f32;
let line_coverage = format!(
"{:.3}% ({}/{})",
line_coverage_ratio * 100.0,
covered_lines.len(),
lines.len()
);
if line_coverage_ratio >= 0.9 {
println!("{}", colors::green(&line_coverage));
} else if line_coverage_ratio >= 0.75 {
println!("{}", colors::yellow(&line_coverage));
} else {
println!("{}", colors::red(&line_coverage));
}
for line_index in uncovered_lines {
println!(
"{:width$}{} {}",
line_index + 1,
colors::gray(" |"),
colors::red(&lines[line_index]),
width = 4
);
}
}
}
}
pub fn filter_script_coverages(
coverages: Vec<Coverage>,
test_file_url: Url,
test_modules: Vec<Url>,
) -> Vec<Coverage> {
coverages
.into_iter()
.filter(|e| {
if let Ok(url) = Url::parse(&e.script_coverage.url) {
if url.path().ends_with("__anonymous__") {
return false;
}
if url == test_file_url {
return false;
}
for test_module_url in &test_modules {
if &url == test_module_url {
return false;
}
}
if let Ok(path) = url.to_file_path() {
for test_module_url in &test_modules {
if let Ok(test_module_path) = test_module_url.to_file_path() {
if path.starts_with(test_module_path.parent().unwrap()) {
return true;
}
}
}
}
}
false
})
.collect::<Vec<Coverage>>()
}