Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { existsSync } from 'fs';
import { cp, mkdir, readFile, rm, writeFile } from 'fs/promises';
import { basename, delimiter, join } from 'path';
import { promisify } from 'util';
import { stringifyForCodeInjection } from '@sentry/core';
import { afterAll, beforeAll, test, type TestAPI } from 'vitest';
import { CLEANUP_STEPS, createRunner, PACKAGE_ROOT } from './createRunner';

Expand Down Expand Up @@ -40,10 +41,11 @@ interface EsmAndCjsTestOptions extends CommonTestOptions {

/**
* Run one or multiple tests in ESM and CJS for a given scenario and instrument file.
* An array runs exported scenario functions sequentially with a fresh isolation scope for each.
*/
export function createEsmAndCjsTests(
cwd: string,
scenarioPath: string,
scenarioPath: string | string[],
instrumentPath: string,
callback: (
createTestRunner: () => ReturnType<typeof createRunner>,
Expand Down Expand Up @@ -98,7 +100,7 @@ export function createEsmAndCjsTests(
*/
export function createEsmTests(
cwd: string,
scenarioPath: string,
scenarioPath: string | string[],
instrumentPath: string,
callback: (createTestRunner: () => ReturnType<typeof createRunner>, testFn: typeof test, cwd: string) => void,
options?: CommonTestOptions,
Expand All @@ -123,7 +125,7 @@ export function createEsmTests(
*/
export function createCjsTests(
cwd: string,
scenarioPath: string,
scenarioPath: string | string[],
instrumentPath: string,
callback: (createTestRunner: () => ReturnType<typeof createRunner>, testFn: typeof test, cwd: string) => void,
options?: CommonTestOptions,
Expand Down Expand Up @@ -193,11 +195,13 @@ function setupAndCleanup(
/** Returns: tmpDir, createTmpDir(), scenarioPaths */
function prepareTmpDir(
cwd: string,
scenarioPath: string,
scenarioPath: string | string[],
instrumentPath: string,
options?: CommonTestOptions,
): [string, () => Promise<void>, ScenarioPaths] {
const mjsScenarioPath = join(cwd, scenarioPath);
const scenarios = Array.isArray(scenarioPath) ? scenarioPath : undefined;
const entryPath = typeof scenarioPath === 'string' ? scenarioPath : 'batched-scenarios.mjs';
const mjsScenarioPath = join(cwd, entryPath);
const mjsInstrumentPath = join(cwd, instrumentPath);

if (!mjsScenarioPath.endsWith('.mjs')) {
Expand All @@ -212,7 +216,7 @@ function prepareTmpDir(
// If additionalDependencies are provided, we also create a nested package.json and install them there.
const uniqueId = `${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`;
const tmpDirPath = join(cwd, `tmp_${uniqueId}`);
const esmScenarioBasename = basename(scenarioPath);
const esmScenarioBasename = basename(entryPath);
const esmInstrumentBasename = basename(instrumentPath);
const esmScenarioPathForRun = join(tmpDirPath, esmScenarioBasename);
const esmInstrumentPathForRun = join(tmpDirPath, esmInstrumentBasename);
Expand All @@ -229,7 +233,38 @@ function prepareTmpDir(
await mkdir(tmpDirPath);

// Copy ESM files as-is into tmp dir
await writeFile(esmScenarioPathForRun, await readFile(mjsScenarioPath, 'utf8'));
if (scenarios) {
for (const scenario of scenarios) {
const destination = join(tmpDirPath, basename(scenario));
await writeFile(destination, await readFile(join(cwd, scenario), 'utf8'));
await convertEsmFileToCjs(destination, destination.replace(/\.mjs$/, '.cjs'));
}
const imports = scenarios.map(
(scenario, index) => `import * as scenario${index} from './${basename(scenario)}';`,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Batched scenarios flatten to colliding names

Medium Severity

Batched scenario entries are read with join(cwd, scenario), so subdirectory paths work, but each file is copied and imported as basename(scenario). Two entries that share a filename (for example v5/scenario.mjs and v6/scenario.mjs) overwrite the same tmp file, and both generated imports load whichever copy was written last. The test.scenario attribute still records the original path, so later assertions can look correct while the wrong scenario ran.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 16e37e1. Configure here.

);
const runs = scenarios.map(
(scenario, index) =>
`await Sentry.withIsolationScope(scope => {
scope.setAttribute('test.scenario', ${stringifyForCodeInjection(scenario)});
return scenario${index}.run();
});`,
);
await writeFile(
esmScenarioPathForRun,
[
"import * as Sentry from '@sentry/node';",
...imports,
`async function run() { ${runs.join('\n')} await Sentry.flush(2000); }`,
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
'run();',
].join('\n'),
);
} else {
const scenario = await readFile(mjsScenarioPath, 'utf8');
await writeFile(
esmScenarioPathForRun,
scenario.includes('export async function run()') ? `${scenario}\nrun();\n` : scenario,
);
}
await writeFile(esmInstrumentPathForRun, await readFile(mjsInstrumentPath, 'utf8'));

// Pre-create CJS converted files inside tmp dir
Expand Down Expand Up @@ -329,6 +364,14 @@ function convertEsmToCjs(content: string): string {
return `require('${module}')`;
});

if (content.includes('export async function run()')) {
newContent = newContent.replace('export async function run()', 'async function run()');
newContent += '\nexports.run = run;\n';
}
if (content.includes("import * as scenario0 from './")) {
newContent = newContent.replace(/require\('([.][^']+)\.mjs'\)/g, "require('$1.cjs')");
}

return newContent;
}

Expand Down
Loading