1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-02 12:47:41 +02:00

Plugins: Fixed tests

This commit is contained in:
Laurent Cozic 2020-10-22 14:51:59 +01:00
parent 5bc25aefce
commit 6ce5240e12
5 changed files with 41 additions and 10 deletions

View File

@ -54,15 +54,25 @@ export default class PluginRunner extends BasePluginRunner {
};
}
async run(plugin:Plugin, sandbox:Global) {
const vmSandbox = vm.createContext(this.newSandboxProxy(plugin.id, sandbox));
async run(plugin:Plugin, sandbox:Global):Promise<void> {
return new Promise((resolve:Function, reject:Function) => {
const onStarted = () => {
plugin.off('started', onStarted);
resolve();
};
try {
vm.runInContext(plugin.scriptText, vmSandbox);
} catch (error) {
this.logger().error(`In plugin ${plugin.id}:`, error);
return;
}
plugin.on('started', onStarted);
const vmSandbox = vm.createContext(this.newSandboxProxy(plugin.id, sandbox));
try {
vm.runInContext(plugin.scriptText, vmSandbox);
} catch (error) {
reject(error);
// this.logger().error(`In plugin ${plugin.id}:`, error);
// return;
}
});
}
}

View File

@ -65,6 +65,9 @@ describe('services_PluginService', function() {
const allFolders = await Folder.all();
expect(allFolders.length).toBe(1);
// If you have an error here, it might mean you need to run `npm i` from
// the "withExternalModules" folder. Not clear exactly why.
expect(allFolders[0].title).toBe(' foo');
}));

View File

@ -4,7 +4,7 @@ import Global from './api/Global';
export default abstract class BasePluginRunner extends BaseService {
async run(plugin:Plugin, sandbox:Global) {
async run(plugin:Plugin, sandbox:Global):Promise<void> {
throw new Error(`Not implemented: ${plugin} / ${sandbox}`);
}

View File

@ -4,6 +4,7 @@ import shim from 'lib/shim';
import { ViewHandle } from './utils/createViewHandle';
import { ContentScriptType } from './api/types';
import Logger from 'lib/Logger';
const EventEmitter = require('events');
interface ViewControllers {
[key:string]: ViewController
@ -29,6 +30,7 @@ export default class Plugin {
private viewControllers_:ViewControllers = {};
private contentScripts_:ContentScripts = {};
private dispatch_:Function;
private eventEmitter_:any;
constructor(id:string, baseDir:string, manifest:PluginManifest, scriptText:string, logger:Logger, dispatch:Function) {
this.id_ = id;
@ -37,6 +39,7 @@ export default class Plugin {
this.scriptText_ = scriptText;
this.logger_ = logger;
this.dispatch_ = dispatch;
this.eventEmitter_ = new EventEmitter();
}
public get id():string {
@ -59,11 +62,25 @@ export default class Plugin {
return this.baseDir_;
}
public registerContentScript(type:ContentScriptType, id:string, path:string) {
on(eventName:string, callback:Function) {
return this.eventEmitter_.on(eventName, callback);
}
off(eventName:string, callback:Function) {
return this.eventEmitter_.removeListener(eventName, callback);
}
emit(eventName:string, event:any = null) {
return this.eventEmitter_.emit(eventName, event);
}
public async registerContentScript(type:ContentScriptType, id:string, path:string) {
if (!this.contentScripts_[type]) this.contentScripts_[type] = [];
const absolutePath = shim.fsDriver().resolveRelativePathWithinDir(this.baseDir, path);
if (!(await shim.fsDriver().exists(absolutePath))) throw new Error(`Could not find content script at path ${absolutePath}`);
this.contentScripts_[type].push({ id, path: absolutePath });
this.logger_.debug(`Plugin: ${this.id}: Registered content script: ${type}: ${id}: ${absolutePath}`);

View File

@ -45,6 +45,7 @@ export default class JoplinPlugins {
this.logger.error(`In plugin ${this.plugin.id}:`, newError);
}).then(() => {
this.logger.info(`Finished running onStart handler: ${this.plugin.id} (Took ${Date.now() - startTime}ms)`);
this.plugin.emit('started');
});
}
}