Skip to content

Commit ec4ec09

Browse files
authored
test(core): add initial tests (#195)
2 parents 081f357 + fc3a263 commit ec4ec09

File tree

12 files changed

+3523
-1800
lines changed

12 files changed

+3523
-1800
lines changed

.changeset/gold-dryers-bake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@clack/core": patch
3+
---
4+
5+
Fixes a bug which kept the terminal cursor hidden after a prompt is cancelled

.github/workflows/ci.yml

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
name: CI
22

33
on:
4-
push:
5-
branches:
6-
- main
7-
8-
permissions:
9-
contents: write
10-
pull-requests: write
11-
packages: write
4+
pull_request_target:
125

136
# Automatically cancel in-progress actions on the same branch
147
concurrency:
@@ -28,17 +21,5 @@ jobs:
2821
cache: "pnpm"
2922
- if: ${{ steps.cache-node.outputs.cache-hit != 'true' }}
3023
run: pnpm install
31-
# TODO: is this manual build step actually needed?
32-
# `prepack` hook _should_ run but doesn't seem to.
3324
- run: pnpm run type-check
34-
- run: pnpm run build
35-
- uses: changesets/action@v1
36-
if: ${{ github.event_name != 'pull_request' }}
37-
with:
38-
version: pnpm run ci:version
39-
publish: pnpm run ci:publish
40-
commit: "[ci] release"
41-
title: "[ci] release"
42-
env:
43-
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
44-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
25+
- run: pnpm run test

.github/workflows/release.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
permissions:
9+
contents: write
10+
pull-requests: write
11+
packages: write
12+
13+
# Automatically cancel in-progress actions on the same branch
14+
concurrency:
15+
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request_target' && github.head_ref || github.ref }}
16+
cancel-in-progress: true
17+
18+
jobs:
19+
build:
20+
name: Build Packages
21+
runs-on: ubuntu-latest
22+
steps:
23+
- uses: actions/checkout@v3
24+
- uses: pnpm/action-setup@v2
25+
- uses: actions/setup-node@v3
26+
with:
27+
node-version: 18
28+
cache: "pnpm"
29+
- if: ${{ steps.cache-node.outputs.cache-hit != 'true' }}
30+
run: pnpm install
31+
# TODO: is this manual build step actually needed?
32+
# `prepack` hook _should_ run but doesn't seem to.
33+
- run: pnpm run type-check
34+
- run: pnpm run build
35+
- uses: changesets/action@v1
36+
if: ${{ github.event_name != 'pull_request' }}
37+
with:
38+
version: pnpm run ci:version
39+
publish: pnpm run ci:publish
40+
commit: "[ci] release"
41+
title: "[ci] release"
42+
env:
43+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
44+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"format:code": "prettier -w . --cache",
1414
"format:imports": "organize-imports-cli ./packages/*/tsconfig.json",
1515
"type-check": "tsc",
16+
"test": "pnpm -r run test",
1617
"ci:version": "changeset version && pnpm install --no-frozen-lockfile",
1718
"ci:publish": "changeset publish",
1819
"ci:format": "pnpm run format"

packages/core/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@
5050
"packageManager": "[email protected]",
5151
"scripts": {
5252
"build": "unbuild",
53-
"prepack": "pnpm build"
53+
"prepack": "pnpm build",
54+
"test": "vitest run"
5455
},
5556
"dependencies": {
5657
"picocolors": "^1.0.0",
5758
"sisteransi": "^1.0.5"
5859
},
5960
"devDependencies": {
61+
"vitest": "^1.6.0",
6062
"wrap-ansi": "^8.1.0"
6163
}
6264
}

packages/core/src/utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ export function block({
2424
const clear = (data: Buffer, { name }: Key) => {
2525
const str = String(data);
2626
if (str === '\x03') {
27+
if (hideCursor) output.write(cursor.show);
2728
process.exit(0);
29+
return;
2830
}
2931
if (!overwrite) return;
3032
let dx = name === 'return' ? 0 : -1;
@@ -36,12 +38,12 @@ export function block({
3638
});
3739
});
3840
};
39-
if (hideCursor) process.stdout.write(cursor.hide);
41+
if (hideCursor) output.write(cursor.hide);
4042
input.once('keypress', clear);
4143

4244
return () => {
4345
input.off('keypress', clear);
44-
if (hideCursor) process.stdout.write(cursor.show);
46+
if (hideCursor) output.write(cursor.show);
4547

4648
// Prevent Windows specific issues: https://github.com/natemoo-re/clack/issues/176
4749
if (input.isTTY && !isWindows) input.setRawMode(false);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Readable } from 'node:stream';
2+
3+
export class MockReadable extends Readable {
4+
protected _buffer: unknown[] | null = [];
5+
6+
_read() {
7+
if (this._buffer === null) {
8+
this.push(null);
9+
return;
10+
}
11+
12+
for (const val of this._buffer) {
13+
this.push(val);
14+
}
15+
16+
this._buffer = [];
17+
}
18+
19+
pushValue(val: unknown): void {
20+
this._buffer?.push(val);
21+
}
22+
23+
close(): void {
24+
this._buffer = null;
25+
}
26+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Writable } from 'node:stream';
2+
3+
export class MockWritable extends Writable {
4+
public buffer: string[] = [];
5+
6+
// biome-ignore lint/suspicious/noExplicitAny: any is the official type
7+
_write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null | undefined) => void): void {
8+
this.buffer.push(chunk.toString());
9+
callback();
10+
}
11+
}

0 commit comments

Comments
 (0)