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);
blueslip.set_test_data('warn', 'Unknown emails: bob@example.com');
unread_ids = candidate_ids();
assert.deepEqual(unread_ids, []);

View File

@ -30,10 +30,6 @@ run_test('basics', () => {
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
// throw an error.
assert.throws(throw_an_error);
@ -66,9 +62,6 @@ run_test('basics', () => {
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);
blueslip.clear_test_data();

View File

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

View File

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