From 3794b14deaf755d90273fa1a2e44cdea49cb4037 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Thu, 24 Jun 2021 06:07:33 -0400 Subject: [PATCH] Massively speed up tests - Make a copy of the database after first initialization that can be swapped in when reinitializing in resetDB() - Avoid unnecessary one-second delay on every reset Probably more that can be done, but this should take minutes off the test runs --- chrome/content/zotero/xpcom/schema.js | 2 +- test/content/runtests.js | 18 ++++++++++-------- test/content/support.js | 7 ++++--- test/tests/supportTest.js | 8 +++++--- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/chrome/content/zotero/xpcom/schema.js b/chrome/content/zotero/xpcom/schema.js index 54912f009e..a8050e0bb7 100644 --- a/chrome/content/zotero/xpcom/schema.js +++ b/chrome/content/zotero/xpcom/schema.js @@ -331,7 +331,7 @@ Zotero.Schema = new function(){ }, 250); } } - }.bind(this), 1000); + }.bind(this), Zotero.isStandalone ? 1000 : 0); }); return updated; diff --git a/test/content/runtests.js b/test/content/runtests.js index 3ba7bfa6f3..4b6db28844 100644 --- a/test/content/runtests.js +++ b/test/content/runtests.js @@ -275,13 +275,15 @@ if(run && ZoteroUnit.tests) { } if(run) { - window.onload = function() { - Zotero.spawn(function* () { - yield Zotero.Schema.schemaUpdatePromise; - - initPDFToolsPath(); - - return mocha.run(); - }) + window.onload = async function () { + await Zotero.Schema.schemaUpdatePromise; + + // Make a copy of the database that can be used in resetDB() + var dbFile = Zotero.DataDirectory.getDatabase(); + await OS.File.copy(dbFile, dbFile + '-test-template'); + + initPDFToolsPath(); + + return mocha.run(); }; } \ No newline at end of file diff --git a/test/content/support.js b/test/content/support.js index 02ac0796af..a9c8858fe6 100644 --- a/test/content/support.js +++ b/test/content/support.js @@ -623,10 +623,11 @@ async function resetDB(options = {}) { } var db = Zotero.DataDirectory.getDatabase(); await Zotero.reinit( - Zotero.Promise.coroutine(function* () { - yield OS.File.remove(db); + async function () { + // Swap in the initial copy we made of the DB + await OS.File.copy(db + '-test-template', db); _defaultGroup = null; - }), + }, false, options ); diff --git a/test/tests/supportTest.js b/test/tests/supportTest.js index 33a48d557d..870c6ed1f6 100644 --- a/test/tests/supportTest.js +++ b/test/tests/supportTest.js @@ -1,10 +1,12 @@ describe("Support Functions for Unit Testing", function() { describe("resetDB", function() { - it("should restore the DB to factory settings", function* () { - yield resetDB({ + it("should restore the DB to factory settings", async function () { + await Zotero.DB.queryAsync("CREATE TABLE testTable (foo INTEGER PRIMARY KEY)"); + assert.isTrue(await Zotero.DB.tableExists('testTable')); + await resetDB({ thisArg: this }); - assert.equal((yield Zotero.DB.valueQueryAsync("SELECT COUNT(*) FROM items")), 0); + assert.isFalse(await Zotero.DB.tableExists('testTable')); }); }); describe("loadSampleData", function() {