Skip to content

Commit

Permalink
fix(gradle): fix gradle exclude src/test
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Jun 27, 2024
1 parent 336d371 commit 7a9ce19
Show file tree
Hide file tree
Showing 5 changed files with 128 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,89 @@
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 add the namedInputs production if it does not exist', async () => {
tree.write(
'nx.json',
JSON.stringify({ namedInputs: {}, plugins: ['@nx/gradle'] })
);
update(tree);
expect(readNxJson(tree)).toMatchInlineSnapshot(`
{
"namedInputs": {
"default": [
"{projectRoot}/**/*",
],
"production": [
"default",
"!{projectRoot}/src/test/**/*",
],
},
"plugins": [
"@nx/gradle",
],
}
`);
});

it('should add 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": {
"default": [
"{projectRoot}/**/*",
],
"production": [
"default",
"!{projectRoot}/src/test/**/*",
],
},
"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": {
"default": [
"{projectRoot}/**/*",
],
"production": [
"default",
"!{projectRoot}/src/test/**/*",
],
},
"plugins": [
"@nx/gradle",
],
}
`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Tree, readNxJson } from '@nx/devkit';
import { hasGradlePlugin } from '../../utils/has-gradle-plugin';
import { updateNxJsonConfiguration } from '../../generators/init/init';

// 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.splice(production.indexOf('!{projectRoot}/test/**/*'), 1);
tree.write('nx.json', JSON.stringify(nxJson, null, 2));
}
updateNxJsonConfiguration(tree);
}
}

0 comments on commit 7a9ce19

Please sign in to comment.