feat(test): Add support for regex in filter flag (#6343)

Currently, the documentation makes it sound like the test subcommand's filter
flag could accept some kind of pattern matching value like a glob or a regex,
although the function "createFilterFn" accepts a regex as an argument, there's
no way to pass an actual regex value from the CLI.

This commit makes it possible to pass a string that could be cast as regex 
when string matches "^/.*/$".

With this change, a user can use the filter flag as follow:

deno test --filter "/test-.+/"

Also tested that `\` get escaped properly, on MacOS at least, and this is 
also a valid flag:

deno test --filter "/test-\d+/"
This commit is contained in:
Sebastien Filion 2020-07-07 09:13:38 -04:00 committed by GitHub
parent 14a44464a6
commit 4534db656d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 84 additions and 3 deletions

View file

@ -1105,7 +1105,7 @@ fn test_subcommand<'a, 'b>() -> App<'a, 'b> {
Arg::with_name("filter")
.long("filter")
.takes_value(true)
.help("Run tests with this string in the test name"),
.help("Run tests with this string or pattern in the test name"),
)
.arg(
Arg::with_name("files")

View file

@ -308,6 +308,9 @@ function createFilterFn(
if (filter) {
if (filter instanceof RegExp) {
passes = passes && filter.test(def.name);
} else if (filter.startsWith("/") && filter.endsWith("/")) {
const filterAsRegex = new RegExp(filter.slice(1, filter.length - 1));
passes = passes && filterAsRegex.test(def.name);
} else {
passes = passes && def.name.includes(filter);
}
@ -325,6 +328,8 @@ function createFilterFn(
};
}
exposeForTest("createFilterFn", createFilterFn);
interface RunTestsOptions {
exitOnFail?: boolean;
failFast?: boolean;

View file

@ -0,0 +1,52 @@
import { unitTest, assertEquals } from "./test_util.ts";
// @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol
const { createFilterFn } = Deno[Deno.internal];
unitTest(function filterAsString(): void {
const filterFn = createFilterFn("my-test");
const tests = [
{
fn(): void {},
name: "my-test",
},
{
fn(): void {},
name: "other-test",
},
];
const filteredTests = tests.filter(filterFn);
assertEquals(filteredTests.length, 1);
});
unitTest(function filterAsREGEX(): void {
const filterFn = createFilterFn("/.+-test/");
const tests = [
{
fn(): void {},
name: "my-test",
},
{
fn(): void {},
name: "other-test",
},
];
const filteredTests = tests.filter(filterFn);
assertEquals(filteredTests.length, 2);
});
unitTest(function filterAsEscapedREGEX(): void {
const filterFn = createFilterFn("/\\w+-test/");
const tests = [
{
fn(): void {},
name: "my-test",
},
{
fn(): void {},
name: "other-test",
},
];
const filteredTests = tests.filter(filterFn);
assertEquals(filteredTests.length, 2);
});

View file

@ -24,6 +24,7 @@ import "./event_target_test.ts";
import "./fetch_test.ts";
import "./file_test.ts";
import "./files_test.ts";
import "./filter_function_test.ts";
import "./form_data_test.ts";
import "./format_error_test.ts";
import "./fs_events_test.ts";

View file

@ -129,10 +129,33 @@ There are a number of options to filter the tests you are running.
Tests can be run individually or in groups using the command line `--filter`
option.
```shell
deno test --filter "hello world" tests/
The filter flags accept a string or a pattern as value.
Assuming the following tests:
```ts
Deno.test({ name: "my-test", fn: myTest });
Deno.test({ name: "test-1", fn: test1 });
Deno.test({ name: "test2", fn: test2 });
```
This command will run all of these tests because they all contain the word
"test".
```shell
deno test --filter "test" tests/
```
On the flip side, the following command uses a pattern and will run the second
and third tests.
```shell
deno test --filter "/test-*\d/" tests/
```
_To let Deno know that you want to use a pattern, wrap your filter with
forward-slashes like the JavaScript syntactic sugar for a REGEX._
This command will run any test which contains the string "hello world" in its
test name for tests found within files in the `tests/` directory.