stream output of js unit tests (#4146)

This commit is contained in:
Bartek Iwańczuk 2020-02-26 20:33:18 +01:00 committed by GitHub
parent 671f0b83be
commit 9adcdabd65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 16 deletions

View file

@ -7,6 +7,7 @@
// 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 { assert, assertEquals } from "../../std/testing/asserts.ts"; import { assert, assertEquals } from "../../std/testing/asserts.ts";
export { export {
assert, assert,
@ -140,16 +141,13 @@ function extractNumber(re: RegExp, str: string): number | undefined {
} }
} }
export function parseUnitTestOutput( export async function parseUnitTestOutput(
rawOutput: Uint8Array, reader: Deno.Reader,
print: boolean print: boolean
): { actual?: number; expected?: number; resultOutput?: string } { ): Promise<{ actual?: number; expected?: number; resultOutput?: string }> {
const decoder = new TextDecoder();
const output = decoder.decode(rawOutput);
let expected, actual, result; let expected, actual, result;
for (const line of output.split("\n")) { for await (const line of readLines(reader)) {
if (!expected) { if (!expected) {
// expect "running 30 tests" // expect "running 30 tests"
expected = extractNumber(/running (\d+) tests/, line); expected = extractNumber(/running (\d+) tests/, line);
@ -267,31 +265,32 @@ testPerm({ read: true }, async function parsingUnitTestOutput(): Promise<void> {
let result; let result;
// This is an example of a successful unit test output. // This is an example of a successful unit test output.
result = parseUnitTestOutput( result = await parseUnitTestOutput(
await Deno.readFile(`${testDataPath}/unit_test_output1.txt`), await Deno.open(`${testDataPath}/unit_test_output1.txt`),
false false
); );
assertEquals(result.actual, 96); assertEquals(result.actual, 96);
assertEquals(result.expected, 96); assertEquals(result.expected, 96);
// This is an example of a silently dying unit test. // This is an example of a silently dying unit test.
result = parseUnitTestOutput( result = await parseUnitTestOutput(
await Deno.readFile(`${testDataPath}/unit_test_output2.txt`), await Deno.open(`${testDataPath}/unit_test_output2.txt`),
false false
); );
assertEquals(result.actual, undefined); assertEquals(result.actual, undefined);
assertEquals(result.expected, 96); assertEquals(result.expected, 96);
// This is an example of compiling before successful unit tests. // This is an example of compiling before successful unit tests.
result = parseUnitTestOutput( result = await parseUnitTestOutput(
await Deno.readFile(`${testDataPath}/unit_test_output3.txt`), await Deno.open(`${testDataPath}/unit_test_output3.txt`),
false false
); );
assertEquals(result.actual, 96); assertEquals(result.actual, 96);
assertEquals(result.expected, 96); assertEquals(result.expected, 96);
// Check what happens on empty output. // Check what happens on empty output.
result = parseUnitTestOutput(new TextEncoder().encode("\n\n\n"), false); const f = new Deno.Buffer(new TextEncoder().encode("\n\n\n"));
result = await parseUnitTestOutput(f, false);
assertEquals(result.actual, undefined); assertEquals(result.actual, undefined);
assertEquals(result.expected, undefined); assertEquals(result.expected, undefined);
}); });

View file

@ -61,8 +61,8 @@ async function main(): Promise<void> {
stdout: "piped" stdout: "piped"
}); });
const { actual, expected, resultOutput } = parseUnitTestOutput( const { actual, expected, resultOutput } = await parseUnitTestOutput(
await p.output(), p.stdout!,
true true
); );