From 2774a070d66a8d52d58119b905d5a8c4d039971f Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Fri, 27 Sep 2024 11:57:14 +1000 Subject: [PATCH] fix(log): ensure consistent behavior with `@std/log` (#5974) * fix(log): ensure consistent behavior with `@std/log` * update * fixes --- log/critical.ts | 1 + log/critical_test.ts | 9 +++++++++ log/debug.ts | 1 + log/debug_test.ts | 1 - log/error.ts | 1 + log/error_test.ts | 9 +++++++++ log/info.ts | 1 + log/info_test.ts | 9 +++++++++ log/warn.ts | 1 + log/warn_test.ts | 9 +++++++++ 10 files changed, 41 insertions(+), 1 deletion(-) diff --git a/log/critical.ts b/log/critical.ts index 62c772f9a..e143e44e7 100644 --- a/log/critical.ts +++ b/log/critical.ts @@ -3,6 +3,7 @@ import { getLogger } from "./get_logger.ts"; import type { GenericFunction } from "./logger.ts"; +import "./setup.ts"; /** Log with critical level, using default logger. */ export function critical(msg: () => T, ...args: unknown[]): T | undefined; diff --git a/log/critical_test.ts b/log/critical_test.ts index 80b070d3d..2b8f9aa05 100644 --- a/log/critical_test.ts +++ b/log/critical_test.ts @@ -1,10 +1,19 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. import { assertEquals } from "@std/assert"; +import { assertSpyCall, spy } from "@std/testing/mock"; +import { bold, red } from "@std/fmt/colors"; import { critical } from "./critical.ts"; Deno.test("critical()", () => { + using consoleInfoSpy = spy(console, "log"); const criticalData: string = critical("foo"); const criticalResolver: string | undefined = critical(() => "bar"); assertEquals(criticalData, "foo"); assertEquals(criticalResolver, "bar"); + assertSpyCall(consoleInfoSpy, 0, { + args: [bold(red("CRITICAL foo"))], + }); + assertSpyCall(consoleInfoSpy, 1, { + args: [bold(red("CRITICAL bar"))], + }); }); diff --git a/log/debug.ts b/log/debug.ts index 0c6d6663d..129748b1a 100644 --- a/log/debug.ts +++ b/log/debug.ts @@ -3,6 +3,7 @@ import { getLogger } from "./get_logger.ts"; import type { GenericFunction } from "./logger.ts"; +import "./setup.ts"; /** Log with debug level, using default logger. */ export function debug(msg: () => T, ...args: unknown[]): T | undefined; diff --git a/log/debug_test.ts b/log/debug_test.ts index 27779cbe3..3251521fb 100644 --- a/log/debug_test.ts +++ b/log/debug_test.ts @@ -1,6 +1,5 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. import { assertEquals } from "@std/assert"; -import "./setup.ts"; import { debug } from "./debug.ts"; Deno.test("debug()", () => { diff --git a/log/error.ts b/log/error.ts index 51aa89102..8d53e6c51 100644 --- a/log/error.ts +++ b/log/error.ts @@ -3,6 +3,7 @@ import { getLogger } from "./get_logger.ts"; import type { GenericFunction } from "./logger.ts"; +import "./setup.ts"; /** Log with error level, using default logger. */ export function error(msg: () => T, ...args: unknown[]): T | undefined; diff --git a/log/error_test.ts b/log/error_test.ts index 6ea1c5dd2..02b3024d2 100644 --- a/log/error_test.ts +++ b/log/error_test.ts @@ -1,10 +1,19 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. import { assertEquals } from "@std/assert"; +import { assertSpyCall, spy } from "@std/testing/mock"; +import { red } from "@std/fmt/colors"; import { error } from "./error.ts"; Deno.test("error()", () => { + using consoleInfoSpy = spy(console, "log"); const errorData: undefined = error(undefined, 1, 2, 3); const errorResolver: bigint | undefined = error(() => 5n); assertEquals(errorData, undefined); assertEquals(errorResolver, 5n); + assertSpyCall(consoleInfoSpy, 0, { + args: [red("ERROR undefined")], + }); + assertSpyCall(consoleInfoSpy, 1, { + args: [red("ERROR 5")], + }); }); diff --git a/log/info.ts b/log/info.ts index d86405d2c..fa10ae12b 100644 --- a/log/info.ts +++ b/log/info.ts @@ -3,6 +3,7 @@ import { getLogger } from "./get_logger.ts"; import type { GenericFunction } from "./logger.ts"; +import "./setup.ts"; /** Log with info level, using default logger. */ export function info(msg: () => T, ...args: unknown[]): T | undefined; diff --git a/log/info_test.ts b/log/info_test.ts index bb8788681..9d0e2a40d 100644 --- a/log/info_test.ts +++ b/log/info_test.ts @@ -1,10 +1,19 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. import { assertEquals } from "@std/assert"; +import { assertSpyCall, spy } from "@std/testing/mock"; +import { blue } from "@std/fmt/colors"; import { info } from "./info.ts"; Deno.test("info()", () => { + using consoleInfoSpy = spy(console, "log"); const infoData: number = info(456, 1, 2, 3); const infoResolver: boolean | undefined = info(() => true); assertEquals(infoData, 456); assertEquals(infoResolver, true); + assertSpyCall(consoleInfoSpy, 0, { + args: [blue("INFO 456")], + }); + assertSpyCall(consoleInfoSpy, 1, { + args: [blue("INFO true")], + }); }); diff --git a/log/warn.ts b/log/warn.ts index 95883693e..66d067c93 100644 --- a/log/warn.ts +++ b/log/warn.ts @@ -3,6 +3,7 @@ import { getLogger } from "./get_logger.ts"; import type { GenericFunction } from "./logger.ts"; +import "./setup.ts"; export type { GenericFunction }; diff --git a/log/warn_test.ts b/log/warn_test.ts index b5612285c..cb81c8f83 100644 --- a/log/warn_test.ts +++ b/log/warn_test.ts @@ -1,11 +1,20 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. import { assertEquals } from "@std/assert"; +import { assertSpyCall, spy } from "@std/testing/mock"; +import { yellow } from "@std/fmt/colors"; import { warn } from "./warn.ts"; Deno.test("warn()", () => { + using consoleInfoSpy = spy(console, "log"); const sym = Symbol("a"); const warnData: symbol = warn(sym); const warnResolver: null | undefined = warn(() => null); assertEquals(warnData, sym); assertEquals(warnResolver, null); + assertSpyCall(consoleInfoSpy, 0, { + args: [yellow("WARN Symbol(a)")], + }); + assertSpyCall(consoleInfoSpy, 1, { + args: [yellow("WARN null")], + }); });