test(javascript): preserve fixture runtime context for path analysis

This commit is contained in:
morluto committed 2026-10-24 08:22:13 +08:00
1 parent 54a663ddd5
commit c3ad8fc024
8 files changed
+132 -97

No files matched your search

@@ -5,7 +5,7 @@ import { runInThisContext } from "node:vm";
import { expect, it } from "vitest";
import { analyzeJavaScriptStaticSource } from "./javascriptStaticAnalysis.js";
import { analyzeCommonJsStaticFixture } from "../../../tests/fixtures/javascriptCommonJsAnalysis.js";
it.each([false, true])(
"preserves only untouched authored CommonJS dirname vars: assigned=%s",
@@ -25,7 +25,7 @@ it.each([false, true])(
expect(fixture.exports).toBe(
assigned ? "relative/preload.cjs" : "/source/app/preload.cjs",
);
const analysis = analyzeJavaScriptStaticSource(
const analysis = analyzeCommonJsStaticFixture(
`${setup}const{BrowserWindow}=require('electron');new BrowserWindow({webPreferences:{preload:path.join(__dirname,'preload.cjs')}});`,
);
expect(analysis.electron.browser_windows).toMatchObject([
@@ -43,7 +43,7 @@ it("keeps local dirname methods from proving a source anchor", () => {
"decoy.cjs",
),
).toBe("/other/decoy.cjs");
const analysis = analyzeJavaScriptStaticSource(
const analysis = analyzeCommonJsStaticFixture(
`import path from 'node:path';const helper={dirname:()=>'/other'};const{BrowserWindow}=require('electron');new BrowserWindow({webPreferences:{preload:path.resolve(runtimeBase,helper.dirname(import.meta.url),'decoy.cjs')}});`,
);
expect(analysis.electron.browser_windows).toMatchObject([
@@ -57,7 +57,7 @@ it("retains a proven Node dirname of the decoded module URL", () => {
fileURLToPath("file:///source/app/main.mjs", { windows: false }),
),
).toBe("/source/app");
const analysis = analyzeJavaScriptStaticSource(
const analysis = analyzeCommonJsStaticFixture(
`import path from 'node:path';import{fileURLToPath}from'node:url';const{BrowserWindow}=require('electron');new BrowserWindow({webPreferences:{preload:path.resolve(runtimeBase,path.dirname(fileURLToPath(import.meta.url)),'preload.cjs')}});`,
);
expect(analysis.electron.browser_windows).toMatchObject([
@@ -75,7 +75,7 @@ it.each([false, true])(
expect(local ? helper.dirname(decoded) : posix.dirname(decoded)).toBe(
local ? "/other" : "/source/app",
);
const analysis = analyzeJavaScriptStaticSource(`
const analysis = analyzeCommonJsStaticFixture(`
import path from 'node:path';import*as url from 'node:url';
const helper={dirname:()=>'/other'};const{BrowserWindow}=require('electron');
new BrowserWindow({webPreferences:{preload:path.resolve(runtimeBase,${local ? "helper" : "path"}.dirname(url.fileURLToPath(import.meta.url)),'preload.cjs')}});
@@ -88,7 +88,7 @@ it.each([false, true])(
it("does not use a URL string as a filesystem source-file anchor", () => {
expect(posix.dirname("file:///source/app/main.mjs")).not.toBe("/source/app");
const analysis = analyzeJavaScriptStaticSource(
const analysis = analyzeCommonJsStaticFixture(
`import path from 'node:path';const{BrowserWindow}=require('electron');new BrowserWindow({webPreferences:{preload:path.resolve(runtimeBase,path.dirname(import.meta.url),'decoy.cjs')}});`,
);
expect(analysis.electron.browser_windows).toMatchObject([
@@ -99,7 +99,7 @@ it("does not use a URL string as a filesystem source-file anchor", () => {
it.each([false, true])(
"proves the CommonJS filename dirname owner: local=%s",
(local) => {
const analysis = analyzeJavaScriptStaticSource(
const analysis = analyzeCommonJsStaticFixture(
`const path=require('node:path');const helper={dirname:()=>'.'};const{BrowserWindow}=require('electron');new BrowserWindow({webPreferences:{preload:path.resolve(runtimeBase,${local ? "helper" : "path"}.dirname(__filename),'decoy.cjs')}});`,
);
expect(analysis.electron.browser_windows).toMatchObject([
@@ -111,7 +111,7 @@ it.each([false, true])(
it.each([false, true])(
"preserves the utility-process dirname parameter until assigned: assigned=%s",
(assigned) => {
const analysis = analyzeJavaScriptStaticSource(
const analysis = analyzeCommonJsStaticFixture(
`var __dirname;${assigned ? "__dirname='relative';" : ""}const path=require('node:path');const{utilityProcess}=require('electron');utilityProcess.fork(path.join(__dirname,'worker.cjs'));`,
);
expect(analysis.electron.utility_processes).toMatchObject([