Skip to content

Commit

Permalink
fix(test output): trim test output
Browse files Browse the repository at this point in the history
also extracted the core for run test from dist and docker
  • Loading branch information
rluvaton committed Sep 29, 2023
1 parent d6320e4 commit b30bf08
Showing 1 changed file with 43 additions and 42 deletions.
85 changes: 43 additions & 42 deletions src/test-file.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { execFile, ExecFileException } from 'node:child_process';
import { execFile } from 'node:child_process';
import path from 'node:path';
import type { ChildProcess } from 'node:child_process';

import { downloadNodeImage, downloadNodeVersion } from './download-node.js';
import { InvalidVersionError } from './errors/invalid-version.js';
import { NodeVersion } from './node-versions.js';
import { Ora } from 'ora';
import chalk from 'chalk';
import path from 'node:path';

export async function testVersionDist({
version,
Expand All @@ -27,38 +28,19 @@ export async function testVersionDist({
}

try {
return await runTest({ version, nodePath, testFile, spinner });
return await runTest({
version,
testFile,
spinner,
runTestFn: ({ testFile, execCb }) => {
const child = execFile(nodePath, [testFile], (_: unknown, stdout: string) => execCb({ child, stdout }));
},
});
} finally {
await cleanup();
}
}

export function runTest({
version,
nodePath,
testFile,
spinner,
}: {
version: NodeVersion;
nodePath: string;
testFile: string;
spinner: Ora;
}): Promise<boolean> {
spinner.text = `Running test for ${version.version}`;
return new Promise((resolve) => {
const child = execFile(nodePath, [testFile], (_error: ExecFileException | null, stdout: string) => {
const success = child.exitCode === 0;

if (success) {
spinner.succeed(`${chalk.bold(version.version)} passed ${chalk.dim(`(output: ${stdout})`)}`);
} else {
spinner.fail(`${chalk.bold(version.version)} failed ${chalk.dim(`(output: ${stdout})`)}`);
}
resolve(success);
});
});
}

export async function testVersionDocker({
version,
testFile,
Expand All @@ -77,37 +59,56 @@ export async function testVersionDocker({
throw new InvalidVersionError(version.version, err as Error);
}

return await runTestDocker({ version, image, testFile, spinner });
return await runTest({
version,
testFile,
spinner,
runTestFn: ({ testFile, execCb }) => {
const child = execFile(
'docker',
['run', '--rm', '-v', `${path.dirname(testFile)}:/app`, image, `app/${path.basename(testFile)}`],
(_: unknown, stdout: string) => execCb({ child, stdout }),
);
},
});
}

export function runTestDocker({
type RunTestFn = ({
version,
testFile,
execCb,
}: {
version: NodeVersion;
testFile: string;
execCb: ({ child, stdout }: { child: ChildProcess; stdout: string }) => void;
}) => void;

function runTest({
version,
image,
testFile,
spinner,
runTestFn,
}: {
version: NodeVersion;
image: string;
testFile: string;
spinner: Ora;
runTestFn: RunTestFn;
}): Promise<boolean> {
spinner.text = `Running test for ${version.version}`;
return new Promise((resolve) => {
// docker run --rm -v `pwd`:/app node:18.0.0-alpine app/test.cjs

const child = execFile(
'docker',
['run', '--rm', '-v', `${path.dirname(testFile)}:/app`, image, `app/${path.basename(testFile)}`],
(error: ExecFileException | null, stdout: string) => {
runTestFn({
version,
testFile,
execCb({ child, stdout }) {
const success = child.exitCode === 0;

if (success) {
spinner.succeed(`${chalk.bold(version.version)} passed ${chalk.dim(`(output: ${stdout})`)}`);
spinner.succeed(`${chalk.bold(version.version)} passed ${chalk.dim(`(output: ${stdout.trim()})`)}`);
} else {
spinner.fail(`${chalk.bold(version.version)} failed ${chalk.dim(`(output: ${stdout})`)}`);
spinner.fail(`${chalk.bold(version.version)} failed ${chalk.dim(`(output: ${stdout.trim()})`)}`);
}
resolve(success);
},
);
});
});
}

0 comments on commit b30bf08

Please sign in to comment.