Skip to content

Commit

Permalink
test(repl): refactor to use processInteract (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored Nov 9, 2023
1 parent a8fb152 commit 005bcef
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 115 deletions.
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();
};

0 comments on commit 005bcef

Please sign in to comment.