Skip to content

Commit 7be4f03

Browse files
authored
FIX (CodeAnalyzer) @W-17370746@ - Java Version identification bug fixed (#1689)
1 parent d4d9840 commit 7be4f03

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/lib/JreSetupManager.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,13 @@ class JreSetupManager extends AsyncCreatable {
116116
}
117117

118118
private async verifyJavaVersion(javaHome: string): Promise<void> {
119-
const versionCommandOut = await this.fetchJavaVersion(javaHome);
119+
let versionCommandOut: string;
120+
try {
121+
versionCommandOut = await this.fetchJavaVersion(javaHome);
122+
} catch (e) {
123+
const msg: string = e instanceof Error ? e.message : e as string;
124+
throw new SfError(msg, 'CouldNotFindJavaVersion');
125+
}
120126

121127
// We are using "java -version" below which has output that typically looks like:
122128
// * (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"

test/lib/JreSetupManager.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,30 @@ describe('JreSetupManager #verifyJreSetup', () => {
203203
statStub.restore();
204204
});
205205

206+
it('should fail when valid path is found, but Java version cannot be returned', async () => {
207+
// More stubbing
208+
Sinon.stub(Config.prototype, 'getJavaHome').returns(javaHomeValidPath);
209+
// FileHandler claims the path is valid.
210+
Sinon.stub(FileHandler.prototype, 'stats').resolves();
211+
// Error indicates that version could not be retrieved.
212+
Sinon.stub(childProcess, 'execFile').yields(error, emptyStdout, 'irrelevant');
213+
214+
// Execute and verify
215+
let threw = false;
216+
let name: string = '';
217+
let message: string = '';
218+
try {
219+
await verifyJreSetup();
220+
} catch (err) {
221+
threw = true;
222+
name = err.name;
223+
message = err instanceof Error ? err.message : err as string;
224+
}
225+
expect(threw).equals(true);
226+
expect(message).contains('Could not fetch Java version from path');
227+
expect(name).equals('CouldNotFindJavaVersion');
228+
});
229+
206230
it('should fail when valid path is found, but Java version is not acceptable', async () => {
207231
// More stubbing
208232
const configGetJavaHomeStub = Sinon.stub(Config.prototype, 'getJavaHome').returns(javaHomeValidPath);

0 commit comments

Comments
 (0)