From 3997550c914d8c7c7cb73e444594671d1f84b7f6 Mon Sep 17 00:00:00 2001 From: Emily Xiong Date: Thu, 27 Jun 2024 17:25:06 -0400 Subject: [PATCH] fix(gradle): fix gradle exclude src/test --- 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 | 18 +++++ 5 files changed, 109 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 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..ba1d20b0151cc2 --- /dev/null +++ b/packages/gradle/src/migrations/19-4-1/change-regex-test-production.ts @@ -0,0 +1,18 @@ +import { Tree, readNxJson } 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; + } + if (tree.exists('nx.json')) { + const nxJson = readNxJson(tree); + const production = nxJson?.namedInputs?.production; + if (production?.includes('!{projectRoot}/test/**/*')) { + production[production.indexOf('!{projectRoot}/test/**/*')] = + '!{projectRoot}/src/test/**/*'; + tree.write('nx.json', JSON.stringify(nxJson, null, 2)); + } + } +}