Skip to content

Commit 3902d1c

Browse files
authored
fix: ensure concurrency for multiple inputs (#973)
* fix: ensure concurrent runs for multiple inputs * debug: check for `.cts` files * chore: adjust global configs * chore: improve logic * chore: reduce logic * ci: remove debug
1 parent f7b4518 commit 3902d1c

File tree

12 files changed

+48
-42
lines changed

12 files changed

+48
-42
lines changed

src/modules/essentials/poku.ts

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import type { Code } from '../../@types/code.js';
22
import type { Configs } from '../../@types/poku.js';
3+
import { join } from 'node:path';
34
import process from 'node:process';
45
import { GLOBAL, results, timespan } from '../../configs/poku.js';
56
import { reporter } from '../../services/reporter.js';
67
import { runTests } from '../../services/run-tests.js';
78
import { exit } from '../helpers/exit.js';
9+
import { listFiles } from '../helpers/list-files.js';
810

911
/* c8 ignore next 1 */ // Process-based
1012
export const onSigint = () => process.stdout.write('\u001B[?25h');
@@ -23,8 +25,6 @@ export async function poku(
2325
targetPaths: string | string[],
2426
configs?: Configs
2527
): Promise<Code | undefined> {
26-
let code: Code = 0;
27-
2828
if (configs) GLOBAL.configs = { ...GLOBAL.configs, ...configs };
2929

3030
timespan.started = new Date();
@@ -33,24 +33,26 @@ export async function poku(
3333
const paths: string[] = Array.prototype.concat(targetPaths);
3434
const showLogs = !GLOBAL.configs.quiet;
3535
const { reporter: plugin } = GLOBAL.configs;
36+
const { cwd } = GLOBAL;
37+
38+
const testFiles = (
39+
await Promise.all(
40+
paths.map((dir) => listFiles(join(cwd, dir), GLOBAL.configs))
41+
)
42+
).flat(1);
3643

3744
if (typeof plugin === 'string' && plugin !== 'poku')
3845
GLOBAL.reporter = reporter[plugin]();
3946

4047
if (showLogs) GLOBAL.reporter.onRunStart();
4148

42-
try {
43-
const promises = paths.map(async (dir) => await runTests(dir));
44-
const concurrency = await Promise.all(promises);
45-
46-
if (concurrency.some((result) => !result)) code = 1;
47-
} finally {
48-
const end = process.hrtime(start);
49-
const total = end[0] * 1e3 + end[1] / 1e6;
49+
const result = await runTests(testFiles);
50+
const code: Code = result ? 0 : 1;
51+
const end = process.hrtime(start);
52+
const total = end[0] * 1e3 + end[1] / 1e6;
5053

51-
timespan.duration = total;
52-
timespan.finished = new Date();
53-
}
54+
timespan.duration = total;
55+
timespan.finished = new Date();
5456

5557
if (showLogs) GLOBAL.reporter.onRunResult({ code, timespan, results });
5658
if (GLOBAL.configs.noExit) return code;

src/services/run-tests.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { join, relative } from 'node:path';
2-
import process from 'node:process';
1+
import { relative } from 'node:path';
2+
import { exit } from 'node:process';
33
import { deepOptions, GLOBAL, results } from '../configs/poku.js';
4-
import { listFiles } from '../modules/helpers/list-files.js';
54
import { hasOnly } from '../parsers/get-arg.js';
65
import { availableParallelism } from '../polyfills/os.js';
76
import { hr, log } from '../services/write.js';
@@ -12,14 +11,12 @@ const { cwd } = GLOBAL;
1211

1312
if (hasOnly) deepOptions.push('--only');
1413

15-
export const runTests = async (dir: string): Promise<boolean> => {
14+
export const runTests = async (files: string[]): Promise<boolean> => {
1615
let allPassed = true;
1716
let activeTests = 0;
1817
let resolveDone: (value: boolean) => void;
1918

2019
const { configs } = GLOBAL;
21-
const testDir = join(cwd, dir);
22-
const files = await listFiles(testDir, configs);
2320
const showLogs = !configs.quiet;
2421
const failFastError = ` ${format('ℹ').fail()} ${format('failFast').bold()} is enabled`;
2522
const concurrency: number = (() => {
@@ -62,7 +59,7 @@ export const runTests = async (dir: string): Promise<boolean> => {
6259
hr();
6360
}
6461

65-
process.exit(1);
62+
exit(1);
6663
}
6764
}
6865

test/__fixtures__/e2e/sequential/a.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { existsSync } from 'node:fs';
22
import { mkdir, rm, writeFile } from 'node:fs/promises';
3-
import { assert, test } from '../../../../src/modules/index.js';
3+
import { assert } from '../../../../src/modules/essentials/assert.js';
4+
import { test } from '../../../../src/modules/helpers/test.js';
45

56
test(async () => {
67
const testDir = '../../.temp/sequential';

test/__fixtures__/e2e/sequential/b.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { existsSync } from 'node:fs';
22
import { mkdir, rm, writeFile } from 'node:fs/promises';
3-
import { assert, test } from '../../../../src/modules/index.js';
3+
import { assert } from '../../../../src/modules/essentials/assert.js';
4+
import { test } from '../../../../src/modules/helpers/test.js';
45

56
test(async () => {
67
const testDir = '../../.temp/sequential';

test/__fixtures__/e2e/sequential/c.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { existsSync } from 'node:fs';
22
import { mkdir, rm, writeFile } from 'node:fs/promises';
3-
import { assert, test } from '../../../../src/modules/index.js';
3+
import { assert } from '../../../../src/modules/essentials/assert.js';
4+
import { test } from '../../../../src/modules/helpers/test.js';
45

56
test(async () => {
67
const testDir = '../../.temp/sequential';

test/__fixtures__/e2e/sequential/d.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { existsSync } from 'node:fs';
22
import { mkdir, rm, writeFile } from 'node:fs/promises';
3-
import { assert, test } from '../../../../src/modules/index.js';
3+
import { assert } from '../../../../src/modules/essentials/assert.js';
4+
import { test } from '../../../../src/modules/helpers/test.js';
45

56
test(async () => {
67
const testDir = '../../.temp/sequential';

test/__fixtures__/e2e/sequential/e.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { existsSync } from 'node:fs';
22
import { mkdir, rm, writeFile } from 'node:fs/promises';
3-
import { assert, test } from '../../../../src/modules/index.js';
3+
import { assert } from '../../../../src/modules/essentials/assert.js';
4+
import { test } from '../../../../src/modules/helpers/test.js';
45

56
test(async () => {
67
const testDir = '../../.temp/sequential';

test/e2e/concurrency.test.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import { inspectPoku, isBuild } from '../__utils__/capture-cli.test.js';
1+
import { inspectPoku } from '../__utils__/capture-cli.test.js';
22
import { assert } from '../../src/modules/essentials/assert.js';
3-
import { skip } from '../../src/modules/helpers/skip.js';
43
import { test } from '../../src/modules/helpers/test.js';
54

6-
if (isBuild) skip();
7-
85
test('Start 30 servers on the same port', async () => {
9-
const results = await inspectPoku('--killPort=8000 -d --sequential', {
6+
const results = await inspectPoku('-d --sequential', {
107
cwd: 'test/__fixtures__/e2e/concurrency/sequence',
118
});
129

test/e2e/runners.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('Test Runtimes/Platforms + Extensions', async () => {
7070
hasDeno &&
7171
(await it('Deno', async () => {
7272
const output = await inspectCLI(
73-
'deno run --unstable-sloppy-imports --allow-read --allow-env --allow-run src/bin/index.ts test/__fixtures__/e2e/extensions -d --exclude=.cts'
73+
'deno run --unstable-sloppy-imports --allow-read --allow-env --allow-run src/bin/index.ts test/__fixtures__/e2e/extensions -d'
7474
);
7575

7676
if (output.exitCode !== 0) {
@@ -79,7 +79,7 @@ describe('Test Runtimes/Platforms + Extensions', async () => {
7979
}
8080

8181
assert.strictEqual(output.exitCode, 0, 'Exit Code needs to be 0');
82-
assert(/PASS 10/.test(output.stdout), 'CLI needs to pass 10');
82+
assert(/PASS 12/.test(output.stdout), 'CLI needs to pass 12');
8383
assert(/FAIL 0/.test(output.stdout), 'CLI needs to fail 0');
8484
}));
8585
});

test/e2e/sequential.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { inspectPoku, isBuild } from '../__utils__/capture-cli.test.js';
1+
import { inspectPoku } from '../__utils__/capture-cli.test.js';
22
import { GLOBAL } from '../../src/configs/poku.js';
33
import { assert } from '../../src/modules/essentials/assert.js';
44
import { describe } from '../../src/modules/helpers/describe.js';
55
import { skip } from '../../src/modules/helpers/skip.js';
66

7-
if (GLOBAL.runtime === 'deno' || isBuild) skip();
7+
if (GLOBAL.runtime === 'deno') skip();
88

99
describe('Ensure sequential runs', async () => {
1010
const results = await inspectPoku('--debug --sequential', {

test/integration/wait-for/wait-for-port.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ const stopServer = (server: Server): Promise<void> =>
2121

2222
test('Wait For Port', async () => {
2323
try {
24-
await kill.range(8000, 8003);
24+
await kill.range(8001, 8004);
2525
} catch {}
2626

2727
await Promise.all([
2828
it(async () => {
29-
const port = 8000;
29+
const port = 8001;
3030
const server = await startServer(port);
3131

3232
try {
@@ -40,7 +40,7 @@ test('Wait For Port', async () => {
4040
}),
4141

4242
it(async () => {
43-
const port = 8001;
43+
const port = 8002;
4444
const server = await startServer(port);
4545

4646
try {
@@ -54,7 +54,7 @@ test('Wait For Port', async () => {
5454
}),
5555

5656
it(async () => {
57-
const port = 8002;
57+
const port = 8003;
5858

5959
try {
6060
await waitForPort(port, { timeout: 1000 });
@@ -69,7 +69,7 @@ test('Wait For Port', async () => {
6969
}),
7070

7171
it(async () => {
72-
const port = 8003;
72+
const port = 8004;
7373
const server = await startServer(port);
7474

7575
try {
@@ -97,6 +97,6 @@ test('Wait For Port', async () => {
9797
]);
9898

9999
try {
100-
await kill.range(8000, 8003);
100+
await kill.range(8001, 8004);
101101
} catch {}
102102
});

test/unit/run-tests.test.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { GLOBAL } from '../../src/configs/poku.js';
22
import { assert } from '../../src/modules/essentials/assert.js';
33
import { describe } from '../../src/modules/helpers/describe.js';
44
import { it } from '../../src/modules/helpers/it/core.js';
5+
import { listFiles } from '../../src/modules/helpers/list-files.js';
56
import { runTests } from '../../src/services/run-tests.js';
67

78
GLOBAL.configs.noExit = true;
@@ -10,13 +11,17 @@ GLOBAL.configs.quiet = true;
1011
describe('Service: runTests', async () => {
1112
await Promise.all([
1213
it(async () => {
13-
const code = await runTests('test/__fixtures__/e2e/fail');
14+
const code = await runTests(
15+
await listFiles('test/__fixtures__/e2e/fail')
16+
);
1417

1518
assert.deepStrictEqual(code, false, 'Failure test directory case');
1619
}),
1720

1821
it(async () => {
19-
const code = await runTests('test/__fixtures__/e2e/success');
22+
const code = await runTests(
23+
await listFiles('test/__fixtures__/e2e/success')
24+
);
2025

2126
assert.deepStrictEqual(code, true, 'Success test directory case');
2227
}),

0 commit comments

Comments
 (0)