backend-test-utils: merge mockFactories into mockServices

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-01-16 17:13:34 +01:00
parent d0901c9ba4
commit 846b310dbb
6 changed files with 187 additions and 143 deletions
+1 -1
View File
@@ -2,4 +2,4 @@
'@backstage/backend-test-utils': patch
---
All mock service factories and mock service implementations are now available via two new exports, `mockServices`, and `mockFactories`.
All mock service factories and mock service implementations are now available via the new experimental `mockServices` export.
+89 -38
View File
@@ -30,56 +30,84 @@ import { UrlReaderService } from '@backstage/backend-plugin-api';
// @public (undocumented)
export function isDockerDisabledForTests(): boolean;
// @alpha (undocumented)
export namespace mockFactories {
const // (undocumented)
config: (
options?: mockServices.config.Options | undefined,
) => ServiceFactory<ConfigService>;
const // (undocumented)
rootLogger: (
options?: mockServices.rootLogger.Options | undefined,
) => ServiceFactory<RootLoggerService>;
const // (undocumented)
tokenManager: () => ServiceFactory<TokenManagerService>;
const // (undocumented)
identity: () => ServiceFactory<IdentityService>;
const // (undocumented)
cache: () => ServiceFactory<CacheService>;
const // (undocumented)
database: () => ServiceFactory<DatabaseService>;
const // (undocumented)
httpRouter: (
options?: HttpRouterFactoryOptions | undefined,
) => ServiceFactory<HttpRouterService>;
const // (undocumented)
lifecycle: () => ServiceFactory<LifecycleService>;
const // (undocumented)
logger: () => ServiceFactory<LoggerService>;
const // (undocumented)
permissions: () => ServiceFactory<PermissionsService>;
const // (undocumented)
rootLifecycle: () => ServiceFactory<RootLifecycleService>;
const // (undocumented)
scheduler: () => ServiceFactory<SchedulerService>;
const // (undocumented)
urlReader: () => ServiceFactory<UrlReaderService>;
}
// @alpha (undocumented)
export namespace mockServices {
// (undocumented)
export namespace cache {
const // (undocumented)
ref: ServiceRef<CacheService, 'plugin'>;
const // (undocumented)
factory: () => ServiceFactory<CacheService>;
}
// (undocumented)
export function config(options?: config.Options): ConfigReader;
// (undocumented)
export namespace config {
// (undocumented)
export type Options = {
data?: JsonObject;
};
const // (undocumented)
ref: ServiceRef<ConfigService, 'root'>;
const // (undocumented)
factory: (options?: Options | undefined) => ServiceFactory<ConfigService>;
}
// (undocumented)
export function config(options?: config.Options): ConfigReader;
export namespace database {
const // (undocumented)
ref: ServiceRef<DatabaseService, 'plugin'>;
const // (undocumented)
factory: () => ServiceFactory<DatabaseService>;
}
// (undocumented)
export namespace httpRouter {
const // (undocumented)
ref: ServiceRef<HttpRouterService, 'plugin'>;
const // (undocumented)
factory: (
options?: HttpRouterFactoryOptions | undefined,
) => ServiceFactory<HttpRouterService>;
}
// (undocumented)
export function identity(): IdentityService;
// (undocumented)
export namespace identity {
const // (undocumented)
ref: ServiceRef<IdentityService, 'plugin'>;
const // (undocumented)
factory: () => ServiceFactory<IdentityService>;
}
// (undocumented)
export namespace lifecycle {
const // (undocumented)
ref: ServiceRef<LifecycleService, 'plugin'>;
const // (undocumented)
factory: () => ServiceFactory<LifecycleService>;
}
// (undocumented)
export namespace logger {
const // (undocumented)
ref: ServiceRef<LoggerService, 'plugin'>;
const // (undocumented)
factory: () => ServiceFactory<LoggerService>;
}
// (undocumented)
export namespace permissions {
const // (undocumented)
ref: ServiceRef<PermissionsService, 'plugin'>;
const // (undocumented)
factory: () => ServiceFactory<PermissionsService>;
}
// (undocumented)
export namespace rootLifecycle {
const // (undocumented)
ref: ServiceRef<RootLifecycleService, 'root'>;
const // (undocumented)
factory: () => ServiceFactory<RootLifecycleService>;
}
// (undocumented)
export function rootLogger(options?: rootLogger.Options): LoggerService;
// (undocumented)
export namespace rootLogger {
// (undocumented)
export type Options = {
@@ -92,11 +120,34 @@ export namespace mockServices {
debug: boolean;
};
};
const // (undocumented)
ref: ServiceRef<RootLoggerService, 'root'>;
const // (undocumented)
factory: (options?: Options | undefined) => ServiceFactory<LoggerService>;
}
// (undocumented)
export function rootLogger(options?: rootLogger.Options): LoggerService;
export namespace scheduler {
const // (undocumented)
ref: ServiceRef<SchedulerService, 'plugin'>;
const // (undocumented)
factory: () => ServiceFactory<SchedulerService>;
}
// (undocumented)
export function tokenManager(): TokenManagerService;
// (undocumented)
export namespace tokenManager {
const // (undocumented)
ref: ServiceRef<TokenManagerService, 'plugin'>;
const // (undocumented)
factory: () => ServiceFactory<TokenManagerService>;
}
// (undocumented)
export namespace urlReader {
const // (undocumented)
ref: ServiceRef<UrlReaderService, 'plugin'>;
const // (undocumented)
factory: () => ServiceFactory<UrlReaderService>;
}
}
// @public
@@ -13,5 +13,4 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { mockFactories } from './mockFactories';
export { mockServices } from './mockServices';
@@ -1,84 +0,0 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
cacheFactory,
databaseFactory,
httpRouterFactory,
lifecycleFactory,
loggerFactory,
permissionsFactory,
rootLifecycleFactory,
schedulerFactory,
urlReaderFactory,
} from '@backstage/backend-app-api';
import {
coreServices,
createServiceFactory,
} from '@backstage/backend-plugin-api';
import { mockServices } from './mockServices';
/**
* @alpha
*/
export namespace mockFactories {
export const config = createServiceFactory(
(options?: mockServices.config.Options) => ({
service: coreServices.config,
deps: {},
async factory() {
return mockServices.config(options);
},
}),
);
export const rootLogger = createServiceFactory(
(options?: mockServices.rootLogger.Options) => ({
service: coreServices.rootLogger,
deps: {},
async factory() {
return mockServices.rootLogger(options);
},
}),
);
export const tokenManager = createServiceFactory({
service: coreServices.tokenManager,
deps: {},
async factory() {
return mockServices.tokenManager();
},
});
export const identity = createServiceFactory({
service: coreServices.identity,
deps: {},
async factory() {
return mockServices.identity();
},
});
// For all other services, we just use the default implementations
export const cache = cacheFactory;
export const database = databaseFactory;
export const httpRouter = httpRouterFactory;
export const lifecycle = lifecycleFactory;
export const logger = loggerFactory;
export const permissions = permissionsFactory;
export const rootLifecycle = rootLifecycleFactory;
export const scheduler = schedulerFactory;
export const urlReader = urlReaderFactory;
}
@@ -15,35 +15,69 @@
*/
import {
coreServices,
createServiceFactory,
IdentityService,
LoggerService,
ServiceFactory,
ServiceRef,
TokenManagerService,
} from '@backstage/backend-plugin-api';
import {
cacheFactory,
databaseFactory,
httpRouterFactory,
lifecycleFactory,
loggerFactory,
permissionsFactory,
rootLifecycleFactory,
schedulerFactory,
urlReaderFactory,
} from '@backstage/backend-app-api';
import { ConfigReader } from '@backstage/config';
import { JsonObject } from '@backstage/types';
import { MockIdentityService } from './mockIdentityService';
import { MockLogger } from './mockRootLoggerService';
function simpleFactory<TService, TOptions extends [options?: object] = []>(
ref: ServiceRef<TService>,
factory: (...options: TOptions) => TService,
): (...options: TOptions) => ServiceFactory<TService> {
return createServiceFactory((options: unknown) => ({
service: ref as ServiceRef<TService, any>,
deps: {},
async factory() {
return (factory as any)(options);
},
}));
}
/**
* @alpha
*/
export namespace mockServices {
export namespace config {
export type Options = { data?: JsonObject };
}
export function config(options?: config.Options) {
return new ConfigReader(options?.data, 'mock-config');
}
export namespace config {
export type Options = { data?: JsonObject };
export const ref = coreServices.config;
export const factory = simpleFactory(ref, config);
}
export function rootLogger(options?: rootLogger.Options): LoggerService {
return new MockLogger(options?.levels ?? false, {});
}
export namespace rootLogger {
export type Options = {
levels:
| boolean
| { error: boolean; warn: boolean; info: boolean; debug: boolean };
};
}
export function rootLogger(options?: rootLogger.Options): LoggerService {
return new MockLogger(options?.levels ?? false, {});
export const ref = coreServices.rootLogger;
export const factory = simpleFactory(ref, rootLogger);
}
export function tokenManager(): TokenManagerService {
@@ -58,12 +92,56 @@ export namespace mockServices {
},
};
}
export namespace tokenManager {
export const ref = coreServices.tokenManager;
export const factory = simpleFactory(ref, tokenManager);
}
export function identity(): IdentityService {
return new MockIdentityService();
}
export namespace identity {
export const ref = coreServices.identity;
export const factory = simpleFactory(ref, identity);
}
// TODO(Rugvip): Not all core services have implementations available here yet.
// some may need a bit more refactoring for it to be simpler to
// re-implement functioning mock versions here.
export namespace cache {
export const ref = coreServices.cache;
export const factory = cacheFactory;
}
export namespace database {
export const ref = coreServices.database;
export const factory = databaseFactory;
}
export namespace httpRouter {
export const ref = coreServices.httpRouter;
export const factory = httpRouterFactory;
}
export namespace lifecycle {
export const ref = coreServices.lifecycle;
export const factory = lifecycleFactory;
}
export namespace logger {
export const ref = coreServices.logger;
export const factory = loggerFactory;
}
export namespace permissions {
export const ref = coreServices.permissions;
export const factory = permissionsFactory;
}
export namespace rootLifecycle {
export const ref = coreServices.rootLifecycle;
export const factory = rootLifecycleFactory;
}
export namespace scheduler {
export const ref = coreServices.scheduler;
export const factory = schedulerFactory;
}
export namespace urlReader {
export const ref = coreServices.urlReader;
export const factory = urlReaderFactory;
}
}
@@ -32,7 +32,7 @@ import {
coreServices,
} from '@backstage/backend-plugin-api';
import { mockFactories } from '../implementations';
import { mockServices } from '../implementations';
import { ConfigReader } from '@backstage/config';
import express from 'express';
@@ -72,18 +72,18 @@ export interface TestBackend extends Backend {
}
const defaultServiceFactories = [
mockFactories.cache(),
mockFactories.database(),
mockFactories.httpRouter(),
mockFactories.lifecycle(),
mockFactories.logger(),
mockFactories.config(),
mockFactories.identity(),
mockFactories.tokenManager(),
mockFactories.permissions(),
mockFactories.rootLifecycle(),
mockFactories.scheduler(),
mockFactories.urlReader(),
mockServices.cache.factory(),
mockServices.database.factory(),
mockServices.httpRouter.factory(),
mockServices.lifecycle.factory(),
mockServices.logger.factory(),
mockServices.config.factory(),
mockServices.identity.factory(),
mockServices.tokenManager.factory(),
mockServices.permissions.factory(),
mockServices.rootLifecycle.factory(),
mockServices.scheduler.factory(),
mockServices.urlReader.factory(),
];
const backendInstancesToCleanUp = new Array<Backend>();