Skip to content

Commit

Permalink
chore!: drop parallel in favor of sequential option (#888)
Browse files Browse the repository at this point in the history
* chore!: drop `parallel` in favor of `sequential`

* ci: adjust scripts

* chore: remove debug

* ci: add tests

* ci: fix test logic

* docs: version the documentation

* chore(docs): clear and lint

* ci(website): adjust tests

* docs: document `sequential` option
  • Loading branch information
wellwelwel authored Dec 11, 2024
1 parent 96b8707 commit fc2c72d
Show file tree
Hide file tree
Showing 164 changed files with 11,100 additions and 587 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci_compatibility-bun.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ jobs:
run: npm run build

- name: 🔬 Bun ${{ matrix.bun-version }}
run: npm run test:bun:parallel
run: npm run test:bun
2 changes: 1 addition & 1 deletion .github/workflows/ci_compatibility-deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ jobs:
run: npm run build

- name: 🔬 Deno ${{ matrix.deno-version }}
run: npm run test:deno:parallel
run: npm run test:deno
6 changes: 3 additions & 3 deletions benchmark/benchmark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ echo '### 🚀 Benchmark Results\n'
echo '```'

hyperfine -i --warmup 3 --export-json results.json \
--command-name "🐷 Poku ($SHORT_SHA)" '../lib/bin/index.js --parallel ./test/poku' \
--command-name "🐷 Poku ($SHORT_SHA)" '../lib/bin/index.js ./test/poku' \
--command-name 'Mocha (10.7.3)' './node_modules/mocha/bin/mocha.js --parallel ./test/mocha' \
--command-name 'Jest (29.7.0)' 'node --experimental-vm-modules ./node_modules/jest/bin/jest.js ./test/jest' \
--command-name 'Vitest (2.1.3)' './node_modules/vitest/vitest.mjs run ./test/vitest' |
Expand All @@ -21,7 +21,7 @@ echo '```'
# Not included in results.json
hyperfine -i --warmup 3 \
--command-name 'Node.js' 'node --test "./test/node/**.spec.js"' \
--command-name "🐷 Poku ($SHORT_SHA)" '../lib/bin/index.js --parallel --node ./test/poku' |
--command-name "🐷 Poku ($SHORT_SHA)" '../lib/bin/index.js --node ./test/poku' |
awk '/Summary/ {flag=1} flag'

echo '```\n'
Expand All @@ -31,7 +31,7 @@ echo '```'
# Not included in results.json
hyperfine -i --warmup 3 \
--command-name 'Bun' 'bun test "test/bun/"' \
--command-name "🐷 Poku ($SHORT_SHA)" '../lib/bin/index.js --parallel --bun ./test/poku' |
--command-name "🐷 Poku ($SHORT_SHA)" '../lib/bin/index.js --bun ./test/poku' |
awk '/Summary/ {flag=1} flag'

echo '```\n'
Expand Down
12 changes: 3 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,9 @@
"typescript": ">=5.x.x"
},
"scripts": {
"test": "npm run test:parallel && npm run test:sequential",
"test:bun": "npm run test:bun:parallel && npm run test:bun:sequential",
"test:deno": "npm run test:deno:parallel && npm run test:deno:sequential",
"test:sequential": "tsx src/bin/index.ts test/unit test/integration test/e2e",
"test:parallel": "tsx src/bin/index.ts -p test/unit test/integration test/e2e",
"test:bun:sequential": "bun src/bin/index.ts --bun test/unit test/integration test/e2e",
"test:bun:parallel": "bun src/bin/index.ts --bun -p test/unit test/integration test/e2e",
"test:deno:sequential": "tsx src/bin/index.ts --deno --denoAllow=all test/unit test/integration test/e2e",
"test:deno:parallel": "tsx src/bin/index.ts --deno --denoAllow=all -p test/unit test/integration test/e2e",
"test": "tsx src/bin/index.ts test/unit test/integration test/e2e",
"test:bun": "bun src/bin/index.ts --bun test/unit test/integration test/e2e",
"test:deno": "tsx src/bin/index.ts --deno --denoAllow=all test/unit test/integration test/e2e",
"pretest:c8": "tsx tools/build/c8-file.ts",
"test:c8": "c8 --experimental-monocart tsx test/c8.test.ts",
"posttest:c8": "rm -rf ./.nycrc.json",
Expand Down
4 changes: 2 additions & 2 deletions src/@types/poku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type Configs = {
*
* @default false
*/
parallel?: boolean;
sequential?: boolean;
/**
* Determines the platform for test execution.
*
Expand All @@ -50,7 +50,7 @@ export type Configs = {
/**
* Limits the number of tests running concurrently.
*
* @default 0
* @default (availableParallelism() || cpus().lenght) - 1
*/
concurrency?: number;
/**
Expand Down
3 changes: 1 addition & 2 deletions src/bin/enforce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ export const checkFlags = () => {
'--killRange',
'--node',
'--only',
'--parallel',
'--platform',
'--quiet',
'--sequential',
'--watch',
'--watchInterval',
'-c',
'-d',
'-p',
'-q',
'-w',
'-x',
Expand Down
2 changes: 1 addition & 1 deletion src/bin/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ const summary: [string, string][] = [
['--listFiles', 'Display all the files returned in the terminal.'],
['--node', 'Enforce tests to run through Node.js.'],
['--only', 'Enable selective execution of tests.'],
['--parallel, -p', 'Run tests files in parallel.'],
['--platform', 'Enforce tests to run through a platform.'],
['--quiet, -q', 'Run tests with no logs.'],
['--sequential', 'Run tests files sequentially.'],
['--version, -v', "Show Poku's installed version."],
['--watch, -w', 'Watch for test events.'],
['--watchInterval', 'Set an interval for watch events.'],
Expand Down
8 changes: 2 additions & 6 deletions src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,16 @@ import { getConfigs } from '../parsers/options.js';
hasArg('denoCjs') ||
defaultConfigs?.deno?.cjs;
/* c8 ignore stop */
const parallel =
hasArg('parallel') || hasArg('p', '-') || defaultConfigs?.parallel;
const quiet = hasArg('quiet') || hasArg('q', '-') || defaultConfigs?.quiet;
const debug = hasArg('debug') || hasArg('d', '-') || defaultConfigs?.debug;
const failFast = hasArg('failFast') || defaultConfigs?.failFast;
const watchMode = hasArg('watch') || hasArg('w', '-');
const hasEnvFile = hasArg('envFile');
const concurrency = (() => {
if (!(parallel || defaultConfigs?.parallel)) return;

const value = Number(getArg('concurrency'));

return Number.isNaN(value) ? defaultConfigs?.concurrency : value;
})();
const sequential = hasArg('sequential');

if (dirs.length === 1) states.isSinglePath = true;

Expand Down Expand Up @@ -161,8 +157,8 @@ import { getConfigs } from '../parsers/options.js';
typeof filter === 'string' ? new RegExp(escapeRegExp(filter)) : filter,
exclude:
typeof exclude === 'string' ? new RegExp(escapeRegExp(exclude)) : exclude,
parallel,
concurrency,
sequential,
quiet,
debug,
failFast,
Expand Down
37 changes: 3 additions & 34 deletions src/modules/essentials/poku.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { Code } from '../../@types/code.js';
import type { Configs } from '../../@types/poku.js';
import process from 'node:process';
import { runTests, runTestsParallel } from '../../services/run-tests.js';
import { Write } from '../../services/write.js';
import { exit } from '../helpers/exit.js';
import { format, showTestResults } from '../../services/format.js';
import { isQuiet } from '../../parsers/output.js';
import { finalResults } from '../../configs/files.js';
import { runTests } from '../../services/run-tests.js';

/* c8 ignore start */ // Process-based
export const onSigint = () => process.stdout.write('\u001B[?25h');
Expand Down Expand Up @@ -34,44 +34,13 @@ export async function poku(
const dirs = Array.prototype.concat(targetPaths);
const showLogs = !isQuiet(configs);

// Sequential
if (!configs?.parallel) {
for (const dir of dirs) {
const result = await runTests(dir, configs);

if (!result) {
code = 1;
if (configs?.failFast) break;
}
}

if (configs?.noExit) return code;

const end = process.hrtime(start);
const total = (end[0] * 1e3 + end[1] / 1e6).toFixed(6);

finalResults.time = total;

showLogs && showTestResults();

exit(code, configs?.quiet);
return;
}

// Parallel
if (showLogs) {
Write.hr();
Write.log(`${format('Running the Test Suite in Parallel').bold()}\n`);
Write.log(`${format('Running Tests').bold()}\n`);
}

try {
const promises = dirs.map(async (dir) => {
const result = await runTestsParallel(dir, configs);
if (!result && configs?.failFast) throw new Error('quiet');

return result;
});

const promises = dirs.map(async (dir) => await runTests(dir, configs));
const concurrency = await Promise.all(promises);

if (concurrency.some((result) => !result)) code = 1;
Expand Down
15 changes: 10 additions & 5 deletions src/modules/helpers/list-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,17 @@ export const getAllFiles = async (
let isFullPath = false;

const currentFiles = await (async () => {
if (await isFile(dirPath)) {
isFullPath = true;
return [sanitizePath(dirPath)];
try {
if (await isFile(dirPath)) {
isFullPath = true;
return [sanitizePath(dirPath)];
}

return await readdir(sanitizePath(dirPath));
} catch (error) {
console.error(error);
process.exit(1);
}

return await readdir(sanitizePath(dirPath));
})();

const filter: RegExp = (() => {
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const parserOutput = (options: {
if (hasTodo) results.todo += hasTodo.length;

const debug = isDebug(configs);
const pad = configs?.parallel ? ' ' : ' ';
const pad = ' ';
const splittedOutput = output.split('\n');

const outputs = (
Expand Down
11 changes: 1 addition & 10 deletions src/services/run-test-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import type { Configs } from '../@types/poku.js';
import { cwd as processCWD, hrtime, env } from 'node:process';
import { relative } from 'node:path';
import { spawn } from 'node:child_process';
import { indentation } from '../configs/indentation.js';
import { fileResults } from '../configs/files.js';
import { isWindows, runner } from '../parsers/get-runner.js';
import { format } from './format.js';
import { isQuiet, parserOutput } from '../parsers/output.js';
import { beforeEach, afterEach } from './each.js';
import { Write } from './write.js';
Expand Down Expand Up @@ -38,13 +36,6 @@ export const runTestFile = async (
output += String(data);
};

if (!configs?.parallel) {
showLogs &&
Write.log(
`${indentation.test}${format('●').info().dim()} ${format(fileRelative).dim()}`
);
}

const start = hrtime();
let end: ReturnType<typeof hrtime>;

Expand All @@ -56,7 +47,7 @@ export const runTestFile = async (
shell: isWindows,
env: {
...env,
FILE: configs?.parallel || configs?.deno?.cjs ? fileRelative : '',
FILE: fileRelative,
},
});

Expand Down
78 changes: 4 additions & 74 deletions src/services/run-tests.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type { Configs } from '../@types/poku.js';
import process from 'node:process';
import { join, relative, sep } from 'node:path';
import { runner } from '../parsers/get-runner.js';
import { indentation } from '../configs/indentation.js';
import { isFile as IS_FILE, listFiles } from '../modules/helpers/list-files.js';
import { join } from 'node:path';
import { listFiles } from '../modules/helpers/list-files.js';
import { Write } from '../services/write.js';
import { format } from './format.js';
import { runTestFile } from './run-test-file.js';
Expand All @@ -22,75 +20,6 @@ else if (hasOnly) deepOptions.push('--only');
export const runTests = async (
dir: string,
configs?: Configs
): Promise<boolean> => {
const testDir = join(cwd, dir);
const currentDir = relative(cwd, testDir);
const isFile = await IS_FILE(testDir);
const files = await listFiles(testDir, configs);
const totalTests = files.length;
const showLogs = !isQuiet(configs);

let passed = true;

if (showLogs && files.length > 0) {
Write.hr();
Write.log(
`${format(isFile ? 'File:' : 'Directory:').bold()} ${format(`.${sep}${currentDir}`).underline()}\n`
);
}

for (let i = 0; i < files.length; i++) {
const filePath = files[i];
const fileRelative = relative(cwd, filePath);

const start = process.hrtime();
const testPassed = await runTestFile(filePath, configs);
const end = process.hrtime(start);
const total = (end[0] * 1e3 + end[1] / 1e6).toFixed(6);

const testNumber = i + 1;
const counter = format('').counter(testNumber, totalTests);
const command = `${runner(fileRelative, configs).join(' ')} ${fileRelative}`;
const nextLine = i + 1 !== files.length ? '\n' : '';
const log = `${counter}/${totalTests} ${command}`;

if (testPassed) {
++results.success;

showLogs &&
Write.log(
`${indentation.test}${format('✔').success()} ${log}${format(` › ${total}ms`).success().dim()}${nextLine}`
);
} else {
++results.fail;

if (showLogs) {
Write.log(
`${indentation.test}${format('✘').fail()} ${log}${format(` › ${total}ms`).fail().dim()}${nextLine}`
);
}

passed = false;

if (configs?.failFast) {
process.exitCode = 1;

if (showLogs) {
Write.hr();
Write.log(failFastError);
}

break;
}
}
}

return passed;
};

export const runTestsParallel = async (
dir: string,
configs?: Configs
): Promise<boolean> => {
let allPassed = true;
let activeTests = 0;
Expand All @@ -101,6 +30,7 @@ export const runTestsParallel = async (
const files = await listFiles(testDir, configs);
const showLogs = !isQuiet(configs);
const concurrency: number = (() => {
if (configs?.sequential) return 1;
const limit =
configs?.concurrency ?? Math.max(availableParallelism() - 1, 1);
return limit <= 0 ? files.length || 1 : limit;
Expand All @@ -118,7 +48,7 @@ export const runTestsParallel = async (
}

const filePath = files.shift();
if (!filePath) return;
if (typeof filePath === 'undefined') return;

activeTests++;

Expand Down
1 change: 0 additions & 1 deletion test/__fixtures__/e2e/fail-fast/parallel/.pokurc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"$schema": "https://poku.io/schemas/configs.json",
"failFast": true,
"parallel": true,
"concurrency": 0
}
15 changes: 15 additions & 0 deletions test/__fixtures__/e2e/sequential/a.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { existsSync } from 'node:fs';
import { writeFile, rm, mkdir } from 'node:fs/promises';
import { test, assert } from '../../../../src/modules/index.js';

test(async () => {
const testDir = '../../.temp/sequential';
const testFile = `${testDir}/once-per-time.json`;

if (existsSync(testFile)) assert.fail("File shoudn't exists");

await mkdir(testDir);
await writeFile(testFile, 'test', 'utf8');
await rm(testFile);
await rm(testDir, { recursive: true, force: true });
});
Loading

0 comments on commit fc2c72d

Please sign in to comment.