1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(coverage): ignore files from npm registry (#18457)

Fixes https://github.com/denoland/deno/issues/17664 and part of
https://github.com/denoland/deno/issues/18454 by excluding files
belonging to npm modules by default in the coverage output.
This commit is contained in:
Geert-Jan Zwiers 2023-03-30 19:40:22 +02:00 committed by GitHub
parent 30ee846588
commit 206c593519
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 65 additions and 0 deletions

View file

@ -74,6 +74,10 @@ impl NpmPackageResolver {
}
}
pub fn root_dir_url(&self) -> &Url {
self.fs_resolver.root_dir_url()
}
pub fn resolve_pkg_id_from_pkg_req(
&self,
req: &NpmPackageReq,

View file

@ -277,6 +277,54 @@ fn no_snaps_included(test_name: &str, extension: &str) {
output.assert_exit_code(0);
}
#[test]
fn no_npm_cache_coverage() {
let context = TestContext::default();
let tempdir = context.deno_dir();
let tempdir = tempdir.path().join("cov");
let output = context
.new_command()
.args_vec(vec![
"test".to_string(),
"--quiet".to_string(),
"--allow-read".to_string(),
format!("--coverage={}", tempdir.to_str().unwrap()),
format!("coverage/no_npm_coverage/no_npm_coverage_test.ts"),
])
.run();
output.assert_exit_code(0);
output.skip_output_check();
let output = context
.new_command()
.args_vec(vec![
"coverage".to_string(),
format!("{}/", tempdir.to_str().unwrap()),
])
.split_output()
.run();
// Verify there's no "Check" being printed
assert!(output.stderr().is_empty());
let actual = util::strip_ansi_codes(output.stdout()).to_string();
let expected = fs::read_to_string(
util::testdata_path().join("coverage/no_npm_coverage/expected.out"),
)
.unwrap();
if !util::wildcard_match(&expected, &actual) {
println!("OUTPUT\n{actual}\nOUTPUT");
println!("EXPECTED\n{expected}\nEXPECTED");
panic!("pattern match failed");
}
output.assert_exit_code(0);
}
#[test]
fn no_transpiled_lines() {
let context = TestContext::default();

View file

@ -0,0 +1 @@
cover [WILDCARD]/no_npm_coverage/no_npm_coverage.ts ... 100.000% (4/4)

View file

@ -0,0 +1,4 @@
import chalk from "npm:chalk";
export function main() {
console.log(chalk.red("RED"));
}

View file

@ -0,0 +1,4 @@
import { main } from "./no_npm_coverage.ts";
Deno.test("main", () => {
main();
});

View file

@ -588,6 +588,7 @@ fn filter_coverages(
coverages: Vec<ScriptCoverage>,
include: Vec<String>,
exclude: Vec<String>,
npm_root_dir: &str,
) -> Vec<ScriptCoverage> {
let include: Vec<Regex> =
include.iter().map(|e| Regex::new(e).unwrap()).collect();
@ -599,6 +600,7 @@ fn filter_coverages(
.into_iter()
.filter(|e| {
let is_internal = e.url.starts_with("ext:")
|| e.url.starts_with(npm_root_dir)
|| e.url.ends_with("__anonymous__")
|| e.url.ends_with("$deno$test.js")
|| e.url.ends_with(".snap");
@ -620,12 +622,14 @@ pub async fn cover_files(
}
let ps = ProcState::build(flags).await?;
let root_dir_url = ps.npm_resolver.root_dir_url();
let script_coverages = collect_coverages(coverage_flags.files)?;
let script_coverages = filter_coverages(
script_coverages,
coverage_flags.include,
coverage_flags.exclude,
root_dir_url.as_str(),
);
let proc_coverages: Vec<_> = script_coverages