Skip to content

Commit

Permalink
Merge pull request #20 from oclif/mdonnalley/test
Browse files Browse the repository at this point in the history
fix: ensure compatibility with @oclif/test
  • Loading branch information
mdonnalley authored Oct 8, 2024
2 parents 3416dc8 + 1d44eb3 commit 320e102
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"devDependencies": {
"@commitlint/config-conventional": "^19",
"@oclif/prettier-config": "^0.2.1",
"@oclif/test": "^4.0.9",
"@types/chai": "^4.3.16",
"@types/mocha": "^10.0.8",
"@types/node": "^18",
Expand Down
31 changes: 24 additions & 7 deletions src/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,9 @@ export function Skeleton(props: React.PropsWithChildren & {readonly height?: num
* because it uses ansiEscapes.clearTerminal, which doesn't seem to have
* the desired effect in powershell.
*/
class Stdout extends WriteStream {
class Stream extends WriteStream {
private frames: string[] = []

public lastFrame(): string | undefined {
return this.frames.filter((f) => stripAnsi(f) !== '').at(-1)
}
Expand All @@ -403,15 +404,31 @@ class Stdout extends WriteStream {
}
}

class Output {
public stream: Stream | WriteStream

public constructor(fd = 1) {
this.stream = process.env.NODE_ENV === 'test' ? process.stdout : new WriteStream(fd)
}

public maybePrintLastFrame() {
if (this.stream instanceof Stream) {
process.stdout.write(`${this.stream.lastFrame()}\n`)
} else {
process.stdout.write('\n')
}
}
}

/**
* Renders a table with the given data.
* @param options see {@link TableOptions}
*/
export function printTable<T extends Record<string, unknown>>(options: TableOptions<T>): void {
const stdout = new Stdout(1)
const instance = render(<Table {...options} />, {stdout})
const output = new Output()
const instance = render(<Table {...options} />, {stdout: output.stream})
instance.unmount()
process.stdout.write(`${stdout.lastFrame()}\n`)
output.maybePrintLastFrame()
}

function Container(props: ContainerProps) {
Expand All @@ -426,7 +443,7 @@ export function printTables<T extends Record<string, unknown>[]>(
tables: {[P in keyof T]: TableOptions<T[P]>},
options?: Omit<ContainerProps, 'children'>,
): void {
const stdout = new Stdout(1)
const output = new Output()
const leftMargin = options?.marginLeft ?? options?.margin ?? 0
const rightMargin = options?.marginRight ?? options?.margin ?? 0
const columns = process.stdout.columns - (leftMargin + rightMargin)
Expand All @@ -443,8 +460,8 @@ export function printTables<T extends Record<string, unknown>[]>(
<Table key={sha1(table)} {...table} />
))}
</Container>,
{stdout},
{stdout: output.stream},
)
instance.unmount()
process.stdout.write(`${stdout.lastFrame()}\n`)
output.maybePrintLastFrame()
}
30 changes: 29 additions & 1 deletion test/table.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* eslint-disable perfectionist/sort-objects */
import {captureOutput} from '@oclif/test'
import ansis from 'ansis'
import {config, expect} from 'chai'
import {Box} from 'ink'
import {render} from 'ink-testing-library'
import React from 'react'

import {Cell, Header, Skeleton, Table, formatTextWithMargins} from '../src/table.js'
import {Cell, Header, Skeleton, Table, formatTextWithMargins, printTable} from '../src/table.js'

config.truncateThreshold = 0

Expand Down Expand Up @@ -689,3 +690,30 @@ scing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.`,
})
})
})

describe('printTable compatibility with @oclif/test', () => {
it('should print a simple table', async () => {
const data = [
{name: 'Foo', age: 12},
{name: 'Bar', age: 15},
]

const expected = `┌──────┬─────┐
│ name │ age │
├──────┼─────┤
│ Foo │ 12 │
├──────┼─────┤
│ Bar │ 15 │
└──────┴─────┘
`

const {stdout} = await captureOutput(async () =>
printTable({
data,
columns: ['name', 'age'],
}),
)
expect(stdout).to.equal(expected)
})
})
15 changes: 15 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,14 @@
resolved "https://registry.yarnpkg.com/@oclif/prettier-config/-/prettier-config-0.2.1.tgz#1def9f38134f9bfb229257f48a35f7d0d183dc78"
integrity sha512-XB8kwQj8zynXjIIWRm+6gO/r8Qft2xKtwBMSmq1JRqtA6TpwpqECqiu8LosBCyg2JBXuUy2lU23/L98KIR7FrQ==

"@oclif/test@^4.0.9":
version "4.0.9"
resolved "https://registry.yarnpkg.com/@oclif/test/-/test-4.0.9.tgz#c4b4b4878911489a79f296a15448e76d860b39d2"
integrity sha512-xDGBFBNE6ckoBT9EhMi6ZvwAaEeJRGvRmn2qZWujJl9EJ56a72KHZsvTJVgl2p/AQ2vZ1UH06YZ440GOnjExzQ==
dependencies:
ansis "^3.3.2"
debug "^4.3.6"

"@rtsao/scc@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@rtsao/scc/-/scc-1.1.0.tgz#927dd2fae9bc3361403ac2c7a00c32ddce9ad7e8"
Expand Down Expand Up @@ -1218,6 +1226,13 @@ debug@^4.3.1, debug@^4.3.2, debug@^4.3.4, debug@^4.3.5, debug@~4.3.6:
dependencies:
ms "2.1.2"

debug@^4.3.6:
version "4.3.7"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.7.tgz#87945b4151a011d76d95a198d7111c865c360a52"
integrity sha512-Er2nc/H7RrMXZBFCEim6TCmMk02Z8vLC2Rbi1KEBggpo0fS6l0S1nnapwmIi3yW/+GOJap1Krg4w0Hg80oCqgQ==
dependencies:
ms "^2.1.3"

decamelize@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-4.0.0.tgz#aa472d7bf660eb15f3494efd531cab7f2a709837"
Expand Down

0 comments on commit 320e102

Please sign in to comment.