test(cli): use mock-fs in removePlugin
This commit is contained in:
@@ -31,6 +31,7 @@ import {
|
||||
removeSymLink,
|
||||
removePluginFromCodeOwners,
|
||||
} from './removePlugin';
|
||||
import mockFs from 'mock-fs';
|
||||
|
||||
const BACKSTAGE = `@backstage`;
|
||||
const testPluginName = 'yarn-test-package';
|
||||
@@ -47,13 +48,24 @@ const createTestPackageFile = async (
|
||||
// Copy contents of package file for test
|
||||
const packageFileContent = JSON.parse(fse.readFileSync(packageFile, 'utf8'));
|
||||
|
||||
packageFileContent.dependencies[testPluginPackage] = '0.1.0';
|
||||
fse.createFileSync(testFilePath);
|
||||
fse.writeFileSync(
|
||||
testFilePath,
|
||||
`${JSON.stringify(packageFileContent, null, 2)}\n`,
|
||||
'utf8',
|
||||
);
|
||||
const testFileContent = {
|
||||
...packageFileContent,
|
||||
dependencies: {
|
||||
...packageFileContent.dependencies,
|
||||
[testPluginPackage]: '0.1.0',
|
||||
},
|
||||
};
|
||||
|
||||
mockFs({
|
||||
'/packages': {
|
||||
app: {
|
||||
'package.json': `${JSON.stringify(packageFileContent, null, 2)}\n`,
|
||||
},
|
||||
},
|
||||
'/remove-plugin-test': {
|
||||
[testFilePath]: `${JSON.stringify(testFileContent, null, 2)}\n`,
|
||||
},
|
||||
});
|
||||
return;
|
||||
};
|
||||
|
||||
@@ -62,21 +74,47 @@ const createTestPluginFile = async (
|
||||
pluginsFilePath: string,
|
||||
) => {
|
||||
// Copy contents of package file for test
|
||||
fse.copyFileSync(pluginsFilePath, testFilePath);
|
||||
const pluginsFileContent = fse.readFileSync(pluginsFilePath);
|
||||
|
||||
mockFs({
|
||||
'/remove-plugin-test': {
|
||||
[testFilePath]: `${pluginsFileContent}\n`,
|
||||
[pluginsFilePath]: `${pluginsFileContent}\n`,
|
||||
},
|
||||
'/packages': {
|
||||
app: {
|
||||
src: {
|
||||
'plugin.ts': `${pluginsFileContent}\n`,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const pluginNameCapitalized = testPluginName
|
||||
.split('-')
|
||||
.map(name => capitalize(name))
|
||||
.join('');
|
||||
const exportStatement = `export { plugin as ${pluginNameCapitalized}} from @backstage/plugin-${testPluginName}`;
|
||||
await addExportStatement(testFilePath, exportStatement);
|
||||
const exportStatement = `export { default as ${pluginNameCapitalized}} from @backstage/plugin-${testPluginName}`;
|
||||
addExportStatement(`/remove-plugin-test/${testFilePath}`, exportStatement);
|
||||
};
|
||||
|
||||
const mkTestPluginDir = (testDirPath: string) => {
|
||||
fse.mkdirSync(testDirPath);
|
||||
for (let i = 0; i < 50; i++)
|
||||
fse.createFileSync(path.join(testDirPath, `testFile${i}.ts`));
|
||||
const dirPath = `/${testDirPath}`;
|
||||
|
||||
const pluginFiles: { [index: number]: string } = {};
|
||||
for (let i = 0; i < 50; i++) {
|
||||
pluginFiles[i] = '';
|
||||
}
|
||||
|
||||
mockFs({
|
||||
[dirPath]: pluginFiles,
|
||||
});
|
||||
};
|
||||
|
||||
afterEach(() => {
|
||||
mockFs.restore();
|
||||
});
|
||||
|
||||
describe('removePlugin', () => {
|
||||
beforeAll(() => {
|
||||
// Create temporary directory for all tests
|
||||
@@ -95,60 +133,65 @@ describe('removePlugin', () => {
|
||||
it('removes plugin references from /packages/app/package.json', async () => {
|
||||
// Set up test
|
||||
const packageFilePath = path.join(appPath, 'package.json');
|
||||
const testFilePath = path.join(tempDir, 'test.json');
|
||||
const testFilePath = 'test.json';
|
||||
createTestPackageFile(testFilePath, packageFilePath);
|
||||
try {
|
||||
await removeReferencesFromAppPackage(testFilePath, testPluginName);
|
||||
const testFileContent = removeEmptyLines(
|
||||
fse.readFileSync(testFilePath, 'utf8'),
|
||||
);
|
||||
const packageFileContent = removeEmptyLines(
|
||||
fse.readFileSync(packageFilePath, 'utf8'),
|
||||
);
|
||||
expect(testFileContent).toBe(packageFileContent);
|
||||
} finally {
|
||||
fse.removeSync(testFilePath);
|
||||
}
|
||||
});
|
||||
await removeReferencesFromAppPackage(
|
||||
'/remove-plugin-test/test.json',
|
||||
testPluginName,
|
||||
);
|
||||
const testFileContent = removeEmptyLines(
|
||||
fse.readFileSync(`/remove-plugin-test/${testFilePath}`, 'utf8'),
|
||||
);
|
||||
|
||||
it('removes plugin exports from /packages/app/src/package.json', async () => {
|
||||
const testFilePath = path.join(tempDir, 'test.ts');
|
||||
const packageFileContent = removeEmptyLines(
|
||||
fse.readFileSync('/packages/app/package.json', 'utf8'),
|
||||
);
|
||||
expect(testFileContent).toBe(packageFileContent);
|
||||
});
|
||||
it('removes plugin exports from /packages/app/src/packacge.json', async () => {
|
||||
const testFilePath = 'test.ts';
|
||||
const pluginsFilePaths = path.join(appPath, 'src', 'plugins.ts');
|
||||
await createTestPluginFile(testFilePath, pluginsFilePaths);
|
||||
try {
|
||||
await removeReferencesFromPluginsFile(testFilePath, testPluginName);
|
||||
const testFileContent = removeEmptyLines(
|
||||
fse.readFileSync(testFilePath, 'utf8'),
|
||||
);
|
||||
const pluginsFileContent = removeEmptyLines(
|
||||
fse.readFileSync(pluginsFilePaths, 'utf8'),
|
||||
);
|
||||
expect(testFileContent).toBe(pluginsFileContent);
|
||||
} finally {
|
||||
fse.removeSync(testFilePath);
|
||||
}
|
||||
createTestPluginFile(testFilePath, pluginsFilePaths);
|
||||
await removeReferencesFromPluginsFile(
|
||||
`/remove-plugin-test/${testFilePath}`,
|
||||
testPluginName,
|
||||
);
|
||||
const testFileContent = removeEmptyLines(
|
||||
fse.readFileSync(`/remove-plugin-test/${testFilePath}`, 'utf8'),
|
||||
);
|
||||
const pluginsFileContent = removeEmptyLines(
|
||||
fse.readFileSync('/packages/app/src/plugin.ts', 'utf8'),
|
||||
);
|
||||
expect(testFileContent).toBe(pluginsFileContent);
|
||||
});
|
||||
|
||||
it('removes codeOwners references', async () => {
|
||||
const testFilePath = path.join(tempDir, 'test');
|
||||
const testFileName = 'test';
|
||||
const testFilePath = `/remove-plugin-test/${testFileName}`;
|
||||
const codeownersPath = path.join(githubDir, 'CODEOWNERS');
|
||||
try {
|
||||
fse.copySync(codeownersPath, testFilePath);
|
||||
const testFileContent = removeEmptyLines(
|
||||
fse.readFileSync(testFilePath, 'utf8'),
|
||||
);
|
||||
const codeOwnersFileContent = removeEmptyLines(
|
||||
fse.readFileSync(codeownersPath, 'utf8'),
|
||||
);
|
||||
await addCodeownersEntry(testFilePath!, `/plugins/${testPluginName}`, [
|
||||
'@thisIsAtestTeam',
|
||||
'test@gmail.com',
|
||||
]);
|
||||
await removePluginFromCodeOwners(testFilePath, testPluginName);
|
||||
expect(testFileContent).toBe(codeOwnersFileContent);
|
||||
} finally {
|
||||
if (fse.existsSync(testFilePath)) fse.removeSync(testFilePath);
|
||||
}
|
||||
const mockedCodeownersPath = '/.github/CODEOWNERS';
|
||||
|
||||
mockFs({
|
||||
'/remove-plugin-test': {
|
||||
[testFileName]: '',
|
||||
},
|
||||
'/.github': {
|
||||
CODEOWNERS: fse.readFileSync(codeownersPath),
|
||||
},
|
||||
});
|
||||
fse.copySync(mockedCodeownersPath, testFilePath);
|
||||
const testFileContent = removeEmptyLines(
|
||||
fse.readFileSync(testFilePath, 'utf8'),
|
||||
);
|
||||
const codeOwnersFileContent = removeEmptyLines(
|
||||
fse.readFileSync(mockedCodeownersPath, 'utf8'),
|
||||
);
|
||||
await addCodeownersEntry(testFilePath!, `/plugins/${testPluginName}`, [
|
||||
'@thisIsAtestTeam',
|
||||
'test@gmail.com',
|
||||
]);
|
||||
await removePluginFromCodeOwners(testFilePath, testPluginName);
|
||||
expect(testFileContent).toBe(codeOwnersFileContent);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user