nx/packages/plugin/src/migrations/update-16-0-0/cli-in-schema-json.spec.ts
Jack Hsu 27edf71cef
feat(misc): make directory a required option for generators (#28093)
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #

---------

Co-authored-by: Colum Ferry <cferry09@gmail.com>
Co-authored-by: Nicholas Cunningham <ndcunningham@gmail.com>
2024-10-01 09:29:44 -04:00

282 lines
8.4 KiB
TypeScript

import 'nx/src/internal-testing-utils/mock-project-graph';
import {
ExecutorsJson,
GeneratorsJson,
joinPathFragments,
MigrationsJson,
readJson,
readProjectConfiguration,
Tree,
updateJson,
writeJson,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { Linter } from '@nx/eslint';
import { assertRunsAgainstNxRepo } from '@nx/devkit/internal-testing-utils';
import { PackageJson } from 'nx/src/utils/package-json';
import executorGenerator from '../../generators/executor/executor';
import generatorGenerator from '../../generators/generator/generator';
import pluginGenerator from '../../generators/plugin/plugin';
import { updateCliPropsForPlugins } from './cli-in-schema-json';
describe('updateCliPropsForPlugins', () => {
let originalEnv: string;
beforeEach(() => {
originalEnv = process.env.NX_ADD_PLUGINS;
process.env.NX_ADD_PLUGINS = 'false';
});
afterAll(() => {
process.env.NX_ADD_PLUGINS = originalEnv;
});
it('should move non-nx generators to schematics for migrations.json', async () => {
const tree = createTreeWithEmptyWorkspace();
const { root } = await createPlugin(tree);
updatePluginPackageJson(tree, {
'nx-migrations': 'migrations.json',
});
writeJson<MigrationsJson>(
tree,
joinPathFragments(root, 'migrations.json'),
{
version: '1.0.0',
generators: {
'migration-1': {
version: '1.0.0',
description: 'My Plugin 1',
factory: './migrations/my-plugin-1',
},
},
}
);
await updateCliPropsForPlugins(tree);
const updated = readJson<MigrationsJson>(
tree,
joinPathFragments(root, 'migrations.json')
);
expect(updated.generators).not.toHaveProperty('migration-1');
expect(updated.schematics).toHaveProperty('migration-1');
});
it('should move nx generators to generators for migrations.json', async () => {
const tree = createTreeWithEmptyWorkspace();
const { root } = await createPlugin(tree);
updatePluginPackageJson(tree, {
'nx-migrations': 'migrations.json',
});
writeJson<MigrationsJson>(
tree,
joinPathFragments(root, 'migrations.json'),
{
version: '1.0.0',
schematics: {
'migration-1': {
version: '1.0.0',
description: 'My Plugin 1',
factory: './migrations/my-plugin-1',
cli: 'nx',
},
},
}
);
await updateCliPropsForPlugins(tree);
const updated = readJson<MigrationsJson>(
tree,
joinPathFragments(root, 'migrations.json')
);
expect(updated.schematics).not.toHaveProperty('migration-1');
expect(updated.generators).toHaveProperty('migration-1');
});
it('should move both nx generators to generators and non-nx schematics to schematics for migrations.json', async () => {
const tree = createTreeWithEmptyWorkspace();
const { root } = await createPlugin(tree);
updatePluginPackageJson(tree, {
'nx-migrations': 'migrations.json',
});
writeJson<MigrationsJson>(
tree,
joinPathFragments(root, 'migrations.json'),
{
version: '1.0.0',
schematics: {
'migration-1': {
version: '1.0.0',
description: 'My Plugin 1',
factory: './migrations/my-plugin-1',
cli: 'nx',
},
'migration-2': {
version: '1.0.0',
description: 'My Plugin 2',
factory: './migrations/my-plugin-2',
},
},
generators: {
'migration-3': {
version: '1.0.0',
description: 'My Plugin 3',
factory: './migrations/my-plugin-3',
cli: 'nx',
},
'migration-4': {
version: '1.0.0',
description: 'My Plugin 4',
factory: './migrations/my-plugin-4',
},
},
}
);
await updateCliPropsForPlugins(tree);
const updated = readJson<MigrationsJson>(
tree,
joinPathFragments(root, 'migrations.json')
);
expect(updated.schematics).not.toHaveProperty('migration-1');
expect(updated.generators).toHaveProperty('migration-1');
expect(updated.schematics).toHaveProperty('migration-2');
expect(updated.generators).not.toHaveProperty('migration-2');
expect(updated.schematics).not.toHaveProperty('migration-3');
expect(updated.generators).toHaveProperty('migration-3');
expect(updated.schematics).toHaveProperty('migration-4');
expect(updated.generators).not.toHaveProperty('migration-4');
});
it('should remove cli property from executors', async () => {
const tree = createTreeWithEmptyWorkspace();
const { root, name } = await createPlugin(tree);
await executorGenerator(tree, {
name: 'my-executor',
path: `${name}/src/executors/my-executor`,
unitTestRunner: 'jest',
includeHasher: false,
});
const schemaPath = joinPathFragments(
root,
'src/executors/my-executor/schema.json'
);
updateJson(tree, schemaPath, (schema) => {
schema.cli = 'nx';
return schema;
});
await updateCliPropsForPlugins(tree);
const updated = readJson(tree, schemaPath);
expect(updated).not.toHaveProperty('cli');
});
it('should remove cli property from builders', async () => {
const tree = createTreeWithEmptyWorkspace();
const { root, name } = await createPlugin(tree);
await executorGenerator(tree, {
name: 'my-executor',
path: `${name}/src/executors/my-executor`,
unitTestRunner: 'jest',
includeHasher: false,
});
updateJson<ExecutorsJson>(
tree,
joinPathFragments(root, 'executors.json'),
(json) => {
json.builders = json.executors;
delete json.builders;
return json;
}
);
const schemaPath = joinPathFragments(
root,
'src/executors/my-executor/schema.json'
);
updateJson(tree, schemaPath, (schema) => {
schema.cli = 'nx';
return schema;
});
await updateCliPropsForPlugins(tree);
const updated = readJson(tree, schemaPath);
expect(updated).not.toHaveProperty('cli');
});
it('should remove cli property from generators', async () => {
const tree = createTreeWithEmptyWorkspace();
const { root, name } = await createPlugin(tree);
await generatorGenerator(tree, {
name: 'my-generator',
path: `${name}/src/generators/my-generator`,
unitTestRunner: 'jest',
});
const schemaPath = joinPathFragments(
root,
'src/generators/my-generator/schema.json'
);
updateJson(tree, schemaPath, (schema) => {
schema.cli = 'nx';
return schema;
});
await updateCliPropsForPlugins(tree);
const updated = readJson(tree, schemaPath);
expect(updated).not.toHaveProperty('cli');
});
it('should remove cli property from schematics', async () => {
const tree = createTreeWithEmptyWorkspace();
const { root, name } = await createPlugin(tree);
await generatorGenerator(tree, {
name: 'my-schematic',
path: `${name}/src/generators/my-schematic`,
unitTestRunner: 'jest',
});
updateJson<GeneratorsJson>(
tree,
joinPathFragments(root, 'generators.json'),
(json) => {
json.schematics = json.generators;
delete json.generators;
return json;
}
);
const schemaPath = joinPathFragments(
root,
'src/generators/my-schematic/schema.json'
);
updateJson(tree, schemaPath, (schema) => {
schema.cli = 'nx';
return schema;
});
await updateCliPropsForPlugins(tree);
const updated = readJson(tree, schemaPath);
expect(updated).not.toHaveProperty('cli');
});
assertRunsAgainstNxRepo(updateCliPropsForPlugins);
});
async function createPlugin(tree: Tree) {
await pluginGenerator(tree, {
directory: 'my-plugin',
compiler: 'tsc',
linter: Linter.EsLint,
unitTestRunner: 'jest',
skipFormat: true,
skipLintChecks: false,
skipTsConfig: false,
});
return readProjectConfiguration(tree, 'my-plugin');
}
function updatePluginPackageJson(
tree: Tree,
packageJsonProps: Partial<PackageJson>
) {
const { root } = readProjectConfiguration(tree, 'my-plugin');
updateJson(tree, root + '/package.json', (json) => {
const base = { json, ...packageJsonProps };
for (const prop in base) {
if (base[prop] === null || base[prop] === undefined) {
delete json[prop];
}
}
return base;
});
}