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

feat(cli/test): deno test --clean #23519

Merged
merged 7 commits into from
May 23, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ pub struct TestFlags {
pub doc: bool,
pub no_run: bool,
pub coverage_dir: Option<String>,
pub clean: bool,
pub fail_fast: Option<NonZeroUsize>,
pub files: FileFlags,
pub allow_none: bool,
Expand Down Expand Up @@ -2630,6 +2631,14 @@ Directory arguments are expanded to all contained files matching the glob
.conflicts_with("inspect-brk")
.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::new("parallel")
.long("parallel")
Expand Down Expand Up @@ -4240,6 +4249,7 @@ fn test_parse(flags: &mut Flags, matches: &mut ArgMatches) {
let doc = matches.get_flag("doc");
let allow_none = matches.get_flag("allow-none");
let filter = matches.remove_one::<String>("filter");
let clean = matches.get_flag("clean");

let fail_fast = if matches.contains_id("fail-fast") {
Some(
Expand Down Expand Up @@ -4325,6 +4335,7 @@ fn test_parse(flags: &mut Flags, matches: &mut ArgMatches) {
no_run,
doc,
coverage_dir: matches.remove_one::<String>("coverage"),
clean,
fail_fast,
files: FileFlags { include, ignore },
filter,
Expand Down Expand Up @@ -8189,7 +8200,7 @@ mod tests {
#[test]
fn test_with_flags() {
#[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!(
r.unwrap(),
Flags {
Expand All @@ -8207,6 +8218,7 @@ mod tests {
concurrent_jobs: None,
trace_leaks: true,
coverage_dir: Some("cov".to_string()),
clean: true,
watch: Default::default(),
reporter: Default::default(),
junit_path: None,
Expand Down Expand Up @@ -8294,6 +8306,7 @@ mod tests {
concurrent_jobs: Some(NonZeroUsize::new(4).unwrap()),
trace_leaks: false,
coverage_dir: None,
clean: false,
watch: Default::default(),
junit_path: None,
}),
Expand Down Expand Up @@ -8330,6 +8343,7 @@ mod tests {
concurrent_jobs: None,
trace_leaks: false,
coverage_dir: None,
clean: false,
watch: Default::default(),
reporter: Default::default(),
junit_path: None,
Expand Down Expand Up @@ -8371,6 +8385,7 @@ mod tests {
concurrent_jobs: None,
trace_leaks: false,
coverage_dir: None,
clean: false,
watch: Default::default(),
reporter: Default::default(),
junit_path: None,
Expand Down Expand Up @@ -8506,6 +8521,7 @@ mod tests {
concurrent_jobs: None,
trace_leaks: false,
coverage_dir: None,
clean: false,
watch: Default::default(),
reporter: Default::default(),
junit_path: None,
Expand Down Expand Up @@ -8540,6 +8556,7 @@ mod tests {
concurrent_jobs: None,
trace_leaks: false,
coverage_dir: None,
clean: false,
watch: Some(Default::default()),
reporter: Default::default(),
junit_path: None,
Expand Down Expand Up @@ -8573,6 +8590,7 @@ mod tests {
concurrent_jobs: None,
trace_leaks: false,
coverage_dir: None,
clean: false,
watch: Some(Default::default()),
reporter: Default::default(),
junit_path: None,
Expand Down Expand Up @@ -8608,6 +8626,7 @@ mod tests {
concurrent_jobs: None,
trace_leaks: false,
coverage_dir: None,
clean: false,
watch: Some(WatchFlags {
hmr: false,
no_clear_screen: true,
Expand Down
3 changes: 3 additions & 0 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ async fn run_subcommand(flags: Flags) -> Result<i32, AnyError> {
DenoSubcommand::Test(test_flags) => {
spawn_subcommand(async {
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)
.with_context(|| format!("Failed creating: {coverage_dir}"))?;
// this is set in order to ensure spawned processes use the same
Expand Down
5 changes: 5 additions & 0 deletions tests/specs/test/clean_flag/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"args": "run -A main.js",
"exitCode": 0,
"output": "main.out"
}
23 changes: 23 additions & 0 deletions tests/specs/test/clean_flag/main.js
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions tests/specs/test/clean_flag/main.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
true
true
3 changes: 3 additions & 0 deletions tests/specs/test/clean_flag/sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function sum(a, b) {
return a + b;
}
6 changes: 6 additions & 0 deletions tests/specs/test/clean_flag/sum_test.js
Original file line number Diff line number Diff line change
@@ -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);
});