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;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
Components.classes['@mozilla.org/toolkit/app-startup;1'].
|
|
|
|
getService(Components.interfaces.nsIAppStartup).
|
|
|
|
quit(Components.interfaces.nsIAppStartup.eForceQuit);
|
|
|
|
}
|
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]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'citeProcJSExport',
|
|
|
|
func: generateCiteProcJSExportData
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'translatorExportLegacy',
|
|
|
|
func: generateTranslatorExportData,
|
|
|
|
args: [true]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'translatorExport',
|
|
|
|
func: generateTranslatorExportData,
|
|
|
|
args: [false]
|
|
|
|
}
|
|
|
|
];
|
|
|
|
let p = Q.resolve();
|
|
|
|
for (let i=0; i<dataFiles.length; i++) {
|
|
|
|
let first = !i;
|
|
|
|
let params = dataFiles[i];
|
|
|
|
|
|
|
|
p = p.then(function() {
|
|
|
|
// Make sure to not run next loop if previous fails
|
|
|
|
return Q.try(function() {
|
|
|
|
if (!first) dump('\n');
|
|
|
|
dump('Generating data for ' + params.name + '...');
|
|
|
|
|
|
|
|
let filePath = OS.Path.join(dataPath, params.name + '.js');
|
|
|
|
|
|
|
|
return Q.resolve(OS.File.exists(filePath))
|
|
|
|
.then(function(exists) {
|
|
|
|
let currentData;
|
|
|
|
if (exists) {
|
|
|
|
currentData = loadSampleData(params.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
let args = params.args || [];
|
|
|
|
args.push(currentData);
|
|
|
|
let str = stableStringify(params.func.apply(null, args));
|
|
|
|
|
|
|
|
return OS.File.writeAtomic(OS.Path.join(dataPath, params.name + '.js'), str);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(function() { dump("done."); })
|
|
|
|
.catch(function(e) { dump("failed!"); throw e })
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
p.catch(function(e) { dump('\n'); dump(Zotero.Utilities.varDump(e)) })
|
|
|
|
.finally(function() { quit(false) });
|
|
|
|
}
|
|
|
|
|
2015-03-07 17:18:27 +00:00
|
|
|
function Reporter(runner) {
|
|
|
|
var indents = 0, passed = 0, failed = 0;
|
|
|
|
|
|
|
|
function indent() {
|
|
|
|
return Array(indents).join(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
runner.on('start', function(){});
|
|
|
|
|
|
|
|
runner.on('suite', function(suite){
|
|
|
|
++indents;
|
|
|
|
dump(indent()+suite.title+"\n");
|
|
|
|
});
|
|
|
|
|
|
|
|
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){
|
|
|
|
failed++;
|
2015-03-08 03:17:44 +00:00
|
|
|
dump("\r"+indent()+Mocha.reporters.Base.symbols.err+" "+test.title+"\n"+
|
|
|
|
indent()+" "+err.toString()+" at\n"+
|
|
|
|
indent()+" "+err.stack.replace("\n", "\n"+indent()+" ", "g"));
|
2015-03-07 17:18:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
runner.on('end', function() {
|
|
|
|
dump(passed+"/"+(passed+failed)+" tests passed.\n");
|
|
|
|
quit(failed != 0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup Mocha
|
|
|
|
mocha.setup({ui:"bdd", reporter:Reporter});
|
|
|
|
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) {
|
2015-03-09 21:41:17 +00:00
|
|
|
var testDirectory = getTestDataDirectory().parent,
|
|
|
|
testFiles = [];
|
|
|
|
if(ZoteroUnit.tests == "all") {
|
|
|
|
var enumerator = testDirectory.directoryEntries;
|
|
|
|
while(enumerator.hasMoreElements()) {
|
|
|
|
var file = enumerator.getNext().QueryInterface(Components.interfaces.nsIFile);
|
|
|
|
if(file.leafName.endsWith(".js")) {
|
|
|
|
testFiles.push(file.leafName);
|
|
|
|
}
|
2015-03-07 17:18:27 +00:00
|
|
|
}
|
2015-03-09 21:41:17 +00:00
|
|
|
} else {
|
|
|
|
var specifiedTests = ZoteroUnit.tests.split(",");
|
|
|
|
for(var test of specifiedTests) {
|
|
|
|
var fname = test+".js",
|
|
|
|
file = testDirectory.clone();
|
|
|
|
file.append(fname);
|
|
|
|
if(!file.exists()) {
|
|
|
|
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;
|
|
|
|
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-03-11 17:18:11 +00:00
|
|
|
Zotero.Schema.schemaUpdatePromise.then(function() {
|
|
|
|
mocha.run();
|
|
|
|
}).done();
|
2015-03-09 21:41:17 +00:00
|
|
|
};
|
|
|
|
}
|