Skip to content

Commit

Permalink
refactor: rewrite deno test, add Deno.test() (#3865)
Browse files Browse the repository at this point in the history
* rewrite test runner in Rust
* migrate "test" and "runTests" functions from std to "Deno" namespace
* use "Deno.test()" to run internal JS unit tests
* remove std downloads for Deno subcommands
  • Loading branch information
bartlomieju authored Feb 11, 2020
1 parent 701ce9b commit a3bfbcc
Show file tree
Hide file tree
Showing 13 changed files with 452 additions and 102 deletions.
95 changes: 31 additions & 64 deletions cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ macro_rules! sset {
}}
}

macro_rules! std_url {
($x:expr) => {
concat!("https://deno.land/[email protected]/", $x)
};
}

/// Used for `deno test...` subcommand
const TEST_RUNNER_URL: &str = std_url!("testing/runner.ts");

#[derive(Clone, Debug, PartialEq)]
pub enum DenoSubcommand {
Bundle {
Expand Down Expand Up @@ -65,6 +56,12 @@ pub enum DenoSubcommand {
Run {
script: String,
},
Test {
fail_fast: bool,
quiet: bool,
allow_none: bool,
include: Option<Vec<String>>,
},
Types,
}

Expand Down Expand Up @@ -495,40 +492,31 @@ fn run_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
}

fn test_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
flags.subcommand = DenoSubcommand::Run {
script: TEST_RUNNER_URL.to_string(),
};
flags.allow_read = true;

run_test_args_parse(flags, matches);

if matches.is_present("quiet") {
flags.argv.push("--quiet".to_string());
}

if matches.is_present("failfast") {
flags.argv.push("--failfast".to_string());
}

if matches.is_present("exclude") {
flags.argv.push("--exclude".to_string());
let exclude: Vec<String> = matches
.values_of("exclude")
.unwrap()
.map(String::from)
.collect();
flags.argv.extend(exclude);
}
let quiet = matches.is_present("quiet");
let failfast = matches.is_present("failfast");
let allow_none = matches.is_present("allow_none");

if matches.is_present("files") {
flags.argv.push("--".to_string());
let include = if matches.is_present("files") {
let files: Vec<String> = matches
.values_of("files")
.unwrap()
.map(String::from)
.collect();
flags.argv.extend(files);
}
Some(files)
} else {
None
};

flags.subcommand = DenoSubcommand::Test {
quiet,
fail_fast: failfast,
include,
allow_none,
};
}

fn types_subcommand<'a, 'b>() -> App<'a, 'b> {
Expand Down Expand Up @@ -857,11 +845,10 @@ fn test_subcommand<'a, 'b>() -> App<'a, 'b> {
.takes_value(false),
)
.arg(
Arg::with_name("exclude")
.short("e")
.long("exclude")
.help("List of file names to exclude from run")
.takes_value(true),
Arg::with_name("allow_none")
.long("allow-none")
.help("Don't return error code if no test files are found")
.takes_value(false),
)
.arg(
Arg::with_name("files")
Expand Down Expand Up @@ -2041,45 +2028,25 @@ mod tests {
);
}

#[test]
fn test_with_exclude() {
let r = flags_from_vec_safe(svec![
"deno",
"test",
"--exclude",
"some_dir/",
"dir1/",
"dir2/"
]);
assert_eq!(
r.unwrap(),
DenoFlags {
subcommand: DenoSubcommand::Run {
script: TEST_RUNNER_URL.to_string(),
},
argv: svec!["--exclude", "some_dir/", "--", "dir1/", "dir2/"],
allow_read: true,
..DenoFlags::default()
}
);
}

#[test]
fn test_with_allow_net() {
let r = flags_from_vec_safe(svec![
"deno",
"test",
"--allow-net",
"--allow-none",
"dir1/",
"dir2/"
]);
assert_eq!(
r.unwrap(),
DenoFlags {
subcommand: DenoSubcommand::Run {
script: TEST_RUNNER_URL.to_string(),
subcommand: DenoSubcommand::Test {
fail_fast: false,
quiet: false,
allow_none: true,
include: Some(svec!["dir1/", "dir2/"]),
},
argv: svec!["--", "dir1/", "dir2/"],
allow_read: true,
allow_net: true,
..DenoFlags::default()
Expand Down
2 changes: 1 addition & 1 deletion cli/installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ lazy_static! {
).case_insensitive(true).build().unwrap();
}

fn is_remote_url(module_url: &str) -> bool {
pub fn is_remote_url(module_url: &str) -> bool {
module_url.starts_with("http://") || module_url.starts_with("https://")
}

Expand Down
24 changes: 24 additions & 0 deletions cli/js/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,34 @@ export function bold(str: string): string {
return run(str, code(1, 22));
}

export function italic(str: string): string {
return run(str, code(3, 23));
}

export function yellow(str: string): string {
return run(str, code(33, 39));
}

export function cyan(str: string): string {
return run(str, code(36, 39));
}

export function red(str: string): string {
return run(str, code(31, 39));
}

export function green(str: string): string {
return run(str, code(32, 39));
}

export function bgRed(str: string): string {
return run(str, code(41, 49));
}

export function white(str: string): string {
return run(str, code(37, 39));
}

export function gray(str: string): string {
return run(str, code(90, 39));
}
1 change: 1 addition & 0 deletions cli/js/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export { utimeSync, utime } from "./utime.ts";
export { version } from "./version.ts";
export { writeFileSync, writeFile, WriteFileOptions } from "./write_file.ts";
export const args: string[] = [];
export { test, runTests } from "./testing.ts";

// These are internal Deno APIs. We are marking them as internal so they do not
// appear in the runtime type library.
Expand Down
8 changes: 8 additions & 0 deletions cli/js/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ declare global {
thrown: any;
}

interface ImportMeta {
url: string;
main: boolean;
}

interface DenoCore {
print(s: string, isErr?: boolean): void;
dispatch(
Expand Down Expand Up @@ -137,6 +142,9 @@ declare global {
// Assigned to `self` global - compiler
var bootstrapTsCompilerRuntime: (() => void) | undefined;
var bootstrapWasmCompilerRuntime: (() => void) | undefined;

var performance: performanceUtil.Performance;
var setTimeout: typeof timers.setTimeout;
/* eslint-enable */
}

Expand Down
20 changes: 20 additions & 0 deletions cli/js/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ declare namespace Deno {
/** Reflects the NO_COLOR environment variable: https://no-color.org/ */
export let noColor: boolean;

export type TestFunction = () => void | Promise<void>;

export interface TestDefinition {
fn: TestFunction;
name: string;
}

export function test(t: TestDefinition): void;
export function test(fn: TestFunction): void;
export function test(name: string, fn: TestFunction): void;

export interface RunTestsOptions {
exitOnFail?: boolean;
only?: RegExp;
skip?: RegExp;
disableLog?: boolean;
}

export function runTests(opts?: RunTestsOptions): Promise<void>;

/** Check if running in terminal.
*
* console.log(Deno.isTTY().stdout);
Expand Down
3 changes: 0 additions & 3 deletions cli/js/net_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { testPerm, assert, assertEquals } from "./test_util.ts";
import { runIfMain } from "../../std/testing/mod.ts";

testPerm({ net: true }, function netListenClose(): void {
const listener = Deno.listen({ hostname: "127.0.0.1", port: 4500 });
Expand Down Expand Up @@ -240,5 +239,3 @@ testPerm({ net: true }, async function netDoubleCloseWrite() {
conn.close();
});
*/

runIfMain(import.meta);
10 changes: 3 additions & 7 deletions cli/js/test_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// tests by the special string. permW1N0 means allow-write but not allow-net.
// See tools/unit_tests.py for more details.

import * as testing from "../../std/testing/mod.ts";
import { assert, assertEquals } from "../../std/testing/asserts.ts";
export {
assert,
Expand Down Expand Up @@ -103,10 +102,7 @@ function normalizeTestPermissions(perms: TestPermissions): Permissions {
};
}

export function testPerm(
perms: TestPermissions,
fn: testing.TestFunction
): void {
export function testPerm(perms: TestPermissions, fn: Deno.TestFunction): void {
const normalizedPerms = normalizeTestPermissions(perms);

registerPermCombination(normalizedPerms);
Expand All @@ -115,10 +111,10 @@ export function testPerm(
return;
}

testing.test(fn);
Deno.test(fn);
}

export function test(fn: testing.TestFunction): void {
export function test(fn: Deno.TestFunction): void {
testPerm(
{
read: false,
Expand Down
Loading

0 comments on commit a3bfbcc

Please sign in to comment.