Fix #4763. Parse java version even when it does not come in the first line of the command output

code style improvements

bind callback to local function

code style
This commit is contained in:
Filipe Esperandio
2015-03-24 14:19:25 -03:00
parent bf2abf55f9
commit 202c6e65ce
3 changed files with 65 additions and 7 deletions

View File

@@ -1025,22 +1025,32 @@ androidCommon.hideKeyboard = function () {
}.bind(this));
};
androidCommon.parseJavaVersion = function (stderr) {
var lines = stderr.split("\n");
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (new RegExp("java version").test(line)) {
return line.split(" ")[2].replace(/"/g, '');
}
}
return null;
};
androidCommon.getJavaVersion = function (cb) {
exec("java -version", function (err, stdout, stderr) {
var extractVersion = function (err, stdout, stderr) {
var javaVersion = null;
if (err) {
return cb(new Error("'java -version' failed. " + err));
} else if (stderr) {
var firstLine = stderr.split("\n")[0];
if (new RegExp("java version").test(firstLine)) {
javaVersion = firstLine.split(" ")[2].replace(/"/g, '');
}
javaVersion = this.parseJavaVersion(stderr);
}
if (javaVersion === null) {
return cb(new Error("Could not get the Java version. Is Java installed?"));
}
return cb(null, javaVersion);
});
}.bind(this);
exec("java -version", extractVersion);
};
androidCommon.initJavaVersion = function (cb) {

View File

@@ -139,6 +139,7 @@
"wd": "~0.3.11",
"wd-bridge": "~0.0.2",
"yargs": "^3.0.4",
"yiewd": "~0.6.0"
"yiewd": "~0.6.0",
"rewire": "^2.3.1"
}
}

View File

@@ -0,0 +1,47 @@
'use strict';
var rewire = require('rewire')
, androidCommon = rewire('../../lib/devices/android/android-common.js')
, chai = require('chai');
chai.should();
var javaVersionStdErr = 'Picked up _JAVA_OPTIONS: -Djava.awt.headless=true\njava version "1.8.0_22"';
var fakeExec = function (_, fn) {
fn(null, null, javaVersionStdErr);
};
var _testDouble = function () {
var _version = 'Version not set';
return {
callback : function (_, version) {
_version = version;
},
value : function () {
return _version;
}
};
};
androidCommon.__set__("exec", fakeExec);
describe('devices/android/android-common.js', function () {
describe('java version parsing', function () {
it('parses single line output', function () {
androidCommon.parseJavaVersion('java version 1.8').should.be.equal('1.8');
});
it('parses multiple line output', function () {
androidCommon.parseJavaVersion(javaVersionStdErr).should.be.equal('1.8.0_22');
});
it('passes version to callback', function () {
var testDouble = _testDouble();
androidCommon.getJavaVersion(testDouble.callback);
testDouble.value().should.be.equal('1.8.0_22');
});
});
});