diff --git a/scripts/compiler/test.mjs b/scripts/compiler/test.mjs index 26be85c..21ef3fb 100644 --- a/scripts/compiler/test.mjs +++ b/scripts/compiler/test.mjs @@ -10,10 +10,12 @@ import {runTool} from './process.mjs'; import {createHash} from 'node:crypto'; import {BackendClient,bounded} from './backend-client.mjs'; const scratch=realpathSync.native(mkdtempSync(join(tmpdir(),'lithe-maven-compiler-'))); +const emptySettings=join(scratch,'empty-settings.xml');writeFileSync(emptySettings,''); const pkg=resolve(process.env.LITHE_TEST_PLUGIN_DIR??'dist/package/io.github.lithe-idea.maven-build'); const maven=process.env.MVN??'mvn'; +const mavenSettings=['-gs',emptySettings,'-s',emptySettings]; const root=join(scratch,'workspace');mkdirSync(root); -const command=(tool,args,cwd=root)=>runTool(tool,args,{cwd,timeout:120000}); +const command=(tool,args,cwd=root)=>runTool(tool,args,{cwd,env:{...process.env,MAVEN_ARGS:''},timeout:120000}); const pom=(body)=>`4.0.0${body}`; const file=join(root,'pom.xml'); writeFileSync(file,pom('fixtureparent1pomabnever-export-thisorg.apache.maven.pluginsmaven-compiler-plugin3.13.011-parameters')); @@ -22,7 +24,7 @@ const digest=bytes=>'sha256:'+createHash('sha256').update(bytes).digest('hex'); let client; const report=createReport('native-compiler'); const step=report.step; -async function model(){const output=join(scratch,'model.json');await command(maven,['-B','-ntp','-q',`-Dmaven.ext.class.path=${join(pkg,'lib/lithe-maven-extension.jar')}`,`-Dlithe.modelOutput=${output}`,'pre-clean']);const model=JSON.parse(readFileSync(output));for(const project of model.projects)project.path=relative(root,project.path).replaceAll('\\','/')||'.';return model;} +async function model(){const output=join(scratch,'model.json');await command(maven,['-B','-ntp','-q',...mavenSettings,`-Dmaven.ext.class.path=${join(pkg,'lib/lithe-maven-extension.jar')}`,`-Dlithe.modelOutput=${output}`,'pre-clean']);const model=JSON.parse(readFileSync(output));for(const project of model.projects)project.path=relative(root,project.path).replaceAll('\\','/')||'.';return model;} try{ client=new BackendClient({java:process.env.LITHE_TEST_JAVA??'java',jar:join(pkg,'lib/lithe-maven-settings.jar'),args:[],hostRequests:{'lithe/host/workspace/getFolders':()=>({folders:[{rootID:'r',name:'workspace',path:root}]})}}); await client.initialize(root,{functions:true}); @@ -31,9 +33,9 @@ try{ const read=async(language='zh-CN')=>invoke('compiler.settings',{context:{language,builds:[{root:'.',options:{},tool:{executable:maven},model:await model()}]}}); let snapshot; await step('Read actual Maven inheritance without modifying a POM or exposing private fields',async()=>{const original=readFileSync(file);snapshot=await read();assert.deepEqual(readFileSync(file),original);assert.equal(JSON.stringify(snapshot).includes('never-export-this'),false);assert.equal(snapshot.sources.length,3);assert.deepEqual(snapshot.sources.find(s=>s.path==='pom.xml').fields.find(f=>f.id==='arguments').effectiveValue,['-parameters']);for(const source of snapshot.sources)assert.equal(source.fields.find(f=>f.id==='target').effectiveValue,'11');assert.equal(snapshot.sources.find(s=>s.path==='a/pom.xml').fields.find(f=>f.id==='target').value,null);}); - await step('Save only module a bytecode override, compile and inspect the class header',async()=>{const source=snapshot.sources.find(s=>s.path==='a/pom.xml');const stage=join(scratch,'staged-pom.xml');writeFileSync(stage,'');const reply=await invoke('compiler.prepareSettings',{operation:'editSource',sourceID:source.id,revision:source.revision,values:{target:'8'},outputFile:stage});assert.equal(reply.revision,digest(readFileSync(stage)));assert.equal(readFileSync(join(root,'a/pom.xml'),'utf8').includes('8'),false,'backend must not publish');renameSync(stage,join(root,'a/pom.xml'));await command(maven,['-B','-ntp','-q','compile']);assert.equal(readFileSync(join(root,'a/target/classes/Main.class')).readUInt16BE(6),52);assert.equal(readFileSync(join(root,'b/target/classes/Main.class')).readUInt16BE(6),55);snapshot=await read();assert.equal(snapshot.sources.find(s=>s.path==='a/pom.xml').fields.find(f=>f.id==='target').effectiveValue,'8');assert.equal(snapshot.sources.find(s=>s.path==='a/pom.xml').fields.find(f=>f.id==='target').inheritedValue,'11');assert.match(readFileSync(file,'utf8'),/keep-comment/);}); + await step('Save only module a bytecode override, compile and inspect the class header',async()=>{const source=snapshot.sources.find(s=>s.path==='a/pom.xml');const stage=join(scratch,'staged-pom.xml');writeFileSync(stage,'');const reply=await invoke('compiler.prepareSettings',{operation:'editSource',sourceID:source.id,revision:source.revision,values:{target:'8'},outputFile:stage});assert.equal(reply.revision,digest(readFileSync(stage)));assert.equal(readFileSync(join(root,'a/pom.xml'),'utf8').includes('8'),false,'backend must not publish');renameSync(stage,join(root,'a/pom.xml'));await command(maven,['-B','-ntp','-q',...mavenSettings,'compile']);assert.equal(readFileSync(join(root,'a/target/classes/Main.class')).readUInt16BE(6),52);assert.equal(readFileSync(join(root,'b/target/classes/Main.class')).readUInt16BE(6),55);snapshot=await read();assert.equal(snapshot.sources.find(s=>s.path==='a/pom.xml').fields.find(f=>f.id==='target').effectiveValue,'8');assert.equal(snapshot.sources.find(s=>s.path==='a/pom.xml').fields.find(f=>f.id==='target').inheritedValue,'11');assert.match(readFileSync(file,'utf8'),/keep-comment/);}); capture('zh',snapshot);capture('en',await read('en')); - await step('A local JAR is written as a native dependency and used by Maven compilation',async()=>{const dir=join(scratch,'jar');mkdirSync(dir);writeFileSync(join(dir,'Library.java'),'package fixture; public class Library { public static String value() { return "jar-ok"; } }');await command('javac',['--release','8','-d',dir,join(dir,'Library.java')]);const jar=join(scratch,'External Library.jar');await command('jar',['--create','--file',jar,'-C',dir,'fixture']);const source=snapshot.sources.find(s=>s.path==='a/pom.xml'),stage=join(scratch,'library-pom.xml');writeFileSync(stage,'');await invoke('compiler.prepareSettings',{operation:'editSource',sourceID:source.id,revision:source.revision,values:{libraries:[jar]},outputFile:stage});renameSync(stage,join(root,'a/pom.xml'));writeFileSync(join(root,'a/src/main/java/Main.java'),'public class Main { public static void main(String[] args) { System.out.print(fixture.Library.value()); } }');await command(maven,['-B','-ntp','-q','compile']);assert.equal((await command('java',['-cp',join(root,'a/target/classes')+requirePathSeparator()+jar,'Main'])).toString(),'jar-ok');snapshot=await read();}); + await step('A local JAR is written as a native dependency and used by Maven compilation',async()=>{const dir=join(scratch,'jar');mkdirSync(dir);writeFileSync(join(dir,'Library.java'),'package fixture; public class Library { public static String value() { return "jar-ok"; } }');await command('javac',['--release','8','-d',dir,join(dir,'Library.java')]);const jar=join(scratch,'External Library.jar');await command('jar',['--create','--file',jar,'-C',dir,'fixture']);const source=snapshot.sources.find(s=>s.path==='a/pom.xml'),stage=join(scratch,'library-pom.xml');writeFileSync(stage,'');await invoke('compiler.prepareSettings',{operation:'editSource',sourceID:source.id,revision:source.revision,values:{libraries:[jar]},outputFile:stage});renameSync(stage,join(root,'a/pom.xml'));writeFileSync(join(root,'a/src/main/java/Main.java'),'public class Main { public static void main(String[] args) { System.out.print(fixture.Library.value()); } }');await command(maven,['-B','-ntp','-q',...mavenSettings,'compile']);assert.equal((await command('java',['-cp',join(root,'a/target/classes')+requirePathSeparator()+jar,'Main'])).toString(),'jar-ok');snapshot=await read();}); await step('A changed POM and a removed staging file are rejected without overwriting project content',async()=>{const source=snapshot.sources.find(s=>s.path==='a/pom.xml');const original=readFileSync(join(root,'a/pom.xml'));writeFileSync(join(root,'a/pom.xml'),Buffer.concat([original,Buffer.from('\n')]));const stage=join(scratch,'conflict.xml');writeFileSync(stage,'');await assert.rejects(invoke('compiler.prepareSettings',{operation:'editSource',sourceID:source.id,revision:source.revision,values:{target:'11'},outputFile:stage}));assert.equal(readFileSync(stage).length,0);assert.match(readFileSync(join(root,'a/pom.xml'),'utf8'),/external/);}); await step('SDK successes distinguish native saves from read projections',async()=>{assert.equal(client.diagnostics.some(f=>f.operation==='compiler.settings'&&f.kind==='operationFinished'),false);assert.equal(client.diagnostics.some(f=>f.operation==='compiler.prepareSettings'&&f.kind==='operationFinished'),true);assert.equal(JSON.stringify(client.diagnostics).includes('never-export-this'),false);}); assert.equal(await client.close(),0);client=undefined; diff --git a/scripts/test.mjs b/scripts/test.mjs index e8ad30a..75785bd 100644 --- a/scripts/test.mjs +++ b/scripts/test.mjs @@ -70,10 +70,12 @@ function expand(list, { project, suite, outputFile }) { const report = createReport("maven-integration"); const timeoutMs = mavenTimeout(); +// Clear inherited MAVEN_ARGS so user/runner environment flags cannot leak into Maven invocations. +const mavenEnv = { ...process.env, MAVEN_ARGS: "" }; function run(name, root, list, signal, values = {}) { - const argv = [...expand(buildSystem.invocation.globalArguments, values), ...expand(list, values)]; + const argv = [...expand(buildSystem.invocation.globalArguments, values), ...modelSettings(), ...expand(list, values)]; return report.step(name, () => runCommand(maven, argv, { - cwd: root, stdio: ["ignore", "ignore", "inherit"], timeoutMs, signal, + cwd: root, env: mavenEnv, stdio: ["ignore", "ignore", "inherit"], timeoutMs, signal, })); } @@ -82,6 +84,12 @@ function run(name, root, list, signal, values = {}) { // but resolves `-pl core` against the working directory as given, so under a // short name it finds no selected project. Lithe passes the long path it opened. const scratch = realpathSync.native(mkdtempSync(join(tmpdir(), "lithe-maven-test-"))); +const emptySettings = join(scratch, "empty-settings.xml"); +writeFileSync(emptySettings, ""); +// Both Maven settings files must be isolated from the runner's local installation. +function modelSettings(userSettings = emptySettings) { + return ["-gs", emptySettings, "-s", userSettings]; +} const root = join(scratch, "reactor"); const outputs = join(scratch, "outputs"); const captureDir = process.env.LITHE_CAPTURE_DIR; @@ -123,12 +131,12 @@ async function document(name, declaration, signal, values = {}) { * project's JDK. */ async function prepareRuns(signal) { - const client = new BackendClient({ jar: join(pluginDir, "lib", "lithe-maven-provider.jar"), args: [], timeoutMs: timeoutMs }); + const client = new BackendClient({ jar: join(pluginDir, "lib", "lithe-maven-provider.jar"), args: [], env: mavenEnv, timeoutMs: timeoutMs }); try { await report.step("start the build provider", () => client.initialize(root)); let operation = 0; const request = (target, extra = {}) => client.request("lithe/build/prepareRun", { - operationID: `run-${++operation}`, buildSystemID: "maven", root: ".", options: {}, optionArguments: [], environment: {}, + operationID: `run-${++operation}`, buildSystemID: "maven", root: ".", options: {}, optionArguments: modelSettings(), environment: {}, tool: { executable: maven }, run: target.tags.includes("spring-boot") ? "spring-boot" : "application", project: { name: target.path, path: target.path }, target, userArguments: ["typed"], ...extra, }); @@ -283,19 +291,17 @@ async function test(signal) { }); const profileRoot = join(scratch, "profiles-reactor"); - const emptySettings = join(scratch, "profile-empty-settings.xml"); - writeFileSync(emptySettings, ""); const readProfileModel = async (tag, profileArg, settings = emptySettings) => { const outputFile = join(outputs, `profiles-${tag}.json`); const extra = profileArg ? ["-P", profileArg] : []; const argv = [ ...expand(buildSystem.invocation.globalArguments, { outputFile }), - "-gs", emptySettings, "-s", settings, + ...modelSettings(settings), ...extra, ...expand(buildSystem.model.arguments, { outputFile }), ]; await runCommand(maven, argv, { - cwd: profileRoot, env: { ...process.env, MAVEN_ARGS: "" }, + cwd: profileRoot, env: mavenEnv, stdio: ["ignore", "ignore", "inherit"], timeoutMs, signal, }); return JSON.parse(readFileSync(outputFile, "utf8")); @@ -491,8 +497,8 @@ async function test(signal) { await check("without Lithe's properties the extension does nothing", async () => { rmSync(join(root, "core", "target"), { recursive: true, force: true }); - await runCommand(maven, ["-B", "-ntp", "-q", `-Dmaven.ext.class.path=${pluginDir}/lib/lithe-maven-extension.jar`, "validate"], { - cwd: root, stdio: ["ignore", "ignore", "inherit"], timeoutMs, signal, + await runCommand(maven, ["-B", "-ntp", "-q", ...modelSettings(), `-Dmaven.ext.class.path=${pluginDir}/lib/lithe-maven-extension.jar`, "validate"], { + cwd: root, env: mavenEnv, stdio: ["ignore", "ignore", "inherit"], timeoutMs, signal, }); }); @@ -640,7 +646,7 @@ async function verifyExtendedBootAndProvided(signal) { ]); }); - const client = new BackendClient({ jar: join(pluginDir, "lib", "lithe-maven-provider.jar"), args: [], timeoutMs: timeoutMs }); + const client = new BackendClient({ jar: join(pluginDir, "lib", "lithe-maven-provider.jar"), args: [], env: mavenEnv, timeoutMs: timeoutMs }); try { await report.step("start provider for extended boot and Java 21 entrypoints", () => client.initialize(extRoot)); let operation = 0; @@ -663,26 +669,26 @@ async function verifyExtendedBootAndProvided(signal) { rmSync(join(extRoot, compactFile)); const bootProvided = await client.request("lithe/build/prepareRun", { - operationID: `ext-run-${++operation}`, buildSystemID: "maven", root: ".", options: {}, optionArguments: [], environment: {}, + operationID: `ext-run-${++operation}`, buildSystemID: "maven", root: ".", options: {}, optionArguments: modelSettings(), environment: {}, tool: { executable: maven }, run: "spring-boot", project: { name: "apps", path: "apps" }, target: { name: "CustomMetaApp", path: "apps", tags: ["spring-boot"], properties: { mainClass: "demo.apps.CustomMetaApp" } }, userArguments: [], }); const bootNoProvided = await client.request("lithe/build/prepareRun", { operationID: `ext-run-${++operation}`, buildSystemID: "maven", root: ".", options: {}, - optionArguments: ["-Dlithe.runIncludeProvided=false"], environment: {}, + optionArguments: [...modelSettings(), "-Dlithe.runIncludeProvided=false"], environment: {}, tool: { executable: maven }, run: "spring-boot", project: { name: "apps", path: "apps" }, target: { name: "CustomMetaApp", path: "apps", tags: ["spring-boot"], properties: { mainClass: "demo.apps.CustomMetaApp" } }, userArguments: [], }); const customBootDiscovered = await client.request("lithe/build/prepareRun", { - operationID: `ext-run-${++operation}`, buildSystemID: "maven", root: ".", options: {}, optionArguments: [], environment: {}, + operationID: `ext-run-${++operation}`, buildSystemID: "maven", root: ".", options: {}, optionArguments: modelSettings(), environment: {}, tool: { executable: maven }, run: "spring-boot", project: { name: "custom-boot", path: "custom-boot" }, target: { name: "HelperMain", path: "custom-boot", tags: ["spring-boot"], properties: {} }, userArguments: [], }); const plainPrepared = await client.request("lithe/build/prepareRun", { - operationID: `ext-run-${++operation}`, buildSystemID: "maven", root: ".", options: {}, optionArguments: [], environment: {}, + operationID: `ext-run-${++operation}`, buildSystemID: "maven", root: ".", options: {}, optionArguments: modelSettings(), environment: {}, tool: { executable: maven }, run: "application", project: { name: "plain", path: "plain" }, target: { name: "InstanceVoidMain", path: "plain", tags: ["java-application"], properties: {} }, userArguments: [],