mirror of
https://github.com/denoland/deno.git
synced 2024-10-29 08:58:01 -04:00
fix: exclude internal JS files from coverage (#20448)
This commit is contained in:
parent
4460336fda
commit
9d1385896f
4 changed files with 87 additions and 0 deletions
|
@ -1,8 +1,10 @@
|
|||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use deno_core::serde_json;
|
||||
use std::fs;
|
||||
use test_util as util;
|
||||
use test_util::TempDir;
|
||||
use util::assert_starts_with;
|
||||
use util::env_vars_for_npm_tests;
|
||||
use util::TestContext;
|
||||
use util::TestContextBuilder;
|
||||
|
@ -439,3 +441,66 @@ fn no_transpiled_lines() {
|
|||
|
||||
output.assert_exit_code(0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_internal_code() {
|
||||
let context = TestContext::default();
|
||||
let tempdir = context.temp_dir();
|
||||
let tempdir = tempdir.path().join("cov");
|
||||
|
||||
let output = context
|
||||
.new_command()
|
||||
.args_vec(vec![
|
||||
"test".to_string(),
|
||||
"--quiet".to_string(),
|
||||
format!("--coverage={}", tempdir),
|
||||
"coverage/no_internal_code_test.ts".to_string(),
|
||||
])
|
||||
.run();
|
||||
|
||||
output.assert_exit_code(0);
|
||||
output.skip_output_check();
|
||||
|
||||
// Check that coverage files contain no internal urls
|
||||
let paths = fs::read_dir(tempdir).unwrap();
|
||||
for path in paths {
|
||||
let unwrapped = path.unwrap().path();
|
||||
let data = fs::read_to_string(&unwrapped.clone()).unwrap();
|
||||
|
||||
let value: serde_json::Value = serde_json::from_str(&data).unwrap();
|
||||
let url = value["url"].as_str().unwrap();
|
||||
assert_starts_with!(url, "file:");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_internal_node_code() {
|
||||
let context = TestContext::default();
|
||||
let tempdir = context.temp_dir();
|
||||
let tempdir = tempdir.path().join("cov");
|
||||
|
||||
let output = context
|
||||
.new_command()
|
||||
.args_vec(vec![
|
||||
"test".to_string(),
|
||||
"--quiet".to_string(),
|
||||
"--no-check".to_string(),
|
||||
format!("--coverage={}", tempdir),
|
||||
"coverage/no_internal_node_code_test.ts".to_string(),
|
||||
])
|
||||
.run();
|
||||
|
||||
output.assert_exit_code(0);
|
||||
output.skip_output_check();
|
||||
|
||||
// Check that coverage files contain no internal urls
|
||||
let paths = fs::read_dir(tempdir).unwrap();
|
||||
for path in paths {
|
||||
let unwrapped = path.unwrap().path();
|
||||
let data = fs::read_to_string(&unwrapped.clone()).unwrap();
|
||||
|
||||
let value: serde_json::Value = serde_json::from_str(&data).unwrap();
|
||||
let url = value["url"].as_str().unwrap();
|
||||
assert_starts_with!(url, "file:");
|
||||
}
|
||||
}
|
||||
|
|
7
cli/tests/testdata/coverage/no_internal_code_test.ts
vendored
Normal file
7
cli/tests/testdata/coverage/no_internal_code_test.ts
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
const add = (a: number, b: number) => a + b;
|
||||
|
||||
Deno.test(function addTest() {
|
||||
if (add(2, 3) !== 5) {
|
||||
throw new Error("fail");
|
||||
}
|
||||
});
|
8
cli/tests/testdata/coverage/no_internal_node_code_test.ts
vendored
Normal file
8
cli/tests/testdata/coverage/no_internal_node_code_test.ts
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
import * as path from "node:path";
|
||||
|
||||
Deno.test(function test() {
|
||||
const res = path.join("foo", "bar");
|
||||
if (!res.includes("foo")) {
|
||||
throw new Error("fail");
|
||||
}
|
||||
});
|
|
@ -127,6 +127,13 @@ impl CoverageCollector {
|
|||
|
||||
let script_coverages = self.take_precise_coverage().await?.result;
|
||||
for script_coverage in script_coverages {
|
||||
// Filter out internal JS files from being included in coverage reports
|
||||
if script_coverage.url.starts_with("ext:")
|
||||
|| script_coverage.url.starts_with("[ext:")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
let filename = format!("{}.json", Uuid::new_v4());
|
||||
let filepath = self.dir.join(filename);
|
||||
|
||||
|
|
Loading…
Reference in a new issue