From a2df99d92aca7d30c912abab1757718757f364e2 Mon Sep 17 00:00:00 2001 From: Remi Date: Tue, 20 Oct 2020 14:55:26 +0200 Subject: [PATCH] test(cli): use mock-fs in removePlugin --- .../remove-plugin/removePlugin.test.ts | 163 +++++++++++------- 1 file changed, 103 insertions(+), 60 deletions(-) diff --git a/packages/cli/src/commands/remove-plugin/removePlugin.test.ts b/packages/cli/src/commands/remove-plugin/removePlugin.test.ts index 6433cb6116..3ef9cd113f 100644 --- a/packages/cli/src/commands/remove-plugin/removePlugin.test.ts +++ b/packages/cli/src/commands/remove-plugin/removePlugin.test.ts @@ -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); }); });