1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00

Tools: Setup test framework for CLI app

This commit is contained in:
Laurent Cozic 2022-11-01 15:28:14 +00:00
parent 3dd008ae9a
commit 7992fe5b63
44 changed files with 339 additions and 168 deletions

View File

@ -78,6 +78,12 @@ readme/
packages/app-cli/app/LinkSelector.d.ts
packages/app-cli/app/LinkSelector.js
packages/app-cli/app/LinkSelector.js.map
packages/app-cli/app/base-command.d.ts
packages/app-cli/app/base-command.js
packages/app-cli/app/base-command.js.map
packages/app-cli/app/command-done.test.d.ts
packages/app-cli/app/command-done.test.js
packages/app-cli/app/command-done.test.js.map
packages/app-cli/app/command-e2ee.d.ts
packages/app-cli/app/command-e2ee.js
packages/app-cli/app/command-e2ee.js.map
@ -93,6 +99,12 @@ packages/app-cli/app/command-testing.js.map
packages/app-cli/app/services/plugins/PluginRunner.d.ts
packages/app-cli/app/services/plugins/PluginRunner.js
packages/app-cli/app/services/plugins/PluginRunner.js.map
packages/app-cli/app/setupCommand.d.ts
packages/app-cli/app/setupCommand.js
packages/app-cli/app/setupCommand.js.map
packages/app-cli/app/utils/testUtils.d.ts
packages/app-cli/app/utils/testUtils.js
packages/app-cli/app/utils/testUtils.js.map
packages/app-cli/tests/HtmlToMd.d.ts
packages/app-cli/tests/HtmlToMd.js
packages/app-cli/tests/HtmlToMd.js.map

12
.gitignore vendored
View File

@ -66,6 +66,12 @@ docs/**/*.mustache
packages/app-cli/app/LinkSelector.d.ts
packages/app-cli/app/LinkSelector.js
packages/app-cli/app/LinkSelector.js.map
packages/app-cli/app/base-command.d.ts
packages/app-cli/app/base-command.js
packages/app-cli/app/base-command.js.map
packages/app-cli/app/command-done.test.d.ts
packages/app-cli/app/command-done.test.js
packages/app-cli/app/command-done.test.js.map
packages/app-cli/app/command-e2ee.d.ts
packages/app-cli/app/command-e2ee.js
packages/app-cli/app/command-e2ee.js.map
@ -81,6 +87,12 @@ packages/app-cli/app/command-testing.js.map
packages/app-cli/app/services/plugins/PluginRunner.d.ts
packages/app-cli/app/services/plugins/PluginRunner.js
packages/app-cli/app/services/plugins/PluginRunner.js.map
packages/app-cli/app/setupCommand.d.ts
packages/app-cli/app/setupCommand.js
packages/app-cli/app/setupCommand.js.map
packages/app-cli/app/utils/testUtils.d.ts
packages/app-cli/app/utils/testUtils.js
packages/app-cli/app/utils/testUtils.js.map
packages/app-cli/tests/HtmlToMd.d.ts
packages/app-cli/tests/HtmlToMd.js
packages/app-cli/tests/HtmlToMd.js.map

View File

@ -16,6 +16,7 @@ const { cliUtils } = require('./cli-utils.js');
const Cache = require('@joplin/lib/Cache');
const RevisionService = require('@joplin/lib/services/RevisionService').default;
const shim = require('@joplin/lib/shim').default;
const setupCommand = require('./setupCommand').default;
class Application extends BaseApplication {
constructor() {
@ -114,46 +115,12 @@ class Application extends BaseApplication {
return [];
}
stdout(text) {
return this.gui().stdout(text);
setupCommand(cmd) {
return setupCommand(cmd, t => this.stdout(t), () => this.store(), () => this.gui());
}
setupCommand(cmd) {
cmd.setStdout(text => {
return this.stdout(text);
});
cmd.setDispatcher(action => {
if (this.store()) {
return this.store().dispatch(action);
} else {
return () => {};
}
});
cmd.setPrompt(async (message, options) => {
if (!options) options = {};
if (!options.type) options.type = 'boolean';
if (!options.booleanAnswerDefault) options.booleanAnswerDefault = 'y';
if (!options.answers) options.answers = options.booleanAnswerDefault === 'y' ? [_('Y'), _('n')] : [_('N'), _('y')];
if (options.type === 'boolean') {
message += ` (${options.answers.join('/')})`;
}
let answer = await this.gui().prompt('', `${message} `, options);
if (options.type === 'boolean') {
if (answer === null) return false; // Pressed ESCAPE
if (!answer) answer = options.answers[0];
const positiveIndex = options.booleanAnswerDefault === 'y' ? 0 : 1;
return answer.toLowerCase() === options.answers[positiveIndex].toLowerCase();
} else {
return answer;
}
});
return cmd;
stdout(text) {
return this.gui().stdout(text);
}
async exit(code = 0) {
@ -180,6 +147,7 @@ class Application extends BaseApplication {
if (!this.allCommandsLoaded_) {
fs.readdirSync(__dirname).forEach(path => {
if (path.indexOf('command-') !== 0) return;
if (path.endsWith('.test.js')) return;
const ext = fileExtension(path);
if (ext !== 'js') return;

View File

@ -1,97 +1,97 @@
const { _ } = require('@joplin/lib/locale');
const { reg } = require('@joplin/lib/registry.js');
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const locale_1 = require("@joplin/lib/locale");
const registry_js_1 = require("@joplin/lib/registry.js");
class BaseCommand {
constructor() {
this.stdout_ = null;
this.prompt_ = null;
}
usage() {
throw new Error('Usage not defined');
}
encryptionCheck(item) {
if (item && item.encryption_applied) throw new Error(_('Cannot change encrypted item'));
}
description() {
throw new Error('Description not defined');
}
async action() {
throw new Error('Action not defined');
}
compatibleUis() {
return ['cli', 'gui'];
}
supportsUi(ui) {
return this.compatibleUis().indexOf(ui) >= 0;
}
options() {
return [];
}
hidden() {
return false;
}
enabled() {
return true;
}
cancellable() {
return false;
}
async cancel() {}
name() {
const r = this.usage().split(' ');
return r[0];
}
setDispatcher(fn) {
this.dispatcher_ = fn;
}
dispatch(action) {
if (!this.dispatcher_) throw new Error('Dispatcher not defined');
return this.dispatcher_(action);
}
setStdout(fn) {
this.stdout_ = fn;
}
stdout(text) {
if (this.stdout_) this.stdout_(text);
}
setPrompt(fn) {
this.prompt_ = fn;
}
async prompt(message, options = null) {
if (!this.prompt_) throw new Error('Prompt is undefined');
return await this.prompt_(message, options);
}
metadata() {
return {
name: this.name(),
usage: this.usage(),
options: this.options(),
hidden: this.hidden(),
};
}
logger() {
return reg.logger();
}
constructor() {
this.stdout_ = null;
this.prompt_ = null;
}
usage() {
throw new Error('Usage not defined');
}
encryptionCheck(item) {
if (item && item.encryption_applied)
throw new Error((0, locale_1._)('Cannot change encrypted item'));
}
description() {
throw new Error('Description not defined');
}
action(_args) {
return __awaiter(this, void 0, void 0, function* () {
throw new Error('Action not defined');
});
}
compatibleUis() {
return ['cli', 'gui'];
}
supportsUi(ui) {
return this.compatibleUis().indexOf(ui) >= 0;
}
options() {
return [];
}
hidden() {
return false;
}
enabled() {
return true;
}
cancellable() {
return false;
}
cancel() {
return __awaiter(this, void 0, void 0, function* () { });
}
name() {
const r = this.usage().split(' ');
return r[0];
}
setDispatcher(fn) {
this.dispatcher_ = fn;
}
dispatch(action) {
if (!this.dispatcher_)
throw new Error('Dispatcher not defined');
return this.dispatcher_(action);
}
setStdout(fn) {
this.stdout_ = fn;
}
stdout(text) {
if (this.stdout_)
this.stdout_(text);
}
setPrompt(fn) {
this.prompt_ = fn;
}
prompt(message, options = null) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.prompt_)
throw new Error('Prompt is undefined');
return yield this.prompt_(message, options);
});
}
metadata() {
return {
name: this.name(),
usage: this.usage(),
options: this.options(),
hidden: this.hidden(),
};
}
logger() {
return registry_js_1.reg.logger();
}
}
module.exports = { BaseCommand };
exports.default = BaseCommand;
//# sourceMappingURL=base-command.js.map

View File

@ -0,0 +1,95 @@
import { _ } from '@joplin/lib/locale';
import { reg } from '@joplin/lib/registry.js';
export default class BaseCommand {
protected stdout_: any = null;
protected prompt_: any = null;
protected dispatcher_: any;
usage(): string {
throw new Error('Usage not defined');
}
encryptionCheck(item: any) {
if (item && item.encryption_applied) throw new Error(_('Cannot change encrypted item'));
}
description() {
throw new Error('Description not defined');
}
async action(_args: any) {
throw new Error('Action not defined');
}
compatibleUis() {
return ['cli', 'gui'];
}
supportsUi(ui: string) {
return this.compatibleUis().indexOf(ui) >= 0;
}
options(): any[] {
return [];
}
hidden() {
return false;
}
enabled() {
return true;
}
cancellable() {
return false;
}
async cancel() {}
name() {
const r = this.usage().split(' ');
return r[0];
}
setDispatcher(fn: Function) {
this.dispatcher_ = fn;
}
dispatch(action: any) {
if (!this.dispatcher_) throw new Error('Dispatcher not defined');
return this.dispatcher_(action);
}
setStdout(fn: Function) {
this.stdout_ = fn;
}
stdout(text: string) {
if (this.stdout_) this.stdout_(text);
}
setPrompt(fn: Function) {
this.prompt_ = fn;
}
async prompt(message: string, options: any = null) {
if (!this.prompt_) throw new Error('Prompt is undefined');
return await this.prompt_(message, options);
}
metadata() {
return {
name: this.name(),
usage: this.usage(),
options: this.options(),
hidden: this.hidden(),
};
}
logger() {
return reg.logger();
}
}

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const BaseItem = require('@joplin/lib/models/BaseItem').default;
const BaseModel = require('@joplin/lib/BaseModel').default;
const { toTitleCase } = require('@joplin/lib/string-utils.js');

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const { _ } = require('@joplin/lib/locale');
const BaseModel = require('@joplin/lib/BaseModel').default;

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { _ } = require('@joplin/lib/locale');
class Command extends BaseCommand {

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const { _ } = require('@joplin/lib/locale');
const BaseModel = require('@joplin/lib/BaseModel').default;

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { _, setLocale } = require('@joplin/lib/locale');
const { app } = require('./app.js');
const fs = require('fs-extra');

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const { _ } = require('@joplin/lib/locale');
const BaseModel = require('@joplin/lib/BaseModel').default;

View File

@ -0,0 +1,27 @@
import Note from '@joplin/lib/models/Note';
import { NoteEntity } from '@joplin/lib/services/database/types';
import { setupDatabaseAndSynchronizer, switchClient } from '@joplin/lib/testing/test-utils';
import { setupCommandForTesting, setupApplication } from './utils/testUtils';
const Command = require('./command-done');
describe('command-done', () => {
beforeEach(async () => {
await setupDatabaseAndSynchronizer(1);
await switchClient(1);
await setupApplication();
});
it('should make a note as "done"', async () => {
const note = await Note.save({ title: 'hello', is_todo: 1, todo_completed: 0 });
const command = setupCommandForTesting(Command);
const now = Date.now();
await command.action({ note: note.id });
const checkNote: NoteEntity = await Note.load(note.id);
expect(checkNote.todo_completed).toBeGreaterThanOrEqual(now);
});
});

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const Folder = require('@joplin/lib/models/Folder').default;
const Note = require('@joplin/lib/models/Note').default;
const Tag = require('@joplin/lib/models/Tag').default;

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
import { _ } from '@joplin/lib/locale';
import EncryptionService from '@joplin/lib/services/e2ee/EncryptionService';
import DecryptionWorker from '@joplin/lib/services/DecryptionWorker';

View File

@ -1,5 +1,5 @@
const fs = require('fs-extra');
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { splitCommandString } = require('@joplin/lib/string-utils.js');
const uuid = require('@joplin/lib/uuid').default;
const { app } = require('./app.js');

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const { _ } = require('@joplin/lib/locale');

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const Setting = require('@joplin/lib/models/Setting').default;
const ReportService = require('@joplin/lib/services/ReportService').default;

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const InteropService = require('@joplin/lib/services/interop/InteropService').default;
const BaseModel = require('@joplin/lib/BaseModel').default;
const { app } = require('./app.js');

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const { _ } = require('@joplin/lib/locale');
const BaseModel = require('@joplin/lib/BaseModel').default;

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const { renderCommandHelp } = require('./help-utils.js');
const { _ } = require('@joplin/lib/locale');

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const InteropService = require('@joplin/lib/services/interop/InteropService').default;
const BaseModel = require('@joplin/lib/BaseModel').default;
const { cliUtils } = require('./cli-utils.js');

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const { _ } = require('@joplin/lib/locale');
const BaseModel = require('@joplin/lib/BaseModel').default;

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const { _ } = require('@joplin/lib/locale');
const Folder = require('@joplin/lib/models/Folder').default;

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const { _ } = require('@joplin/lib/locale');
const Note = require('@joplin/lib/models/Note').default;

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const { _ } = require('@joplin/lib/locale');
const Note = require('@joplin/lib/models/Note').default;

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const { _ } = require('@joplin/lib/locale');
const BaseModel = require('@joplin/lib/BaseModel').default;

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const { _ } = require('@joplin/lib/locale');
const BaseModel = require('@joplin/lib/BaseModel').default;

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const { _ } = require('@joplin/lib/locale');
const Folder = require('@joplin/lib/models/Folder').default;

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const { _ } = require('@joplin/lib/locale');
const Note = require('@joplin/lib/models/Note').default;

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { _ } = require('@joplin/lib/locale');
const BaseModel = require('@joplin/lib/BaseModel').default;
const Folder = require('@joplin/lib/models/Folder').default;

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { _ } = require('@joplin/lib/locale');
const Setting = require('@joplin/lib/models/Setting').default;
const Logger = require('@joplin/lib/Logger').default;

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const { _ } = require('@joplin/lib/locale');
const BaseModel = require('@joplin/lib/BaseModel').default;

View File

@ -2,7 +2,7 @@ import Setting, { SettingStorage } from '@joplin/lib/models/Setting';
import { SettingItemType } from '@joplin/lib/services/plugins/api/types';
import shim from '@joplin/lib/shim';
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
function settingTypeToSchemaType(type: SettingItemType): string {
const map: Record<SettingItemType, string> = {

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const Setting = require('@joplin/lib/models/Setting').default;
const { _ } = require('@joplin/lib/locale');

View File

@ -6,7 +6,7 @@ import ResourceFetcher from '@joplin/lib/services/ResourceFetcher';
import Synchronizer from '@joplin/lib/Synchronizer';
import { masterKeysWithoutPassword } from '@joplin/lib/services/e2ee/utils';
import { appTypeToLockType } from '@joplin/lib/services/synchronizer/LockHandler';
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const { OneDriveApiNodeUtils } = require('@joplin/lib/onedrive-api-node-utils.js');
const { reg } = require('@joplin/lib/registry.js');

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const { _ } = require('@joplin/lib/locale');
const Tag = require('@joplin/lib/models/Tag').default;

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
import { reg } from '@joplin/lib/registry';
import Note from '@joplin/lib/models/Note';
import uuid from '@joplin/lib/uuid';

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const { _ } = require('@joplin/lib/locale');
const BaseModel = require('@joplin/lib/BaseModel').default;

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { _ } = require('@joplin/lib/locale');
const CommandDone = require('./command-done.js');

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { app } = require('./app.js');
const { _ } = require('@joplin/lib/locale');
const BaseModel = require('@joplin/lib/BaseModel').default;

View File

@ -1,4 +1,4 @@
const { BaseCommand } = require('./base-command.js');
const BaseCommand = require('./base-command').default;
const { _ } = require('@joplin/lib/locale');
const versionInfo = require('@joplin/lib/versionInfo').default;

View File

@ -0,0 +1,39 @@
import { _ } from '@joplin/lib/locale';
export default (cmd: any, stdout: Function, store: Function, gui: Function) => {
cmd.setStdout((text: string) => {
return stdout(text);
});
cmd.setDispatcher((action: any) => {
if (store()) {
return store().dispatch(action);
} else {
return () => {};
}
});
cmd.setPrompt(async (message: string, options: any) => {
if (!options) options = {};
if (!options.type) options.type = 'boolean';
if (!options.booleanAnswerDefault) options.booleanAnswerDefault = 'y';
if (!options.answers) options.answers = options.booleanAnswerDefault === 'y' ? [_('Y'), _('n')] : [_('N'), _('y')];
if (options.type === 'boolean') {
message += ` (${options.answers.join('/')})`;
}
let answer = await gui().prompt('', `${message} `, options);
if (options.type === 'boolean') {
if (answer === null) return false; // Pressed ESCAPE
if (!answer) answer = options.answers[0];
const positiveIndex = options.booleanAnswerDefault === 'y' ? 0 : 1;
return answer.toLowerCase() === options.answers[positiveIndex].toLowerCase();
} else {
return answer;
}
});
return cmd;
};

View File

@ -0,0 +1,17 @@
const { app } = require('../app');
import Folder from '@joplin/lib/models/Folder';
import BaseCommand from '../base-command';
import setupCommand from '../setupCommand';
export const setupCommandForTesting = (CommandClass: any, stdout: Function = null): BaseCommand => {
const command = new CommandClass();
setupCommand(command, stdout, null, null);
return command;
};
export const setupApplication = async () => {
// We create a notebook and set it as default since most commands require
// such notebook.
await Folder.save({ title: 'default' });
await app().refreshCurrentFolder();
};

View File

@ -27,6 +27,7 @@
module.exports = {
testMatch: [
'**/tests/**/*.js',
'**/*.test.js',
],
testPathIgnorePatterns: [