zblueslip: Prevent spurious expected errors.

This also cleans up some idioms in the zblueslip
code.
This commit is contained in:
Steve Howell
2020-04-03 14:41:13 +00:00
committed by showell
parent 26baaab34c
commit ec2aaa52dd
4 changed files with 35 additions and 23 deletions

View File

@@ -192,7 +192,6 @@ run_test('get_unread_ids', () => {
]; ];
set_filter(terms); set_filter(terms);
blueslip.set_test_data('warn', 'Unknown emails: bob@example.com');
unread_ids = candidate_ids(); unread_ids = candidate_ids();
assert.deepEqual(unread_ids, []); assert.deepEqual(unread_ids, []);

View File

@@ -30,10 +30,6 @@ run_test('basics', () => {
blueslip.error('world'); blueslip.error('world');
} }
// Let's add an error that we are expecting from the module.
// The function 'set_test_data' pushes the expected error message to the array
// of messages expected for that log type; here, 'error'.
blueslip.set_test_data('error', 'hello');
// Since the error 'world' is not being expected, blueslip will // Since the error 'world' is not being expected, blueslip will
// throw an error. // throw an error.
assert.throws(throw_an_error); assert.throws(throw_an_error);
@@ -66,9 +62,6 @@ run_test('basics', () => {
blueslip.warn('world'); blueslip.warn('world');
} }
// Populate one valid value, and test with an invalid value.
// This should throw an error, and we'll assert it was thrown by zblueslip.
blueslip.set_test_data('warn', 'hello');
assert.throws(throw_a_warning); assert.throws(throw_a_warning);
blueslip.clear_test_data(); blueslip.clear_test_data();

View File

@@ -116,6 +116,11 @@ try {
set_global('i18n', stub_i18n); set_global('i18n', stub_i18n);
namespace.clear_zulip_refs(); namespace.clear_zulip_refs();
run_one_module(file); run_one_module(file);
if (global.blueslip && blueslip.clear_test_data) {
blueslip.clear_test_data();
}
namespace.restore(); namespace.restore();
}); });
} catch (e) { } catch (e) {

View File

@@ -11,28 +11,41 @@ exports.make_zblueslip = function () {
error: true, error: true,
fatal: true, fatal: true,
}; };
const names = Array.from(Object.keys(opts));
// Store valid test data for options. // Store valid test data for options.
lib.test_data = {}; lib.test_data = {};
lib.test_logs = {}; lib.test_logs = {};
Object.keys(opts).forEach(name => { lib.seen_messages = {};
for (const name of names) {
lib.test_data[name] = []; lib.test_data[name] = [];
lib.test_logs[name] = []; lib.test_logs[name] = [];
}); lib.seen_messages[name] = new Set();
}
lib.set_test_data = (name, message) => { lib.set_test_data = (name, message) => {
lib.test_data[name].push(message); lib.test_data[name].push(message);
}; };
lib.clear_test_data = (name) => {
if (!name) { lib.check_seen_messages = () => {
// Clear all data for (const name of names) {
Object.keys(opts).forEach(name => { for (const message of lib.test_data[name]) {
lib.test_data[name] = []; if (!lib.seen_messages[name].has(message)) {
lib.test_logs[name] = []; throw Error('Never saw: ' + message);
});
return;
} }
}
}
};
lib.clear_test_data = () => {
lib.check_seen_messages();
for (const name of names) {
lib.test_data[name] = []; lib.test_data[name] = [];
lib.test_logs[name] = []; lib.test_logs[name] = [];
lib.seen_messages[name].clear();
}
}; };
lib.get_test_logs = (name) => { lib.get_test_logs = (name) => {
@@ -40,13 +53,14 @@ exports.make_zblueslip = function () {
}; };
// Create logging functions // Create logging functions
Object.keys(opts).forEach(name => { for (const name of names) {
if (!opts[name]) { if (!opts[name]) {
// should just log the message. // should just log the message.
lib[name] = function (message, more_info, stack) { lib[name] = function (message, more_info, stack) {
lib.seen_messages[name].add(message);
lib.test_logs[name].push({message, more_info, stack}); lib.test_logs[name].push({message, more_info, stack});
}; };
return; continue;
} }
lib[name] = function (message, more_info, stack) { lib[name] = function (message, more_info, stack) {
if (typeof message !== 'string') { if (typeof message !== 'string') {
@@ -58,6 +72,7 @@ exports.make_zblueslip = function () {
throw Error('message should be string: ' + message); throw Error('message should be string: ' + message);
} }
} }
lib.seen_messages[name].add(message);
lib.test_logs[name].push({message, more_info, stack}); lib.test_logs[name].push({message, more_info, stack});
const exact_match_fail = !lib.test_data[name].includes(message); const exact_match_fail = !lib.test_data[name].includes(message);
if (exact_match_fail) { if (exact_match_fail) {
@@ -66,7 +81,7 @@ exports.make_zblueslip = function () {
throw error; throw error;
} }
}; };
}); }
lib.exception_msg = function (ex) { lib.exception_msg = function (ex) {
return ex.message; return ex.message;