mirror of
https://github.com/denoland/deno.git
synced 2025-01-21 21:50:00 -05:00
feat(unstable): collect coverage from the run command (#8893)
This adds implicit coverage collection to the run command when a coverage collection directory is set (via an environment variable).
This commit is contained in:
parent
c98c487fda
commit
091059450e
5 changed files with 70 additions and 0 deletions
20
cli/main.rs
20
cli/main.rs
|
@ -948,11 +948,31 @@ async fn run_command(flags: Flags, script: String) -> Result<(), AnyError> {
|
||||||
let permissions = Permissions::from_options(&flags.clone().into());
|
let permissions = Permissions::from_options(&flags.clone().into());
|
||||||
let mut worker =
|
let mut worker =
|
||||||
create_main_worker(&program_state, main_module.clone(), permissions);
|
create_main_worker(&program_state, main_module.clone(), permissions);
|
||||||
|
|
||||||
|
let mut maybe_coverage_collector =
|
||||||
|
if let Some(ref coverage_dir) = program_state.coverage_dir {
|
||||||
|
let session = worker.create_inspector_session();
|
||||||
|
|
||||||
|
let coverage_dir = PathBuf::from(coverage_dir);
|
||||||
|
let mut coverage_collector =
|
||||||
|
tools::coverage::CoverageCollector::new(coverage_dir, session);
|
||||||
|
coverage_collector.start_collecting().await?;
|
||||||
|
|
||||||
|
Some(coverage_collector)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
debug!("main_module {}", main_module);
|
debug!("main_module {}", main_module);
|
||||||
worker.execute_module(&main_module).await?;
|
worker.execute_module(&main_module).await?;
|
||||||
worker.execute("window.dispatchEvent(new Event('load'))")?;
|
worker.execute("window.dispatchEvent(new Event('load'))")?;
|
||||||
worker.run_event_loop().await?;
|
worker.run_event_loop().await?;
|
||||||
worker.execute("window.dispatchEvent(new Event('unload'))")?;
|
worker.execute("window.dispatchEvent(new Event('unload'))")?;
|
||||||
|
|
||||||
|
if let Some(coverage_collector) = maybe_coverage_collector.as_mut() {
|
||||||
|
coverage_collector.stop_collecting().await?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3322,6 +3322,12 @@ itest!(deno_test_run_test_coverage {
|
||||||
exit_code: 0,
|
exit_code: 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
itest!(deno_test_run_run_coverage {
|
||||||
|
args: "test --allow-all --coverage --unstable test_run_run_coverage.ts",
|
||||||
|
output: "test_run_run_coverage.out",
|
||||||
|
exit_code: 0,
|
||||||
|
});
|
||||||
|
|
||||||
itest!(deno_lint {
|
itest!(deno_lint {
|
||||||
args: "lint --unstable lint/file1.js lint/file2.ts lint/ignored_file.ts",
|
args: "lint --unstable lint/file1.js lint/file2.ts lint/ignored_file.ts",
|
||||||
output: "lint/expected.out",
|
output: "lint/expected.out",
|
||||||
|
|
3
cli/tests/run_coverage.ts
Normal file
3
cli/tests/run_coverage.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import { returnsFoo2 } from "./subdir/mod1.ts";
|
||||||
|
|
||||||
|
returnsFoo2();
|
27
cli/tests/test_run_run_coverage.out
Normal file
27
cli/tests/test_run_run_coverage.out
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
Check [WILDCARD]/$deno$test.ts
|
||||||
|
running 1 tests
|
||||||
|
test spawn test ... Check [WILDCARD]/run_coverage.ts
|
||||||
|
ok ([WILDCARD])
|
||||||
|
|
||||||
|
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out ([WILDCARD])
|
||||||
|
|
||||||
|
cover [WILDCARD]/tests/run_coverage.ts ... 100.000% (3/3)
|
||||||
|
cover [WILDCARD]/tests/subdir/mod1.ts ... 35.714% (5/14)
|
||||||
|
2 | export function returnsHi() {
|
||||||
|
3 | return "Hi";
|
||||||
|
4 | }
|
||||||
|
-----|-----
|
||||||
|
8 | export function printHello3() {
|
||||||
|
9 | printHello2();
|
||||||
|
10 | }
|
||||||
|
11 | export function throwsError() {
|
||||||
|
12 | throw Error("exception from mod1");
|
||||||
|
13 | }
|
||||||
|
cover [WILDCARD]/tests/subdir/print_hello.ts ... 25.000% (1/4)
|
||||||
|
1 | export function printHello() {
|
||||||
|
2 | console.log("Hello");
|
||||||
|
3 | }
|
||||||
|
cover [WILDCARD]/tests/subdir/subdir2/mod2.ts ... 62.500% (5/8)
|
||||||
|
5 | export function printHello2() {
|
||||||
|
6 | printHello();
|
||||||
|
7 | }
|
14
cli/tests/test_run_run_coverage.ts
Normal file
14
cli/tests/test_run_run_coverage.ts
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
Deno.test("spawn test", async function () {
|
||||||
|
const process = Deno.run({
|
||||||
|
cmd: [
|
||||||
|
Deno.execPath(),
|
||||||
|
"run",
|
||||||
|
"--allow-all",
|
||||||
|
"--unstable",
|
||||||
|
"run_coverage.ts",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
await process.status();
|
||||||
|
process.close();
|
||||||
|
});
|
Loading…
Add table
Reference in a new issue