Skip to content

Commit

Permalink
fix(Windows): adapt for Node.js breaking change (#210)
Browse files Browse the repository at this point in the history
* ci: enable windows workflow

* ci: use bin/index.ts directly

* fix: adaptation for Node.js breaking change on Windows

* fix: adaptation for Node.js breaking change on Windows

* ci: adaptation for Node.js breaking change on Windows

* ci: fix c8 comments

* ci: fix c8 comments
  • Loading branch information
wellwelwel committed Apr 26, 2024
1 parent f711a71 commit 71d36e9
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 22 deletions.
File renamed without changes.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"description": "🐷 Poku makes testing easy for Node.js, Bun & Deno at the same time.",
"main": "./lib/index.js",
"scripts": {
"test": "npx poku --parallel --debug test/unit,test/integration,test/e2e",
"pretest:c8": "npm run build",
"test": "tsx src/bin/index.ts --parallel --debug test/unit,test/integration,test/e2e",
"test:c8": "c8 npm run test",
"test:ci": "tsx ./test/ci.test.ts",
"test:node": "FILTER='node-' npm run test:ci",
Expand Down
4 changes: 2 additions & 2 deletions src/bin/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#! /usr/bin/env node

/* c8 ignore start */
import { escapeRegExp } from '../modules/list-files.js';
import { getArg, getLastParam, hasArg } from '../helpers/get-arg.js';
import { poku } from '../index.js';
import { platformIsValid } from '../helpers/get-runtime.js';
import { format } from '../helpers/format.js';

/* c8 ignore start */
const dirs =
(hasArg('include')
? getArg('include')?.split(',')
Expand All @@ -22,7 +22,6 @@ if (hasArg('log-success'))
console.log(
`The flag ${format.bold('--log-success')} is deprecated. Use ${format.bold('--debug')} instead.`
);
/* c8 ignore end */

poku(dirs, {
platform: platformIsValid(platform) ? platform : undefined,
Expand All @@ -32,3 +31,4 @@ poku(dirs, {
quiet,
debug,
});
/* c8 ignore stop */
2 changes: 1 addition & 1 deletion src/helpers/find-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ export const findFile = (error: Error) => {

return file;
};
/* c8 ignore end */
/* c8 ignore stop */
2 changes: 1 addition & 1 deletion src/helpers/pad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ export const padStart = (

return fullPadString + str;
};
/* c8 ignore end */
/* c8 ignore stop */
8 changes: 4 additions & 4 deletions src/modules/assert-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,15 @@ async function doesNotReject(
);
}

/* c8 ignore start */
const match = async (
value: string,
regExp: RegExp,
message?: ParseAssertionOptions['message']
): Promise<void> => {
/* c8 ignore start */
if (typeof nodeVersion === 'number' && nodeVersion < 12) {
throw new Error('match is available from Node.js 12 or higher');
}
/* c8 ignore stop */

await parseAssertion(() => nodeAssert?.match(value, regExp), {
message,
Expand All @@ -295,17 +294,17 @@ const match = async (
defaultMessage: 'Value should match regExp',
});
};
/* c8 ignore stop */

/* c8 ignore start */
const doesNotMatch = async (
value: string,
regExp: RegExp,
message?: ParseAssertionOptions['message']
): Promise<void> => {
/* c8 ignore start */
if (typeof nodeVersion === 'number' && nodeVersion < 12) {
throw new Error('doesNotMatch is available from Node.js 12 or higher');
}
/* c8 ignore stop */

await parseAssertion(() => nodeAssert.doesNotMatch(value, regExp), {
message,
Expand All @@ -314,6 +313,7 @@ const doesNotMatch = async (
defaultMessage: 'Value should not match regExp',
});
};
/* c8 ignore stop */

export const assertPromise = Object.assign(
(value: unknown, message?: ParseAssertionOptions['message']) =>
Expand Down
8 changes: 4 additions & 4 deletions src/modules/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,16 +269,15 @@ async function doesNotReject(
);
}

/* c8 ignore start */
const match = (
value: string,
regExp: RegExp,
message?: ParseAssertionOptions['message']
): void => {
/* c8 ignore start */
if (typeof nodeVersion === 'number' && nodeVersion < 12) {
throw new Error('match is available from Node.js 12 or higher');
}
/* c8 ignore stop */

parseAssertion(() => nodeAssert?.match(value, regExp), {
message,
Expand All @@ -287,17 +286,17 @@ const match = (
defaultMessage: 'Value should match regExp',
});
};
/* c8 ignore stop */

/* c8 ignore start */
const doesNotMatch = (
value: string,
regExp: RegExp,
message?: ParseAssertionOptions['message']
): void => {
/* c8 ignore start */
if (typeof nodeVersion === 'number' && nodeVersion < 12) {
throw new Error('doesNotMatch is available from Node.js 12 or higher');
}
/* c8 ignore stop */

parseAssertion(() => nodeAssert.doesNotMatch(value, regExp), {
message,
Expand All @@ -306,6 +305,7 @@ const doesNotMatch = (
defaultMessage: 'Value should not match regExp',
});
};
/* c8 ignore stop */

export const assert = Object.assign(
(value: unknown, message?: ParseAssertionOptions['message']) =>
Expand Down
11 changes: 7 additions & 4 deletions src/modules/create-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const killWindowsProcess = (PID: number) =>
process.once('SIGINT', () => {
secureEnds();
});
/* c8 ignore end */
/* c8 ignore stop */

const backgroundProcess = (
runtime: string,
Expand All @@ -35,10 +35,13 @@ const backgroundProcess = (

const service = spawn(runtime, args, {
stdio: ['inherit', 'pipe', 'pipe'],
shell: false,
/* c8 ignore next */
shell: isWindows,
cwd: options?.cwd ? sanitizePath(path.normalize(options.cwd)) : undefined,
env: process.env,
/* c8 ignore next */
detached: !isWindows,
/* c8 ignore next */
windowsHide: isWindows,
timeout: options?.timeout,
});
Expand Down Expand Up @@ -69,7 +72,7 @@ const backgroundProcess = (
};

runningProcesses[PID] = end;
/* c8 ignore end */
/* c8 ignore stop */

service.stdout.on('data', (data: Buffer) => {
if (!isResolved && typeof options?.startAfter !== 'number') {
Expand Down Expand Up @@ -187,7 +190,7 @@ export const startScript = async (
throw new Error(
`${format.bold('startScript')} currently doesn't works for Bun and Deno.${EOL}See: https://github.com/wellwelwel/poku/issues/143`
);
/* c8 ignore end */
/* c8 ignore stop */

return await backgroundProcess(runtime, runtimeArgs, script, {
...options,
Expand Down
4 changes: 4 additions & 0 deletions src/modules/exit.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* c8 ignore start */

import process from 'node:process';
import { hr } from '../helpers/hr.js';
import { Code } from '../@types/code.js';
Expand Down Expand Up @@ -38,3 +40,5 @@ process.on('uncaughtException', (err) => {
console.log('uncaughtException', err);
process.exit(1);
});

/* c8 ignore stop */
9 changes: 7 additions & 2 deletions src/services/run-test-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@ import process from 'node:process';
import path from 'node:path';
import { EOL } from 'node:os';
import { spawn } from 'node:child_process';
import { runner } from '../helpers/runner.js';
import { isWindows, runner } from '../helpers/runner.js';
import { indentation } from '../helpers/indentation.js';
import { format } from '../helpers/format.js';
import { Configs } from '../@types/poku.js';
import { isDebug, isQuiet } from '../helpers/logs.js';
import { removeConsecutiveRepeats } from '../helpers/remove-repeats.js';
import { beforeEach, afterEach } from './each.js';

/* c8 ignore start */
export type FileResults = {
success: string[];
fail: string[];
};
/* c8 ignore stop */

/* c8 ignore start */
export const fileResults: FileResults = {
success: [],
fail: [],
};
/* c8 ignore stop */

export const runTestFile = (
filePath: string,
Expand Down Expand Up @@ -98,7 +102,8 @@ export const runTestFile = (
// Export spawn helper is not an option
const child = spawn(runtime, runtimeArguments, {
stdio: ['inherit', 'pipe', 'pipe'],
shell: false,
/* c8 ignore next */
shell: isWindows,
env: {
...process.env,
FILE: configs?.parallel ? fileRelative : '',
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/capture-cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import process from 'node:process';
import { spawn } from 'node:child_process';
import { runner } from '../../src/helpers/runner.js';
import { isWindows, runner } from '../../src/helpers/runner.js';

// `/_.ts`: Simulate TypeScript file for Deno
const currentFile = typeof __filename === 'string' ? __filename : '/_.ts';
Expand All @@ -16,7 +16,7 @@ export const executeCLI = (args: string[]): Promise<string> =>
const runtimeArguments = [...runtimeOptions, ...args];

const childProcess = spawn(runtime, runtimeArguments, {
shell: false,
shell: isWindows,
});

let output: string = '';
Expand Down
2 changes: 2 additions & 0 deletions test/helpers/check-node.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { spawn } from 'node:child_process';
import path from 'node:path';
import { isWindows } from '../../src/helpers/runner.js';

export const executeDockerCompose = (serviceName: string): Promise<number> => {
const cwd = path.resolve('./test/docker');
Expand All @@ -25,6 +26,7 @@ export const executeDockerCompose = (serviceName: string): Promise<number> => {
const logsProcess = spawn(command, ['logs', '-f', serviceName], {
cwd,
stdio: 'inherit',
shell: isWindows,
});

logsProcess.on('close', () => resolve(1));
Expand Down

0 comments on commit 71d36e9

Please sign in to comment.