mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 15:06:54 -05:00
fix(coverage): ignore comments (#8639)
This commit fixes coverage collection by ignoring comments when tallying up line counts.
This commit is contained in:
parent
47263ef6fa
commit
413f79a494
5 changed files with 45 additions and 1 deletions
|
@ -3437,6 +3437,12 @@ itest!(deno_test_coverage {
|
|||
exit_code: 0,
|
||||
});
|
||||
|
||||
itest!(deno_test_comment_coverage {
|
||||
args: "test --coverage --unstable test_comment_coverage.ts",
|
||||
output: "test_comment_coverage.out",
|
||||
exit_code: 0,
|
||||
});
|
||||
|
||||
itest!(deno_test_branch_coverage {
|
||||
args: "test --coverage --unstable test_branch_coverage.ts",
|
||||
output: "test_branch_coverage.out",
|
||||
|
|
4
cli/tests/subdir/comment.ts
Normal file
4
cli/tests/subdir/comment.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
// This is a comment.
|
||||
export function comment(): string {
|
||||
return "comment";
|
||||
}
|
7
cli/tests/test_comment_coverage.out
Normal file
7
cli/tests/test_comment_coverage.out
Normal file
|
@ -0,0 +1,7 @@
|
|||
[WILDCARD]/tests/$deno$test.ts
|
||||
running 1 tests
|
||||
test comment ... ok ([WILDCARD])
|
||||
|
||||
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out ([WILDCARD])
|
||||
|
||||
[WILDCARD]/tests/subdir/comment.ts ... 100.000% (4/4)
|
5
cli/tests/test_comment_coverage.ts
Normal file
5
cli/tests/test_comment_coverage.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
import { comment } from "./subdir/comment.ts";
|
||||
|
||||
Deno.test("comment", function () {
|
||||
comment();
|
||||
});
|
|
@ -1,6 +1,9 @@
|
|||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use crate::ast;
|
||||
use crate::ast::TokenOrComment;
|
||||
use crate::colors;
|
||||
use crate::media_type::MediaType;
|
||||
use crate::module_graph::TypeLib;
|
||||
use crate::program_state::ProgramState;
|
||||
use deno_core::error::AnyError;
|
||||
|
@ -15,6 +18,7 @@ use serde::Serialize;
|
|||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use swc_common::Span;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct CoverageCollector {
|
||||
|
@ -127,8 +131,16 @@ impl PrettyCoverageReporter {
|
|||
script_coverage: &ScriptCoverage,
|
||||
script_source: &str,
|
||||
) {
|
||||
let lines = script_source.split('\n').collect::<Vec<_>>();
|
||||
let mut ignored_spans: Vec<Span> = Vec::new();
|
||||
for item in ast::lex("", script_source, &MediaType::JavaScript) {
|
||||
if let TokenOrComment::Token(_) = item.inner {
|
||||
continue;
|
||||
}
|
||||
|
||||
ignored_spans.push(item.span);
|
||||
}
|
||||
|
||||
let lines = script_source.split('\n').collect::<Vec<_>>();
|
||||
let mut covered_lines: Vec<usize> = Vec::new();
|
||||
let mut uncovered_lines: Vec<usize> = Vec::new();
|
||||
|
||||
|
@ -138,6 +150,16 @@ impl PrettyCoverageReporter {
|
|||
|
||||
let mut count = 0;
|
||||
|
||||
let ignore = ignored_spans.iter().any(|span| {
|
||||
(span.lo.0 as usize) <= line_start_offset
|
||||
&& (span.hi.0 as usize) >= line_end_offset
|
||||
});
|
||||
|
||||
if ignore {
|
||||
covered_lines.push(index);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Count the hits of ranges that include the entire line which will always be at-least one
|
||||
// as long as the code has been evaluated.
|
||||
for function in &script_coverage.functions {
|
||||
|
|
Loading…
Reference in a new issue