From 7be4f03da8ed016ce95614114d10b20f31cc661a Mon Sep 17 00:00:00 2001 From: Josh Feingold Date: Thu, 5 Dec 2024 12:36:16 -0600 Subject: [PATCH] FIX (CodeAnalyzer) @W-17370746@ - Java Version identification bug fixed (#1689) --- src/lib/JreSetupManager.ts | 8 +++++++- test/lib/JreSetupManager.test.ts | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/lib/JreSetupManager.ts b/src/lib/JreSetupManager.ts index 34ceb8869..45684251e 100644 --- a/src/lib/JreSetupManager.ts +++ b/src/lib/JreSetupManager.ts @@ -116,7 +116,13 @@ class JreSetupManager extends AsyncCreatable { } private async verifyJavaVersion(javaHome: string): Promise { - const versionCommandOut = await this.fetchJavaVersion(javaHome); + let versionCommandOut: string; + try { + versionCommandOut = await this.fetchJavaVersion(javaHome); + } catch (e) { + const msg: string = e instanceof Error ? e.message : e as string; + throw new SfError(msg, 'CouldNotFindJavaVersion'); + } // We are using "java -version" below which has output that typically looks like: // * (from MacOS): "openjdk version "11.0.6" 2020-01-14 LTS\nOpenJDK Runtime Environment Zulu11.37+17-CA (build 11.0.6+10-LTS)\nOpenJDK 64-Bit Server VM Zulu11.37+17-CA (build 11.0.6+10-LTS, mixed mode)\n" diff --git a/test/lib/JreSetupManager.test.ts b/test/lib/JreSetupManager.test.ts index aef7ddb3f..744f23cf1 100644 --- a/test/lib/JreSetupManager.test.ts +++ b/test/lib/JreSetupManager.test.ts @@ -203,6 +203,30 @@ describe('JreSetupManager #verifyJreSetup', () => { statStub.restore(); }); + it('should fail when valid path is found, but Java version cannot be returned', async () => { + // More stubbing + Sinon.stub(Config.prototype, 'getJavaHome').returns(javaHomeValidPath); + // FileHandler claims the path is valid. + Sinon.stub(FileHandler.prototype, 'stats').resolves(); + // Error indicates that version could not be retrieved. + Sinon.stub(childProcess, 'execFile').yields(error, emptyStdout, 'irrelevant'); + + // Execute and verify + let threw = false; + let name: string = ''; + let message: string = ''; + try { + await verifyJreSetup(); + } catch (err) { + threw = true; + name = err.name; + message = err instanceof Error ? err.message : err as string; + } + expect(threw).equals(true); + expect(message).contains('Could not fetch Java version from path'); + expect(name).equals('CouldNotFindJavaVersion'); + }); + it('should fail when valid path is found, but Java version is not acceptable', async () => { // More stubbing const configGetJavaHomeStub = Sinon.stub(Config.prototype, 'getJavaHome').returns(javaHomeValidPath);