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

feat: console logging accommodate cy.log message markdown #263

Merged
merged 24 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 21 additions & 4 deletions src/outputProcessor/consoleProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import CONSTANTS from "../constants";
import type {Log, LogType, MessageData} from "../types";
import utils from "../utils";
import type {PluginOptions} from "../installLogsPrinter.types";
import chalk from "chalk";

Expand Down Expand Up @@ -127,8 +128,8 @@ function consoleProcessor(
severity,
timeString
}) => {
let processedMessage = message;

let {isItalic, isBold, color: messageColor, processedMessage} = utils.checkMessageMarkdown(message);
archfz marked this conversation as resolved.
Show resolved Hide resolved
let {color, icon, trim} = TYPE_COMPUTE[type](options);
trim = trim || options.defaultTrimLength || 800;

Expand All @@ -140,8 +141,24 @@ function consoleProcessor(
icon = LOG_SYMBOLS.warning;
}

if (message.length > trim) {
processedMessage = message.substring(0, trim) + ' ...';
if (processedMessage.length > trim) {
processedMessage = processedMessage.substring(0, trim) + ' ...';
}
if (isItalic) {
processedMessage = chalk.italic(processedMessage)
}
if (isBold) {
processedMessage = chalk.bold(processedMessage)
}
if (messageColor) {
try {
const colorFunction = chalk[messageColor as keyof typeof chalk]
if (colorFunction === chalk) {
processedMessage = colorFunction(processedMessage)
}
}
catch {
}
}

if (timeString) {
Expand Down
37 changes: 36 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,45 @@ const utils = {
return json;
},

validatorErrToStr: function (errorList: Failure[]) {
validatorErrToStr(errorList: Failure[]) {
return '\n' + errorList.map((error) => {
return ` => ${error.path.join('.')}: ${error.message}`;
}).join('\n') + '\n';
},

/**
* The Cypress GUI runner allows markdown in `cy.log` messages. We can take this
* into account for our loggers as well.
* - italic: _text_
* - bold: **text**
* - colored: [color](text)
* https://github.com/cypress-io/cypress-documentation/issues/778
*/
checkMessageMarkdown(message: string) {
const colorMatch = message.match(/^\[(.*)\]\((.*)\)/) // ie [blue](http://example.com)
let color: string | undefined = undefined;
if (colorMatch) {
color = colorMatch[1]
message = colorMatch[2]
}

const italicMatch = message.match(/^_(.*?)_$/); // ie _italic_
if (italicMatch) {
message = italicMatch[1]
}

const boldMatch = message.match(/^\*\*(.*?)\*\*$/); // ie **bold**
if (boldMatch) {
message = boldMatch[1]
}

// TODO: account for both bold and italic?
return {
isItalic: Boolean(italicMatch),
isBold: Boolean(boldMatch),
color,
processedMessage: message
}
}
}

Expand Down
30 changes: 30 additions & 0 deletions test/specs/utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import utils from '../../src/utils'
import {expect} from 'chai'

const {checkMessageMarkdown} = utils

describe('utils', () => {
archfz marked this conversation as resolved.
Show resolved Hide resolved
describe('checkMessageMarkdown', () => {
it('correctly detects cypress log message markdown and returns processed message with markdown removed', () => {
const tests = [
{message: '_text text_text_', isItalic: true, processedMessage: 'text text_text'},
{message: '_text _text'},
{message: 'text text_'},
{message: '**text **text**', isBold: true, processedMessage: 'text **text'},
{message: '*text text**'},
{message: '**text text*'},
{message: '*text text*'},
{message: '[blue](text text)', color: 'blue', processedMessage: 'text text'},
]
tests.forEach(({message, ...expected}) => {
expect(checkMessageMarkdown(message)).to.deep.equal({
color: undefined,
isItalic: false,
isBold: false,
processedMessage: message,
...expected
})
})
})
})
})