Skip to content

Commit

Permalink
fix(logger): appended error messages now rendered properly
Browse files Browse the repository at this point in the history
  • Loading branch information
eysi09 committed Sep 25, 2018
1 parent 5168c0a commit f964b3b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
8 changes: 5 additions & 3 deletions garden-cli/src/logger/renderers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const cliPadEnd = (s: string, width: number): string => {
}
const truncateSection = (s: string) => cliTruncate(s, SECTION_PREFIX_WIDTH)
const sectionStyle = (s: string) => chalk.cyan.italic(cliPadEnd(truncateSection(s), SECTION_PREFIX_WIDTH))
const msgStyle = (s: string) => hasAnsi(s) ? s : chalk.gray(s)
const errorStyle = chalk.red
export const msgStyle = (s: string) => hasAnsi(s) ? s : chalk.gray(s)
export const errorStyle = chalk.red

/*** RENDER HELPERS ***/
function insertVal(out: string[], idx: number, toRender: Function | string, renderArgs: any[]): string[] {
Expand Down Expand Up @@ -111,7 +111,9 @@ export function renderMsg(entry: LogEntry): string {

const styleFn = status === "error" ? errorStyle : msgStyle
if (isArray(msg)) {
return msg.map(styleFn).join(chalk.gray(" → "))
// We apply the style function to each item (as opposed to the entire string) in case some
// part of the message already has a style
return msg.map(str => styleFn(str)).join(styleFn(" → "))
}
return msg ? styleFn(msg) : ""
}
Expand Down
35 changes: 35 additions & 0 deletions garden-cli/test/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BasicTerminalWriter } from "../../src/logger/writers/basic-terminal-wri
import { FancyTerminalWriter } from "../../src/logger/writers/fancy-terminal-writer"
import { getLogger } from "../../src/logger/logger"
import { getChildNodes } from "../../src/logger/util"
import { renderMsg, msgStyle, errorStyle } from "../../src/logger/renderers"

const logger = getLogger()

Expand Down Expand Up @@ -218,6 +219,40 @@ describe("LogEntry", () => {
})
})

describe("renderers", () => {
describe("renderMsg", () => {
it("should return an empty string if the entry is empty", () => {
const entry = logger.info()
expect(renderMsg(entry)).to.equal("")
})
it("should render the message with the message style", () => {
const entry = logger.info({ msg: "hello message" })
expect(renderMsg(entry)).to.equal(msgStyle("hello message"))
})
it("should join an array of messages with an arrow symbol and render with the message style", () => {
const entry = logger.info({ msg: ["message a", "message b"] })
expect(renderMsg(entry)).to.equal(msgStyle("message a") + msgStyle(" → ") + msgStyle("message b"))
})
it("should render the message without styles if the entry is from an intercepted stream", () => {
const entry = logger.info({ fromStdStream: true, msg: "hello stream" })
expect(renderMsg(entry)).to.equal("hello stream")
})
it("should join an array of messages and render without styles if the entry is from an intercepted stream", () => {
const entry = logger.info({ fromStdStream: true, msg: ["stream a", "stream b"] })
expect(renderMsg(entry)).to.equal("stream a stream b")
})
it("should render the message with the error style if the entry has error status", () => {
const entry = logger.info({ msg: "hello error", status: "error" })
expect(renderMsg(entry)).to.equal(errorStyle("hello error"))
})
it("should join an array of messages with an arrow symbol and render with the error style" +
" if the entry has error status", () => {
const entry = logger.info({ msg: ["error a", "error b"], status: "error" })
expect(renderMsg(entry)).to.equal(errorStyle("error a") + errorStyle(" → ") + errorStyle("error b"))
})
})
})

describe("util", () => {
describe("getChildNodes", () => {
it("should convert an n-ary tree into an ordered list of child nodes (skipping the root)", () => {
Expand Down

0 comments on commit f964b3b

Please sign in to comment.