Skip to content

Commit

Permalink
test(core): add initial tests (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re authored Nov 22, 2024
2 parents 081f357 + fc3a263 commit ec4ec09
Show file tree
Hide file tree
Showing 12 changed files with 3,523 additions and 1,800 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-dryers-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clack/core": patch
---

Fixes a bug which kept the terminal cursor hidden after a prompt is cancelled
23 changes: 2 additions & 21 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
name: CI

on:
push:
branches:
- main

permissions:
contents: write
pull-requests: write
packages: write
pull_request_target:

# Automatically cancel in-progress actions on the same branch
concurrency:
Expand All @@ -28,17 +21,5 @@ jobs:
cache: "pnpm"
- if: ${{ steps.cache-node.outputs.cache-hit != 'true' }}
run: pnpm install
# TODO: is this manual build step actually needed?
# `prepack` hook _should_ run but doesn't seem to.
- run: pnpm run type-check
- run: pnpm run build
- uses: changesets/action@v1
if: ${{ github.event_name != 'pull_request' }}
with:
version: pnpm run ci:version
publish: pnpm run ci:publish
commit: "[ci] release"
title: "[ci] release"
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: pnpm run test
44 changes: 44 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: CI

on:
push:
branches:
- main

permissions:
contents: write
pull-requests: write
packages: write

# Automatically cancel in-progress actions on the same branch
concurrency:
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request_target' && github.head_ref || github.ref }}
cancel-in-progress: true

jobs:
build:
name: Build Packages
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
- uses: actions/setup-node@v3
with:
node-version: 18
cache: "pnpm"
- if: ${{ steps.cache-node.outputs.cache-hit != 'true' }}
run: pnpm install
# TODO: is this manual build step actually needed?
# `prepack` hook _should_ run but doesn't seem to.
- run: pnpm run type-check
- run: pnpm run build
- uses: changesets/action@v1
if: ${{ github.event_name != 'pull_request' }}
with:
version: pnpm run ci:version
publish: pnpm run ci:publish
commit: "[ci] release"
title: "[ci] release"
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"format:code": "prettier -w . --cache",
"format:imports": "organize-imports-cli ./packages/*/tsconfig.json",
"type-check": "tsc",
"test": "pnpm -r run test",
"ci:version": "changeset version && pnpm install --no-frozen-lockfile",
"ci:publish": "changeset publish",
"ci:format": "pnpm run format"
Expand Down
4 changes: 3 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@
"packageManager": "[email protected]",
"scripts": {
"build": "unbuild",
"prepack": "pnpm build"
"prepack": "pnpm build",
"test": "vitest run"
},
"dependencies": {
"picocolors": "^1.0.0",
"sisteransi": "^1.0.5"
},
"devDependencies": {
"vitest": "^1.6.0",
"wrap-ansi": "^8.1.0"
}
}
6 changes: 4 additions & 2 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export function block({
const clear = (data: Buffer, { name }: Key) => {
const str = String(data);
if (str === '\x03') {
if (hideCursor) output.write(cursor.show);
process.exit(0);
return;
}
if (!overwrite) return;
let dx = name === 'return' ? 0 : -1;
Expand All @@ -36,12 +38,12 @@ export function block({
});
});
};
if (hideCursor) process.stdout.write(cursor.hide);
if (hideCursor) output.write(cursor.hide);
input.once('keypress', clear);

return () => {
input.off('keypress', clear);
if (hideCursor) process.stdout.write(cursor.show);
if (hideCursor) output.write(cursor.show);

// Prevent Windows specific issues: https://github.com/natemoo-re/clack/issues/176
if (input.isTTY && !isWindows) input.setRawMode(false);
Expand Down
26 changes: 26 additions & 0 deletions packages/core/test/mock-readable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Readable } from 'node:stream';

export class MockReadable extends Readable {
protected _buffer: unknown[] | null = [];

_read() {
if (this._buffer === null) {
this.push(null);
return;
}

for (const val of this._buffer) {
this.push(val);
}

this._buffer = [];
}

pushValue(val: unknown): void {
this._buffer?.push(val);
}

close(): void {
this._buffer = null;
}
}
11 changes: 11 additions & 0 deletions packages/core/test/mock-writable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Writable } from 'node:stream';

export class MockWritable extends Writable {
public buffer: string[] = [];

// biome-ignore lint/suspicious/noExplicitAny: any is the official type
_write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null | undefined) => void): void {
this.buffer.push(chunk.toString());
callback();
}
}
Loading

0 comments on commit ec4ec09

Please sign in to comment.