0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

feat: deno test --filter (#4570)

This commit is contained in:
Ryan Dahl 2020-04-02 09:26:40 -04:00 committed by GitHub
parent ff0b32f81d
commit c738797944
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 325 additions and 289 deletions

View file

@ -66,6 +66,7 @@ pub enum DenoSubcommand {
fail_fast: bool,
allow_none: bool,
include: Option<Vec<String>>,
filter: Option<String>,
},
Types,
Upgrade {
@ -535,7 +536,7 @@ fn test_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
let failfast = matches.is_present("failfast");
let allow_none = matches.is_present("allow_none");
let filter = matches.value_of("filter").map(String::from);
let include = if matches.is_present("files") {
let files: Vec<String> = matches
.values_of("files")
@ -550,6 +551,7 @@ fn test_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
flags.subcommand = DenoSubcommand::Test {
fail_fast: failfast,
include,
filter,
allow_none,
};
}
@ -564,11 +566,7 @@ fn doc_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
reload_arg_parse(flags, matches);
let source_file = matches.value_of("source_file").map(String::from).unwrap();
let json = matches.is_present("json");
let filter = if matches.is_present("filter") {
Some(matches.value_of("filter").unwrap().to_string())
} else {
None
};
let filter = matches.value_of("filter").map(String::from);
flags.subcommand = DenoSubcommand::Doc {
source_file,
json,
@ -945,7 +943,6 @@ fn test_subcommand<'a, 'b>() -> App<'a, 'b> {
run_test_args(SubCommand::with_name("test"))
.arg(
Arg::with_name("failfast")
.short("f")
.long("failfast")
.help("Stop on first error")
.takes_value(false),
@ -956,6 +953,12 @@ fn test_subcommand<'a, 'b>() -> App<'a, 'b> {
.help("Don't return error code if no test files are found")
.takes_value(false),
)
.arg(
Arg::with_name("filter")
.long("filter")
.takes_value(true)
.help("A pattern to filter the tests to run by"),
)
.arg(
Arg::with_name("files")
.help("List of file names to run")
@ -2271,6 +2274,7 @@ mod tests {
Flags {
subcommand: DenoSubcommand::Test {
fail_fast: false,
filter: None,
allow_none: true,
include: Some(svec!["dir1/", "dir2/"]),
},
@ -2280,6 +2284,23 @@ mod tests {
}
);
}
#[test]
fn test_filter() {
let r = flags_from_vec_safe(svec!["deno", "test", "--filter=foo", "dir1"]);
assert_eq!(
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Test {
fail_fast: false,
allow_none: false,
filter: Some("foo".to_string()),
include: Some(svec!["dir1"]),
},
allow_read: true,
..Flags::default()
}
);
}
#[test]
@ -2560,3 +2581,4 @@ fn inspect_custom_host() {
}
);
}
}

View file

@ -120,7 +120,7 @@ declare namespace Deno {
failFast?: boolean;
/** String or RegExp used to filter test to run. Only test with names
* matching provided `String` or `RegExp` will be run. */
only?: string | RegExp;
filter?: string | RegExp;
/** String or RegExp used to skip tests to run. Tests with names
* matching provided `String` or `RegExp` will not be run. */
skip?: string | RegExp;

View file

@ -270,17 +270,17 @@ class TestApi {
}
function createFilterFn(
only: undefined | string | RegExp,
filter: undefined | string | RegExp,
skip: undefined | string | RegExp
): (def: TestDefinition) => boolean {
return (def: TestDefinition): boolean => {
let passes = true;
if (only) {
if (only instanceof RegExp) {
passes = passes && only.test(def.name);
if (filter) {
if (filter instanceof RegExp) {
passes = passes && filter.test(def.name);
} else {
passes = passes && def.name.includes(only);
passes = passes && def.name.includes(filter);
}
}
@ -299,7 +299,7 @@ function createFilterFn(
export interface RunTestsOptions {
exitOnFail?: boolean;
failFast?: boolean;
only?: string | RegExp;
filter?: string | RegExp;
skip?: string | RegExp;
disableLog?: boolean;
reportToConsole?: boolean;
@ -309,13 +309,13 @@ export interface RunTestsOptions {
export async function runTests({
exitOnFail = true,
failFast = false,
only = undefined,
filter = undefined,
skip = undefined,
disableLog = false,
reportToConsole: reportToConsole_ = true,
onMessage = undefined,
}: RunTestsOptions = {}): Promise<TestMessage["end"] & {}> {
const filterFn = createFilterFn(only, skip);
const filterFn = createFilterFn(filter, skip);
const testApi = new TestApi(TEST_REGISTRY, filterFn, failFast);
// @ts-ignore

View file

@ -68,7 +68,7 @@ async function workerRunnerMain(
// Execute tests
await Deno.runTests({
exitOnFail: false,
only: filter,
filter,
reportToConsole: false,
onMessage: reportToConn.bind(null, conn),
});
@ -296,7 +296,7 @@ async function main(): Promise<void> {
// Running tests matching current process permissions
await registerUnitTests();
await Deno.runTests({ only: filter });
await Deno.runTests({ filter });
}
main();

View file

@ -445,6 +445,7 @@ async fn test_command(
include: Option<Vec<String>>,
fail_fast: bool,
allow_none: bool,
filter: Option<String>,
) -> Result<(), ErrBox> {
let global_state = GlobalState::new(flags.clone())?;
let cwd = std::env::current_dir().expect("No current directory");
@ -462,7 +463,8 @@ async fn test_command(
let test_file_path = cwd.join(".deno.test.ts");
let test_file_url =
Url::from_file_path(&test_file_path).expect("Should be valid file url");
let test_file = test_runner::render_test_file(test_modules, fail_fast);
let test_file =
test_runner::render_test_file(test_modules, fail_fast, filter);
let main_module =
ModuleSpecifier::resolve_url(&test_file_url.to_string()).unwrap();
let mut worker =
@ -545,7 +547,10 @@ pub fn main() {
fail_fast,
include,
allow_none,
} => test_command(flags, include, fail_fast, allow_none).boxed_local(),
filter,
} => {
test_command(flags, include, fail_fast, allow_none, filter).boxed_local()
}
DenoSubcommand::Completions { buf } => {
if let Err(e) = write_to_stdout_ignore_sigpipe(&buf) {
eprintln!("{}", e);

View file

@ -61,15 +61,24 @@ pub fn prepare_test_modules_urls(
Ok(prepared)
}
pub fn render_test_file(modules: Vec<Url>, fail_fast: bool) -> String {
pub fn render_test_file(
modules: Vec<Url>,
fail_fast: bool,
filter: Option<String>,
) -> String {
let mut test_file = "".to_string();
for module in modules {
test_file.push_str(&format!("import \"{}\";\n", module.to_string()));
}
let run_tests_cmd =
format!("Deno.runTests({{ failFast: {} }});\n", fail_fast);
let options = if let Some(filter) = filter {
json!({ "failFast": fail_fast, "filter": filter })
} else {
json!({ "failFast": fail_fast })
};
let run_tests_cmd = format!("Deno.runTests({});\n", options);
test_file.push_str(&run_tests_cmd);
test_file