2017-05-08 22:45:57 +00:00
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
|
|
|
Services.scriptloader.loadSubScript("resource://zotero/polyfill.js");
|
2015-03-08 03:17:44 +00:00
|
|
|
Components.utils.import("resource://gre/modules/osfile.jsm");
|
2015-03-08 22:39:42 +00:00
|
|
|
var EventUtils = Components.utils.import("resource://zotero-unit/EventUtils.jsm");
|
2015-03-07 17:18:27 +00:00
|
|
|
|
|
|
|
var ZoteroUnit = Components.classes["@mozilla.org/commandlinehandler/general-startup;1?type=zotero-unit"].
|
2015-03-24 04:52:36 +00:00
|
|
|
getService(Components.interfaces.nsISupports).
|
|
|
|
wrappedJSObject;
|
|
|
|
|
2015-03-07 17:18:27 +00:00
|
|
|
var dump = ZoteroUnit.dump;
|
|
|
|
|
2016-01-12 13:28:15 +00:00
|
|
|
// Mocha HTML reporter doesn't show deepEqual diffs, so we change this.
|
|
|
|
chai.config.truncateThreshold = 0
|
|
|
|
|
2015-03-07 17:18:27 +00:00
|
|
|
function quit(failed) {
|
|
|
|
// Quit with exit status
|
|
|
|
if(!failed) {
|
2015-03-24 04:52:36 +00:00
|
|
|
OS.File.writeAtomic(OS.Path.join(OS.Constants.Path.profileDir, "success"), new Uint8Array(0));
|
2015-03-07 17:18:27 +00:00
|
|
|
}
|
2015-03-08 03:17:44 +00:00
|
|
|
if(!ZoteroUnit.noquit) {
|
2015-05-29 02:22:47 +00:00
|
|
|
setTimeout(function () {
|
|
|
|
Components.classes['@mozilla.org/toolkit/app-startup;1']
|
|
|
|
.getService(Components.interfaces.nsIAppStartup)
|
|
|
|
.quit(Components.interfaces.nsIAppStartup.eForceQuit);
|
|
|
|
}, 250);
|
2015-03-08 03:17:44 +00:00
|
|
|
}
|
2015-03-07 17:18:27 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 04:52:36 +00:00
|
|
|
if (ZoteroUnit.makeTestData) {
|
|
|
|
let dataPath = getTestDataDirectory().path;
|
|
|
|
|
|
|
|
Zotero.Prefs.set("export.citePaperJournalArticleURL", true);
|
|
|
|
|
|
|
|
let dataFiles = [
|
|
|
|
{
|
|
|
|
name: 'allTypesAndFields',
|
|
|
|
func: generateAllTypesAndFieldsData
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'itemJSON',
|
|
|
|
func: generateItemJSONData,
|
|
|
|
args: [null]
|
|
|
|
},
|
2015-06-01 03:59:15 +00:00
|
|
|
// {
|
|
|
|
// name: 'citeProcJSExport',
|
|
|
|
// func: generateCiteProcJSExportData
|
|
|
|
// },
|
2015-03-24 04:52:36 +00:00
|
|
|
{
|
|
|
|
name: 'translatorExportLegacy',
|
|
|
|
func: generateTranslatorExportData,
|
|
|
|
args: [true]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'translatorExport',
|
|
|
|
func: generateTranslatorExportData,
|
|
|
|
args: [false]
|
|
|
|
}
|
|
|
|
];
|
2015-06-01 03:59:15 +00:00
|
|
|
Zotero.Promise.coroutine(function* () {
|
|
|
|
yield Zotero.initializationPromise;
|
|
|
|
for (let i=0; i<dataFiles.length; i++) {
|
|
|
|
let first = !i;
|
|
|
|
let params = dataFiles[i];
|
|
|
|
|
2015-03-24 04:52:36 +00:00
|
|
|
// Make sure to not run next loop if previous fails
|
2015-06-01 03:59:15 +00:00
|
|
|
if (!first) dump('\n');
|
|
|
|
dump('Generating data for ' + params.name + '...');
|
|
|
|
|
|
|
|
let filePath = OS.Path.join(dataPath, params.name + '.js');
|
|
|
|
let exists = yield OS.File.exists(filePath);
|
|
|
|
let currentData;
|
|
|
|
if (exists) {
|
|
|
|
currentData = loadSampleData(params.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
let args = params.args || [];
|
|
|
|
args.push(currentData);
|
|
|
|
let newData = params.func.apply(null, args);
|
|
|
|
if (newData instanceof Zotero.Promise) {
|
|
|
|
newData = yield newData;
|
|
|
|
}
|
|
|
|
let str = stableStringify(newData);
|
|
|
|
|
|
|
|
yield OS.File.writeAtomic(OS.Path.join(dataPath, params.name + '.js'), str);
|
|
|
|
dump("done.");
|
|
|
|
}
|
|
|
|
})()
|
|
|
|
.catch(function(e) { dump('\n'); dump(Zotero.Utilities.varDump(e)) })
|
2015-03-24 04:52:36 +00:00
|
|
|
.finally(function() { quit(false) });
|
|
|
|
}
|
|
|
|
|
2015-03-07 17:18:27 +00:00
|
|
|
function Reporter(runner) {
|
2015-05-29 08:01:40 +00:00
|
|
|
var indents = 0, passed = 0, failed = 0, aborted = false;
|
2015-03-07 17:18:27 +00:00
|
|
|
|
|
|
|
function indent() {
|
|
|
|
return Array(indents).join(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
runner.on('start', function(){});
|
|
|
|
|
|
|
|
runner.on('suite', function(suite){
|
|
|
|
++indents;
|
2015-09-16 22:55:50 +00:00
|
|
|
dump("\r"+indent()+suite.title+"\n");
|
2015-03-07 17:18:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
runner.on('suite end', function(suite){
|
|
|
|
--indents;
|
|
|
|
if (1 == indents) dump("\n");
|
|
|
|
});
|
|
|
|
|
|
|
|
runner.on('pending', function(test){
|
2015-04-28 05:36:56 +00:00
|
|
|
dump("\r"+indent()+"pending -"+test.title+"\n");
|
2015-03-07 17:18:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
runner.on('pass', function(test){
|
|
|
|
passed++;
|
|
|
|
var msg = "\r"+indent()+Mocha.reporters.Base.symbols.ok+" "+test.title;
|
|
|
|
if ('fast' != test.speed) {
|
|
|
|
msg += " ("+Math.round(test.duration)+" ms)";
|
|
|
|
}
|
|
|
|
dump(msg+"\n");
|
|
|
|
});
|
|
|
|
|
|
|
|
runner.on('fail', function(test, err){
|
2015-09-16 22:55:50 +00:00
|
|
|
// Remove internal code references
|
|
|
|
err.stack = err.stack.replace(/.+(?:zotero-unit\/|\/Task\.jsm|\/bluebird\.js).+\n?/g, "");
|
|
|
|
|
2015-06-02 20:31:33 +00:00
|
|
|
// Strip "From previous event:" block if it's all internals
|
2015-09-16 22:55:50 +00:00
|
|
|
if (err.stack.indexOf('From previous event:') != -1) {
|
|
|
|
err.stack = err.stack
|
|
|
|
// Drop first line, because it contains the error message
|
|
|
|
.replace(/^.+\n/, '')
|
|
|
|
// Drop "From previous event:" labels for empty blocks
|
|
|
|
.replace(/.*From previous event:.*(?:\n(?=\s*From previous event:)|\s*$)/g, '');
|
2015-06-02 20:31:33 +00:00
|
|
|
}
|
2015-09-16 22:55:50 +00:00
|
|
|
|
|
|
|
// Make sure there's a blank line after all stack traces
|
|
|
|
err.stack = err.stack.replace(/\s*$/, '\n\n');
|
2015-06-02 20:31:33 +00:00
|
|
|
|
2015-03-07 17:18:27 +00:00
|
|
|
failed++;
|
2015-09-16 22:55:50 +00:00
|
|
|
let indentStr = indent();
|
|
|
|
dump("\r" + indentStr
|
2015-05-10 06:43:44 +00:00
|
|
|
// Dark red X for errors
|
2017-05-24 05:12:05 +00:00
|
|
|
+ "\x1B[31;40m" + Mocha.reporters.Base.symbols.err + " [FAIL]\x1B[0m"
|
2016-03-15 05:18:01 +00:00
|
|
|
// Trigger bell if interactive
|
2017-05-24 05:12:05 +00:00
|
|
|
+ (Zotero.automatedTest ? "" : "\x07")
|
2015-05-10 06:43:44 +00:00
|
|
|
+ " " + test.title + "\n"
|
2015-09-16 22:55:50 +00:00
|
|
|
+ indentStr + " " + err.toString() + " at\n"
|
|
|
|
+ err.stack.replace(/^/gm, indentStr + " "));
|
2015-05-29 08:01:40 +00:00
|
|
|
|
|
|
|
if (ZoteroUnit.bail) {
|
|
|
|
aborted = true;
|
|
|
|
runner.abort();
|
|
|
|
}
|
2015-03-07 17:18:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
runner.on('end', function() {
|
2015-05-29 08:01:40 +00:00
|
|
|
dump(passed + "/" + (passed + failed) + " tests passed"
|
|
|
|
+ (aborted ? " -- aborting" : "") + "\n");
|
2015-03-07 17:18:27 +00:00
|
|
|
quit(failed != 0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-02 18:34:51 +00:00
|
|
|
// Monkey-patch Mocha to check instanceof Error using compartment-local
|
2015-06-01 03:59:15 +00:00
|
|
|
// Error object
|
|
|
|
Mocha.Runner.prototype.fail = function(test, err){
|
|
|
|
++this.failures;
|
|
|
|
test.state = 'failed';
|
|
|
|
|
|
|
|
if ('string' == typeof err) {
|
|
|
|
err = new Error('the string "' + err + '" was thrown, throw an Error :)');
|
|
|
|
} else if (!(err instanceof Components.utils.getGlobalForObject(err).Error)) {
|
|
|
|
err = new Error('the ' + Mocha.utils.type(err) + ' ' + Mocha.utils.stringify(err) + ' was thrown, throw an Error :)');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.emit('fail', test, err);
|
|
|
|
};
|
|
|
|
|
2015-03-07 17:18:27 +00:00
|
|
|
// Setup Mocha
|
2015-05-29 05:34:20 +00:00
|
|
|
mocha.setup({
|
|
|
|
ui: "bdd",
|
|
|
|
reporter: Reporter,
|
2016-04-27 01:46:05 +00:00
|
|
|
timeout: ZoteroUnit.timeout || 10000,
|
2015-05-31 07:27:03 +00:00
|
|
|
grep: ZoteroUnit.grep
|
2015-05-29 05:34:20 +00:00
|
|
|
});
|
2015-04-15 05:32:40 +00:00
|
|
|
|
2017-05-31 15:30:56 +00:00
|
|
|
coMocha(Mocha);
|
2015-04-15 05:32:40 +00:00
|
|
|
|
2016-11-29 08:27:44 +00:00
|
|
|
before(function () {
|
|
|
|
// Store all prefs set in runtests.sh
|
2016-11-29 08:52:07 +00:00
|
|
|
Components.utils.import("resource://zotero/config.js");
|
2016-11-29 08:27:44 +00:00
|
|
|
var prefBranch = Services.prefs.getBranch(ZOTERO_CONFIG.PREF_BRANCH);
|
|
|
|
ZoteroUnit.customPrefs = {};
|
|
|
|
prefBranch.getChildList("", {})
|
|
|
|
.filter(key => prefBranch.prefHasUserValue(key))
|
|
|
|
.forEach(key => ZoteroUnit.customPrefs[key] = Zotero.Prefs.get(key));
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear all prefs, and reset those set in runtests.sh to original values
|
|
|
|
*/
|
|
|
|
function resetPrefs() {
|
2016-11-29 08:52:07 +00:00
|
|
|
Components.utils.import("resource://zotero/config.js");
|
2016-11-29 08:27:44 +00:00
|
|
|
var prefBranch = Services.prefs.getBranch(ZOTERO_CONFIG.PREF_BRANCH);
|
|
|
|
prefBranch.getChildList("", {}).forEach(key => {
|
|
|
|
var origVal = ZoteroUnit.customPrefs[key];
|
|
|
|
if (origVal !== undefined) {
|
|
|
|
if (origVal != Zotero.Prefs.get(key)) {
|
|
|
|
Zotero.Prefs.set(key, ZoteroUnit.customPrefs[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (prefBranch.prefHasUserValue(key)) {
|
|
|
|
Zotero.Prefs.clear(key)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
resetPrefs();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-03-07 17:18:27 +00:00
|
|
|
var assert = chai.assert,
|
|
|
|
expect = chai.expect;
|
|
|
|
|
|
|
|
// Set up tests to run
|
2015-03-24 04:52:36 +00:00
|
|
|
var run = ZoteroUnit.runTests;
|
|
|
|
if(run && ZoteroUnit.tests) {
|
2016-12-16 09:21:21 +00:00
|
|
|
function getTestFilename(test) {
|
|
|
|
// Allow foo, fooTest, fooTest.js, and tests/fooTest.js
|
|
|
|
test = test.replace(/\.js$/, "");
|
|
|
|
test = test.replace(/Test$/, "");
|
|
|
|
test = test.replace(/^tests[/\\]/, "");
|
|
|
|
return test + "Test.js";
|
|
|
|
}
|
|
|
|
|
2015-03-09 21:41:17 +00:00
|
|
|
var testDirectory = getTestDataDirectory().parent,
|
|
|
|
testFiles = [];
|
|
|
|
if(ZoteroUnit.tests == "all") {
|
|
|
|
var enumerator = testDirectory.directoryEntries;
|
2016-12-16 09:21:21 +00:00
|
|
|
let startFile = ZoteroUnit.startAt ? getTestFilename(ZoteroUnit.startAt) : false;
|
|
|
|
let started = !startFile;
|
2017-06-20 04:47:13 +00:00
|
|
|
let stopFile = ZoteroUnit.stopAt ? getTestFilename(ZoteroUnit.stopAt) : false;
|
2015-03-09 21:41:17 +00:00
|
|
|
while(enumerator.hasMoreElements()) {
|
|
|
|
var file = enumerator.getNext().QueryInterface(Components.interfaces.nsIFile);
|
2017-10-27 08:03:11 +00:00
|
|
|
if (file.leafName.endsWith(".js")) {
|
|
|
|
testFiles.push(file.leafName);
|
2015-03-09 21:41:17 +00:00
|
|
|
}
|
2015-03-07 17:18:27 +00:00
|
|
|
}
|
2017-10-27 08:03:11 +00:00
|
|
|
testFiles.sort();
|
|
|
|
|
|
|
|
// Find the start and stop files
|
|
|
|
let startPos = 0;
|
|
|
|
let stopPos = testFiles.length - 1;
|
|
|
|
for (let i = 0; i < testFiles.length; i++) {
|
|
|
|
if (testFiles[i] == startFile) {
|
|
|
|
startPos = i;
|
|
|
|
}
|
|
|
|
if (testFiles[i] == stopFile) {
|
|
|
|
stopPos = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (startFile && startPos == 0 && startFile != testFiles[0]) {
|
2016-12-16 09:21:21 +00:00
|
|
|
dump(`Invalid start file ${startFile}\n`);
|
|
|
|
}
|
2017-10-27 08:03:11 +00:00
|
|
|
testFiles = testFiles.slice(startPos, stopPos + 1);
|
2015-03-09 21:41:17 +00:00
|
|
|
} else {
|
|
|
|
var specifiedTests = ZoteroUnit.tests.split(",");
|
2015-05-12 18:49:36 +00:00
|
|
|
for (let test of specifiedTests) {
|
2016-12-16 09:21:21 +00:00
|
|
|
let fname = getTestFilename(test);
|
2015-05-12 18:49:36 +00:00
|
|
|
let file = testDirectory.clone();
|
2015-03-09 21:41:17 +00:00
|
|
|
file.append(fname);
|
2015-05-12 18:49:36 +00:00
|
|
|
if (!file.exists()) {
|
2015-03-09 21:41:17 +00:00
|
|
|
dump("Invalid test file "+test+"\n");
|
|
|
|
run = false;
|
|
|
|
quit(true);
|
|
|
|
}
|
|
|
|
testFiles.push(fname);
|
2015-03-07 17:18:27 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-09 21:41:17 +00:00
|
|
|
|
|
|
|
for(var fname of testFiles) {
|
|
|
|
var el = document.createElement("script");
|
|
|
|
el.type = "application/javascript;version=1.8";
|
|
|
|
el.src = "resource://zotero-unit-tests/"+fname;
|
2015-06-02 03:32:57 +00:00
|
|
|
el.async = false;
|
2015-03-09 21:41:17 +00:00
|
|
|
document.body.appendChild(el);
|
|
|
|
}
|
2015-03-07 17:18:27 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 21:41:17 +00:00
|
|
|
if(run) {
|
|
|
|
window.onload = function() {
|
2015-06-01 03:07:24 +00:00
|
|
|
Zotero.spawn(function* () {
|
|
|
|
yield Zotero.Schema.schemaUpdatePromise;
|
|
|
|
return mocha.run();
|
|
|
|
})
|
2015-03-09 21:41:17 +00:00
|
|
|
};
|
|
|
|
}
|