feat(core): implicitDependency tests for mv & rm schematics

This commit is contained in:
Jo Hanna Pearce 2020-06-19 12:29:31 +01:00 committed by Jo Hanna Pearce
parent a4fa80a351
commit 06a7080132
5 changed files with 92 additions and 22 deletions

View File

@ -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();
});
});
});

View File

@ -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),

View File

@ -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',
]);
});
});

View File

@ -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 "$@"

View File

@ -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);