Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check java version was criteria was ambiguous #5363

Merged
merged 7 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions packages/salesforcedx-vscode-apex/src/messages/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const messages = {
'An unsupported Java version was detected. Download and install [Java 11](https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html) or [Java 17](https://www.oracle.com/java/technologies/downloads/#java17) to run the extensions. For more information, see [Set Your Java Version](%s).',
wrong_java_version_short:
'Unsupported Java version',
java_version_check_command_failed: 'Running java command %s failed with error: %s',
force_apex_test_suite_build_text: 'SFDX: Build Apex Test Suite',
unable_to_locate_editor: 'You can run this command only on a source file.',
unable_to_locate_document: 'You can run this command only on a source file.',
Expand Down
15 changes: 8 additions & 7 deletions packages/salesforcedx-vscode-apex/src/requirements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,17 @@ const isLocal = (javaHome: string): boolean => {
};

export const checkJavaVersion = async (javaHome: string): Promise<boolean> => {
const cmdFile = path.join(javaHome, 'bin', 'java');
const commandOptions = ['-XshowSettings:properties', '-version'];
return new Promise((resolve, reject) => {
cp.execFile(
javaHome + '/bin/java',
['-version'],
cp.execFile(cmdFile,
commandOptions,
{},
(error, stdout, stderr) => {
if (
stderr.indexOf('build 11.') < 0 &&
stderr.indexOf('build 17.') < 0
) {
if (error) {
reject(nls.localize('java_version_check_command_failed', `${cmdFile} ${commandOptions.join(' ')}`, error.message));
}
if (!/java\.version\s*=\s*(?:11|17)/g.test(stderr)) {
peternhale marked this conversation as resolved.
Show resolved Hide resolved
reject(nls.localize('wrong_java_version_text', SET_JAVA_DOC_LINK));
} else {
resolve(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,24 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

// tslint:disable:no-unused-expression

import { fail } from 'assert';
import { expect } from 'chai';
import * as cp from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import { createSandbox, SinonSandbox, SinonStub } from 'sinon';
import * as vscode from 'vscode';
import { SET_JAVA_DOC_LINK } from '../../src/constants';
import { nls } from '../../src/messages';
import { SET_JAVA_DOC_LINK } from '../../../src/constants';
import { nls } from '../../../src/messages';
import {
checkJavaVersion,
JAVA_HOME_KEY,
resolveRequirements
} from '../../src/requirements';
} from '../../../src/requirements';

const jdk = 'openjdk1.8.0.302_8.56.0.22_x64';
const runtimePath = `~/java_home/real/jdk/${jdk}`;

// TODO: Move this to a new unit test directory

describe('Java Requirements Test', () => {
let sandbox: SinonSandbox;
let settingStub: SinonStub;
Expand All @@ -35,12 +32,9 @@ describe('Java Requirements Test', () => {
beforeEach(() => {
sandbox = createSandbox();
settingStub = sandbox.stub();
sandbox
.stub(vscode.workspace, 'getConfiguration')
.withArgs()
.returns({
get: settingStub
});
sandbox.stub(vscode.workspace, 'getConfiguration').withArgs().returns({
get: settingStub
});
pathExistsStub = sandbox.stub(fs, 'existsSync').resolves(true);
execFileStub = sandbox.stub(cp, 'execFile');
});
Expand All @@ -62,13 +56,13 @@ describe('Java Requirements Test', () => {

it('Should allow valid java runtime path outside the project', async () => {
settingStub.withArgs(JAVA_HOME_KEY).returns(runtimePath);
execFileStub.yields('', '', 'build 11.0.0');
execFileStub.yields('', '', 'java.version = 11.0.0');
const requirements = await resolveRequirements();
expect(requirements.java_home).contains(jdk);
});

it('Should not support Java 8', async () => {
execFileStub.yields('', '', 'build 1.8.0');
execFileStub.yields('', '', 'java.version = 1.8.0');
try {
await checkJavaVersion('~/java_home');
fail('Should have thrown when the Java version is not supported');
Expand All @@ -80,7 +74,7 @@ describe('Java Requirements Test', () => {
});

it('Should support Java 11', async () => {
execFileStub.yields('', '', 'build 11.0.0');
execFileStub.yields('', '', 'java.version = 11.0.0');
try {
const result = await checkJavaVersion('~/java_home');
expect(result).to.equal(true);
Expand All @@ -92,7 +86,7 @@ describe('Java Requirements Test', () => {
});

it('Should support Java 17', async () => {
execFileStub.yields('', '', 'build 17.2.3');
execFileStub.yields('', '', 'java.version = 17.2.3');
try {
const result = await checkJavaVersion('~/java_home');
expect(result).to.equal(true);
Expand All @@ -104,7 +98,7 @@ describe('Java Requirements Test', () => {
});

it('Should not support Java 20', async () => {
execFileStub.yields('', '', 'build 20.0.0');
execFileStub.yields('', '', 'java.version = 20.0.0');
try {
await checkJavaVersion('~/java_home');
fail('Should have thrown when the Java version is not supported');
Expand All @@ -114,4 +108,20 @@ describe('Java Requirements Test', () => {
);
}
});

it('Should reject java version check when execFile fails', async () => {
execFileStub.yields({ message: 'its broken' }, '', '');
try {
await checkJavaVersion(path.join('~', 'java_home'));
fail('Should have thrown when the Java version is not supported');
} catch (err) {
expect(err).to.equal(
nls.localize(
'java_version_check_command_failed',
`${path.join('~', 'java_home', 'bin', 'java')} -XshowSettings:properties -version`,
'its broken'
)
);
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ export const forceSourceDeploySourcePaths = async (
// passed in (sourceUri) is actually an array and not a single URI.
uris = sourceUri;
} else {
uris = [];
uris.push(sourceUri);
uris = [sourceUri];
}
}

Expand Down
Loading