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

fix(coverage): use only string byte indexes and 0-indexed line numbers #13190

Merged
merged 4 commits into from Dec 24, 2021

Conversation

dsherret
Copy link
Member

The coverage code was mixing character indexes and byte indexes along with 0-indexed line numbers with 1-indexed. This PR fixes that along with ignoring blank lines. There is still some work to do on coverage.

Closes #11327

@@ -81,6 +81,7 @@ shell-escape = "=0.1.5"
sourcemap = "=6.0.1"
tempfile = "=3.2.0"
text-size = "=1.1.0"
text_lines = "=0.4.1"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my repo, which is used in deno_ast and already a dependency.

let mut line_counts = Vec::with_capacity(text_lines.lines_count());
for line_index in 0..text_lines.lines_count() {
let line_start_offset = text_lines.line_start(line_index);
let line_end_offset = text_lines.line_end(line_index);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be good to improve text_lines to return an iterator of line information, but this is an O(1) lookup so not critical... just would be nicer.

&& (span.hi.0 as usize) >= line_end_offset
}) || script_source[line_start_offset..line_end_offset]
.trim()
.is_empty();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the blank line issue. I think we need to do something a bit better for determining which lines to ignore in the future though (ex. lines with only a close brace or similar token).

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, massive effort David; I got a few nitpicks and I will check this PR locally against deno_std to make sure nothing breaks.

cli/tools/coverage.rs Outdated Show resolved Hide resolved
Comment on lines 215 to 216
let url = Url::parse(&script_coverage.url).unwrap();
let file_path = url.to_file_path().unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it guaranteed that these unwraps won't fail? Why not make script_coverage.url a ModuleSpecifier?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just kept the code as-is here for the most part. I don't want to change script_coverage.url to a ModuleSpecifier for this PR (maybe a followup)... looks like it might be a filename. I will slightly improve this though.

Comment on lines +294 to +295
// TODO(caspervonb): collect uncovered ranges on the lines so that we can highlight specific
// parts of a line in color (word diff style) instead of the entire line.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment still relevant?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

Comment on lines 330 to 333
let overlaps = std::cmp::max(line_end_offset, range.end_offset)
- std::cmp::min(line_start_offset, range.start_offset)
< (line_end_offset - line_start_offset)
+ (range.end_offset - range.start_offset);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's really hard to read this :D maybe split into two separate variable bindings?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is code from before. I'm not sure why it's using lengths to do this comparison. I changed it to compare offsets and it should be the same (I think).

Comment on lines 413 to 415
impl CoverageReporter for LcovCoverageReporter {
fn report(&mut self, coverage_report: &CoverageReport, _file_text: &str) {
println!("SF:{}", coverage_report.file_path);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very nice cleanup

@bartlomieju
Copy link
Member

I will check this PR locally against deno_std to make sure nothing breaks.

Verified, that everything works correctly.

@dsherret dsherret merged commit 7ebbda7 into denoland:main Dec 24, 2021
@dsherret dsherret deleted the coverage_fix_byte_ branch December 24, 2021 01:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

A final blankline in a file prevents 100% code coverage
2 participants