Skip to content

Commit

Permalink
fix(gradle): fix gradle exclude src/test (#26741)
Browse files Browse the repository at this point in the history
<!-- 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 #
  • Loading branch information
xiongemi authored Jul 5, 2024
1 parent 981eb30 commit 4ae16b3
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 14 deletions.
6 changes: 6 additions & 0 deletions packages/gradle/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {}
Expand Down
24 changes: 12 additions & 12 deletions packages/gradle/src/generators/init/init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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/**/*',
],
},
});
Expand All @@ -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",
],
}
Expand Down
4 changes: 2 additions & 2 deletions packages/gradle/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ allprojects {
}
}

function updateNxJsonConfiguration(tree: Tree) {
export function updateNxJsonConfiguration(tree: Tree) {
const nxJson = readNxJson(tree);

if (!nxJson.namedInputs) {
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
],
}
`);
});
});
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 4ae16b3

Please sign in to comment.