Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
Merge pull request #230 from pranavparikh/master
Browse files Browse the repository at this point in the history
Fixing readAndValidateJson()
  • Loading branch information
pranavparikh committed Apr 18, 2014
2 parents a54642c + d370e5e commit c569a73
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 12 deletions.
8 changes: 4 additions & 4 deletions lib/session/testexecutor.js
Expand Up @@ -10,7 +10,7 @@ var ReportManager = require("../util/reportmanager");
var WdSession = require("../session/wdsession");
var TestSession = require("../session/testsession");
var ProxyManager = require("../proxy/proxymanager");
var _ = require('underscore');
var clone = require('clone');
var path = require("path");
var os = require("os");
var async = require("async");
Expand Down Expand Up @@ -223,7 +223,7 @@ TestExecutor.prototype.setupTestQueues = function(callback) {
for (i = 0; i < wdSessions.length; i += 1) {
testQueues[i] = {curIndex: 0, sessions: []};
for (j = 0; j < tests.length; j += 1) {
test = _.clone(tests[j]);
test = clone(tests[j]);

// If proxy set to true or reuseSession set to false , ignore reuseSession
if (test.startProxyServer === true || test.reuseSession === false) {
Expand All @@ -244,7 +244,7 @@ TestExecutor.prototype.setupTestQueues = function(callback) {
for (i = 0; i < tests.length; i += 1) {
browsers = sf.getBrowsers(tests[i]);
for (j = 0; j < browsers.length; j += 1) {
test = _.clone(tests[i]);
test = clone(tests[i]);
test.browser = browsers[j];
testQueues[0].sessions.push(new TestSession(sf.config, test));
incrementTestSessionCount(test);
Expand Down Expand Up @@ -273,7 +273,7 @@ TestExecutor.prototype.getTests = function(callback) {
}
} else if (sf.tests) {
for (i = 0; i < sf.tests.length; i += 1) {
testParams = _.clone(sf.args);
testParams = clone(sf.args);
testParams.test = sf.tests[i];
// convert the cmd line test to look like a descriptor test
test = {
Expand Down
3 changes: 2 additions & 1 deletion lib/util/capabilitymanager.js
Expand Up @@ -9,6 +9,7 @@
var log4js = require("log4js");
var fs = require("fs");
var _ = require("underscore");
var clone = require("clone");

function CapabilityManager() {
this.logger = log4js.getLogger("CapabilityManager");
Expand All @@ -31,7 +32,7 @@ CapabilityManager.prototype.getCapability = function (caps, capName) {
try {

capJson = self.getCapsJSON(caps);
cap = _.clone(capJson.capabilities[capName]);
cap = clone(capJson.capabilities[capName]);
commonCap = capJson.common_capabilities;
if (cap) {
for (k in commonCap) {
Expand Down
28 changes: 22 additions & 6 deletions lib/util/dataprovider.js
Expand Up @@ -135,27 +135,43 @@ DataProvider.prototype.readAndValidateJSON = function (param) {
json,
parsedJson,
self = this,
pwd = global.workingDirectory || '';
pwd = global.workingDirectory || '',
proc = self.mock || process,
isFile = false;

if (param) {
if (param.indexOf(".json") > 0) {

try {
// If file, read Json from file and validate
paramPath = path.resolve(pwd, param);
json = fs.readFileSync(paramPath, "utf-8");
} else {
isFile = true;
}
catch(e) {
// Not a file, so treat it as json string
json = param;
}

try {
parsedJson = JSON.parse(json);
} catch (msg) {
self.logger.error(param + " is not a valid JSON");

var errMsg;

if (isFile) {
errMsg = "The file " + param + " does not contain valid JSON";
} else {
errMsg = param + " is not a valid JSON";
}
self.logger.error(msg);
process.exit(1);
self.logger.error(errMsg);

proc.exit(1);
}

}
return parsedJson;

return parsedJson;

};

Expand Down
3 changes: 3 additions & 0 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -57,7 +57,8 @@
"xml2js": "0.1.14",
"mkdirp": "0.3.5",
"selenium-webdriver": "2.39.0",
"underscore": "1.6.0"
"underscore": "1.6.0",
"clone":"0.1.11"
},
"optionalDependencies": {
"mocha": "1.7.4",
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/lib/util/dataprovider-tests.js
Expand Up @@ -25,6 +25,12 @@ YUI.add('dataprovider-tests', function (Y) {
var dp = new dataProv(conf, args, __dirname + "/testDescriptor.json");
var dpvalues = dp.getTestData();

dp.mock = {
exit: function (code) {
throw new Error("exit code is "+code);
}
}

suite.add(new Y.Test.Case({
"Confirm constructor works": function(){
Y.Assert.isNotNull(dp, "Confirm initiallizing does not return null");
Expand Down Expand Up @@ -145,6 +151,42 @@ YUI.add('dataprovider-tests', function (Y) {
}
}));


suite.add(new Y.Test.Case({

"Invalid json from file": function() {

var msg;

try {
dp.readAndValidateJSON(__dirname + '/invalid.json');
}
catch(e) {
msg = e;
}
Y.Assert.areEqual( "exit code is 1", msg.message);
}
}));


suite.add(new Y.Test.Case({

"Invalid json ": function() {

var msg;

try {
dp.readAndValidateJSON('{\'a\'}');
}
catch(e) {
msg = e;
}
Y.Assert.areEqual( "exit code is 1", msg.message);
}
}));



suite.add(new Y.Test.Case({
"Replace params": function(){
var conf = {
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/lib/util/invalid.json
@@ -0,0 +1,3 @@
{
invalid
}

0 comments on commit c569a73

Please sign in to comment.