mirror of
https://github.com/denoland/deno.git
synced 2025-10-03 15:44:36 +00:00
reorg: move js runtime tests to cli/js/tests/ (#4250)
All Deno runtime test files were moved to cli/js/tests/ directory. It makes a clear distinction that cli/js/tests/ contains code that is run under Deno runtime as opposed to code in cli/js/ which is used to create bundle and snapshot with "deno_typescript".
This commit is contained in:
parent
dad8036766
commit
68119e1d7e
65 changed files with 76 additions and 18 deletions
47
cli/js/tests/README.md
Normal file
47
cli/js/tests/README.md
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
# Deno runtime tests
|
||||||
|
|
||||||
|
Files in this directory are unit tests for Deno runtime.
|
||||||
|
|
||||||
|
They are run under compiled Deno binary as opposed to files in `cli/js/` which
|
||||||
|
are bundled and snapshotted using `deno_typescript` crate.
|
||||||
|
|
||||||
|
Testing Deno runtime code requires checking API under different runtime
|
||||||
|
permissions (ie. running with different `--allow-*` flags). To accomplish this
|
||||||
|
all tests exercised are created using `unitTest()` function.
|
||||||
|
|
||||||
|
```
|
||||||
|
import { unitTest } from "./test_util.ts";
|
||||||
|
|
||||||
|
unitTest(function simpleTestFn(): void {
|
||||||
|
// test code here
|
||||||
|
});
|
||||||
|
|
||||||
|
unitTest({
|
||||||
|
skip: Deno.build.os === "win",
|
||||||
|
perms: { read: true, write: true },
|
||||||
|
},
|
||||||
|
function complexTestFn(): void {
|
||||||
|
// test code here
|
||||||
|
}
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
`unitTest` is is a wrapper function that enhances `Deno.test()` API in several
|
||||||
|
ways:
|
||||||
|
|
||||||
|
- ability to conditionally skip tests using `UnitTestOptions.skip`
|
||||||
|
- ability to register required set of permissions for given test case using
|
||||||
|
`UnitTestOptions.perms`
|
||||||
|
- sanitization of resources - ensuring that tests close all opened resources
|
||||||
|
preventing interference between tests
|
||||||
|
- sanitization of async ops - ensuring that tests don't leak async ops by
|
||||||
|
ensuring that all started async ops are done before test finishes
|
||||||
|
|
||||||
|
`unit_test_runner.ts` is main script used to run unit tests.
|
||||||
|
|
||||||
|
Runner discoveres required permissions combinations by loading
|
||||||
|
`cli/js/tests/unit_tests.ts` and going through all registered instances of
|
||||||
|
`unitTest`. For each discovered permission combination a new Deno process is
|
||||||
|
created with respective `--allow-*` flags which loads
|
||||||
|
`cli/js/tests/unit_tests.ts` and executes all `unitTest` that match runtime
|
||||||
|
permissions.
|
|
@ -1,5 +1,5 @@
|
||||||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
import { unitTest, assert, assertEquals } from "../test_util.ts";
|
import { unitTest, assert, assertEquals } from "./test_util.ts";
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
||||||
function setup() {
|
function setup() {
|
|
@ -7,8 +7,8 @@
|
||||||
// tests by the special string. permW1N0 means allow-write but not allow-net.
|
// tests by the special string. permW1N0 means allow-write but not allow-net.
|
||||||
// See tools/unit_tests.py for more details.
|
// See tools/unit_tests.py for more details.
|
||||||
|
|
||||||
import { readLines } from "../../std/io/bufio.ts";
|
import { readLines } from "../../../std/io/bufio.ts";
|
||||||
import { assert, assertEquals } from "../../std/testing/asserts.ts";
|
import { assert, assertEquals } from "../../../std/testing/asserts.ts";
|
||||||
export {
|
export {
|
||||||
assert,
|
assert,
|
||||||
assertThrows,
|
assertThrows,
|
||||||
|
@ -19,7 +19,7 @@ export {
|
||||||
assertStrContains,
|
assertStrContains,
|
||||||
unreachable,
|
unreachable,
|
||||||
fail
|
fail
|
||||||
} from "../../std/testing/asserts.ts";
|
} from "../../../std/testing/asserts.ts";
|
||||||
|
|
||||||
interface TestPermissions {
|
interface TestPermissions {
|
||||||
read?: boolean;
|
read?: boolean;
|
||||||
|
@ -380,21 +380,27 @@ unitTest(
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Ensure all unit test files (e.g. xxx_test.ts) are present as imports in
|
* Ensure all unit test files (e.g. xxx_test.ts) are present as imports in
|
||||||
* cli/js/unit_tests.ts as it is easy to miss this out
|
* cli/js/tests/unit_tests.ts as it is easy to miss this out
|
||||||
*/
|
*/
|
||||||
unitTest(
|
unitTest(
|
||||||
{ perms: { read: true } },
|
{ perms: { read: true } },
|
||||||
async function assertAllUnitTestFilesImported(): Promise<void> {
|
async function assertAllUnitTestFilesImported(): Promise<void> {
|
||||||
const directoryTestFiles = Deno.readdirSync("./cli/js")
|
const directoryTestFiles = Deno.readdirSync("./cli/js/tests/")
|
||||||
.map(k => k.name)
|
.map(k => k.name)
|
||||||
.filter(file => file!.endsWith("_test.ts"));
|
.filter(
|
||||||
|
file =>
|
||||||
|
file!.endsWith(".ts") &&
|
||||||
|
!file!.endsWith("unit_tests.ts") &&
|
||||||
|
!file!.endsWith("test_util.ts") &&
|
||||||
|
!file!.endsWith("unit_test_runner.ts")
|
||||||
|
);
|
||||||
const unitTestsFile: Uint8Array = Deno.readFileSync(
|
const unitTestsFile: Uint8Array = Deno.readFileSync(
|
||||||
"./cli/js/unit_tests.ts"
|
"./cli/js/tests/unit_tests.ts"
|
||||||
);
|
);
|
||||||
const importLines = new TextDecoder("utf-8")
|
const importLines = new TextDecoder("utf-8")
|
||||||
.decode(unitTestsFile)
|
.decode(unitTestsFile)
|
||||||
.split("\n")
|
.split("\n")
|
||||||
.filter(line => line.startsWith("import") && line.includes("_test.ts"));
|
.filter(line => line.startsWith("import"));
|
||||||
const importedTestFiles = importLines.map(
|
const importedTestFiles = importLines.map(
|
||||||
relativeFilePath => relativeFilePath.match(/\/([^\/]+)";/)![1]
|
relativeFilePath => relativeFilePath.match(/\/([^\/]+)";/)![1]
|
||||||
);
|
);
|
||||||
|
@ -402,7 +408,7 @@ unitTest(
|
||||||
directoryTestFiles.forEach(dirFile => {
|
directoryTestFiles.forEach(dirFile => {
|
||||||
if (!importedTestFiles.includes(dirFile!)) {
|
if (!importedTestFiles.includes(dirFile!)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"cil/js/unit_tests.ts is missing import of test file: cli/js/" +
|
"cil/js/tests/unit_tests.ts is missing import of test file: cli/js/" +
|
||||||
dirFile
|
dirFile
|
||||||
);
|
);
|
||||||
}
|
}
|
|
@ -5,8 +5,8 @@ import {
|
||||||
createResolvable,
|
createResolvable,
|
||||||
unitTest
|
unitTest
|
||||||
} from "./test_util.ts";
|
} from "./test_util.ts";
|
||||||
import { BufWriter, BufReader } from "../../std/io/bufio.ts";
|
import { BufWriter, BufReader } from "../../../std/io/bufio.ts";
|
||||||
import { TextProtoReader } from "../../std/textproto/mod.ts";
|
import { TextProtoReader } from "../../../std/textproto/mod.ts";
|
||||||
|
|
||||||
const encoder = new TextEncoder();
|
const encoder = new TextEncoder();
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
|
@ -54,7 +54,12 @@ async function main(): Promise<void> {
|
||||||
console.log(`Running tests for: ${permsFmt}`);
|
console.log(`Running tests for: ${permsFmt}`);
|
||||||
const cliPerms = permsToCliFlags(perms);
|
const cliPerms = permsToCliFlags(perms);
|
||||||
// run subsequent tests using same deno executable
|
// run subsequent tests using same deno executable
|
||||||
const args = [Deno.execPath(), "run", ...cliPerms, "cli/js/unit_tests.ts"];
|
const args = [
|
||||||
|
Deno.execPath(),
|
||||||
|
"run",
|
||||||
|
...cliPerms,
|
||||||
|
"cli/js/tests/unit_tests.ts"
|
||||||
|
];
|
||||||
|
|
||||||
const p = Deno.run({
|
const p = Deno.run({
|
||||||
args,
|
args,
|
||||||
|
@ -69,7 +74,7 @@ async function main(): Promise<void> {
|
||||||
let result = 0;
|
let result = 0;
|
||||||
|
|
||||||
if (!actual && !expected) {
|
if (!actual && !expected) {
|
||||||
console.error("Bad cli/js/unit_test.ts output");
|
console.error("Bad cli/js/tests/unit_test.ts output");
|
||||||
result = 1;
|
result = 1;
|
||||||
} else if (expected !== actual) {
|
} else if (expected !== actual) {
|
||||||
result = 1;
|
result = 1;
|
|
@ -1,7 +1,7 @@
|
||||||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
// This test is executed as part of tools/test.py
|
// This test is executed as part of tools/test.py
|
||||||
// But it can also be run manually: ./target/debug/deno cli/js/unit_tests.ts
|
// But it can also be run manually: ./target/debug/deno cli/js/tests/unit_tests.ts
|
||||||
|
|
||||||
import "./blob_test.ts";
|
import "./blob_test.ts";
|
||||||
import "./body_test.ts";
|
import "./body_test.ts";
|
||||||
|
@ -33,7 +33,7 @@ import "./link_test.ts";
|
||||||
import "./location_test.ts";
|
import "./location_test.ts";
|
||||||
import "./make_temp_test.ts";
|
import "./make_temp_test.ts";
|
||||||
import "./metrics_test.ts";
|
import "./metrics_test.ts";
|
||||||
import "./mixins/dom_iterable_test.ts";
|
import "./dom_iterable_test.ts";
|
||||||
import "./mkdir_test.ts";
|
import "./mkdir_test.ts";
|
||||||
import "./net_test.ts";
|
import "./net_test.ts";
|
||||||
import "./os_test.ts";
|
import "./os_test.ts";
|
||||||
|
@ -52,8 +52,8 @@ import "./stat_test.ts";
|
||||||
import "./symbols_test.ts";
|
import "./symbols_test.ts";
|
||||||
import "./symlink_test.ts";
|
import "./symlink_test.ts";
|
||||||
import "./text_encoding_test.ts";
|
import "./text_encoding_test.ts";
|
||||||
import "./timers_test.ts";
|
|
||||||
import "./testing_test.ts";
|
import "./testing_test.ts";
|
||||||
|
import "./timers_test.ts";
|
||||||
import "./tls_test.ts";
|
import "./tls_test.ts";
|
||||||
import "./truncate_test.ts";
|
import "./truncate_test.ts";
|
||||||
import "./tty_test.ts";
|
import "./tty_test.ts";
|
|
@ -274,7 +274,7 @@ fn js_unit_tests() {
|
||||||
.arg("--reload")
|
.arg("--reload")
|
||||||
.arg("--allow-run")
|
.arg("--allow-run")
|
||||||
.arg("--allow-env")
|
.arg("--allow-env")
|
||||||
.arg("cli/js/unit_test_runner.ts")
|
.arg("cli/js/tests/unit_test_runner.ts")
|
||||||
.spawn()
|
.spawn()
|
||||||
.expect("failed to spawn script");
|
.expect("failed to spawn script");
|
||||||
let status = deno.wait().expect("failed to wait for the child process");
|
let status = deno.wait().expect("failed to wait for the child process");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue