From 4dc004f0a24fdc81026ec03326b9943a95f1a31d Mon Sep 17 00:00:00 2001 From: Yusuke Sakurai Date: Tue, 3 Mar 2020 23:51:07 +0900 Subject: [PATCH] misc: reduce unnecesarry output in cli/js tests (#4182) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bartek IwaƄczuk --- cli/js/console_test.ts | 134 ++++++++++++++++++++------------------- cli/js/dir_test.ts | 4 +- cli/js/fs_events_test.ts | 2 - cli/js/location_test.ts | 1 - cli/js/metrics_test.ts | 2 +- cli/js/os_test.ts | 1 - cli/js/process_test.ts | 6 +- cli/js/timers_test.ts | 1 - 8 files changed, 76 insertions(+), 75 deletions(-) diff --git a/cli/js/console_test.ts b/cli/js/console_test.ts index d9492cf2db..344f93ac6f 100644 --- a/cli/js/console_test.ts +++ b/cli/js/console_test.ts @@ -37,16 +37,17 @@ test(function consoleHasRightInstance(): void { }); test(function consoleTestAssertShouldNotThrowError(): void { - console.assert(true); - - let hasThrown = undefined; - try { - console.assert(false); - hasThrown = false; - } catch { - hasThrown = true; - } - assertEquals(hasThrown, false); + mockConsole(console => { + console.assert(true); + let hasThrown = undefined; + try { + console.assert(false); + hasThrown = false; + } catch { + hasThrown = true; + } + assertEquals(hasThrown, false); + }); }); test(function consoleTestStringifyComplexObjects(): void { @@ -302,19 +303,18 @@ test(function consoleTestWithVariousOrInvalidFormatSpecifier(): void { test(function consoleTestCallToStringOnLabel(): void { const methods = ["count", "countReset", "time", "timeLog", "timeEnd"]; - - for (const method of methods) { - let hasCalled = false; - - // @ts-ignore - console[method]({ - toString(): void { - hasCalled = true; - } - }); - - assertEquals(hasCalled, true); - } + mockConsole(console => { + for (const method of methods) { + let hasCalled = false; + // @ts-ignore + console[method]({ + toString(): void { + hasCalled = true; + } + }); + assertEquals(hasCalled, true); + } + }); }); test(function consoleTestError(): void { @@ -355,40 +355,42 @@ test(function consoleTestClear(): void { // Test bound this issue test(function consoleDetachedLog(): void { - const log = console.log; - const dir = console.dir; - const dirxml = console.dirxml; - const debug = console.debug; - const info = console.info; - const warn = console.warn; - const error = console.error; - const consoleAssert = console.assert; - const consoleCount = console.count; - const consoleCountReset = console.countReset; - const consoleTable = console.table; - const consoleTime = console.time; - const consoleTimeLog = console.timeLog; - const consoleTimeEnd = console.timeEnd; - const consoleGroup = console.group; - const consoleGroupEnd = console.groupEnd; - const consoleClear = console.clear; - log("Hello world"); - dir("Hello world"); - dirxml("Hello world"); - debug("Hello world"); - info("Hello world"); - warn("Hello world"); - error("Hello world"); - consoleAssert(true); - consoleCount("Hello world"); - consoleCountReset("Hello world"); - consoleTable({ test: "Hello world" }); - consoleTime("Hello world"); - consoleTimeLog("Hello world"); - consoleTimeEnd("Hello world"); - consoleGroup("Hello world"); - consoleGroupEnd(); - consoleClear(); + mockConsole(console => { + const log = console.log; + const dir = console.dir; + const dirxml = console.dirxml; + const debug = console.debug; + const info = console.info; + const warn = console.warn; + const error = console.error; + const consoleAssert = console.assert; + const consoleCount = console.count; + const consoleCountReset = console.countReset; + const consoleTable = console.table; + const consoleTime = console.time; + const consoleTimeLog = console.timeLog; + const consoleTimeEnd = console.timeEnd; + const consoleGroup = console.group; + const consoleGroupEnd = console.groupEnd; + const consoleClear = console.clear; + log("Hello world"); + dir("Hello world"); + dirxml("Hello world"); + debug("Hello world"); + info("Hello world"); + warn("Hello world"); + error("Hello world"); + consoleAssert(true); + consoleCount("Hello world"); + consoleCountReset("Hello world"); + consoleTable({ test: "Hello world" }); + consoleTime("Hello world"); + consoleTimeLog("Hello world"); + consoleTimeEnd("Hello world"); + consoleGroup("Hello world"); + consoleGroupEnd(); + consoleClear(); + }); }); class StringBuffer { @@ -652,14 +654,16 @@ test(function consoleTable(): void { // console.log(Error) test test(function consoleLogShouldNotThrowError(): void { - let result = 0; - try { - console.log(new Error("foo")); - result = 1; - } catch (e) { - result = 2; - } - assertEquals(result, 1); + mockConsole(console => { + let result = 0; + try { + console.log(new Error("foo")); + result = 1; + } catch (e) { + result = 2; + } + assertEquals(result, 1); + }); // output errors to the console should not include "Uncaught" mockConsole((console, out): void => { diff --git a/cli/js/dir_test.ts b/cli/js/dir_test.ts index 6444f35a3b..38e80f543b 100644 --- a/cli/js/dir_test.ts +++ b/cli/js/dir_test.ts @@ -30,7 +30,7 @@ testPerm({ write: true }, function dirCwdError(): void { throw Error("current directory removed, should throw error"); } catch (err) { if (err instanceof Deno.errors.NotFound) { - console.log(err.name === "NotFound"); + assert(err.name === "NotFound"); } else { throw Error("raised different exception"); } @@ -46,7 +46,7 @@ testPerm({ write: true }, function dirChdirError(): void { throw Error("directory not available, should throw error"); } catch (err) { if (err instanceof Deno.errors.NotFound) { - console.log(err.name === "NotFound"); + assert(err.name === "NotFound"); } else { throw Error("raised different exception"); } diff --git a/cli/js/fs_events_test.ts b/cli/js/fs_events_test.ts index 86a5c28135..2b1a70bcc8 100644 --- a/cli/js/fs_events_test.ts +++ b/cli/js/fs_events_test.ts @@ -19,7 +19,6 @@ async function getTwoEvents( ): Promise { const events = []; for await (const event of iter) { - console.log(">>>> event", event); events.push(event); if (events.length > 2) break; } @@ -43,7 +42,6 @@ testPerm({ read: true, write: true }, async function fsEventsBasic(): Promise< // We should have gotten two fs events. const events = await eventsPromise; - console.log("events", events); assert(events.length >= 2); assert(events[0].kind == "create"); assert(events[0].paths[0].includes(testDir)); diff --git a/cli/js/location_test.ts b/cli/js/location_test.ts index 663e206efe..805ba3e737 100644 --- a/cli/js/location_test.ts +++ b/cli/js/location_test.ts @@ -3,6 +3,5 @@ import { test, assert } from "./test_util.ts"; test(function locationBasic(): void { // location example: file:///Users/rld/src/deno/js/unit_tests.ts - console.log("location", window.location.toString()); assert(window.location.toString().endsWith("unit_tests.ts")); }); diff --git a/cli/js/metrics_test.ts b/cli/js/metrics_test.ts index 3bb5218ef9..109cd1a113 100644 --- a/cli/js/metrics_test.ts +++ b/cli/js/metrics_test.ts @@ -13,7 +13,7 @@ test(async function metrics(): Promise { // Write to stdout to ensure a "data" message gets sent instead of just // control messages. - const dataMsg = new Uint8Array([41, 42, 43]); + const dataMsg = new Uint8Array([13, 13, 13]); // "\r\r\r", await Deno.stdout.write(dataMsg); const m2 = Deno.metrics(); diff --git a/cli/js/os_test.ts b/cli/js/os_test.ts index b0561840bb..35b9f126af 100644 --- a/cli/js/os_test.ts +++ b/cli/js/os_test.ts @@ -112,7 +112,6 @@ if (Deno.build.os === "win") { } test(function osPid(): void { - console.log("pid", Deno.pid); assert(Deno.pid > 0); }); diff --git a/cli/js/process_test.ts b/cli/js/process_test.ts index 29467cc67c..669e11368d 100644 --- a/cli/js/process_test.ts +++ b/cli/js/process_test.ts @@ -21,13 +21,15 @@ test(function runPermissions(): void { testPerm({ run: true }, async function runSuccess(): Promise { const p = run({ - args: ["python", "-c", "print('hello world')"] + args: ["python", "-c", "print('hello world')"], + stdout: "piped", + stderr: "null" }); const status = await p.status(); - console.log("status", status); assertEquals(status.success, true); assertEquals(status.code, 0); assertEquals(status.signal, undefined); + p.stdout!.close(); p.close(); }); diff --git a/cli/js/timers_test.ts b/cli/js/timers_test.ts index 84811ff119..06367fc6d5 100644 --- a/cli/js/timers_test.ts +++ b/cli/js/timers_test.ts @@ -297,7 +297,6 @@ test(async function timerMaxCpuBug(): Promise { const { opsDispatched } = Deno.metrics(); await waitForMs(100); const opsDispatched_ = Deno.metrics().opsDispatched; - console.log("opsDispatched", opsDispatched, "opsDispatched_", opsDispatched_); assert(opsDispatched_ - opsDispatched < 10); });