From 06a7080132cc8524f2e541dbba489e0b9d777cf4 Mon Sep 17 00:00:00 2001 From: Jo Hanna Pearce Date: Fri, 19 Jun 2020 12:29:31 +0100 Subject: [PATCH] feat(core): implicitDependency tests for mv & rm schematics --- .../src/workspace-aux-commands.test.ts | 76 +++++++++++++++---- .../remove/lib/check-dependencies.ts | 3 +- .../remove/lib/update-nx-json.spec.ts | 24 +++++- scripts/e2e.sh | 2 +- scripts/e2e.ts | 9 ++- 5 files changed, 92 insertions(+), 22 deletions(-) diff --git a/e2e/workspace/src/workspace-aux-commands.test.ts b/e2e/workspace/src/workspace-aux-commands.test.ts index bddcc02573..a4fa50f11c 100644 --- a/e2e/workspace/src/workspace-aux-commands.test.ts +++ b/e2e/workspace/src/workspace-aux-commands.test.ts @@ -530,6 +530,7 @@ forEachCli((cli) => { it('should work for libraries', () => { const lib1 = uniq('mylib'); const lib2 = uniq('mylib'); + const lib3 = uniq('mylib'); newProject(); runCLI(`generate @nrwl/workspace:lib ${lib1}/data-access`); @@ -544,7 +545,7 @@ forEachCli((cli) => { ); /** - * Create a library which imports a class from the other lib + * Create a library which imports a class from lib1 */ runCLI(`generate @nrwl/workspace:lib ${lib2}/ui`); @@ -556,6 +557,19 @@ forEachCli((cli) => { export const fromLibTwo = () => fromLibOne(); }` ); + /** + * Create a library which has an implicit dependency on lib1 + */ + + runCLI(`generate @nrwl/workspace:lib ${lib3}`); + let nxJson = JSON.parse(readFile('nx.json')) as NxJson; + nxJson.projects[lib3].implicitDependencies = [`${lib1}-data-access`]; + updateFile(`nx.json`, JSON.stringify(nxJson)); + + /** + * Now try to move lib1 + */ + const moveOutput = runCLI( `generate @nrwl/workspace:move --project ${lib1}-data-access shared/${lib1}/data-access` ); @@ -611,11 +625,14 @@ forEachCli((cli) => { checkFilesExist(rootClassPath); expect(moveOutput).toContain('UPDATE nx.json'); - const nxJson = JSON.parse(readFile('nx.json')) as NxJson; + nxJson = JSON.parse(readFile('nx.json')) as NxJson; expect(nxJson.projects[`${lib1}-data-access`]).toBeUndefined(); expect(nxJson.projects[newName]).toEqual({ tags: [], }); + expect(nxJson.projects[lib3].implicitDependencies).toEqual([ + `shared-${lib1}-data-access`, + ]); expect(moveOutput).toContain('UPDATE tsconfig.json'); const rootTsConfig = readJson('tsconfig.json'); @@ -656,27 +673,58 @@ forEachCli((cli) => { * Tries creating then deleting a lib */ it('should work', () => { - const lib = uniq('mylib'); + const lib1 = uniq('mylib'); + const lib2 = uniq('mylib'); newProject(); - runCLI(`generate @nrwl/workspace:lib ${lib}`); - expect(exists(tmpProjPath(`libs/${lib}`))).toBeTruthy(); + runCLI(`generate @nrwl/workspace:lib ${lib1}`); + expect(exists(tmpProjPath(`libs/${lib1}`))).toBeTruthy(); - const removeOutput = runCLI( - `generate @nrwl/workspace:remove --project ${lib}` + /** + * Create a library which has an implicit dependency on lib1 + */ + + runCLI(`generate @nrwl/workspace:lib ${lib2}`); + let nxJson = JSON.parse(readFile('nx.json')) as NxJson; + nxJson.projects[lib2].implicitDependencies = [lib1]; + updateFile(`nx.json`, JSON.stringify(nxJson)); + + /** + * Try removing the project (should fail) + */ + + let error; + try { + runCLI(`generate @nrwl/workspace:remove --project ${lib1}`); + } catch (e) { + error = e; + } + + expect(error).toBeDefined(); + expect(error.stderr.toString()).toContain( + `${lib1} is still depended on by the following projects:\n${lib2}` ); - expect(removeOutput).toContain(`DELETE libs/${lib}`); - expect(exists(tmpProjPath(`libs/${lib}`))).toBeFalsy(); + /** + * Try force removing the project + */ - expect(removeOutput).toContain(`UPDATE nx.json`); - const nxJson = JSON.parse(readFile('nx.json')) as NxJson; - expect(nxJson.projects[`${lib}`]).toBeUndefined(); + const removeOutputForced = runCLI( + `generate @nrwl/workspace:remove --project ${lib1} --forceRemove` + ); - expect(removeOutput).toContain(`UPDATE ${workspace}.json`); + expect(removeOutputForced).toContain(`DELETE libs/${lib1}`); + expect(exists(tmpProjPath(`libs/${lib1}`))).toBeFalsy(); + + expect(removeOutputForced).toContain(`UPDATE nx.json`); + nxJson = JSON.parse(readFile('nx.json')) as NxJson; + expect(nxJson.projects[`${lib1}`]).toBeUndefined(); + expect(nxJson.projects[lib2].implicitDependencies).toEqual([]); + + expect(removeOutputForced).toContain(`UPDATE ${workspace}.json`); const workspaceJson = readJson(`${workspace}.json`); - expect(workspaceJson.projects[`${lib}`]).toBeUndefined(); + expect(workspaceJson.projects[`${lib1}`]).toBeUndefined(); }); }); }); diff --git a/packages/workspace/src/schematics/remove/lib/check-dependencies.ts b/packages/workspace/src/schematics/remove/lib/check-dependencies.ts index cef2708b50..e65607142d 100644 --- a/packages/workspace/src/schematics/remove/lib/check-dependencies.ts +++ b/packages/workspace/src/schematics/remove/lib/check-dependencies.ts @@ -40,7 +40,8 @@ export function checkDependencies(schema: Schema): Rule { if (ig.ignores(dir)) { return; } - tree.getDir(dir).visit((file) => { + + tree.getDir(dir).visit((file: string) => { files.push({ file: path.relative(workspaceDir, file), ext: path.extname(file), diff --git a/packages/workspace/src/schematics/remove/lib/update-nx-json.spec.ts b/packages/workspace/src/schematics/remove/lib/update-nx-json.spec.ts index e12302837f..c06083ad53 100644 --- a/packages/workspace/src/schematics/remove/lib/update-nx-json.spec.ts +++ b/packages/workspace/src/schematics/remove/lib/update-nx-json.spec.ts @@ -5,6 +5,7 @@ import { createEmptyWorkspace } from '@nrwl/workspace/testing'; import { callRule, runSchematic } from '../../../utils/testing'; import { Schema } from '../schema'; import { updateNxJson } from './update-nx-json'; +import { updateJsonInTree } from '@nrwl/workspace/src/utils/ast-utils'; describe('updateNxJson Rule', () => { let tree: UnitTestTree; @@ -15,13 +16,25 @@ describe('updateNxJson Rule', () => { }); it('should update nx.json', async () => { - tree = await runSchematic('lib', { name: 'my-lib' }, tree); + tree = await runSchematic('lib', { name: 'my-lib1' }, tree); + tree = await runSchematic('lib', { name: 'my-lib2' }, tree); let nxJson = readJsonInTree(tree, '/nx.json'); - expect(nxJson.projects['my-lib']).toBeDefined(); + expect(nxJson.projects['my-lib1']).toBeDefined(); + + tree = (await callRule( + updateJsonInTree('nx.json', (json) => { + json.projects['my-lib2'].implicitDependencies = [ + 'my-lib1', + 'my-other-lib', + ]; + return json; + }), + tree + )) as UnitTestTree; const schema: Schema = { - projectName: 'my-lib', + projectName: 'my-lib1', skipFormat: false, forceRemove: false, }; @@ -29,6 +42,9 @@ describe('updateNxJson Rule', () => { tree = (await callRule(updateNxJson(schema), tree)) as UnitTestTree; nxJson = readJsonInTree(tree, '/nx.json'); - expect(nxJson.projects['my-lib']).toBeUndefined(); + expect(nxJson.projects['my-lib1']).toBeUndefined(); + expect(nxJson.projects['my-lib2'].implicitDependencies).toEqual([ + 'my-other-lib', + ]); }); }); diff --git a/scripts/e2e.sh b/scripts/e2e.sh index 44764d79d7..4aac7b2f98 100755 --- a/scripts/e2e.sh +++ b/scripts/e2e.sh @@ -1,3 +1,3 @@ #!/usr/bin/env bash export SELECTED_CLI=$SELECTED_CLI -ts-node --project scripts/tsconfig.e2e.json ./scripts/e2e.ts $1 +ts-node --project scripts/tsconfig.e2e.json ./scripts/e2e.ts "$@" diff --git a/scripts/e2e.ts b/scripts/e2e.ts index 3d5faf1197..e20269528e 100755 --- a/scripts/e2e.ts +++ b/scripts/e2e.ts @@ -86,6 +86,11 @@ export async function setup() { async function runTest() { let selectedProjects = process.argv[2]; + let testNamePattern = ''; + if (process.argv[3] === '-t' || process.argv[3] == '--testNamePattern') { + testNamePattern = `--testNamePattern "${process.argv[4]}"`; + } + if (process.argv[3] === 'affected') { const affected = execSync( `nx print-affected --base=origin/master --select=projects` @@ -116,7 +121,7 @@ async function runTest() { console.log('No tests to run'); } else if (selectedProjects) { execSync( - `node --max-old-space-size=4000 ./node_modules/.bin/nx run-many --target=e2e --projects=${selectedProjects}`, + `node --max-old-space-size=4000 ./node_modules/.bin/nx run-many --target=e2e --projects=${selectedProjects} ${testNamePattern}`, { stdio: [0, 1, 2], env: { ...process.env, NX_TERMINAL_CAPTURE_STDERR: 'true' }, @@ -165,8 +170,8 @@ process.on('SIGINT', () => cleanUp(1)); runTest() .then(() => { - process.exit(0); console.log('done'); + process.exit(0); }) .catch((e) => { console.error('error', e);