diff --git a/packages/gradle/migrations.json b/packages/gradle/migrations.json index 5439fbbaab6ea6..57427b5653f6c5 100644 --- a/packages/gradle/migrations.json +++ b/packages/gradle/migrations.json @@ -5,6 +5,12 @@ "cli": "nx", "description": "Add task projectReportAll to build.gradle file", "factory": "./src/migrations/19-4-0/add-project-report-all" + }, + "change-regex-production-test": { + "version": "19.4.1-beta.0", + "cli": "nx", + "description": "This function changes !{projectRoot}/test/**/* in nx.json for production to !{projectRoot}/src/test/**/*", + "factory": "./src/migrations/19-4-1/change-regex-test-production" } }, "packageJsonUpdates": {} diff --git a/packages/gradle/src/generators/init/init.spec.ts b/packages/gradle/src/generators/init/init.spec.ts index c1107825f4d512..09b6fbf2774e7d 100644 --- a/packages/gradle/src/generators/init/init.spec.ts +++ b/packages/gradle/src/generators/init/init.spec.ts @@ -81,16 +81,16 @@ describe('@nx/gradle:init', () => { }); const nxJson = readNxJson(tree); expect(nxJson.namedInputs).toMatchInlineSnapshot(` - { - "default": [ - "{projectRoot}/**/*", - ], - "production": [ - "default", - "!{projectRoot}/test/**/*", - ], - } - `); + { + "default": [ + "{projectRoot}/**/*", + ], + "production": [ + "default", + "!{projectRoot}/src/test/**/*", + ], + } + `); }); it('should not overwrite existing namedInputs', async () => { @@ -102,7 +102,7 @@ describe('@nx/gradle:init', () => { '!{projectRoot}/cypress/**/*', '!{projectRoot}/**/*.cy.[jt]s?(x)', '!{projectRoot}/cypress.config.[jt]s', - '!{projectRoot}/test/**/*', + '!{projectRoot}/src/test/**/*', ], }, }); @@ -122,7 +122,7 @@ describe('@nx/gradle:init', () => { "!{projectRoot}/cypress/**/*", "!{projectRoot}/**/*.cy.[jt]s?(x)", "!{projectRoot}/cypress.config.[jt]s", - "!{projectRoot}/test/**/*", + "!{projectRoot}/src/test/**/*", "default", ], } diff --git a/packages/gradle/src/generators/init/init.ts b/packages/gradle/src/generators/init/init.ts index ae24c6831cfc1d..bf35ce4dcc48af 100644 --- a/packages/gradle/src/generators/init/init.ts +++ b/packages/gradle/src/generators/init/init.ts @@ -146,7 +146,7 @@ allprojects { } } -function updateNxJsonConfiguration(tree: Tree) { +export function updateNxJsonConfiguration(tree: Tree) { const nxJson = readNxJson(tree); if (!nxJson.namedInputs) { @@ -158,7 +158,7 @@ function updateNxJsonConfiguration(tree: Tree) { ); const productionFileSet = nxJson.namedInputs.production ?? []; nxJson.namedInputs.production = Array.from( - new Set([...productionFileSet, 'default', '!{projectRoot}/test/**/*']) + new Set([...productionFileSet, 'default', '!{projectRoot}/src/test/**/*']) ); updateNxJson(tree, nxJson); } diff --git a/packages/gradle/src/migrations/19-4-1/change-regex-test-production.spec.ts b/packages/gradle/src/migrations/19-4-1/change-regex-test-production.spec.ts new file mode 100644 index 00000000000000..8cf742779fd8e9 --- /dev/null +++ b/packages/gradle/src/migrations/19-4-1/change-regex-test-production.spec.ts @@ -0,0 +1,71 @@ +import { Tree, readNxJson } from '@nx/devkit'; +import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; +import update from './change-regex-test-production'; + +describe('change-regex-test-production', () => { + let tree: Tree; + + beforeAll(() => { + tree = createTreeWithEmptyWorkspace(); + }); + + it('should not add to the namedInputs if it does not exist', async () => { + tree.write( + 'nx.json', + JSON.stringify({ namedInputs: {}, plugins: ['@nx/gradle'] }) + ); + update(tree); + expect(readNxJson(tree)).toMatchInlineSnapshot(` + { + "namedInputs": {}, + "plugins": [ + "@nx/gradle", + ], + } + `); + }); + + it('should not add to the namedInputs production if it is empty', async () => { + tree.write( + 'nx.json', + JSON.stringify({ + namedInputs: { production: [] }, + plugins: ['@nx/gradle'], + }) + ); + update(tree); + expect(readNxJson(tree)).toMatchInlineSnapshot(` + { + "namedInputs": { + "production": [], + }, + "plugins": [ + "@nx/gradle", + ], + } + `); + }); + + it('should remove !{projectRoot}/test/**/* from the namedInputs production', async () => { + tree.write( + 'nx.json', + JSON.stringify({ + namedInputs: { production: ['!{projectRoot}/test/**/*'] }, + plugins: ['@nx/gradle'], + }) + ); + update(tree); + expect(readNxJson(tree)).toMatchInlineSnapshot(` + { + "namedInputs": { + "production": [ + "!{projectRoot}/src/test/**/*", + ], + }, + "plugins": [ + "@nx/gradle", + ], + } + `); + }); +}); diff --git a/packages/gradle/src/migrations/19-4-1/change-regex-test-production.ts b/packages/gradle/src/migrations/19-4-1/change-regex-test-production.ts new file mode 100644 index 00000000000000..c54401ebc29fd6 --- /dev/null +++ b/packages/gradle/src/migrations/19-4-1/change-regex-test-production.ts @@ -0,0 +1,19 @@ +import { Tree, readNxJson, updateNxJson } from '@nx/devkit'; +import { hasGradlePlugin } from '../../utils/has-gradle-plugin'; + +// This function changes !{projectRoot}/test/**/* in nx.json for production to !{projectRoot}/src/test/**/* +export default function update(tree: Tree) { + if (!hasGradlePlugin(tree)) { + return; + } + const nxJson = readNxJson(tree); + if (!nxJson) { + return; + } + const production = nxJson.namedInputs?.production; + if (production?.includes('!{projectRoot}/test/**/*')) { + production[production.indexOf('!{projectRoot}/test/**/*')] = + '!{projectRoot}/src/test/**/*'; + updateNxJson(tree, nxJson); + } +}