Skip to content

Commit

Permalink
Bare strings break colorize (#473)
Browse files Browse the repository at this point in the history
* Stringify before colorize

---------

Co-authored-by: echo-bravo-yahoo <[email protected]>
  • Loading branch information
ecooper and echo-bravo-yahoo authored Dec 5, 2024
1 parent dd4799a commit 9585314
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/config/setup-container.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions src/config/setup-test-container.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
6 changes: 3 additions & 3 deletions src/lib/misc.mjs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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));
}
17 changes: 13 additions & 4 deletions test/query.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit 9585314

Please sign in to comment.