feat: adjust the test

Signed-off-by: Alper Altay <alper.altay@lego.com>
This commit is contained in:
Alper Altay
2024-06-24 17:23:28 +02:00
parent e09456b711
commit f6581944cc
2 changed files with 24 additions and 32 deletions
@@ -27,10 +27,10 @@ import { backendPlugin } from './backendPlugin';
import { createMockDirectory } from '@backstage/backend-test-utils';
const backendIndexTsContent = `
import { createBackend } from '@backstage/backend-defaults';
const backend = createBackend();
import { createBackend } from '@backstage/backend-defaults';
const backend = createBackend();
backend.start();
`;
@@ -112,11 +112,11 @@ describe('backendPlugin factory', () => {
await expect(
fs.readFile(mockDir.resolve('packages/backend/src/index.ts'), 'utf8'),
).resolves.toBe(`
import { createBackend } from '@backstage/backend-defaults';
import { createBackend } from '@backstage/backend-defaults';
const backend = createBackend();
backend.add(import("backstage-plugin-test-backend"));
const backend = createBackend();
backend.add(import('backstage-plugin-test-backend'));
backend.start();
`);
@@ -174,12 +174,13 @@ backend.start();
await expect(
fs.readFile(mockDir.resolve('packages/backend/src/index.ts'), 'utf8'),
).resolves.toBe(`
import { createBackend } from '@backstage/backend-defaults';
const backend = createBackend();
backend.add(import("@internal/backstage-plugin-test-backend"));
backend.start();`);
import { createBackend } from '@backstage/backend-defaults';
const backend = createBackend();
backend.add(import('@internal/backstage-plugin-test-backend'));
backend.start();
`);
expect(Task.forCommand).toHaveBeenCalledTimes(2);
expect(Task.forCommand).toHaveBeenCalledWith('yarn install', {
@@ -86,29 +86,20 @@ export const backendPlugin = createFactory<Options>({
if (!(await fs.pathExists(backendFilePath))) {
return;
}
const content = await fs.readFile(backendFilePath, 'utf8');
const revLines = content.split('\n').reverse();
const lines = content.split('\n');
const backendAddLine = `backend.add(import('${name}'));`;
const lastImportIndex = revLines.findIndex(line =>
line.match(/ from ("|').*("|')/),
);
const lastBackendAddIndex = revLines.findIndex(line =>
line.match(/backend.add/),
const backendStartIndex = lines.findIndex(line =>
line.match(/backend.start/),
);
const backendAddLine = `backend.add(import("${name}"));`;
if (backendStartIndex !== -1) {
const [indentation] = lines[backendStartIndex].match(/^\s*/) ?? [];
lines.splice(backendStartIndex, 0, indentation + backendAddLine);
if (lastImportIndex !== -1 && lastBackendAddIndex !== -1) {
if (!content.includes(backendAddLine)) {
const [indentation] =
revLines[lastBackendAddIndex + 1].match(/^\s*/) ?? [];
revLines.splice(
lastBackendAddIndex + 1,
0,
indentation + backendAddLine,
);
}
const newContent = revLines.reverse().join('\n');
const newContent = lines.join('\n');
await fs.writeFile(backendFilePath, newContent, 'utf8');
}
});