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): ignore files from npm registry #18457

Merged
merged 18 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cli/npm/resolvers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
48 changes: 48 additions & 0 deletions cli/tests/integration/coverage_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions cli/tests/testdata/coverage/no_npm_coverage/expected.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cover [WILDCARD]/no_npm_coverage/no_npm_coverage.ts ... 100.000% (4/4)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import chalk from "npm:chalk";
export function main() {
console.log(chalk.red("RED"));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { main } from "./no_npm_coverage.ts";
Deno.test("main", () => {
main();
});
4 changes: 4 additions & 0 deletions cli/tools/coverage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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");
Expand All @@ -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
Expand Down