Skip to content

Commit

Permalink
Silent API Calls and Tests
Browse files Browse the repository at this point in the history
SetRootDir - assert validation failures
setPersistentLogging - assert exists; silently do error handling
Tests use intercept-stdout or SG's own logging to capture
messages, then compare with grand truth.  The intercept-stdout
was showing msges to console, not anymore.
  • Loading branch information
Setogit committed Aug 20, 2016
1 parent 50bcee8 commit b7196c4
Show file tree
Hide file tree
Showing 19 changed files with 114 additions and 95 deletions.
3 changes: 0 additions & 3 deletions index.js
Expand Up @@ -39,9 +39,6 @@ function SetRootDir(rootDir, options) {
options.autonomousMsgLoading =
helper.validateAmlValue(options.autonomousMsgLoading);
if (!options.autonomousMsgLoading) {
console.log(
'*** SetRootDire: invalid autonomousMsgLoading: %j -- \'%s\' is used.',
options.autonomousMsgLoading, defaults.autonomousMsgLoading);
options.autonomousMsgLoading = defaults.autonomousMsgLoading;
}
globalize.setRootDir(rootDir);
Expand Down
6 changes: 2 additions & 4 deletions lib/globalize.js
Expand Up @@ -168,9 +168,8 @@ function formatJson(fullPath, variables, lang) {
if (fileType === 'json') jsonData = JSON.parse(contentStr);
if (fileType === 'yml' || fileType === 'yaml')
jsonData = YAML.parse(contentStr);
} catch (e) {
console.log(e);
return fullPath;
} catch (_e) {
return '*** read failure: ' + fullPath;
}
var msges = helper.scanJson(variables, jsonData);
var transMsges = [];
Expand Down Expand Up @@ -469,7 +468,6 @@ function setPersistentLogging(logFn, disableConsole) {
});
global.STRONGLOOP_GLB.LOG_FN = logFn;
} catch (e) {
console.log('*** strong-globalize: setPersistentLogging: invalid logFn.');
global.STRONGLOOP_GLB.LOG_FN = null;
}
}
Expand Down
16 changes: 12 additions & 4 deletions test/slt-test-helper.js
Expand Up @@ -13,6 +13,7 @@ var stdout = require('intercept-stdout');
exports.testHarness = testHarness;

var DEBUG = false;
var VERBOSE = process.env.SG_VERBOSE;

// testHarness(t, targets, testCallback)
// targets = {
Expand Down Expand Up @@ -60,13 +61,14 @@ function testHarness(t, targets, noFixtures, testCallback, testAllDone) {
function passTemporaryFailure(found, target) {
var failureMsg = helper.MSG_GPB_UNAVAILABLE;
found = found.trim();
return (found !== target && found === failureMsg);
var isTempFailure = (found !== target) && (found === failureMsg);
return isTempFailure;
}

function checkErrMsg(outMsg, errMsg, key, targets, t) {
outMsg = stripRootDirInfo(outMsg, key);
errMsg = stripRootDirInfo(errMsg, key);
if (DEBUG) t.comment(
if (DEBUG) console.log(
'\n<<< BEGIN', key,
'\nout ________________\n',
outMsg,
Expand Down Expand Up @@ -139,8 +141,14 @@ function testHarness(t, targets, noFixtures, testCallback, testAllDone) {
asyncTasks.push(function(cb) {
var myStdoutMsg = [];
var myStderrMsg = [];
function stdoutCb(txt) { myStdoutMsg.push(txt); }
function stderrCb(txt) { myStderrMsg.push(txt); }
function stdoutCb(txt) {
myStdoutMsg.push(txt);
return VERBOSE ? null : '';
}
function stderrCb(txt) {
myStderrMsg.push(txt);
return VERBOSE ? null : '';
}
var unhook_intercept = stdout(stdoutCb, stderrCb);
var name = this.toString();
initRootDir(name);
Expand Down
16 changes: 15 additions & 1 deletion test/test-auto-msg-load.js
Expand Up @@ -11,9 +11,19 @@ var loadMsgHelper = require('./load-msg-helper');
var mktmpdir = require('mktmpdir');
var path = require('path');
var shell = require('shelljs');
var stdout = require('intercept-stdout');
var test = require('tap').test;
var translate = require('../lib/translate');

var VERBOSE = process.env.SG_VERBOSE;

function stdoutCb(txt) {
return VERBOSE ? null : '';
}
function stderrCb(txt) {
return VERBOSE ? null : '';
}

var wellKnownLangs = loadMsgHelper.wellKnownLangs;
var secondaryMgr = loadMsgHelper.secondaryMgr;
var POSITIVE_TEST = true;
Expand Down Expand Up @@ -57,7 +67,9 @@ test('deep extraction and autonomous msg loading NOT forking', function(t) {
helper.setRootDir(destDir);
var savedMaxDepth = process.env.STRONGLOOP_GLOBALIZE_MAX_DEPTH;
process.env.STRONGLOOP_GLOBALIZE_MAX_DEPTH = null;
var unhook_intercept = stdout(stdoutCb, stderrCb);
extract.extractMessages(null, true, true, function(err, result) {
unhook_intercept();
if (err) t.fail('extractMessages failed.');
else t.pass('extractMessages succeeds.');
process.env.STRONGLOOP_GLOBALIZE_MAX_DEPTH = savedMaxDepth;
Expand Down Expand Up @@ -96,9 +108,11 @@ test('deep extraction and autonomous msg loading NOT forking', function(t) {
}
global.STRONGLOOP_GLB = undefined;
helper.setRootDir(destDir);
var unhook_intercept = stdout(stdoutCb, stderrCb);
translate.translateResource(function(err) {
unhook_intercept();
if (err) {
console.error('*** translate is unavailable; skipping.');
t.comment('*** translate is unavailable; skipping.');
translateMaybeSkip = true;
} else {
t.pass('translate succeeds.');
Expand Down
25 changes: 10 additions & 15 deletions test/test-format-json.js
Expand Up @@ -72,25 +72,21 @@ test('test formatJson and formatYaml', function(t) {
switch (name) {
case 'formatjson001':
case 'formatyaml001':
t.doesNotThrow(
function() {
g.formatMessage(fileName, allKeys);
},
fileName + ' must exist under the root directory.');
var langs = loadMsgHelper.wellKnownLangs;
langs.forEach(function(lang) {
g.setLanguage(lang);
var dataJson = g.formatMessage(fileName,
'[' +
'"title",' +
'["types", 0],' +
'["types", 1],' +
'["types", 2],' +
'["types", 3],' +
'["threeWrites", "e"],' +
'["threeWrites", "o"],' +
'["threeWrites", "w"]' +
']', lang);
'[' +
'"title",' +
'["types", 0],' +
'["types", 1],' +
'["types", 2],' +
'["types", 3],' +
'["threeWrites", "e"],' +
'["threeWrites", "o"],' +
'["threeWrites", "w"]' +
']', lang);
console.log(JSON.stringify(dataJson));
});
break;
Expand All @@ -102,4 +98,3 @@ test('test formatJson and formatYaml', function(t) {
t.end();
});
});

12 changes: 6 additions & 6 deletions test/test-globalize-errors.js
Expand Up @@ -37,34 +37,34 @@ var targets = {
test('test globalize misc testing', function(t) {
sltTH.testHarness(t, targets, true,
function(name, unhook_intercept, callback) {
unhook_intercept();
callback();
var g = SG();
var target;
var found;
switch (name) {
case 'formatMessage':
target = 'invalidMessage abc xyz';
found = g.formatMessage('invalidMessage', ['abc', 'xyz']);
t.equal(found, target, target);
t.match(found, target, target);
break;
case 'formatNumber':
target = 'wrongNumber';
found = g.formatNumber(target);
t.equal(found, target, target);
t.match(found, target, target);
break;
case 'formatDate':
target = 'wrongDate';
found = g.formatDate(target);
t.equal(found, target, target);
t.match(found, target, target);
break;
case 'formatCurrency':
target = 'invalidCurrencySymbol100';
found = g.formatCurrency(100, 'invalidCurrencySymbol');
t.equal(found, target, target);
t.match(found, target, target);
break;
default:
}
unhook_intercept();
callback();
}, function() {
t.end();
});
Expand Down
10 changes: 0 additions & 10 deletions test/test-lint.js
Expand Up @@ -3,20 +3,10 @@
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0

var SG = require('../index');
var lint = require('../lib/lint');
var sltTH = require('./slt-test-helper');
var test = require('tap').test;

SG.SetRootDir(__dirname);

test('lint message', function(t) {
lint.lintMessageFiles(false, function(err) {
t.assert(!err, 'No lint errors.');
});
t.end();
});

var targets = {
lint000: {
out: [
Expand Down
12 changes: 4 additions & 8 deletions test/test-load-msg-forking.js
Expand Up @@ -4,7 +4,6 @@
// License text available at https://opensource.org/licenses/Artistic-2.0

var async = require('async');
var f = require('util').format;
var helper = require('../lib/helper');
var loadMsgHelper = require('./load-msg-helper');

Expand All @@ -14,26 +13,23 @@ var secondaryMgr = loadMsgHelper.secondaryMgr;
var cluster = require('cluster');

if (cluster.isMaster && !process.argv[2]) {
var msg = f('Master is %s', process.pid);
console.log(msg);
cluster.setupMaster({
exec: __filename,
args: ['second_invoke'],
silent: false,
});
cluster.fork();
cluster.on('online', function(worker) {
msg = f('Worker %s has started', worker.process.pid);
console.log(msg);
// worker has started.
});
cluster.on('exit', function(worker) {
msg = f('Worker %s has completed', worker.process.pid);
console.log(msg);
// worker has completed.
});
} else if (cluster.isWorker) {
var test = require('tap').test;
test('secondary test on forking', function(t) {
t.equal(process.argv[2], 'second_invoke', 'worker in the second invoke');
t.match(process.argv[2], 'second_invoke',
'worker in the second invoke');
async.forEachOfSeries(wellKnownLangs, function(lang, ix, callback) {
secondaryMgr(__dirname, lang, t, helper.AML_ALL, true,
function() {
Expand Down
20 changes: 14 additions & 6 deletions test/test-logging-multiple.js
Expand Up @@ -7,6 +7,8 @@ var SG = require('../index');
var stdout = require('intercept-stdout');
var test = require('tap').test;

var VERBOSE = process.env.SG_VERBOSE;

SG.SetRootDir(__dirname);
SG.SetDefaultLanguage();
var g = SG();
Expand Down Expand Up @@ -64,7 +66,7 @@ var aliases = [

aliases.forEach(function(alias) {
var title = alias.level +
' (this msg is shown in the console)';
' (this msg is shown in the console - multiple)';
test(title, function(t) {
var called = false;
function myLogCb(level, msg) {
Expand All @@ -82,13 +84,20 @@ aliases.forEach(function(alias) {
function logTestWithConsoleEnabled(myLogCbMsg, t, alias, expectedMsg) {
var myStdoutMsg = null;
var myStderrMsg = null;
function stdoutCb(txt) { myStdoutMsg = txt; }
function stderrCb(txt) { myStderrMsg = txt; }
function stdoutCb(txt) {
myStdoutMsg = txt;
return VERBOSE ? null : '';
}
function stderrCb(txt) {
myStderrMsg = txt;
return VERBOSE ? null : '';
}
var unhook_intercept = stdout(stdoutCb, stderrCb);
setTimeout(function() {
var myStdMsg = alias.err ? myStderrMsg : myStdoutMsg;
t.comment('myLogCbMsg: %j', myLogCbMsg);
t.comment('myStdMsg: %s', myStdMsg);
unhook_intercept();
if (myLogCbMsg && myStdMsg) {
t.equal(myLogCbMsg.level, alias.level,
'Persistent logging callback returns the level:' + alias.level);
Expand All @@ -100,13 +109,12 @@ function logTestWithConsoleEnabled(myLogCbMsg, t, alias, expectedMsg) {
} else {
t.fail('Both persistent logging callback and stdout should return.');
}
unhook_intercept();
t.end();
}, 50);
}

aliases.forEach(function(alias) {
var title = alias.level + ' (console disabled)';
var title = alias.level + ' (console disabled in multiple logging)';
test(title, function(t) {
var called = false;
function myLogCb(level, msg) {
Expand All @@ -131,6 +139,7 @@ function logTestWithConsoleDisabled(myLogCbMsg, t, alias, expectedMsg) {
var myStdMsg = alias.err ? myStderrMsg : myStdoutMsg;
t.comment('myLogCbMsg: %j', myLogCbMsg);
t.comment('myStdMsg: %s', myStdMsg);
unhook_intercept();
if (myLogCbMsg && !myStdMsg) {
t.equal(myLogCbMsg.level, alias.level,
'Persistent logging callback returns the level:' + alias.level);
Expand All @@ -141,7 +150,6 @@ function logTestWithConsoleDisabled(myLogCbMsg, t, alias, expectedMsg) {
} else {
t.fail('Only persistent logging callback should return.');
}
unhook_intercept();
t.end();
}, 50);
}

0 comments on commit b7196c4

Please sign in to comment.