From b6df68b846f07af368b1f0f9509baf50c8017a67 Mon Sep 17 00:00:00 2001 From: Remi Date: Fri, 16 Oct 2020 23:20:50 +0200 Subject: [PATCH 1/9] test(cli): use mock-fs in createPlugin --- .../create-plugin/createPlugin.test.ts | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/packages/cli/src/commands/create-plugin/createPlugin.test.ts b/packages/cli/src/commands/create-plugin/createPlugin.test.ts index 72b95b072c..9e824c30b6 100644 --- a/packages/cli/src/commands/create-plugin/createPlugin.test.ts +++ b/packages/cli/src/commands/create-plugin/createPlugin.test.ts @@ -16,11 +16,25 @@ import fs from 'fs-extra'; import path from 'path'; +import mockFs from 'mock-fs'; import os from 'os'; import del from 'del'; import { createTemporaryPluginFolder, movePlugin } from './createPlugin'; describe('createPlugin', () => { + beforeEach(() => { + mockFs({ + '/testPluginMock': {}, + 'test-temp': { + plugins: { + testPluginMock: {}, + }, + }, + }); + }); + afterEach(() => { + mockFs.restore(); + }); describe('createPluginFolder', () => { it('should create a temporary plugin directory in the correct place', async () => { const id = 'testPlugin'; @@ -35,35 +49,22 @@ describe('createPlugin', () => { }); it('should not create a temporary plugin directory if it already exists', async () => { - const id = 'testPlugin'; - const tempDir = path.join(os.tmpdir(), id); - try { - await createTemporaryPluginFolder(tempDir); - await expect(fs.pathExists(tempDir)).resolves.toBe(true); - await expect(createTemporaryPluginFolder(tempDir)).rejects.toThrow( - /Failed to create temporary plugin directory/, - ); - } finally { - await del(tempDir, { force: true }); - } + const id = '/testPluginMock'; + await expect(createTemporaryPluginFolder(id)).rejects.toThrow( + /Failed to create temporary plugin directory/, + ); }); }); describe('movePlugin', () => { it('should move the temporary plugin directory to its final place', async () => { - const id = 'testPlugin'; - const tempDir = path.join(os.tmpdir(), id); - const rootDir = await fs.mkdtemp(path.join(os.tmpdir(), 'test-')); - const pluginDir = path.join(rootDir, 'plugins', id); - try { - await createTemporaryPluginFolder(tempDir); - await movePlugin(tempDir, pluginDir, id); - await expect(fs.pathExists(pluginDir)).resolves.toBe(true); - expect(pluginDir).toMatch(path.join('', 'plugins', id)); - } finally { - await del(tempDir, { force: true }); - await del(rootDir, { force: true }); - } + const id = 'testPluginMock'; + const tempDir = `/${id}`; + const pluginDir = `/test-temp/plugins/${id}`; + + await movePlugin(tempDir, pluginDir, id); + await expect(fs.pathExists(pluginDir)).resolves.toBe(true); + expect(pluginDir).toMatch(path.join('', 'plugins', id)); }); }); }); From f2a7cd07ea3a61667bf49dcfb16c4c70271c546c Mon Sep 17 00:00:00 2001 From: Remi Date: Mon, 19 Oct 2020 19:08:00 +0200 Subject: [PATCH 2/9] test(cli): use mock-fs in tasks --- packages/cli/src/lib/tasks.test.ts | 54 ++++++++++++++++-------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/packages/cli/src/lib/tasks.test.ts b/packages/cli/src/lib/tasks.test.ts index 871d5a4d37..a3ff3feeaa 100644 --- a/packages/cli/src/lib/tasks.test.ts +++ b/packages/cli/src/lib/tasks.test.ts @@ -15,39 +15,41 @@ */ import fs from 'fs-extra'; +import mockFs from 'mock-fs'; import { resolve as resolvePath } from 'path'; -import os from 'os'; -import del from 'del'; import { templatingTask } from './tasks'; describe('templatingTask', () => { + afterEach(() => { + mockFs.restore(); + }); + it('should template a directory with mix of regular files and templates', async () => { - // Set up a testing template directory - const tmplDir = await fs.mkdtemp(resolvePath(os.tmpdir(), 'test-')); - await fs.ensureDir(resolvePath(tmplDir, 'sub')); - await fs.writeFile(resolvePath(tmplDir, 'test.txt'), 'testing'); - await fs.writeFile( - resolvePath(tmplDir, 'sub/version.txt.hbs'), - 'version: {{version}}', - ); + // Testing template directory + const tmplDir = 'test-tmpl'; - // Set up a temporary dest dir to write the template to - const destDir = await fs.mkdtemp(resolvePath(os.tmpdir(), 'test-')); + // Temporary dest dir to write the template to + const destDir = 'test-dest'; - try { - await templatingTask(tmplDir, destDir, { - version: '0.0.0', - }); + mockFs({ + [tmplDir]: { + 'sub': { + 'version.txt.hbs': 'version: {{version}}', + }, + 'test.txt': 'testing', + }, + [destDir]: {}, + }); - await expect( - fs.readFile(resolvePath(destDir, 'test.txt'), 'utf8'), - ).resolves.toBe('testing'); - await expect( - fs.readFile(resolvePath(destDir, 'sub/version.txt'), 'utf8'), - ).resolves.toBe('version: 0.0.0'); - } finally { - await del(tmplDir, { force: true }); - await del(destDir, { force: true }); - } + await templatingTask(tmplDir, destDir, { + version: '0.0.0', + }); + + await expect( + fs.readFile(resolvePath(destDir, 'test.txt'), 'utf8'), + ).resolves.toBe('testing'); + await expect( + fs.readFile(resolvePath(destDir, 'sub/version.txt'), 'utf8'), + ).resolves.toBe('version: 0.0.0'); }); }); From a2df99d92aca7d30c912abab1757718757f364e2 Mon Sep 17 00:00:00 2001 From: Remi Date: Tue, 20 Oct 2020 14:55:26 +0200 Subject: [PATCH 3/9] 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); }); }); From 59729fbbccb311021a660b471ec00365a3140165 Mon Sep 17 00:00:00 2001 From: Remi Date: Wed, 21 Oct 2020 10:58:40 +0200 Subject: [PATCH 4/9] test(cli): refactor mock-fs --- .../create-plugin/createPlugin.test.ts | 30 +++--- .../remove-plugin/removePlugin.test.ts | 98 +++++++++---------- 2 files changed, 64 insertions(+), 64 deletions(-) diff --git a/packages/cli/src/commands/create-plugin/createPlugin.test.ts b/packages/cli/src/commands/create-plugin/createPlugin.test.ts index 9e824c30b6..839bd4d433 100644 --- a/packages/cli/src/commands/create-plugin/createPlugin.test.ts +++ b/packages/cli/src/commands/create-plugin/createPlugin.test.ts @@ -22,17 +22,7 @@ import del from 'del'; import { createTemporaryPluginFolder, movePlugin } from './createPlugin'; describe('createPlugin', () => { - beforeEach(() => { - mockFs({ - '/testPluginMock': {}, - 'test-temp': { - plugins: { - testPluginMock: {}, - }, - }, - }); - }); - afterEach(() => { + afterAll(() => { mockFs.restore(); }); describe('createPluginFolder', () => { @@ -49,7 +39,12 @@ describe('createPlugin', () => { }); it('should not create a temporary plugin directory if it already exists', async () => { - const id = '/testPluginMock'; + const id = 'testPluginMock'; + + mockFs({ + [id]: {}, + }); + await expect(createTemporaryPluginFolder(id)).rejects.toThrow( /Failed to create temporary plugin directory/, ); @@ -59,9 +54,18 @@ describe('createPlugin', () => { describe('movePlugin', () => { it('should move the temporary plugin directory to its final place', async () => { const id = 'testPluginMock'; - const tempDir = `/${id}`; + const tempDir = id; const pluginDir = `/test-temp/plugins/${id}`; + mockFs({ + [id]: {}, + 'test-temp': { + plugins: { + testPluginMock: {}, + }, + }, + }); + await movePlugin(tempDir, pluginDir, id); await expect(fs.pathExists(pluginDir)).resolves.toBe(true); expect(pluginDir).toMatch(path.join('', 'plugins', id)); diff --git a/packages/cli/src/commands/remove-plugin/removePlugin.test.ts b/packages/cli/src/commands/remove-plugin/removePlugin.test.ts index 3ef9cd113f..43306939bb 100644 --- a/packages/cli/src/commands/remove-plugin/removePlugin.test.ts +++ b/packages/cli/src/commands/remove-plugin/removePlugin.test.ts @@ -16,13 +16,8 @@ import fse from 'fs-extra'; import path from 'path'; -import os from 'os'; import { paths } from '../../lib/paths'; -import { - addExportStatement, - capitalize, - createTemporaryPluginFolder, -} from '../create-plugin/createPlugin'; +import { addExportStatement, capitalize } from '../create-plugin/createPlugin'; import { addCodeownersEntry } from '../../lib/codeowners'; import { removeReferencesFromAppPackage, @@ -31,12 +26,13 @@ import { removeSymLink, removePluginFromCodeOwners, } from './removePlugin'; -import mockFs from 'mock-fs'; + +const mockFs = require('mock-fs'); const BACKSTAGE = `@backstage`; const testPluginName = 'yarn-test-package'; const testPluginPackage = `${BACKSTAGE}/plugin-${testPluginName}`; -const tempDir = path.join(os.tmpdir(), 'remove-plugin-test'); +const tempDir = '/remove-plugin-test'; const removeEmptyLines = (file: string): string => file.split(/\r?\n/).filter(Boolean).join('\n'); @@ -62,7 +58,7 @@ const createTestPackageFile = async ( 'package.json': `${JSON.stringify(packageFileContent, null, 2)}\n`, }, }, - '/remove-plugin-test': { + [tempDir]: { [testFilePath]: `${JSON.stringify(testFileContent, null, 2)}\n`, }, }); @@ -77,7 +73,7 @@ const createTestPluginFile = async ( const pluginsFileContent = fse.readFileSync(pluginsFilePath); mockFs({ - '/remove-plugin-test': { + [tempDir]: { [testFilePath]: `${pluginsFileContent}\n`, [pluginsFilePath]: `${pluginsFileContent}\n`, }, @@ -95,7 +91,7 @@ const createTestPluginFile = async ( .map(name => capitalize(name)) .join(''); const exportStatement = `export { default as ${pluginNameCapitalized}} from @backstage/plugin-${testPluginName}`; - addExportStatement(`/remove-plugin-test/${testFilePath}`, exportStatement); + addExportStatement(`${tempDir}/${testFilePath}`, exportStatement); }; const mkTestPluginDir = (testDirPath: string) => { @@ -111,36 +107,38 @@ const mkTestPluginDir = (testDirPath: string) => { }); }; -afterEach(() => { - mockFs.restore(); -}); - describe('removePlugin', () => { - beforeAll(() => { + beforeEach(() => { // Create temporary directory for all tests - createTemporaryPluginFolder(tempDir); + const appPath = paths.resolveTargetRoot('packages', 'app'); + mockFs({ + [tempDir]: { + 'package.json': mockFs.load(path.join(appPath, 'package.json')), + src: { + 'plugin.ts': mockFs.load(path.join(appPath, 'src', 'plugins.ts')), + }, + }, + }); }); afterAll(() => { - // Remove temporary directory - fse.removeSync(tempDir); + mockFs.restore(); }); describe('Remove Plugin Dependencies', () => { - const appPath = paths.resolveTargetRoot('packages', 'app'); const githubDir = paths.resolveTargetRoot('.github'); it('removes plugin references from /packages/app/package.json', async () => { // Set up test - const packageFilePath = path.join(appPath, 'package.json'); + const packageFilePath = `${tempDir}/package.json`; const testFilePath = 'test.json'; createTestPackageFile(testFilePath, packageFilePath); await removeReferencesFromAppPackage( - '/remove-plugin-test/test.json', + path.join(tempDir, testFilePath), testPluginName, ); const testFileContent = removeEmptyLines( - fse.readFileSync(`/remove-plugin-test/${testFilePath}`, 'utf8'), + fse.readFileSync(path.join(tempDir, testFilePath), 'utf8'), ); const packageFileContent = removeEmptyLines( @@ -150,14 +148,14 @@ describe('removePlugin', () => { }); it('removes plugin exports from /packages/app/src/packacge.json', async () => { const testFilePath = 'test.ts'; - const pluginsFilePaths = path.join(appPath, 'src', 'plugins.ts'); + const pluginsFilePaths = `${tempDir}/src/plugin.ts`; createTestPluginFile(testFilePath, pluginsFilePaths); await removeReferencesFromPluginsFile( - `/remove-plugin-test/${testFilePath}`, + path.join(tempDir, testFilePath), testPluginName, ); const testFileContent = removeEmptyLines( - fse.readFileSync(`/remove-plugin-test/${testFilePath}`, 'utf8'), + fse.readFileSync(path.join(tempDir, testFilePath), 'utf8'), ); const pluginsFileContent = removeEmptyLines( fse.readFileSync('/packages/app/src/plugin.ts', 'utf8'), @@ -167,16 +165,16 @@ describe('removePlugin', () => { it('removes codeOwners references', async () => { const testFileName = 'test'; - const testFilePath = `/remove-plugin-test/${testFileName}`; + const testFilePath = path.join(tempDir, testFileName); const codeownersPath = path.join(githubDir, 'CODEOWNERS'); const mockedCodeownersPath = '/.github/CODEOWNERS'; mockFs({ - '/remove-plugin-test': { + [tempDir]: { [testFileName]: '', }, '/.github': { - CODEOWNERS: fse.readFileSync(codeownersPath), + CODEOWNERS: mockFs.load(codeownersPath), }, }); fse.copySync(mockedCodeownersPath, testFilePath); @@ -204,34 +202,32 @@ describe('removePlugin', () => { describe('Removes Plugin Directory', () => { it('removes plugin directory from /plugins', async () => { - try { - mkTestPluginDir(testDirPath); - expect(fse.existsSync(testDirPath)).toBeTruthy(); - await removePluginDirectory(testDirPath); - expect(fse.existsSync(testDirPath)).toBeFalsy(); - } finally { - if (fse.existsSync(testDirPath)) fse.removeSync(testDirPath); - } + mkTestPluginDir(testDirPath); + expect(fse.existsSync(testDirPath)).toBeTruthy(); + await removePluginDirectory(testDirPath); + expect(fse.existsSync(testDirPath)).toBeFalsy(); }); }); describe('Removes System Link', () => { it('removes system link from @backstage', async () => { - const scopedDir = paths.resolveTargetRoot('node_modules', '@backstage'); - const testSymLinkPath = path.join( - scopedDir, - `plugin-${testPluginName}`, - ); - try { - mkTestPluginDir(testDirPath); - fse.ensureSymlinkSync(testSymLinkPath, testDirPath); + const symLink = `plugin-${testPluginName}`; + const testSymLinkPath = `/node_modules/@backstage/${symLink}`; - await removeSymLink(testSymLinkPath); - expect(fse.existsSync(testSymLinkPath)).toBeFalsy(); - } finally { - if (fse.existsSync(testDirPath)) fse.removeSync(testDirPath); - if (fse.existsSync(testSymLinkPath)) fse.removeSync(testSymLinkPath); - } + mkTestPluginDir(testDirPath); + + mockFs({ + '/node_modules': { + '@backstage': { + [symLink]: mockFs.symlink({ + path: testDirPath, + }), + }, + }, + }); + + await removeSymLink(testSymLinkPath); + expect(fse.existsSync(testSymLinkPath)).toBeFalsy(); }); }); }); From a48c2caefd50a2883444c5f6db8e965101da9842 Mon Sep 17 00:00:00 2001 From: Remi Date: Wed, 21 Oct 2020 11:06:39 +0200 Subject: [PATCH 5/9] test(cli): refactor mock-fs --- .../cli/src/commands/remove-plugin/removePlugin.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/commands/remove-plugin/removePlugin.test.ts b/packages/cli/src/commands/remove-plugin/removePlugin.test.ts index 43306939bb..7b9857a337 100644 --- a/packages/cli/src/commands/remove-plugin/removePlugin.test.ts +++ b/packages/cli/src/commands/remove-plugin/removePlugin.test.ts @@ -91,7 +91,7 @@ const createTestPluginFile = async ( .map(name => capitalize(name)) .join(''); const exportStatement = `export { default as ${pluginNameCapitalized}} from @backstage/plugin-${testPluginName}`; - addExportStatement(`${tempDir}/${testFilePath}`, exportStatement); + await addExportStatement(path.join(tempDir, testFilePath), exportStatement); }; const mkTestPluginDir = (testDirPath: string) => { @@ -130,7 +130,7 @@ describe('removePlugin', () => { it('removes plugin references from /packages/app/package.json', async () => { // Set up test - const packageFilePath = `${tempDir}/package.json`; + const packageFilePath = path.join(tempDir, 'package.json'); const testFilePath = 'test.json'; createTestPackageFile(testFilePath, packageFilePath); await removeReferencesFromAppPackage( @@ -148,7 +148,7 @@ describe('removePlugin', () => { }); it('removes plugin exports from /packages/app/src/packacge.json', async () => { const testFilePath = 'test.ts'; - const pluginsFilePaths = `${tempDir}/src/plugin.ts`; + const pluginsFilePaths = path.join(tempDir, 'src/plugin.ts'); createTestPluginFile(testFilePath, pluginsFilePaths); await removeReferencesFromPluginsFile( path.join(tempDir, testFilePath), From 93aaec54336428fe32021191da484315f03bea4a Mon Sep 17 00:00:00 2001 From: Remi Date: Wed, 21 Oct 2020 11:07:35 +0200 Subject: [PATCH 6/9] fix(cli): format --- packages/cli/src/lib/tasks.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/lib/tasks.test.ts b/packages/cli/src/lib/tasks.test.ts index a3ff3feeaa..1fb07d06fc 100644 --- a/packages/cli/src/lib/tasks.test.ts +++ b/packages/cli/src/lib/tasks.test.ts @@ -33,7 +33,7 @@ describe('templatingTask', () => { mockFs({ [tmplDir]: { - 'sub': { + sub: { 'version.txt.hbs': 'version: {{version}}', }, 'test.txt': 'testing', From 6c4a509bba299a11af42637fa468d0aa780d938b Mon Sep 17 00:00:00 2001 From: Remi Date: Wed, 21 Oct 2020 15:28:59 +0200 Subject: [PATCH 7/9] refactor(cli): createPlugin --- .../commands/create-plugin/createPlugin.test.ts | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/packages/cli/src/commands/create-plugin/createPlugin.test.ts b/packages/cli/src/commands/create-plugin/createPlugin.test.ts index 839bd4d433..356b315380 100644 --- a/packages/cli/src/commands/create-plugin/createPlugin.test.ts +++ b/packages/cli/src/commands/create-plugin/createPlugin.test.ts @@ -21,13 +21,15 @@ import os from 'os'; import del from 'del'; import { createTemporaryPluginFolder, movePlugin } from './createPlugin'; +const id = 'testPluginMock'; + describe('createPlugin', () => { afterAll(() => { mockFs.restore(); }); + describe('createPluginFolder', () => { it('should create a temporary plugin directory in the correct place', async () => { - const id = 'testPlugin'; const tempDir = path.join(os.tmpdir(), id); try { await createTemporaryPluginFolder(tempDir); @@ -39,8 +41,6 @@ describe('createPlugin', () => { }); it('should not create a temporary plugin directory if it already exists', async () => { - const id = 'testPluginMock'; - mockFs({ [id]: {}, }); @@ -53,18 +53,11 @@ describe('createPlugin', () => { describe('movePlugin', () => { it('should move the temporary plugin directory to its final place', async () => { - const id = 'testPluginMock'; - const tempDir = id; - const pluginDir = `/test-temp/plugins/${id}`; - mockFs({ [id]: {}, - 'test-temp': { - plugins: { - testPluginMock: {}, - }, - }, }); + const tempDir = id; + const pluginDir = `/test-temp/plugins/${id}`; await movePlugin(tempDir, pluginDir, id); await expect(fs.pathExists(pluginDir)).resolves.toBe(true); From a45cea627db4bc51a38db8364cd0db35b9ecef9d Mon Sep 17 00:00:00 2001 From: Remi Date: Thu, 22 Oct 2020 12:19:41 +0200 Subject: [PATCH 8/9] feat(cli): add @types/mock-fs dev-dependency --- packages/cli/package.json | 1 + yarn.lock | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/packages/cli/package.json b/packages/cli/package.json index dd9e82c325..2300612d3f 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -114,6 +114,7 @@ "@types/http-proxy": "^1.17.4", "@types/inquirer": "^7.3.1", "@types/mini-css-extract-plugin": "^0.9.1", + "@types/mock-fs": "^4.13.0", "@types/node": "^13.7.2", "@types/ora": "^3.2.0", "@types/react-dev-utils": "^9.0.4", diff --git a/yarn.lock b/yarn.lock index e6377107c2..69f2bbe335 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5206,6 +5206,13 @@ dependencies: "@types/node" "*" +"@types/mock-fs@^4.13.0": + version "4.13.0" + resolved "https://registry.npmjs.org/@types/mock-fs/-/mock-fs-4.13.0.tgz#b8b01cd2db588668b2532ecd21b1babd3fffb2c0" + integrity sha512-FUqxhURwqFtFBCuUj3uQMp7rPSQs//b3O9XecAVxhqS9y4/W8SIJEZFq2mmpnFVZBXwR/2OyPLE97CpyYiB8Mw== + dependencies: + "@types/node" "*" + "@types/morgan@^1.9.0": version "1.9.1" resolved "https://registry.npmjs.org/@types/morgan/-/morgan-1.9.1.tgz#6457872df95647c1dbc6b3741e8146b71ece74bf" From 4cc188a2c81f0e88c835b6da0faecca9e871ae06 Mon Sep 17 00:00:00 2001 From: Remi Date: Thu, 22 Oct 2020 12:20:47 +0200 Subject: [PATCH 9/9] test(cli): system link remove check --- .../src/commands/remove-plugin/removePlugin.test.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/cli/src/commands/remove-plugin/removePlugin.test.ts b/packages/cli/src/commands/remove-plugin/removePlugin.test.ts index 7b9857a337..076f54402b 100644 --- a/packages/cli/src/commands/remove-plugin/removePlugin.test.ts +++ b/packages/cli/src/commands/remove-plugin/removePlugin.test.ts @@ -16,6 +16,7 @@ import fse from 'fs-extra'; import path from 'path'; +import mockFs from 'mock-fs'; import { paths } from '../../lib/paths'; import { addExportStatement, capitalize } from '../create-plugin/createPlugin'; import { addCodeownersEntry } from '../../lib/codeowners'; @@ -27,8 +28,6 @@ import { removePluginFromCodeOwners, } from './removePlugin'; -const mockFs = require('mock-fs'); - const BACKSTAGE = `@backstage`; const testPluginName = 'yarn-test-package'; const testPluginPackage = `${BACKSTAGE}/plugin-${testPluginName}`; @@ -213,19 +212,22 @@ describe('removePlugin', () => { it('removes system link from @backstage', async () => { const symLink = `plugin-${testPluginName}`; const testSymLinkPath = `/node_modules/@backstage/${symLink}`; - - mkTestPluginDir(testDirPath); + const mockedTestDirPath = path.join('/plugins', testPluginName); mockFs({ + '/plugins': { + [testPluginName]: {}, + }, '/node_modules': { '@backstage': { [symLink]: mockFs.symlink({ - path: testDirPath, + path: mockedTestDirPath, }), }, }, }); + expect(fse.existsSync(testSymLinkPath)).toBeTruthy(); await removeSymLink(testSymLinkPath); expect(fse.existsSync(testSymLinkPath)).toBeFalsy(); });