0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-04 01:44:26 -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, fail_fast: bool,
allow_none: bool, allow_none: bool,
include: Option<Vec<String>>, include: Option<Vec<String>>,
filter: Option<String>,
}, },
Types, Types,
Upgrade { Upgrade {
@ -535,7 +536,7 @@ fn test_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
let failfast = matches.is_present("failfast"); let failfast = matches.is_present("failfast");
let allow_none = matches.is_present("allow_none"); 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 include = if matches.is_present("files") {
let files: Vec<String> = matches let files: Vec<String> = matches
.values_of("files") .values_of("files")
@ -550,6 +551,7 @@ fn test_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
flags.subcommand = DenoSubcommand::Test { flags.subcommand = DenoSubcommand::Test {
fail_fast: failfast, fail_fast: failfast,
include, include,
filter,
allow_none, allow_none,
}; };
} }
@ -564,11 +566,7 @@ fn doc_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
reload_arg_parse(flags, matches); reload_arg_parse(flags, matches);
let source_file = matches.value_of("source_file").map(String::from).unwrap(); let source_file = matches.value_of("source_file").map(String::from).unwrap();
let json = matches.is_present("json"); let json = matches.is_present("json");
let filter = if matches.is_present("filter") { let filter = matches.value_of("filter").map(String::from);
Some(matches.value_of("filter").unwrap().to_string())
} else {
None
};
flags.subcommand = DenoSubcommand::Doc { flags.subcommand = DenoSubcommand::Doc {
source_file, source_file,
json, json,
@ -945,7 +943,6 @@ fn test_subcommand<'a, 'b>() -> App<'a, 'b> {
run_test_args(SubCommand::with_name("test")) run_test_args(SubCommand::with_name("test"))
.arg( .arg(
Arg::with_name("failfast") Arg::with_name("failfast")
.short("f")
.long("failfast") .long("failfast")
.help("Stop on first error") .help("Stop on first error")
.takes_value(false), .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") .help("Don't return error code if no test files are found")
.takes_value(false), .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(
Arg::with_name("files") Arg::with_name("files")
.help("List of file names to run") .help("List of file names to run")
@ -2271,6 +2274,7 @@ mod tests {
Flags { Flags {
subcommand: DenoSubcommand::Test { subcommand: DenoSubcommand::Test {
fail_fast: false, fail_fast: false,
filter: None,
allow_none: true, allow_none: true,
include: Some(svec!["dir1/", "dir2/"]), include: Some(svec!["dir1/", "dir2/"]),
}, },
@ -2280,283 +2284,301 @@ mod tests {
} }
); );
} }
}
#[test] #[test]
fn run_with_cafile() { fn test_filter() {
let r = flags_from_vec_safe(svec![ let r = flags_from_vec_safe(svec!["deno", "test", "--filter=foo", "dir1"]);
"deno", assert_eq!(
"run", r.unwrap(),
"--cert", Flags {
"example.crt", subcommand: DenoSubcommand::Test {
"script.ts" fail_fast: false,
]); allow_none: false,
assert_eq!( filter: Some("foo".to_string()),
r.unwrap(), include: Some(svec!["dir1"]),
Flags { },
subcommand: DenoSubcommand::Run { allow_read: true,
script: "script.ts".to_string(), ..Flags::default()
}, }
ca_file: Some("example.crt".to_owned()), );
..Flags::default() }
}
);
}
#[test] #[test]
fn bundle_with_cafile() { fn run_with_cafile() {
let r = flags_from_vec_safe(svec![ let r = flags_from_vec_safe(svec![
"deno", "deno",
"bundle", "run",
"--cert", "--cert",
"example.crt", "example.crt",
"source.ts" "script.ts"
]); ]);
assert_eq!( assert_eq!(
r.unwrap(), r.unwrap(),
Flags { Flags {
subcommand: DenoSubcommand::Bundle { subcommand: DenoSubcommand::Run {
source_file: "source.ts".to_string(), script: "script.ts".to_string(),
out_file: None, },
}, ca_file: Some("example.crt".to_owned()),
ca_file: Some("example.crt".to_owned()), ..Flags::default()
..Flags::default() }
} );
); }
}
#[test] #[test]
fn eval_with_cafile() { fn bundle_with_cafile() {
let r = flags_from_vec_safe(svec![ let r = flags_from_vec_safe(svec![
"deno", "deno",
"eval", "bundle",
"--cert", "--cert",
"example.crt", "example.crt",
"console.log('hello world')" "source.ts"
]); ]);
assert_eq!( assert_eq!(
r.unwrap(), r.unwrap(),
Flags { Flags {
subcommand: DenoSubcommand::Eval { subcommand: DenoSubcommand::Bundle {
code: "console.log('hello world')".to_string(), source_file: "source.ts".to_string(),
as_typescript: false, out_file: None,
}, },
ca_file: Some("example.crt".to_owned()), ca_file: Some("example.crt".to_owned()),
allow_net: true, ..Flags::default()
allow_env: true, }
allow_run: true, );
allow_read: true, }
allow_write: true,
allow_plugin: true,
allow_hrtime: true,
..Flags::default()
}
);
}
#[test] #[test]
fn eval_with_inspect() { fn eval_with_cafile() {
let r = flags_from_vec_safe(svec![ let r = flags_from_vec_safe(svec![
"deno", "deno",
"eval", "eval",
"--inspect", "--cert",
"const foo = 'bar'" "example.crt",
]); "console.log('hello world')"
assert_eq!( ]);
r.unwrap(), assert_eq!(
Flags { r.unwrap(),
subcommand: DenoSubcommand::Eval { Flags {
code: "const foo = 'bar'".to_string(), subcommand: DenoSubcommand::Eval {
as_typescript: false, code: "console.log('hello world')".to_string(),
}, as_typescript: false,
inspect: Some("127.0.0.1:9229".to_string()), },
allow_net: true, ca_file: Some("example.crt".to_owned()),
allow_env: true, allow_net: true,
allow_run: true, allow_env: true,
allow_read: true, allow_run: true,
allow_write: true, allow_read: true,
allow_plugin: true, allow_write: true,
allow_hrtime: true, allow_plugin: true,
..Flags::default() allow_hrtime: true,
} ..Flags::default()
); }
} );
}
#[test] #[test]
fn fetch_with_cafile() { fn eval_with_inspect() {
let r = flags_from_vec_safe(svec![ let r = flags_from_vec_safe(svec![
"deno", "deno",
"fetch", "eval",
"--cert", "--inspect",
"example.crt", "const foo = 'bar'"
"script.ts", ]);
"script_two.ts" assert_eq!(
]); r.unwrap(),
assert_eq!( Flags {
r.unwrap(), subcommand: DenoSubcommand::Eval {
Flags { code: "const foo = 'bar'".to_string(),
subcommand: DenoSubcommand::Fetch { as_typescript: false,
files: svec!["script.ts", "script_two.ts"], },
}, inspect: Some("127.0.0.1:9229".to_string()),
ca_file: Some("example.crt".to_owned()), allow_net: true,
..Flags::default() allow_env: true,
} allow_run: true,
); allow_read: true,
} allow_write: true,
allow_plugin: true,
allow_hrtime: true,
..Flags::default()
}
);
}
#[test] #[test]
fn info_with_cafile() { fn fetch_with_cafile() {
let r = flags_from_vec_safe(svec![ let r = flags_from_vec_safe(svec![
"deno", "deno",
"info", "fetch",
"--cert", "--cert",
"example.crt", "example.crt",
"https://example.com" "script.ts",
]); "script_two.ts"
assert_eq!( ]);
r.unwrap(), assert_eq!(
Flags { r.unwrap(),
subcommand: DenoSubcommand::Info { Flags {
file: Some("https://example.com".to_string()), subcommand: DenoSubcommand::Fetch {
}, files: svec!["script.ts", "script_two.ts"],
ca_file: Some("example.crt".to_owned()), },
..Flags::default() ca_file: Some("example.crt".to_owned()),
} ..Flags::default()
); }
} );
}
#[test] #[test]
fn install_with_cafile() { fn info_with_cafile() {
let r = flags_from_vec_safe(svec![ let r = flags_from_vec_safe(svec![
"deno", "deno",
"install", "info",
"--cert", "--cert",
"example.crt", "example.crt",
"deno_colors", "https://example.com"
"https://deno.land/std/examples/colors.ts" ]);
]); assert_eq!(
assert_eq!( r.unwrap(),
r.unwrap(), Flags {
Flags { subcommand: DenoSubcommand::Info {
subcommand: DenoSubcommand::Install { file: Some("https://example.com".to_string()),
dir: None, },
exe_name: "deno_colors".to_string(), ca_file: Some("example.crt".to_owned()),
module_url: "https://deno.land/std/examples/colors.ts".to_string(), ..Flags::default()
args: vec![], }
force: false, );
}, }
ca_file: Some("example.crt".to_owned()),
..Flags::default()
}
);
}
#[test] #[test]
fn repl_with_cafile() { fn install_with_cafile() {
let r = flags_from_vec_safe(svec!["deno", "repl", "--cert", "example.crt"]); let r = flags_from_vec_safe(svec![
assert_eq!( "deno",
r.unwrap(), "install",
Flags { "--cert",
subcommand: DenoSubcommand::Repl {}, "example.crt",
ca_file: Some("example.crt".to_owned()), "deno_colors",
allow_read: true, "https://deno.land/std/examples/colors.ts"
allow_write: true, ]);
allow_net: true, assert_eq!(
allow_env: true, r.unwrap(),
allow_run: true, Flags {
allow_plugin: true, subcommand: DenoSubcommand::Install {
allow_hrtime: true, dir: None,
..Flags::default() exe_name: "deno_colors".to_string(),
} module_url: "https://deno.land/std/examples/colors.ts".to_string(),
); args: vec![],
} force: false,
},
ca_file: Some("example.crt".to_owned()),
..Flags::default()
}
);
}
#[test] #[test]
fn repl_with_inspect() { fn repl_with_cafile() {
let r = flags_from_vec_safe(svec!["deno", "repl", "--inspect"]); let r = flags_from_vec_safe(svec!["deno", "repl", "--cert", "example.crt"]);
assert_eq!( assert_eq!(
r.unwrap(), r.unwrap(),
Flags { Flags {
subcommand: DenoSubcommand::Repl {}, subcommand: DenoSubcommand::Repl {},
inspect: Some("127.0.0.1:9229".to_string()), ca_file: Some("example.crt".to_owned()),
allow_read: true, allow_read: true,
allow_write: true, allow_write: true,
allow_net: true, allow_net: true,
allow_env: true, allow_env: true,
allow_run: true, allow_run: true,
allow_plugin: true, allow_plugin: true,
allow_hrtime: true, allow_hrtime: true,
..Flags::default() ..Flags::default()
} }
); );
} }
#[test] #[test]
fn doc() { fn repl_with_inspect() {
let r = let r = flags_from_vec_safe(svec!["deno", "repl", "--inspect"]);
flags_from_vec_safe(svec!["deno", "doc", "--json", "path/to/module.ts"]); assert_eq!(
assert_eq!( r.unwrap(),
r.unwrap(), Flags {
Flags { subcommand: DenoSubcommand::Repl {},
subcommand: DenoSubcommand::Doc { inspect: Some("127.0.0.1:9229".to_string()),
json: true, allow_read: true,
source_file: "path/to/module.ts".to_string(), allow_write: true,
filter: None, allow_net: true,
}, allow_env: true,
..Flags::default() allow_run: true,
} allow_plugin: true,
); allow_hrtime: true,
..Flags::default()
}
);
}
let r = flags_from_vec_safe(svec![ #[test]
"deno", fn doc() {
"doc", let r =
"path/to/module.ts", flags_from_vec_safe(svec!["deno", "doc", "--json", "path/to/module.ts"]);
"SomeClass.someField" assert_eq!(
]); r.unwrap(),
assert_eq!( Flags {
r.unwrap(), subcommand: DenoSubcommand::Doc {
Flags { json: true,
subcommand: DenoSubcommand::Doc { source_file: "path/to/module.ts".to_string(),
json: false, filter: None,
source_file: "path/to/module.ts".to_string(), },
filter: Some("SomeClass.someField".to_string()), ..Flags::default()
}, }
..Flags::default() );
}
);
}
#[test] let r = flags_from_vec_safe(svec![
fn inspect_default_host() { "deno",
let r = flags_from_vec_safe(svec!["deno", "run", "--inspect", "foo.js"]); "doc",
assert_eq!( "path/to/module.ts",
r.unwrap(), "SomeClass.someField"
Flags { ]);
subcommand: DenoSubcommand::Run { assert_eq!(
script: "foo.js".to_string(), r.unwrap(),
}, Flags {
inspect: Some("127.0.0.1:9229".to_string()), subcommand: DenoSubcommand::Doc {
..Flags::default() json: false,
} source_file: "path/to/module.ts".to_string(),
); filter: Some("SomeClass.someField".to_string()),
} },
..Flags::default()
}
);
}
#[test] #[test]
fn inspect_custom_host() { fn inspect_default_host() {
let r = flags_from_vec_safe(svec![ let r = flags_from_vec_safe(svec!["deno", "run", "--inspect", "foo.js"]);
"deno", assert_eq!(
"run", r.unwrap(),
"--inspect=deno.land:80", Flags {
"foo.js" subcommand: DenoSubcommand::Run {
]); script: "foo.js".to_string(),
assert_eq!( },
r.unwrap(), inspect: Some("127.0.0.1:9229".to_string()),
Flags { ..Flags::default()
subcommand: DenoSubcommand::Run { }
script: "foo.js".to_string(), );
}, }
inspect: Some("deno.land:80".to_string()),
..Flags::default() #[test]
} fn inspect_custom_host() {
); let r = flags_from_vec_safe(svec![
"deno",
"run",
"--inspect=deno.land:80",
"foo.js"
]);
assert_eq!(
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Run {
script: "foo.js".to_string(),
},
inspect: Some("deno.land:80".to_string()),
..Flags::default()
}
);
}
} }

View file

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

View file

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

View file

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

View file

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

View file

@ -61,15 +61,24 @@ pub fn prepare_test_modules_urls(
Ok(prepared) 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(); let mut test_file = "".to_string();
for module in modules { for module in modules {
test_file.push_str(&format!("import \"{}\";\n", module.to_string())); test_file.push_str(&format!("import \"{}\";\n", module.to_string()));
} }
let run_tests_cmd = let options = if let Some(filter) = filter {
format!("Deno.runTests({{ failFast: {} }});\n", fail_fast); 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.push_str(&run_tests_cmd);
test_file test_file