Skip to content

Commit 3493ef7

Browse files
xiongemiFrozenPandaz
authored andcommitted
fix(gradle): fix gradle exclude src/test (#26741)
<!-- 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 # (cherry picked from commit 4ae16b3)
1 parent 4eae2d2 commit 3493ef7

File tree

5 files changed

+110
-14
lines changed

5 files changed

+110
-14
lines changed

packages/gradle/migrations.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
"cli": "nx",
66
"description": "Add task projectReportAll to build.gradle file",
77
"factory": "./src/migrations/19-4-0/add-project-report-all"
8+
},
9+
"change-regex-production-test": {
10+
"version": "19.4.1-beta.0",
11+
"cli": "nx",
12+
"description": "This function changes !{projectRoot}/test/**/* in nx.json for production to !{projectRoot}/src/test/**/*",
13+
"factory": "./src/migrations/19-4-1/change-regex-test-production"
814
}
915
},
1016
"packageJsonUpdates": {}

packages/gradle/src/generators/init/init.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,16 @@ describe('@nx/gradle:init', () => {
8181
});
8282
const nxJson = readNxJson(tree);
8383
expect(nxJson.namedInputs).toMatchInlineSnapshot(`
84-
{
85-
"default": [
86-
"{projectRoot}/**/*",
87-
],
88-
"production": [
89-
"default",
90-
"!{projectRoot}/test/**/*",
91-
],
92-
}
93-
`);
84+
{
85+
"default": [
86+
"{projectRoot}/**/*",
87+
],
88+
"production": [
89+
"default",
90+
"!{projectRoot}/src/test/**/*",
91+
],
92+
}
93+
`);
9494
});
9595

9696
it('should not overwrite existing namedInputs', async () => {
@@ -102,7 +102,7 @@ describe('@nx/gradle:init', () => {
102102
'!{projectRoot}/cypress/**/*',
103103
'!{projectRoot}/**/*.cy.[jt]s?(x)',
104104
'!{projectRoot}/cypress.config.[jt]s',
105-
'!{projectRoot}/test/**/*',
105+
'!{projectRoot}/src/test/**/*',
106106
],
107107
},
108108
});
@@ -122,7 +122,7 @@ describe('@nx/gradle:init', () => {
122122
"!{projectRoot}/cypress/**/*",
123123
"!{projectRoot}/**/*.cy.[jt]s?(x)",
124124
"!{projectRoot}/cypress.config.[jt]s",
125-
"!{projectRoot}/test/**/*",
125+
"!{projectRoot}/src/test/**/*",
126126
"default",
127127
],
128128
}

packages/gradle/src/generators/init/init.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ allprojects {
146146
}
147147
}
148148

149-
function updateNxJsonConfiguration(tree: Tree) {
149+
export function updateNxJsonConfiguration(tree: Tree) {
150150
const nxJson = readNxJson(tree);
151151

152152
if (!nxJson.namedInputs) {
@@ -158,7 +158,7 @@ function updateNxJsonConfiguration(tree: Tree) {
158158
);
159159
const productionFileSet = nxJson.namedInputs.production ?? [];
160160
nxJson.namedInputs.production = Array.from(
161-
new Set([...productionFileSet, 'default', '!{projectRoot}/test/**/*'])
161+
new Set([...productionFileSet, 'default', '!{projectRoot}/src/test/**/*'])
162162
);
163163
updateNxJson(tree, nxJson);
164164
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { Tree, readNxJson } from '@nx/devkit';
2+
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
3+
import update from './change-regex-test-production';
4+
5+
describe('change-regex-test-production', () => {
6+
let tree: Tree;
7+
8+
beforeAll(() => {
9+
tree = createTreeWithEmptyWorkspace();
10+
});
11+
12+
it('should not add to the namedInputs if it does not exist', async () => {
13+
tree.write(
14+
'nx.json',
15+
JSON.stringify({ namedInputs: {}, plugins: ['@nx/gradle'] })
16+
);
17+
update(tree);
18+
expect(readNxJson(tree)).toMatchInlineSnapshot(`
19+
{
20+
"namedInputs": {},
21+
"plugins": [
22+
"@nx/gradle",
23+
],
24+
}
25+
`);
26+
});
27+
28+
it('should not add to the namedInputs production if it is empty', async () => {
29+
tree.write(
30+
'nx.json',
31+
JSON.stringify({
32+
namedInputs: { production: [] },
33+
plugins: ['@nx/gradle'],
34+
})
35+
);
36+
update(tree);
37+
expect(readNxJson(tree)).toMatchInlineSnapshot(`
38+
{
39+
"namedInputs": {
40+
"production": [],
41+
},
42+
"plugins": [
43+
"@nx/gradle",
44+
],
45+
}
46+
`);
47+
});
48+
49+
it('should remove !{projectRoot}/test/**/* from the namedInputs production', async () => {
50+
tree.write(
51+
'nx.json',
52+
JSON.stringify({
53+
namedInputs: { production: ['!{projectRoot}/test/**/*'] },
54+
plugins: ['@nx/gradle'],
55+
})
56+
);
57+
update(tree);
58+
expect(readNxJson(tree)).toMatchInlineSnapshot(`
59+
{
60+
"namedInputs": {
61+
"production": [
62+
"!{projectRoot}/src/test/**/*",
63+
],
64+
},
65+
"plugins": [
66+
"@nx/gradle",
67+
],
68+
}
69+
`);
70+
});
71+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Tree, readNxJson, updateNxJson } from '@nx/devkit';
2+
import { hasGradlePlugin } from '../../utils/has-gradle-plugin';
3+
4+
// This function changes !{projectRoot}/test/**/* in nx.json for production to !{projectRoot}/src/test/**/*
5+
export default function update(tree: Tree) {
6+
if (!hasGradlePlugin(tree)) {
7+
return;
8+
}
9+
const nxJson = readNxJson(tree);
10+
if (!nxJson) {
11+
return;
12+
}
13+
const production = nxJson.namedInputs?.production;
14+
if (production?.includes('!{projectRoot}/test/**/*')) {
15+
production[production.indexOf('!{projectRoot}/test/**/*')] =
16+
'!{projectRoot}/src/test/**/*';
17+
updateNxJson(tree, nxJson);
18+
}
19+
}

0 commit comments

Comments
 (0)