summaryrefslogtreecommitdiff
path: root/cli/js/unit_test_runner.ts
blob: dd095058f179a78dc997e5f9e2cebb7e0a803982 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env -S deno run --reload --allow-run
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import "./unit_tests.ts";
import {
  permissionCombinations,
  parseUnitTestOutput,
  Permissions
} from "./test_util.ts";

interface TestResult {
  perms: string;
  output?: string;
  result: number;
}

function permsToCliFlags(perms: Permissions): string[] {
  return Object.keys(perms)
    .map(key => {
      if (!perms[key as keyof Permissions]) 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: 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<TestResult>();

  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", ...cliPerms, "cli/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 cli/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) {
    console.log(`Summary for ${testResult.perms}`);
    console.log(testResult.output + "\n");
    testsFailed = testsFailed || Boolean(testResult.result);
  }

  if (testsFailed) {
    console.error("Unit tests failed");
    Deno.exit(1);
  }

  console.log("Unit tests passed");
}

main();