mirror of
https://github.com/denoland/deno.git
synced 2024-11-24 15:19:26 -05:00
feat(cli/test): deno test --clean
(#23519)
The result of the call is ignored as it throws even when the directory does not exist. Closes #23491
This commit is contained in:
parent
fab4c1776b
commit
71375491d1
7 changed files with 62 additions and 1 deletions
|
@ -333,6 +333,7 @@ pub struct TestFlags {
|
||||||
pub doc: bool,
|
pub doc: bool,
|
||||||
pub no_run: bool,
|
pub no_run: bool,
|
||||||
pub coverage_dir: Option<String>,
|
pub coverage_dir: Option<String>,
|
||||||
|
pub clean: bool,
|
||||||
pub fail_fast: Option<NonZeroUsize>,
|
pub fail_fast: Option<NonZeroUsize>,
|
||||||
pub files: FileFlags,
|
pub files: FileFlags,
|
||||||
pub allow_none: bool,
|
pub allow_none: bool,
|
||||||
|
@ -2630,6 +2631,14 @@ Directory arguments are expanded to all contained files matching the glob
|
||||||
.conflicts_with("inspect-brk")
|
.conflicts_with("inspect-brk")
|
||||||
.help("Collect coverage profile data into DIR. If DIR is not specified, it uses 'coverage/'."),
|
.help("Collect coverage profile data into DIR. If DIR is not specified, it uses 'coverage/'."),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::new("clean")
|
||||||
|
.long("clean")
|
||||||
|
.help("Empty the temporary coverage profile data directory before running tests.
|
||||||
|
|
||||||
|
Note: running multiple `deno test --clean` calls in series or parallel for the same coverage directory may cause race conditions.")
|
||||||
|
.action(ArgAction::SetTrue),
|
||||||
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("parallel")
|
Arg::new("parallel")
|
||||||
.long("parallel")
|
.long("parallel")
|
||||||
|
@ -4240,6 +4249,7 @@ fn test_parse(flags: &mut Flags, matches: &mut ArgMatches) {
|
||||||
let doc = matches.get_flag("doc");
|
let doc = matches.get_flag("doc");
|
||||||
let allow_none = matches.get_flag("allow-none");
|
let allow_none = matches.get_flag("allow-none");
|
||||||
let filter = matches.remove_one::<String>("filter");
|
let filter = matches.remove_one::<String>("filter");
|
||||||
|
let clean = matches.get_flag("clean");
|
||||||
|
|
||||||
let fail_fast = if matches.contains_id("fail-fast") {
|
let fail_fast = if matches.contains_id("fail-fast") {
|
||||||
Some(
|
Some(
|
||||||
|
@ -4325,6 +4335,7 @@ fn test_parse(flags: &mut Flags, matches: &mut ArgMatches) {
|
||||||
no_run,
|
no_run,
|
||||||
doc,
|
doc,
|
||||||
coverage_dir: matches.remove_one::<String>("coverage"),
|
coverage_dir: matches.remove_one::<String>("coverage"),
|
||||||
|
clean,
|
||||||
fail_fast,
|
fail_fast,
|
||||||
files: FileFlags { include, ignore },
|
files: FileFlags { include, ignore },
|
||||||
filter,
|
filter,
|
||||||
|
@ -8189,7 +8200,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_with_flags() {
|
fn test_with_flags() {
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
let r = flags_from_vec(svec!["deno", "test", "--unstable", "--no-npm", "--no-remote", "--trace-leaks", "--no-run", "--filter", "- foo", "--coverage=cov", "--location", "https:foo", "--allow-net", "--allow-none", "dir1/", "dir2/", "--", "arg1", "arg2"]);
|
let r = flags_from_vec(svec!["deno", "test", "--unstable", "--no-npm", "--no-remote", "--trace-leaks", "--no-run", "--filter", "- foo", "--coverage=cov", "--clean", "--location", "https:foo", "--allow-net", "--allow-none", "dir1/", "dir2/", "--", "arg1", "arg2"]);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
r.unwrap(),
|
r.unwrap(),
|
||||||
Flags {
|
Flags {
|
||||||
|
@ -8207,6 +8218,7 @@ mod tests {
|
||||||
concurrent_jobs: None,
|
concurrent_jobs: None,
|
||||||
trace_leaks: true,
|
trace_leaks: true,
|
||||||
coverage_dir: Some("cov".to_string()),
|
coverage_dir: Some("cov".to_string()),
|
||||||
|
clean: true,
|
||||||
watch: Default::default(),
|
watch: Default::default(),
|
||||||
reporter: Default::default(),
|
reporter: Default::default(),
|
||||||
junit_path: None,
|
junit_path: None,
|
||||||
|
@ -8294,6 +8306,7 @@ mod tests {
|
||||||
concurrent_jobs: Some(NonZeroUsize::new(4).unwrap()),
|
concurrent_jobs: Some(NonZeroUsize::new(4).unwrap()),
|
||||||
trace_leaks: false,
|
trace_leaks: false,
|
||||||
coverage_dir: None,
|
coverage_dir: None,
|
||||||
|
clean: false,
|
||||||
watch: Default::default(),
|
watch: Default::default(),
|
||||||
junit_path: None,
|
junit_path: None,
|
||||||
}),
|
}),
|
||||||
|
@ -8330,6 +8343,7 @@ mod tests {
|
||||||
concurrent_jobs: None,
|
concurrent_jobs: None,
|
||||||
trace_leaks: false,
|
trace_leaks: false,
|
||||||
coverage_dir: None,
|
coverage_dir: None,
|
||||||
|
clean: false,
|
||||||
watch: Default::default(),
|
watch: Default::default(),
|
||||||
reporter: Default::default(),
|
reporter: Default::default(),
|
||||||
junit_path: None,
|
junit_path: None,
|
||||||
|
@ -8371,6 +8385,7 @@ mod tests {
|
||||||
concurrent_jobs: None,
|
concurrent_jobs: None,
|
||||||
trace_leaks: false,
|
trace_leaks: false,
|
||||||
coverage_dir: None,
|
coverage_dir: None,
|
||||||
|
clean: false,
|
||||||
watch: Default::default(),
|
watch: Default::default(),
|
||||||
reporter: Default::default(),
|
reporter: Default::default(),
|
||||||
junit_path: None,
|
junit_path: None,
|
||||||
|
@ -8506,6 +8521,7 @@ mod tests {
|
||||||
concurrent_jobs: None,
|
concurrent_jobs: None,
|
||||||
trace_leaks: false,
|
trace_leaks: false,
|
||||||
coverage_dir: None,
|
coverage_dir: None,
|
||||||
|
clean: false,
|
||||||
watch: Default::default(),
|
watch: Default::default(),
|
||||||
reporter: Default::default(),
|
reporter: Default::default(),
|
||||||
junit_path: None,
|
junit_path: None,
|
||||||
|
@ -8540,6 +8556,7 @@ mod tests {
|
||||||
concurrent_jobs: None,
|
concurrent_jobs: None,
|
||||||
trace_leaks: false,
|
trace_leaks: false,
|
||||||
coverage_dir: None,
|
coverage_dir: None,
|
||||||
|
clean: false,
|
||||||
watch: Some(Default::default()),
|
watch: Some(Default::default()),
|
||||||
reporter: Default::default(),
|
reporter: Default::default(),
|
||||||
junit_path: None,
|
junit_path: None,
|
||||||
|
@ -8573,6 +8590,7 @@ mod tests {
|
||||||
concurrent_jobs: None,
|
concurrent_jobs: None,
|
||||||
trace_leaks: false,
|
trace_leaks: false,
|
||||||
coverage_dir: None,
|
coverage_dir: None,
|
||||||
|
clean: false,
|
||||||
watch: Some(Default::default()),
|
watch: Some(Default::default()),
|
||||||
reporter: Default::default(),
|
reporter: Default::default(),
|
||||||
junit_path: None,
|
junit_path: None,
|
||||||
|
@ -8608,6 +8626,7 @@ mod tests {
|
||||||
concurrent_jobs: None,
|
concurrent_jobs: None,
|
||||||
trace_leaks: false,
|
trace_leaks: false,
|
||||||
coverage_dir: None,
|
coverage_dir: None,
|
||||||
|
clean: false,
|
||||||
watch: Some(WatchFlags {
|
watch: Some(WatchFlags {
|
||||||
hmr: false,
|
hmr: false,
|
||||||
no_clear_screen: true,
|
no_clear_screen: true,
|
||||||
|
|
|
@ -191,6 +191,9 @@ async fn run_subcommand(flags: Flags) -> Result<i32, AnyError> {
|
||||||
DenoSubcommand::Test(test_flags) => {
|
DenoSubcommand::Test(test_flags) => {
|
||||||
spawn_subcommand(async {
|
spawn_subcommand(async {
|
||||||
if let Some(ref coverage_dir) = test_flags.coverage_dir {
|
if let Some(ref coverage_dir) = test_flags.coverage_dir {
|
||||||
|
if test_flags.clean {
|
||||||
|
let _ = std::fs::remove_dir_all(coverage_dir);
|
||||||
|
}
|
||||||
std::fs::create_dir_all(coverage_dir)
|
std::fs::create_dir_all(coverage_dir)
|
||||||
.with_context(|| format!("Failed creating: {coverage_dir}"))?;
|
.with_context(|| format!("Failed creating: {coverage_dir}"))?;
|
||||||
// this is set in order to ensure spawned processes use the same
|
// this is set in order to ensure spawned processes use the same
|
||||||
|
|
5
tests/specs/test/clean_flag/__test__.jsonc
Normal file
5
tests/specs/test/clean_flag/__test__.jsonc
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"args": "run -A main.js",
|
||||||
|
"exitCode": 0,
|
||||||
|
"output": "main.out"
|
||||||
|
}
|
23
tests/specs/test/clean_flag/main.js
Normal file
23
tests/specs/test/clean_flag/main.js
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
import { emptyDir } from "../../../util/std/fs/empty_dir.ts";
|
||||||
|
|
||||||
|
const DIR = "./coverage";
|
||||||
|
const COMMAND = new Deno.Command(Deno.execPath(), {
|
||||||
|
args: ["test", "--coverage", "--clean"],
|
||||||
|
stdout: "null",
|
||||||
|
});
|
||||||
|
|
||||||
|
async function getCoverageFiles() {
|
||||||
|
return await Array.fromAsync(Deno.readDir(DIR), ({ name }) => name);
|
||||||
|
}
|
||||||
|
|
||||||
|
await emptyDir(DIR);
|
||||||
|
await COMMAND.output();
|
||||||
|
const files1 = new Set(await getCoverageFiles());
|
||||||
|
|
||||||
|
await COMMAND.output();
|
||||||
|
const files2 = new Set(await getCoverageFiles());
|
||||||
|
|
||||||
|
console.log(files1.size === files2.size);
|
||||||
|
console.log(files1.intersection(files2).size === 0);
|
||||||
|
await emptyDir(DIR);
|
||||||
|
await Deno.remove(DIR);
|
2
tests/specs/test/clean_flag/main.out
Normal file
2
tests/specs/test/clean_flag/main.out
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
true
|
||||||
|
true
|
3
tests/specs/test/clean_flag/sum.js
Normal file
3
tests/specs/test/clean_flag/sum.js
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
export function sum(a, b) {
|
||||||
|
return a + b;
|
||||||
|
}
|
6
tests/specs/test/clean_flag/sum_test.js
Normal file
6
tests/specs/test/clean_flag/sum_test.js
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
import { sum } from "./sum.js";
|
||||||
|
import { assertEquals } from "../../../util/std/assert/assert_equals.ts";
|
||||||
|
|
||||||
|
Deno.test("sum()", () => {
|
||||||
|
assertEquals(sum(1, 2), 3);
|
||||||
|
});
|
Loading…
Reference in a new issue