From 3493ef76181ecf39eb0fc91869c5a5e46171dbbb Mon Sep 17 00:00:00 2001 From: Emily Xiong Date: Fri, 5 Jul 2024 13:09:54 -0700 Subject: [PATCH] fix(gradle): fix gradle exclude src/test (#26741) ## Current Behavior ## Expected Behavior ## Related Issue(s) Fixes # (cherry picked from commit 4ae16b361f2e7d670c9bc6427cb01fa8971803bb) --- packages/gradle/migrations.json | 6 ++ .../gradle/src/generators/init/init.spec.ts | 24 +++---- packages/gradle/src/generators/init/init.ts | 4 +- .../change-regex-test-production.spec.ts | 71 +++++++++++++++++++ .../19-4-1/change-regex-test-production.ts | 19 +++++ 5 files changed, 110 insertions(+), 14 deletions(-) create mode 100644 packages/gradle/src/migrations/19-4-1/change-regex-test-production.spec.ts create mode 100644 packages/gradle/src/migrations/19-4-1/change-regex-test-production.ts diff --git a/packages/gradle/migrations.json b/packages/gradle/migrations.json index 5439fbbaab6ea..57427b5653f6c 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 c1107825f4d51..09b6fbf2774e7 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 ae24c6831cfc1..bf35ce4dcc48a 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 0000000000000..8cf742779fd8e --- /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 0000000000000..c54401ebc29fd --- /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); + } +}