Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix colorizing summaries when outputted via --include #548

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/lib/fauna-client.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
//@ts-check

import stripAnsi from "strip-ansi";

import { container } from "../cli.mjs";
import { isUnknownError } from "./errors.mjs";
import { faunaToCommandError } from "./fauna.mjs";
import { faunadbToCommandError } from "./faunadb.mjs";
import { colorize, Format } from "./formatting/colorize.mjs";

const SUMMARY_FQL_REGEX = /^(\s\s\|)|(\d\s\|)/;
/**
* Regex to match the FQL diagnostic line.
* @type {RegExp}
*/
export const FQL_DIAGNOSTIC_REGEX = /^(\s{2,}\|)|(\s*\d{1,}\s\|)/;

/**
* Gets a secret for the current credentials.
Expand Down Expand Up @@ -168,7 +174,7 @@

try {
const lines = summary.split("\n").map((line) => {
if (!line.match(SUMMARY_FQL_REGEX)) {
if (!line.match(FQL_DIAGNOSTIC_REGEX)) {
return line;
}
return colorize(line, { format: Format.FQL });
Expand All @@ -187,7 +193,7 @@
* @param {string[]} include - The query info fields to include
* @returns {object} An object with the selected query info fields
*/
const pickAndCamelCaseQueryInfo = (response, include) => {

Check warning on line 196 in src/lib/fauna-client.mjs

View workflow job for this annotation

GitHub Actions / lint

Arrow function has a complexity of 11. Maximum allowed is 10
const queryInfo = {};

if (include.includes("txnTs") && response.txn_ts)
Expand Down Expand Up @@ -228,14 +234,16 @@

if (Object.keys(queryInfoToDisplay).length === 0) return "";

const SUMMARY_IN_QUERY_INFO_FQL_REGEX = /^(\s\s\s\s\|)|(\d\s\|)/;
// We colorize the entire query info object as YAML, but then need to
// colorize the diagnostic lines individually. To simplify this, we
// strip the ansi when we're checking if the line is a diagnostic line.
const colorized = colorize(queryInfoToDisplay, {
color,
format: Format.YAML,
})
.split("\n")
.map((line) => {
if (!line.match(SUMMARY_IN_QUERY_INFO_FQL_REGEX)) {
if (!stripAnsi(line).match(FQL_DIAGNOSTIC_REGEX)) {
return line;
}
return colorize(line, { format: Format.FQL });
Expand Down
30 changes: 30 additions & 0 deletions test/lib/fauna-client.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect } from "chai";

import { FQL_DIAGNOSTIC_REGEX } from "../../src/lib/fauna-client.mjs";

describe("FQL_DIAGNOSTIC_REGEX", () => {
const validLines = ["1 |", "12 |", "123 |", " |", " |", " |", " 1 |"];

const invalidLines = [
"normal text",
"1 |",
"| invalid",
"abc |",
"|",
"1|",
" | ",
"text | more",
];

validLines.forEach((line) => {
it(`should match diagnostic line: "${line}"`, () => {
expect(line).to.match(FQL_DIAGNOSTIC_REGEX);
});
});

invalidLines.forEach((line) => {
it(`should not match non-diagnostic line: "${line}"`, () => {
expect(line).to.not.match(FQL_DIAGNOSTIC_REGEX);
});
});
});
Loading