From 95853143d256efbd8fe4c53a176d6db2773a2a5e Mon Sep 17 00:00:00 2001 From: "E. Cooper" Date: Thu, 5 Dec 2024 08:23:19 -0800 Subject: [PATCH] Bare strings break colorize (#473) * Stringify before colorize --------- Co-authored-by: echo-bravo-yahoo --- src/config/setup-container.mjs | 2 ++ src/config/setup-test-container.mjs | 1 + src/lib/misc.mjs | 6 +++--- test/query.mjs | 17 +++++++++++++---- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/config/setup-container.mjs b/src/config/setup-container.mjs index 9d36ccae..55822bc5 100644 --- a/src/config/setup-container.mjs +++ b/src/config/setup-container.mjs @@ -9,6 +9,7 @@ import * as awilix from "awilix"; import { Lifetime } from "awilix"; import fauna from "fauna"; import faunadb from "faunadb"; +import { colorize } from "json-colorizer"; import open from "open"; import updateNotifier from "update-notifier"; @@ -64,6 +65,7 @@ export const injectables = { updateNotifier: awilix.asValue(updateNotifier), fauna: awilix.asValue(fauna), faunadb: awilix.asValue(faunadb), + colorize: awilix.asValue(colorize), // generic lib (homemade utilities) parseYargs: awilix.asValue(parseYargs), diff --git a/src/config/setup-test-container.mjs b/src/config/setup-test-container.mjs index b59116ec..349644a4 100644 --- a/src/config/setup-test-container.mjs +++ b/src/config/setup-test-container.mjs @@ -73,6 +73,7 @@ export function setupTestContainer() { faunadb: awilix.asValue({ Client: stub(), }), + colorize: awilix.asValue(stub().returnsArg(0)), logger: awilix.asFunction((cradle) => spy(buildLogger(cradle))).singleton(), AccountClient: awilix.asValue(() => ({ startOAuthRequest: stub(), diff --git a/src/lib/misc.mjs b/src/lib/misc.mjs index 96755c31..c372fa84 100644 --- a/src/lib/misc.mjs +++ b/src/lib/misc.mjs @@ -1,7 +1,7 @@ import util from "node:util"; import { createContext, runInContext } from "node:vm"; -import { colorize } from "json-colorizer"; +import { container } from "../cli.mjs"; export async function runQuery(expression, client) { const faunadb = (await import("faunadb")).default; @@ -50,7 +50,7 @@ export function formatObjectForShell(obj, { color = true } = {}) { return JSON.stringify(obj, null, 2); } - return colorize(obj); + return container.resolve("colorize")(JSON.stringify(obj, null, 2)); } /** @@ -67,5 +67,5 @@ export function formatFullErrorForShell(err, { color = true } = {}) { return JSON.stringify(err, null, 2); } - return colorize(err); + return container.resolve("colorize")(JSON.stringify(err, null, 2)); } diff --git a/test/query.mjs b/test/query.mjs index f8c68cd8..bd7141fa 100644 --- a/test/query.mjs +++ b/test/query.mjs @@ -2,7 +2,6 @@ import { expect } from "chai"; import { ServiceError } from "fauna"; -import { colorize } from "json-colorizer"; import sinon from "sinon"; import { run } from "../src/cli.mjs"; @@ -144,12 +143,22 @@ describe("query", function () { ); }); - // This test is skipped for now because we need to figure out a clean way to - // toggle whether our test stdout is a TTY or not. it.skip("can colorize output by default", async function () { runQueryFromString.resolves({ data: [] }); await run(`query "Database.all()" --secret=foo`, container); - expect(logger.stdout).to.have.been.calledWith(colorize([])); + + const expected = JSON.stringify([], null, 2); + expect(logger.stdout).to.have.been.calledWith(expected); + expect(container.resolve("colorize")).to.have.been.calledWith(expected); + }); + + it.skip("can colorize bare strings", async function () { + runQueryFromString.resolves({ data: "foo" }); + await run(`query "foo" --secret=foo`, container); + + const expected = JSON.stringify("foo", null, 2); + expect(logger.stdout).to.have.been.calledWith(expected); + expect(container.resolve("colorize")).to.have.been.calledWith(expected); }); it("does not colorize output if --no-color is used", async function () {