Skip to content

Commit f393a31

Browse files
committed
refactor: wip
1 parent d761a3a commit f393a31

18 files changed

Lines changed: 136 additions & 160 deletions

packages/utils/src/lib/create-runner-files.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { writeFile } from 'node:fs/promises';
22
import path from 'node:path';
3+
import { threadId } from 'node:worker_threads';
34
import type { RunnerFilesPaths } from '@code-pushup/models';
45
import { ensureDirectoryExists, pluginWorkDir } from './file-system.js';
5-
import { getUniqueProcessThreadId } from './process-id.js';
66

77
/**
88
* Function to create timestamp nested plugin runner files for config and output.
@@ -14,7 +14,9 @@ export async function createRunnerFiles(
1414
pluginSlug: string,
1515
configJSON: string,
1616
): Promise<RunnerFilesPaths> {
17-
const uniqueId = getUniqueProcessThreadId();
17+
// Use timestamp + process ID + threadId
18+
// This prevents race conditions when running the same plugin for multiple projects in parallel
19+
const uniqueId = `${(performance.timeOrigin + performance.now()) * 10}-${process.pid}-${threadId}`;
1820
const runnerWorkDir = path.join(pluginWorkDir(pluginSlug), uniqueId);
1921
const runnerConfigPath = path.join(runnerWorkDir, 'plugin-config.json');
2022
const runnerOutputPath = path.join(runnerWorkDir, 'runner-output.json');

packages/utils/src/lib/execute-process.int.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ process:complete
129129
throwError: true,
130130
}),
131131
),
132-
).rejects.toThrowError('Process failed with exit code 1');
132+
).rejects.toThrow('Process failed with exit code 1');
133133
expect(logger.debug).toHaveBeenCalledWith(
134134
expect.stringMatching(/process:start.*Error: dummy-error/s),
135135
{ force: true },

packages/utils/src/lib/exit-process.int.test.ts

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('subscribeProcessExit', () => {
2525
});
2626

2727
it('should install event listeners for all expected events', () => {
28-
expect(() => subscribeProcessExit({ onError, onExit })).not.toThrowError();
28+
expect(() => subscribeProcessExit({ onError, onExit })).not.toThrow();
2929

3030
expect(processOnSpy).toHaveBeenCalledWith(
3131
'uncaughtException',
@@ -42,91 +42,86 @@ describe('subscribeProcessExit', () => {
4242
});
4343

4444
it('should call onError with error and kind for uncaughtException', () => {
45-
expect(() => subscribeProcessExit({ onError })).not.toThrowError();
45+
expect(() => subscribeProcessExit({ onError })).not.toThrow();
4646

4747
const testError = new Error('Test uncaught exception');
4848

4949
(process as any).emit('uncaughtException', testError);
5050

51-
expect(onError).toHaveBeenCalledExactlyOnceWith(
52-
testError,
53-
'uncaughtException',
54-
);
51+
expect(onError).toHaveBeenCalledWith(testError, 'uncaughtException');
52+
expect(onError).toHaveBeenCalledOnce();
5553
expect(onExit).not.toHaveBeenCalled();
5654
});
5755

5856
it('should call onError with reason and kind for unhandledRejection', () => {
59-
expect(() => subscribeProcessExit({ onError })).not.toThrowError();
57+
expect(() => subscribeProcessExit({ onError })).not.toThrow();
6058

6159
const testReason = 'Test unhandled rejection';
6260

6361
(process as any).emit('unhandledRejection', testReason);
6462

65-
expect(onError).toHaveBeenCalledExactlyOnceWith(
66-
testReason,
67-
'unhandledRejection',
68-
);
63+
expect(onError).toHaveBeenCalledWith(testReason, 'unhandledRejection');
64+
expect(onError).toHaveBeenCalledOnce();
6965
expect(onExit).not.toHaveBeenCalled();
7066
});
7167

7268
it('should call onExit and exit with code 0 for SIGINT', () => {
73-
expect(() => subscribeProcessExit({ onExit })).not.toThrowError();
69+
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
7470

7571
(process as any).emit('SIGINT');
7672

77-
expect(onExit).toHaveBeenCalledExactlyOnceWith(SIGNAL_EXIT_CODES().SIGINT, {
73+
expect(onExit).toHaveBeenCalledOnce();
74+
expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGINT, {
7875
kind: 'signal',
7976
signal: 'SIGINT',
8077
});
8178
expect(onError).not.toHaveBeenCalled();
8279
});
8380

8481
it('should call onExit and exit with code 0 for SIGTERM', () => {
85-
expect(() => subscribeProcessExit({ onExit })).not.toThrowError();
82+
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
8683

8784
(process as any).emit('SIGTERM');
8885

89-
expect(onExit).toHaveBeenCalledExactlyOnceWith(
90-
SIGNAL_EXIT_CODES().SIGTERM,
91-
{
92-
kind: 'signal',
93-
signal: 'SIGTERM',
94-
},
95-
);
86+
expect(onExit).toHaveBeenCalledOnce();
87+
expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGTERM, {
88+
kind: 'signal',
89+
signal: 'SIGTERM',
90+
});
9691
expect(onError).not.toHaveBeenCalled();
9792
});
9893

9994
it('should call onExit and exit with code 0 for SIGQUIT', () => {
100-
expect(() => subscribeProcessExit({ onExit })).not.toThrowError();
95+
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
10196

10297
(process as any).emit('SIGQUIT');
10398

104-
expect(onExit).toHaveBeenCalledExactlyOnceWith(
105-
SIGNAL_EXIT_CODES().SIGQUIT,
106-
{
107-
kind: 'signal',
108-
signal: 'SIGQUIT',
109-
},
110-
);
99+
expect(onExit).toHaveBeenCalledOnce();
100+
expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGQUIT, {
101+
kind: 'signal',
102+
signal: 'SIGQUIT',
103+
});
111104
expect(onError).not.toHaveBeenCalled();
112105
});
113106

114107
it('should call onExit for successful process termination with exit code 0', () => {
115-
expect(() => subscribeProcessExit({ onExit })).not.toThrowError();
108+
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
116109

117110
(process as any).emit('exit', 0);
118111

119-
expect(onExit).toHaveBeenCalledExactlyOnceWith(0, { kind: 'exit' });
112+
expect(onExit).toHaveBeenCalledOnce();
113+
expect(onExit).toHaveBeenCalledWith(0, { kind: 'exit' });
120114
expect(onError).not.toHaveBeenCalled();
121115
expect(processExitSpy).not.toHaveBeenCalled();
122116
});
123117

124118
it('should call onExit for failed process termination with exit code 1', () => {
125-
expect(() => subscribeProcessExit({ onExit })).not.toThrowError();
119+
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
126120

127121
(process as any).emit('exit', 1);
128122

129-
expect(onExit).toHaveBeenCalledExactlyOnceWith(1, { kind: 'exit' });
123+
expect(onExit).toHaveBeenCalledOnce();
124+
expect(onExit).toHaveBeenCalledWith(1, { kind: 'exit' });
130125
expect(onError).not.toHaveBeenCalled();
131126
expect(processExitSpy).not.toHaveBeenCalled();
132127
});

0 commit comments

Comments
 (0)