From 413f79a4941d266ca1e6b61de80f83b16f6a4a4e Mon Sep 17 00:00:00 2001 From: Casper Beyer Date: Wed, 20 Jan 2021 22:51:36 +0800 Subject: [PATCH] fix(coverage): ignore comments (#8639) This commit fixes coverage collection by ignoring comments when tallying up line counts. --- cli/tests/integration_tests.rs | 6 ++++++ cli/tests/subdir/comment.ts | 4 ++++ cli/tests/test_comment_coverage.out | 7 +++++++ cli/tests/test_comment_coverage.ts | 5 +++++ cli/tools/coverage.rs | 24 +++++++++++++++++++++++- 5 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 cli/tests/subdir/comment.ts create mode 100644 cli/tests/test_comment_coverage.out create mode 100644 cli/tests/test_comment_coverage.ts diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index 80a7222e53..be7bb63c52 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -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", diff --git a/cli/tests/subdir/comment.ts b/cli/tests/subdir/comment.ts new file mode 100644 index 0000000000..ea7e630c0e --- /dev/null +++ b/cli/tests/subdir/comment.ts @@ -0,0 +1,4 @@ +// This is a comment. +export function comment(): string { + return "comment"; +} diff --git a/cli/tests/test_comment_coverage.out b/cli/tests/test_comment_coverage.out new file mode 100644 index 0000000000..582152fa18 --- /dev/null +++ b/cli/tests/test_comment_coverage.out @@ -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) diff --git a/cli/tests/test_comment_coverage.ts b/cli/tests/test_comment_coverage.ts new file mode 100644 index 0000000000..28a25c65d7 --- /dev/null +++ b/cli/tests/test_comment_coverage.ts @@ -0,0 +1,5 @@ +import { comment } from "./subdir/comment.ts"; + +Deno.test("comment", function () { + comment(); +}); diff --git a/cli/tools/coverage.rs b/cli/tools/coverage.rs index 615022b743..e3092975fb 100644 --- a/cli/tools/coverage.rs +++ b/cli/tools/coverage.rs @@ -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::>(); + let mut ignored_spans: Vec = 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::>(); let mut covered_lines: Vec = Vec::new(); let mut uncovered_lines: Vec = 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 {