summaryrefslogtreecommitdiff
path: root/js/unit_test_runner.ts
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2019-05-09 01:15:24 +0200
committerRyan Dahl <ry@tinyclouds.org>2019-05-08 19:15:24 -0400
commitac8c6fec5bb2be97c8dbdb2286d2688575a593f2 (patch)
tree835335d3d48d77c6d5299af723b91adc2bc857b3 /js/unit_test_runner.ts
parentec9080f34c936d9af56cca68de664954053bf423 (diff)
Refactor unit test runner (#2294)
Properly discovers the permissions needed for each test.
Diffstat (limited to 'js/unit_test_runner.ts')
-rwxr-xr-xjs/unit_test_runner.ts101
1 files changed, 101 insertions, 0 deletions
diff --git a/js/unit_test_runner.ts b/js/unit_test_runner.ts
new file mode 100755
index 000000000..7810e3f45
--- /dev/null
+++ b/js/unit_test_runner.ts
@@ -0,0 +1,101 @@
+#!/usr/bin/env deno run --reload --allow-run
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+import "./unit_tests.ts";
+import { permissionCombinations, parseUnitTestOutput } from "./test_util.ts";
+
+function permsToCliFlags(perms: Deno.Permissions): string[] {
+ return Object.keys(perms)
+ .map(
+ (key): string => {
+ if (!perms[key]) return "";
+
+ const cliFlag = key.replace(
+ /\.?([A-Z])/g,
+ (x, y): string => `-${y.toLowerCase()}`
+ );
+ return `--allow-${cliFlag}`;
+ }
+ )
+ .filter((e): boolean => e.length > 0);
+}
+
+function fmtPerms(perms: Deno.Permissions): string {
+ let fmt = permsToCliFlags(perms).join(" ");
+
+ if (!fmt) {
+ fmt = "<no permissions>";
+ }
+
+ return fmt;
+}
+
+async function main(): Promise<void> {
+ console.log(
+ "Discovered permission combinations for tests:",
+ permissionCombinations.size
+ );
+
+ for (const perms of permissionCombinations.values()) {
+ console.log("\t" + fmtPerms(perms));
+ }
+
+ const testResults = new Set();
+
+ for (const perms of permissionCombinations.values()) {
+ const permsFmt = fmtPerms(perms);
+ console.log(`Running tests for: ${permsFmt}`);
+ const cliPerms = permsToCliFlags(perms);
+ // run subsequent tests using same deno executable
+ const args = [
+ Deno.execPath,
+ "run",
+ "--no-prompt",
+ ...cliPerms,
+ "js/unit_tests.ts"
+ ];
+
+ const p = Deno.run({
+ args,
+ stdout: "piped"
+ });
+
+ const { actual, expected, resultOutput } = parseUnitTestOutput(
+ await p.output(),
+ true
+ );
+
+ let result = 0;
+
+ if (!actual && !expected) {
+ console.error("Bad js/unit_test.ts output");
+ result = 1;
+ } else if (expected !== actual) {
+ result = 1;
+ }
+
+ testResults.add({
+ perms: permsFmt,
+ output: resultOutput,
+ result
+ });
+ }
+
+ // if any run tests returned non-zero status then whole test
+ // run should fail
+ let testsFailed = false;
+
+ for (const testResult of testResults.values()) {
+ console.log(`Summary for ${testResult.perms}`);
+ console.log(testResult.output + "\n");
+ testsFailed = testsFailed || testResult.result;
+ }
+
+ if (testsFailed) {
+ console.error("Unit tests failed");
+ Deno.exit(1);
+ }
+
+ console.log("Unit tests passed");
+}
+
+main();