Skip to content

Commit 532c50a

Browse files
tests: fix Windows tests
1 parent ec316d3 commit 532c50a

File tree

8 files changed

+194
-155
lines changed

8 files changed

+194
-155
lines changed

.github/workflows/test.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ jobs:
3131
shell: bash
3232
run: echo "PNPM_STORE=$(pnpm store path --silent)" >> $GITHUB_ENV
3333

34-
- uses: actions/cache@v3
35-
name: Cache pnpm
34+
- name: Restore pnpm cache
35+
id: pnpm-cache
36+
uses: actions/cache@v3
3637
with:
3738
path: ${{ env.PNPM_STORE }}
3839
key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }}
@@ -41,6 +42,13 @@ jobs:
4142
- name: Install dependencies
4243
run: pnpm install
4344

45+
- name: Save pnpm cache
46+
if: steps.pnpm-cache.outputs.cache-hit != 'true'
47+
uses: actions/cache/save@v3
48+
with:
49+
path: ${{ env.PNPM_STORE }}
50+
key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }}
51+
4452
- name: Get Node.js cache directory
4553
shell: bash
4654
run: echo "NVE_CACHE=$(node -p 'require("cachedir")("nve")')" >> $GITHUB_ENV

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@
101101
"outdent": "^0.8.0",
102102
"pkgroll": "^2.4.1",
103103
"proxyquire": "^2.1.3",
104-
"split2": "^4.2.0",
105104
"strip-ansi": "^7.1.0",
106105
"type-fest": "^4.20.1",
107106
"type-flag": "^3.0.0",

pnpm-lock.yaml

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/debug.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import {
44
options, bgBlue, black, bgLightYellow, bgGray,
55
} from 'kolorist';
66

7-
options.enabled = true;
8-
options.supportLevel = 3;
9-
107
export const debugEnabled = process.env.TSX_DEBUG;
118

9+
// Force colors in debug mode
10+
if (debugEnabled) {
11+
options.enabled = true;
12+
options.supportLevel = 3;
13+
}
14+
1215
const createLog = (
1316
name: string,
1417
) => (

tests/specs/cli.ts

Lines changed: 74 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,11 @@ export default testSuite(({ describe }, node: NodeApis) => {
149149
const signals = ${JSON.stringify(signals)};
150150
151151
for (const name of signals) {
152-
process.on(name, () => {
153-
console.log(name);
154-
155-
setTimeout(() => {
156-
console.log(name, 'HANDLER COMPLETED');
152+
process.once(name, () => {
153+
console.log(name, 'PRESS AGAIN');
154+
process.once(name, () => {
157155
process.exit(200);
158-
}, 200);
156+
});
159157
});
160158
}
161159
@@ -203,6 +201,12 @@ export default testSuite(({ describe }, node: NodeApis) => {
203201
tsxProcess.kill(signal, {
204202
forceKillAfterTimeout: false,
205203
});
204+
205+
tsxProcess.stdout!.once('data', () => {
206+
tsxProcess.kill(signal, {
207+
forceKillAfterTimeout: false,
208+
});
209+
});
206210
});
207211

208212
const tsxProcessResolved = await tsxProcess;
@@ -225,8 +229,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
225229
expect(tsxProcessResolved.exitCode).toBe(200);
226230
expectMatchInOrder(tsxProcessResolved.stdout, [
227231
'READY\n',
228-
`${signal}\n`,
229-
`${signal} HANDLER COMPLETED`,
232+
`${signal} PRESS AGAIN`,
230233
]);
231234
}
232235
}, {
@@ -306,53 +309,80 @@ export default testSuite(({ describe }, node: NodeApis) => {
306309
describe('Ctrl + C', ({ test }) => {
307310
const CtrlC = '\u0003';
308311

309-
test('Exit code', async () => {
310-
const output = await ptyShell(
311-
[
312-
// Windows doesn't support shebangs
313-
`${node.path} ${tsxPath} ${fixture.getPath('keep-alive.js')}\r`,
314-
stdout => stdout.includes('READY') && CtrlC,
315-
`echo EXIT_CODE: ${isWindows ? '$LastExitCode' : '$?'}\r`,
316-
],
317-
);
318-
expect(output).toMatch(/EXIT_CODE:\s+130/);
312+
test('Exit code', async ({ onTestFail }) => {
313+
const p = ptyShell(async (shell) => {
314+
await shell.waitForPrompt();
315+
// Windows doesn't support shebangs
316+
shell.type(`${node.path} ${tsxPath} ${fixture.getPath('keep-alive.js')}`);
317+
318+
await shell.waitForLine(/READY/);
319+
shell.press(CtrlC);
320+
321+
await shell.waitForPrompt();
322+
shell.type(`echo EXIT_CODE: ${isWindows ? '$LastExitCode' : '$?'}`);
323+
324+
await shell.waitForPrompt();
325+
});
326+
327+
onTestFail(() => {
328+
p.kill();
329+
});
330+
331+
expect(await p.output).toMatch(/EXIT_CODE:\s+130/);
319332
}, 10_000);
320333

321-
test('Catchable', async () => {
322-
const output = await ptyShell(
323-
[
324-
// Windows doesn't support shebangs
325-
`${node.path} ${tsxPath} ${fixture.getPath('catch-signals.js')}\r`,
326-
stdout => stdout.includes('READY') && CtrlC,
327-
`echo EXIT_CODE: ${isWindows ? '$LastExitCode' : '$?'}\r`,
328-
],
329-
9000,
330-
);
331-
332-
expectMatchInOrder(output, [
334+
test('Catchable', async ({ onTestFail }) => {
335+
const p = ptyShell(async (shell) => {
336+
await shell.waitForPrompt();
337+
shell.type(`${node.path} ${tsxPath} ${fixture.getPath('catch-signals.js')}`);
338+
339+
await shell.waitForLine(/READY/);
340+
shell.press(CtrlC);
341+
342+
await shell.waitForLine(/PRESS AGAIN/);
343+
shell.press(CtrlC);
344+
345+
await shell.waitForPrompt();
346+
shell.type(`echo EXIT_CODE: ${isWindows ? '$LastExitCode' : '$?'}`);
347+
348+
await shell.waitForPrompt();
349+
});
350+
351+
onTestFail(() => {
352+
p.kill();
353+
});
354+
355+
expectMatchInOrder(await p.output, [
333356
'READY\r\n',
334357
process.platform === 'win32' ? '' : '^C',
335-
'SIGINT\r\n',
336-
'SIGINT HANDLER COMPLETED\r\n',
358+
'SIGINT PRESS AGAIN\r\n',
337359
/EXIT_CODE:\s+200/,
338360
]);
339361
}, {
340362
timeout: 10_000,
341363
retry: 3,
342364
});
343365

344-
test('Infinite loop', async () => {
345-
const output = await ptyShell(
346-
[
347-
// Windows doesn't support shebangs
348-
`${node.path} ${tsxPath} ${fixture.getPath('infinite-loop.js')}\r`,
349-
stdout => /^\r?\d+$/.test(stdout) && CtrlC,
350-
`echo EXIT_CODE: ${isWindows ? '$LastExitCode' : '$?'}\r`,
351-
],
352-
9000,
353-
);
354-
355-
expect(output).toMatch(/EXIT_CODE:\s+130/);
366+
test('Infinite loop', async ({ onTestFail }) => {
367+
const p = ptyShell(async (shell) => {
368+
await shell.waitForPrompt();
369+
// Windows doesn't support shebangs
370+
shell.type(`${node.path} ${tsxPath} ${fixture.getPath('infinite-loop.js')}`);
371+
372+
await shell.waitForLine(/^\r?\d+$/);
373+
shell.press(CtrlC);
374+
375+
await shell.waitForPrompt();
376+
shell.type(`echo EXIT_CODE: ${isWindows ? '$LastExitCode' : '$?'}`);
377+
378+
await shell.waitForPrompt();
379+
});
380+
381+
onTestFail(() => {
382+
p.kill();
383+
});
384+
385+
expect(await p.output).toMatch(/EXIT_CODE:\s+130/);
356386
}, 10_000);
357387
});
358388
});

tests/specs/watch.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { setTimeout } from 'node:timers/promises';
22
import { testSuite, expect } from 'manten';
33
import { createFixture } from 'fs-fixture';
4-
import stripAnsi from 'strip-ansi';
54
import type { NodeApis } from '../utils/tsx.js';
65
import { processInteract } from '../utils/process-interact.js';
76
import { createPackageJson } from '../fixtures.js';
@@ -62,10 +61,10 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
6261
return true;
6362
}
6463
},
65-
data => stripAnsi(data).includes('[tsx] change in ./value.js Rerunning...\n'),
64+
data => data.includes('[tsx] change in ./value.js Rerunning...\n'),
6665
data => data.includes('goodbye world\n'),
6766
],
68-
5000,
67+
9000,
6968
);
7069

7170
tsxProcess.kill();

tests/utils/process-interact.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Readable } from 'node:stream';
22
import { on } from 'node:events';
33
import { setTimeout } from 'node:timers/promises';
4+
import stripAnsi from 'strip-ansi';
45

56
type OnTimeoutCallback = () => void;
67

@@ -71,7 +72,7 @@ export const processInteract = async (
7172

7273
while (currentAction) {
7374
for await (const [chunk] of on(stdout, 'data')) {
74-
const chunkString = chunk.toString();
75+
const chunkString = stripAnsi(chunk.toString());
7576
logs.push({
7677
time: Date.now() - startTime,
7778
stdout: chunkString,

0 commit comments

Comments
 (0)