Introducing createMockActionContext for scaffolder

Signed-off-by: bnechyporenko <bnechyporenko@bol.com>
This commit is contained in:
bnechyporenko
2024-02-18 13:43:00 +01:00
committed by blam
parent 122f8476ab
commit d50a02bf7d
13 changed files with 196 additions and 13 deletions
@@ -0,0 +1 @@
module.exports = require('@backstage/cli/config/eslint-factory')(__dirname);
@@ -0,0 +1 @@
# @backstage/scaffolder-test-utils
+12
View File
@@ -0,0 +1,12 @@
# @backstage/scaffolder-test-utils
Contains utilities that can be used when testing scaffolder features.
## Installation
Install the package via Yarn into your own packages:
```sh
cd <package-dir> # if within a monorepo
yarn add --dev @backstage/scaffolder-test-utils
```
@@ -0,0 +1,18 @@
## API Report File for "@backstage/scaffolder-test-utils"
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts
import { ActionContext } from '@backstage/plugin-scaffolder-node';
import { JsonObject } from '@backstage/types';
// @public
export const createMockActionContext: <
TActionInput extends JsonObject = JsonObject,
TActionOutput extends JsonObject = JsonObject,
>(
input?: TActionInput | undefined,
) => ActionContext<TActionInput, TActionOutput>;
// (No @packageDocumentation comment for this package)
```
@@ -0,0 +1,9 @@
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: backstage-scaffolder-test-utils
title: '@backstage/scaffolder-test-utils'
spec:
lifecycle: experimental
type: backstage-node-library
owner: maintainers
@@ -0,0 +1,2 @@
# Knip report
@@ -0,0 +1,48 @@
{
"name": "@backstage/scaffolder-test-utils",
"version": "0.0.1",
"main": "src/index.ts",
"types": "src/index.ts",
"license": "Apache-2.0",
"publishConfig": {
"access": "public",
"main": "dist/index.esm.js",
"types": "dist/index.d.ts"
},
"repository": {
"type": "git",
"url": "https://github.com/backstage/backstage",
"directory": "packages/scaffolder-test-utils"
},
"backstage": {
"role": "node-library"
},
"sideEffects": false,
"scripts": {
"start": "backstage-cli package start",
"build": "backstage-cli package build",
"lint": "backstage-cli package lint",
"test": "backstage-cli package test",
"clean": "backstage-cli package clean",
"prepack": "backstage-cli package prepack",
"postpack": "backstage-cli package postpack"
},
"devDependencies": {
"@backstage/cli": "workspace:^",
"@testing-library/jest-dom": "^6.0.0",
"@types/react": "*"
},
"files": [
"dist"
],
"dependencies": {
"@backstage/backend-common": "workspace:^",
"@backstage/backend-test-utils": "workspace:^",
"@backstage/plugin-scaffolder-node": "workspace:^",
"@backstage/test-utils": "workspace:^",
"@backstage/types": "workspace:^"
},
"peerDependencies": {
"@types/jest": "*"
}
}
@@ -0,0 +1,17 @@
/*
* Copyright 2020 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.
*/
export { createMockActionContext } from './mockActionConext';
@@ -0,0 +1,46 @@
/*
* 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 { PassThrough } from 'stream';
import { getVoidLogger } from '@backstage/backend-common';
import { createMockDirectory } from '@backstage/backend-test-utils';
import { JsonObject } from '@backstage/types';
import { ActionContext } from '@backstage/plugin-scaffolder-node';
/**
* A utility method to create a mock action context for scaffolder actions.
*
* @param input - a schema for user input parameters
*
* @public
*/
export const createMockActionContext = <
TActionInput extends JsonObject = JsonObject,
TActionOutput extends JsonObject = JsonObject,
>(
input?: TActionInput,
): ActionContext<TActionInput, TActionOutput> => {
const mockDir = createMockDirectory();
return {
workspacePath: mockDir.path,
logger: getVoidLogger(),
logStream: new PassThrough(),
output: jest.fn(),
createTemporaryDirectory: jest.fn(),
input: (input ? input : {}) as TActionInput,
};
};
@@ -0,0 +1,17 @@
/*
* Copyright 2020 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.
*/
export * from './actions';
@@ -47,7 +47,8 @@
"yaml": "^2.0.0"
},
"devDependencies": {
"@backstage/cli": "workspace:^"
"@backstage/cli": "workspace:^",
"@backstage/scaffolder-test-utils": "workspace:^"
},
"files": [
"dist"
@@ -34,10 +34,9 @@ jest.mock('@backstage/plugin-scaffolder-node', () => {
import { createPublishAzureAction } from './azure';
import { ScmIntegrations } from '@backstage/integration';
import { ConfigReader } from '@backstage/config';
import { getVoidLogger } from '@backstage/backend-common';
import { WebApi } from 'azure-devops-node-api';
import { PassThrough } from 'stream';
import { initRepoAndPush } from '@backstage/plugin-scaffolder-node';
import { createMockActionContext } from '@backstage/scaffolder-test-utils';
describe('publish:azure', () => {
const config = new ConfigReader({
@@ -54,16 +53,10 @@ describe('publish:azure', () => {
const integrations = ScmIntegrations.fromConfig(config);
const action = createPublishAzureAction({ integrations, config });
const mockContext = {
input: {
repoUrl: 'dev.azure.com?repo=repo&owner=owner&organization=org',
},
workspacePath: 'lol',
logger: getVoidLogger(),
logStream: new PassThrough(),
output: jest.fn(),
createTemporaryDirectory: jest.fn(),
};
const mockContext = createMockActionContext({
repoUrl: 'dev.azure.com?repo=repo&owner=owner&organization=org',
});
const mockGitClient = {
createRepository: jest.fn(),
+18
View File
@@ -8209,6 +8209,7 @@ __metadata:
"@backstage/errors": "workspace:^"
"@backstage/integration": "workspace:^"
"@backstage/plugin-scaffolder-node": "workspace:^"
"@backstage/scaffolder-test-utils": "workspace:^"
azure-devops-node-api: ^12.0.0
yaml: ^2.0.0
languageName: unknown
@@ -9864,6 +9865,23 @@ __metadata:
languageName: unknown
linkType: soft
"@backstage/scaffolder-test-utils@workspace:^, @backstage/scaffolder-test-utils@workspace:packages/scaffolder-test-utils":
version: 0.0.0-use.local
resolution: "@backstage/scaffolder-test-utils@workspace:packages/scaffolder-test-utils"
dependencies:
"@backstage/backend-common": "workspace:^"
"@backstage/backend-test-utils": "workspace:^"
"@backstage/cli": "workspace:^"
"@backstage/plugin-scaffolder-node": "workspace:^"
"@backstage/test-utils": "workspace:^"
"@backstage/types": "workspace:^"
"@testing-library/jest-dom": ^6.0.0
"@types/react": "*"
peerDependencies:
"@types/jest": "*"
languageName: unknown
linkType: soft
"@backstage/test-utils@workspace:^, @backstage/test-utils@workspace:packages/test-utils":
version: 0.0.0-use.local
resolution: "@backstage/test-utils@workspace:packages/test-utils"