fix: test with nocobase plugin (#1525)

This commit is contained in:
ChengLei Shao 2023-03-03 13:54:37 +08:00 committed by GitHub
parent 04971c3824
commit 4d44e31b31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 5 deletions

View File

@ -176,6 +176,7 @@ export class PluginManager {
if (!options?.async) { if (!options?.async) {
this._tmpPluginArgs.push([plugin, options]); this._tmpPluginArgs.push([plugin, options]);
} }
let name: string; let name: string;
if (typeof plugin === 'string') { if (typeof plugin === 'string') {
name = plugin; name = plugin;
@ -192,7 +193,9 @@ export class PluginManager {
enabled: true, enabled: true,
...options, ...options,
}); });
const pluginName = instance.getName(); const pluginName = instance.getName();
if (this.plugins.has(pluginName)) { if (this.plugins.has(pluginName)) {
throw new Error(`plugin name [${pluginName}] already exists`); throw new Error(`plugin name [${pluginName}] already exists`);
} }
@ -223,7 +226,12 @@ export class PluginManager {
if (Array.isArray(plugin)) { if (Array.isArray(plugin)) {
const t = transaction || (await this.app.db.sequelize.transaction()); const t = transaction || (await this.app.db.sequelize.transaction());
try { try {
const items = await Promise.all(plugin.map((p) => this.add(p, options, t))); const items = [];
for (const p of plugin) {
items.push(await this.add(p, options, t));
}
await t.commit(); await t.commit();
return items; return items;
} catch (error) { } catch (error) {
@ -231,20 +239,26 @@ export class PluginManager {
throw error; throw error;
} }
} }
const packageName = await PluginManager.findPackage(plugin); const packageName = await PluginManager.findPackage(plugin);
const packageJson = require(`${packageName}/package.json`);
await this.generateClientFile(plugin, packageName); await this.generateClientFile(plugin, packageName);
const instance = this.addStatic(plugin, { const instance = this.addStatic(plugin, {
...options, ...options,
async: true, async: true,
}); });
let model = await this.repository.findOne({ let model = await this.repository.findOne({
transaction, transaction,
filter: { name: plugin }, filter: { name: plugin },
}); });
const packageJson = PluginManager.getPackageJson(packageName);
if (!model) { if (!model) {
const { enabled, builtIn, installed, ...others } = options; const { enabled, builtIn, installed, ...others } = options;
model = await this.repository.create({ await this.repository.create({
transaction, transaction,
values: { values: {
name: plugin, name: plugin,
@ -337,6 +351,10 @@ export class PluginManager {
this.app.reload(); this.app.reload();
} }
static getPackageJson(packageName: string) {
return require(`${packageName}/package.json`);
}
static getPackageName(name: string) { static getPackageName(name: string) {
const prefixes = this.getPluginPkgPrefix(); const prefixes = this.getPluginPkgPrefix();
for (const prefix of prefixes) { for (const prefix of prefixes) {

View File

@ -1,5 +1,5 @@
import { Database, mockDatabase } from '@nocobase/database'; import { Database, mockDatabase } from '@nocobase/database';
import Application, { ApplicationOptions } from '@nocobase/server'; import Application, { ApplicationOptions, PluginManager } from '@nocobase/server';
import qs from 'qs'; import qs from 'qs';
import supertest, { SuperAgentTest } from 'supertest'; import supertest, { SuperAgentTest } from 'supertest';
@ -130,6 +130,26 @@ export class MockServer extends Application {
} }
export function mockServer(options: ApplicationOptions = {}) { export function mockServer(options: ApplicationOptions = {}) {
if (typeof TextEncoder === 'undefined') {
global.TextEncoder = require('util').TextEncoder;
}
if (typeof TextDecoder === 'undefined') {
global.TextDecoder = require('util').TextDecoder;
}
// @ts-ignore
if (!PluginManager.findPackagePatched) {
PluginManager.getPackageJson = () => {
return {
version: '0.0.0',
};
};
// @ts-ignore
PluginManager.findPackagePatched = true;
}
let database; let database;
if (options?.database instanceof Database) { if (options?.database instanceof Database) {
database = options.database; database = options.database;
@ -137,11 +157,15 @@ export function mockServer(options: ApplicationOptions = {}) {
database = mockDatabase(<any>options?.database || {}); database = mockDatabase(<any>options?.database || {});
} }
return new MockServer({ const app = new MockServer({
acl: false, acl: false,
...options, ...options,
database, database,
}); });
app.pm.generateClientFile = async () => {};
return app;
} }
export function createMockServer() {} export function createMockServer() {}