Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit d3fa315

Browse files
Merge pull request #1037 from telerik/vladimirov/java-9
Fix detection of Javac version
2 parents c490c6a + 8445473 commit d3fa315

File tree

5 files changed

+92
-33
lines changed

5 files changed

+92
-33
lines changed

declarations.d.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,8 +1123,6 @@ interface ISysInfoData extends IPlatform {
11231123
nodeGypVer: string;
11241124

11251125
// dependencies
1126-
/** version of java, as returned by `java -version` */
1127-
javaVer: string;
11281126
/** Xcode version string as returned by `xcodebuild -version`. Valid only on Mac */
11291127
xcodeVer: string;
11301128
/** Version string of adb, as returned by `adb version` */
@@ -1156,9 +1154,6 @@ interface ISysInfo {
11561154
*/
11571155
getSysInfo(pathToPackageJson: string, androidToolsInfo?: { pathToAdb: string }): Promise<ISysInfoData>;
11581156

1159-
/** Returns Java version. **/
1160-
getJavaVersion(): Promise<string>;
1161-
11621157
/** Returns Java compiler version. **/
11631158
getJavaCompilerVersion(): Promise<string>;
11641159

helpers.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,18 @@ export async function getFuturesResults<T>(promises: Promise<T | T[]>[], predica
290290
.value();
291291
}
292292

293+
/**
294+
* Appends zeroes to a version string until it reaches a specified length.
295+
* @param {string} version The version on which to append zeroes.
296+
* @param requiredVersionLength The required length of the version string.
297+
* @returns {string} Appended version string. In case input is null, undefined or empty string, it is returned immediately without appending anything.
298+
*/
293299
export function appendZeroesToVersion(version: string, requiredVersionLength: number): string {
294-
const zeroesToAppend = requiredVersionLength - version.split(".").length;
295-
for (let index = 0; index < zeroesToAppend; index++) {
296-
version += ".0";
300+
if (version) {
301+
const zeroesToAppend = requiredVersionLength - version.split(".").length;
302+
for (let index = 0; index < zeroesToAppend; index++) {
303+
version += ".0";
304+
}
297305
}
298306

299307
return version;

sys-info-base.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,6 @@ export class SysInfoBase implements ISysInfo {
1313

1414
private monoVerRegExp = /version (\d+[.]\d+[.]\d+) /gm;
1515
private sysInfoCache: ISysInfoData = undefined;
16-
private javaVerCache: string = null;
17-
public async getJavaVersion(): Promise<string> {
18-
if (!this.javaVerCache) {
19-
try {
20-
// different java has different format for `java -version` command
21-
const output = (await this.$childProcess.spawnFromEvent("java", ["-version"], "exit")).stderr;
22-
this.javaVerCache = /(?:openjdk|java) version \"((?:\d+\.)+(?:\d+))/i.exec(output)[1];
23-
} catch (e) {
24-
this.javaVerCache = null;
25-
}
26-
}
27-
return this.javaVerCache;
28-
}
2916

3017
private npmVerCache: string = null;
3118
public async getNpmVersion(): Promise<string> {
@@ -47,7 +34,7 @@ export class SysInfoBase implements ISysInfo {
4734
const output = await this.exec(`"${pathToJavaCompilerExecutable}" -version`, { showStderr: true });
4835
// for other versions of java javac version output is not on first line
4936
// thus can't use ^ for starts with in regex
50-
this.javaCompilerVerCache = output ? /javac (.*)/i.exec(output.stderr)[1] : null;
37+
this.javaCompilerVerCache = output ? /javac (.*)/i.exec(`${output.stderr}${os.EOL}${output.stdout}`)[1] : null;
5138
} catch (e) {
5239
this.$logger.trace(`Command "${pathToJavaCompilerExecutable} --version" failed: ${e}`);
5340
this.javaCompilerVerCache = null;
@@ -154,8 +141,6 @@ export class SysInfoBase implements ISysInfo {
154141

155142
res.npmVer = await this.getNpmVersion();
156143

157-
res.javaVer = await this.getJavaVersion();
158-
159144
res.nodeGypVer = await this.getNodeGypVersion();
160145
res.xcodeVer = await this.getXCodeVersion();
161146
res.xcodeprojGemLocation = await this.getXCodeProjGemLocation();

test/unit-tests/helpers.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,61 @@ describe("helpers", () => {
1515
assert.deepEqual(actualResult, testData.expectedResult, `For input ${testData.input}, the expected result is: ${testData.expectedResult}, but actual result is: ${actualResult}.`);
1616
};
1717

18+
describe("appendZeroesToVersion", () => {
19+
interface IAppendZeroesToVersionTestData extends ITestData {
20+
requiredVersionLength: number;
21+
}
22+
23+
const testData: IAppendZeroesToVersionTestData[] = [
24+
{
25+
input: "3.0.0",
26+
requiredVersionLength: 3,
27+
expectedResult: "3.0.0"
28+
},
29+
{
30+
input: "3.0",
31+
requiredVersionLength: 3,
32+
expectedResult: "3.0.0"
33+
},
34+
{
35+
input: "3",
36+
requiredVersionLength: 3,
37+
expectedResult: "3.0.0"
38+
},
39+
{
40+
input: "1.8.0_152",
41+
requiredVersionLength: 3,
42+
expectedResult: "1.8.0_152"
43+
},
44+
{
45+
input: "",
46+
requiredVersionLength: 3,
47+
expectedResult: ""
48+
},
49+
{
50+
input: null,
51+
requiredVersionLength: 3,
52+
expectedResult: null
53+
},
54+
{
55+
input: undefined,
56+
requiredVersionLength: 3,
57+
expectedResult: undefined
58+
},
59+
{
60+
input: "1",
61+
requiredVersionLength: 5,
62+
expectedResult: "1.0.0.0.0"
63+
},
64+
];
65+
66+
it("appends correct number of zeroes", () => {
67+
_.each(testData, testCase => {
68+
assert.deepEqual(helpers.appendZeroesToVersion(testCase.input, testCase.requiredVersionLength), testCase.expectedResult);
69+
});
70+
});
71+
});
72+
1873
describe("executeActionByChunks", () => {
1974
const chunkSize = 2;
2075

test/unit-tests/sys-info-base.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ interface IChildProcessResultDescription {
1919
interface IChildProcessResults {
2020
uname: IChildProcessResultDescription;
2121
npmV: IChildProcessResultDescription;
22-
javaVersion: IChildProcessResultDescription;
2322
javacVersion: IChildProcessResultDescription;
2423
nodeGypVersion: IChildProcessResultDescription;
2524
xCodeVersion: IChildProcessResultDescription;
@@ -32,7 +31,7 @@ interface IChildProcessResults {
3231
}
3332

3433
function getResultFromChildProcess(childProcessResultDescription: IChildProcessResultDescription, spawnFromEventOpts?: { throwError: boolean }): any {
35-
if (childProcessResultDescription.shouldThrowError) {
34+
if (!childProcessResultDescription || childProcessResultDescription.shouldThrowError) {
3635
if (spawnFromEventOpts && !spawnFromEventOpts.throwError) {
3736
return {
3837
stderr: "This one throws error.",
@@ -51,7 +50,6 @@ function createChildProcessResults(childProcessResult: IChildProcessResults): ID
5150
return {
5251
"uname -a": childProcessResult.uname,
5352
"npm -v": childProcessResult.npmV,
54-
"java": childProcessResult.javaVersion,
5553
'"javac" -version': childProcessResult.javacVersion,
5654
"node-gyp -v": childProcessResult.nodeGypVersion,
5755
"xcodebuild -version": childProcessResult.xCodeVersion,
@@ -65,7 +63,7 @@ function createChildProcessResults(childProcessResult: IChildProcessResults): ID
6563
};
6664
}
6765

68-
function createTestInjector(childProcessResult: IChildProcessResults, hostInfoData: { isWindows: boolean, dotNetVersion: string, isDarwin: boolean }, itunesError: string): IInjector {
66+
function createTestInjector(childProcessResult: IChildProcessResults, hostInfoData: { isWindows: boolean, dotNetVersion?: string, isDarwin: boolean }, itunesError: string): IInjector {
6967
const injector = new Yok();
7068
const childProcessResultDictionary = createChildProcessResults(childProcessResult);
7169
injector.register("childProcess", {
@@ -79,7 +77,7 @@ function createTestInjector(childProcessResult: IChildProcessResults, hostInfoDa
7977
});
8078

8179
injector.register("hostInfo", {
82-
dotNetVersion: () => Promise.resolve(hostInfoData.dotNetVersion),
80+
dotNetVersion: () => Promise.resolve(hostInfoData.dotNetVersion || "4.5.1"),
8381
isWindows: hostInfoData.isWindows,
8482
isDarwin: hostInfoData.isDarwin
8583
});
@@ -123,7 +121,6 @@ describe("sysInfoBase", () => {
123121
childProcessResult = {
124122
uname: { result: "name" },
125123
npmV: { result: "2.14.1" },
126-
javaVersion: { result: { stderr: 'java version "1.8.0_60"' } },
127124
javacVersion: { result: { stderr: 'javac 1.8.0_60' } },
128125
nodeGypVersion: { result: "2.0.0" },
129126
xCodeVersion: { result: "6.4.0" },
@@ -142,7 +139,6 @@ describe("sysInfoBase", () => {
142139
describe("returns correct results when everything is installed", () => {
143140
const assertCommonValues = (result: ISysInfoData) => {
144141
assert.deepEqual(result.npmVer, childProcessResult.npmV.result);
145-
assert.deepEqual(result.javaVer, "1.8.0");
146142
assert.deepEqual(result.javacVersion, "1.8.0_60");
147143
assert.deepEqual(result.nodeGypVer, childProcessResult.nodeGypVersion.result);
148144
assert.deepEqual(result.adbVer, childProcessResult.adbVersion.result);
@@ -214,12 +210,33 @@ describe("sysInfoBase", () => {
214210
});
215211
});
216212

213+
describe("getJavaCompilerVersion", () => {
214+
const verifyJavaCompilerVersion = async (javaCompilerVersion: string, resultStream: string): Promise<void> => {
215+
const javaCompilerChildProcessResult: any = {
216+
[resultStream]: `javac ${javaCompilerVersion}`
217+
};
218+
219+
javaCompilerChildProcessResult.javacVersion = { result: javaCompilerChildProcessResult };
220+
testInjector = createTestInjector(javaCompilerChildProcessResult, { isWindows: false, isDarwin: true }, null);
221+
sysInfoBase = testInjector.resolve("sysInfoBase");
222+
const actualJavaCompilerVersion = await sysInfoBase.getJavaCompilerVersion();
223+
assert.deepEqual(actualJavaCompilerVersion, javaCompilerVersion);
224+
};
225+
226+
it("returns correct javac version when it is printed on stderr (Java 8)", () => {
227+
return verifyJavaCompilerVersion("1.8.0_152", "stderr");
228+
});
229+
230+
it("returns correct javac version when it is printed on stdout (Java 9)", () => {
231+
return verifyJavaCompilerVersion("9.0.1", "stdout");
232+
});
233+
});
234+
217235
describe("returns correct results when exceptions are raised during sysInfo data collection", () => {
218236
beforeEach(() => {
219237
childProcessResult = {
220238
uname: { shouldThrowError: true },
221239
npmV: { shouldThrowError: true },
222-
javaVersion: { shouldThrowError: true },
223240
javacVersion: { shouldThrowError: true },
224241
nodeGypVersion: { shouldThrowError: true },
225242
xCodeVersion: { shouldThrowError: true },
@@ -253,7 +270,6 @@ describe("sysInfoBase", () => {
253270
sysInfoBase = testInjector.resolve("sysInfoBase");
254271
const result = await sysInfoBase.getSysInfo(toolsPackageJson);
255272
assert.deepEqual(result.npmVer, null);
256-
assert.deepEqual(result.javaVer, null);
257273
assert.deepEqual(result.javacVersion, null);
258274
assert.deepEqual(result.nodeGypVer, null);
259275
assert.deepEqual(result.xcodeVer, null);

0 commit comments

Comments
 (0)