Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(repl): refactor to use processInteract #373

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 71 additions & 69 deletions tests/specs/repl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { testSuite } from 'manten';
import { tsx } from '../utils/tsx';
import { processInteract } from '../utils/process-interact';

export default testSuite(async ({ describe }) => {
describe('REPL', ({ test }) => {
Expand All @@ -8,25 +9,25 @@ export default testSuite(async ({ describe }) => {
args: ['--interactive'],
});

const commands = [
'const message: string = "SUCCESS"',
'message',
];

await new Promise<void>((resolve) => {
tsxProcess.stdout!.on('data', (data: Buffer) => {
const chunkString = data.toString();

if (chunkString.includes('SUCCESS')) {
return resolve();
}

if (chunkString.includes('> ') && commands.length > 0) {
const command = commands.shift();
tsxProcess.stdin!.write(`${command}\r`);
}
});
});
await processInteract(
tsxProcess.stdout!,
[
(data) => {
if (data.includes('> ')) {
tsxProcess.stdin!.write('const message: string = "SUCCESS"\r');
return true;
}
},
(data) => {
if (data.includes('> ')) {
tsxProcess.stdin!.write('message\r');
return true;
}
},
data => data.includes('SUCCESS'),
],
5000,
);

tsxProcess.kill();
}, 40_000);
Expand All @@ -36,23 +37,19 @@ export default testSuite(async ({ describe }) => {
args: ['--interactive'],
});

await new Promise<void>((resolve, reject) => {
tsxProcess.stdout!.on('data', (data: Buffer) => {
const chunkString = data.toString();

if (chunkString.includes('unsupported-require-call')) {
return reject(chunkString);
}

if (chunkString.includes('[Function: resolve]')) {
return resolve();
}

if (chunkString.includes('> ')) {
tsxProcess.stdin!.write('require("path")\r');
}
});
});
await processInteract(
tsxProcess.stdout!,
[
(data) => {
if (data.includes('> ')) {
tsxProcess.stdin!.write('require("path")\r');
return true;
}
},
data => data.includes('[Function: resolve]'),
],
5000,
);

tsxProcess.kill();
}, 40_000);
Expand All @@ -62,27 +59,32 @@ export default testSuite(async ({ describe }) => {
args: ['--interactive'],
});

const commands = [
['> ', '('],
['... ', '1'],
['... ', ')'],
['1'],
];

let [expected, nextCommand] = commands.shift()!;
await new Promise<void>((resolve) => {
tsxProcess.stdout!.on('data', (data: Buffer) => {
const chunkString = data.toString();
if (chunkString.includes(expected)) {
if (nextCommand) {
tsxProcess.stdin!.write(`${nextCommand}\r`);
[expected, nextCommand] = commands.shift()!;
} else {
resolve();
await processInteract(
tsxProcess.stdout!,
[
(data) => {
if (data.includes('> ')) {
tsxProcess.stdin!.write('(\r');
return true;
}
}
});
});
},
(data) => {
if (data.includes('... ')) {
tsxProcess.stdin!.write('1\r');
return true;
}
},
(data) => {
if (data.includes('... ')) {
tsxProcess.stdin!.write(')\r');
return true;
}
},
data => data.includes('1'),
],
5000,
);

tsxProcess.kill();
}, 40_000);

Expand All @@ -91,19 +93,19 @@ export default testSuite(async ({ describe }) => {
args: ['--interactive'],
});

await new Promise<void>((resolve) => {
tsxProcess.stdout!.on('data', (data: Buffer) => {
const chunkString = data.toString();

if (chunkString.includes('SyntaxError: Cannot use import statement')) {
return resolve();
}

if (chunkString.includes('> ')) {
tsxProcess.stdin!.write('import fs from "fs"\r');
}
});
});
await processInteract(
tsxProcess.stdout!,
[
(data) => {
if (data.includes('> ')) {
tsxProcess.stdin!.write('import fs from "fs"\r');
return true;
}
},
data => data.includes('SyntaxError: Cannot use import statement'),
],
5000,
);

tsxProcess.kill();
}, 40_000);
Expand Down
53 changes: 7 additions & 46 deletions tests/specs/watch.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,9 @@
import { type Readable } from 'node:stream';
import path from 'path';
import { setTimeout } from 'timers/promises';
import { on } from 'events';
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import { tsx } from '../utils/tsx';

type MaybePromise<T> = T | Promise<T>;
const interact = async (
stdout: Readable,
actions: ((data: string) => MaybePromise<boolean | void>)[],
timeout: number,
) => {
const startTime = Date.now();
const logs: [time: number, string][] = [];

let currentAction = actions.shift();

setTimeout(timeout).then(
() => {
if (currentAction) {
console.error(`Timeout ${timeout}ms exceeded:`);
console.log(logs);
}
},
() => {},
);

while (currentAction) {
for await (const [chunk] of on(stdout, 'data')) {
const chunkString = chunk.toString();
logs.push([
Date.now() - startTime,
chunkString,
]);

const gotoNextAction = await currentAction(chunkString);
if (gotoNextAction) {
currentAction = actions.shift();
break;
}
}
}
};
import { processInteract } from '../utils/process-interact';

export default testSuite(async ({ describe }) => {
describe('watch', async ({ test, describe, onFinish }) => {
Expand Down Expand Up @@ -82,7 +43,7 @@ export default testSuite(async ({ describe }) => {
cwd: fixtureWatch.path,
});

await interact(
await processInteract(
tsxProcess.stdout!,
[
async (data) => {
Expand Down Expand Up @@ -111,7 +72,7 @@ export default testSuite(async ({ describe }) => {
cwd: fixture.path,
});

await interact(
await processInteract(
tsxProcess.stdout!,
[
(data) => {
Expand Down Expand Up @@ -142,7 +103,7 @@ export default testSuite(async ({ describe }) => {
cwd: fixture.path,
});

await interact(
await processInteract(
tsxProcess.stdout!,
[data => data.startsWith('["')],
5000,
Expand Down Expand Up @@ -191,7 +152,7 @@ export default testSuite(async ({ describe }) => {
await fixtureExit.rm();
});

await interact(
await processInteract(
tsxProcess.stdout!,
[
(data) => {
Expand Down Expand Up @@ -232,7 +193,7 @@ export default testSuite(async ({ describe }) => {
cwd: fixture.path,
});

await interact(
await processInteract(
tsxProcess.stdout!,
[data => data.startsWith('["')],
5000,
Expand Down Expand Up @@ -294,7 +255,7 @@ export default testSuite(async ({ describe }) => {

const negativeSignal = 'fail';

await interact(
await processInteract(
tsxProcess.stdout!,
[
async (data) => {
Expand Down
44 changes: 44 additions & 0 deletions tests/utils/process-interact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { Readable } from 'node:stream';
import { on } from 'events';
import { setTimeout } from 'timers/promises';

type MaybePromise<T> = T | Promise<T>;

export const processInteract = async (
stdout: Readable,
actions: ((data: string) => MaybePromise<boolean | void>)[],
timeout: number,
) => {
const startTime = Date.now();
const logs: [time: number, string][] = [];

let currentAction = actions.shift();

const ac = new AbortController();
setTimeout(timeout, true, ac).then(
() => {
if (currentAction) {
console.error(`Timeout ${timeout}ms exceeded:`);
console.log(logs);
}
},
() => {},
);

while (currentAction) {
for await (const [chunk] of on(stdout, 'data')) {
const chunkString = chunk.toString();
logs.push([
Date.now() - startTime,
chunkString,
]);

const gotoNextAction = await currentAction(chunkString);
if (gotoNextAction) {
currentAction = actions.shift();
break;
}
}
}
ac.abort();
};
Loading