chore(core): add unit tests for not connected to nx cloud (#26885)
<!-- 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 #
This commit is contained in:
parent
adacd983b7
commit
aa7c5dc3ea
@ -1,6 +1,6 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`ci-workflow generator circleci pipeline should match snapshot 1`] = `
|
||||
exports[`ci-workflow generator connected to nxCloud circleci pipeline should match snapshot 1`] = `
|
||||
"version: 2.1
|
||||
|
||||
orbs:
|
||||
@ -25,6 +25,7 @@ jobs:
|
||||
- nx/set-shas:
|
||||
main-branch-name: 'main'
|
||||
|
||||
# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected
|
||||
- run: ./nx affected --base=$NX_BASE --head=$NX_HEAD -t test build
|
||||
|
||||
workflows:
|
||||
@ -36,7 +37,7 @@ workflows:
|
||||
"
|
||||
`;
|
||||
|
||||
exports[`ci-workflow generator github pipeline should match snapshot 1`] = `
|
||||
exports[`ci-workflow generator connected to nxCloud github pipeline should match snapshot 1`] = `
|
||||
"name: CI
|
||||
|
||||
on:
|
||||
@ -74,6 +75,89 @@ jobs:
|
||||
|
||||
- uses: nrwl/nx-set-shas@v4
|
||||
|
||||
# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected
|
||||
- run: ./nx affected -t test build
|
||||
"
|
||||
`;
|
||||
|
||||
exports[`ci-workflow generator not connected to nxCloud circleci pipeline should match snapshot 1`] = `
|
||||
"version: 2.1
|
||||
|
||||
orbs:
|
||||
nx: nrwl/nx@1.6.2
|
||||
|
||||
jobs:
|
||||
main:
|
||||
environment:
|
||||
# Configure the JVM and Gradle to avoid OOM errors
|
||||
_JAVA_OPTIONS: '-Xmx3g'
|
||||
GRADLE_OPTS: '-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=2'
|
||||
docker:
|
||||
- image: cimg/openjdk:17.0-node
|
||||
steps:
|
||||
- checkout
|
||||
|
||||
# This enables task distribution via Nx Cloud
|
||||
# Run this command as early as possible, before dependencies are installed
|
||||
# Learn more at https://nx.dev/ci/reference/nx-cloud-cli#npx-nxcloud-startcirun
|
||||
# Connect your workspace by running "nx connect" and uncomment this
|
||||
# - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-jvm" --stop-agents-after="build"
|
||||
|
||||
- nx/set-shas:
|
||||
main-branch-name: 'main'
|
||||
|
||||
# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected
|
||||
- run: ./nx affected --base=$NX_BASE --head=$NX_HEAD -t test build
|
||||
|
||||
workflows:
|
||||
version: 2
|
||||
|
||||
ci:
|
||||
jobs:
|
||||
- main
|
||||
"
|
||||
`;
|
||||
|
||||
exports[`ci-workflow generator not connected to nxCloud github pipeline should match snapshot 1`] = `
|
||||
"name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
pull_request:
|
||||
|
||||
permissions:
|
||||
actions: read
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
main:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
# This enables task distribution via Nx Cloud
|
||||
# Run this command as early as possible, before dependencies are installed
|
||||
# Learn more at https://nx.dev/ci/reference/nx-cloud-cli#npx-nxcloud-startcirun
|
||||
# Connect your workspace by running "nx connect" and uncomment this
|
||||
# - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-jvm" --stop-agents-after="build"
|
||||
|
||||
- name: Set up JDK 17 for x64
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
java-version: '17'
|
||||
distribution: 'temurin'
|
||||
architecture: x64
|
||||
|
||||
- name: Setup Gradle
|
||||
uses: gradle/gradle-build-action@v2
|
||||
|
||||
- uses: nrwl/nx-set-shas@v4
|
||||
|
||||
# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected
|
||||
- run: ./nx affected -t test build
|
||||
"
|
||||
`;
|
||||
|
||||
@ -25,7 +25,7 @@ jobs:
|
||||
main-branch-name: '<%= mainBranch %>'
|
||||
|
||||
<% for (const command of commands) { %>
|
||||
- run: <%= command %><% } %>
|
||||
<% if (command.command) { %>- run: <%= command.command %><% } else if (command.comment) { %><%= command.comment %><% } else {%>- run: <%= command %><% } %><% } %>
|
||||
|
||||
workflows:
|
||||
version: 2
|
||||
|
||||
@ -39,4 +39,4 @@ jobs:
|
||||
- uses: nrwl/nx-set-shas@v4
|
||||
|
||||
<% for (const command of commands) { %>
|
||||
- run: <%= command %><% } %>
|
||||
<% if (command.command) { %>- run: <%= command.command %><% } else if (command.comment) { %><%= command.comment %><% } else {%>- run: <%= command %><% } %><% } %>
|
||||
|
||||
@ -8,11 +8,34 @@ describe('ci-workflow generator', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
tree = createTreeWithEmptyWorkspace();
|
||||
});
|
||||
|
||||
describe.each([
|
||||
['connected to nxCloud', true],
|
||||
['not connected to nxCloud', false],
|
||||
] as const)(`%s`, (_, connectedToCloud) => {
|
||||
let nxCloudAccessToken: string;
|
||||
|
||||
beforeEach(() => {
|
||||
if (connectedToCloud) {
|
||||
const nxJson = readNxJson(tree);
|
||||
nxJson.nxCloudAccessToken = 'test';
|
||||
updateNxJson(tree, nxJson);
|
||||
} else {
|
||||
nxCloudAccessToken = process.env.NX_CLOUD_ACCESS_TOKEN;
|
||||
delete process.env.NX_CLOUD_ACCESS_TOKEN;
|
||||
}
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (connectedToCloud) {
|
||||
const nxJson = readNxJson(tree);
|
||||
delete nxJson.nxCloudAccessToken;
|
||||
updateNxJson(tree, nxJson);
|
||||
} else {
|
||||
process.env.NX_CLOUD_ACCESS_TOKEN = nxCloudAccessToken;
|
||||
}
|
||||
});
|
||||
describe.each([
|
||||
['github', '.github/workflows/ci.yml'],
|
||||
['circleci', '.circleci/config.yml'],
|
||||
@ -25,4 +48,5 @@ describe('ci-workflow generator', () => {
|
||||
expect(tree.read(output, 'utf-8')).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -11,22 +11,36 @@ import { join } from 'path';
|
||||
import { getNxCloudUrl, isNxCloudUsed } from 'nx/src/utils/nx-cloud-utils';
|
||||
import { deduceDefaultBase } from 'nx/src/utils/default-base';
|
||||
|
||||
function getCiCommands(ci: Schema['ci'], mainBranch: string): string[] {
|
||||
function getCiCommands(ci: Schema['ci']): Command[] {
|
||||
switch (ci) {
|
||||
case 'circleci': {
|
||||
return [`./nx affected --base=$NX_BASE --head=$NX_HEAD -t test build`];
|
||||
return [
|
||||
{
|
||||
comment: `# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected`,
|
||||
},
|
||||
{
|
||||
command: `./nx affected --base=$NX_BASE --head=$NX_HEAD -t test build`,
|
||||
},
|
||||
];
|
||||
}
|
||||
default: {
|
||||
return [`./nx affected -t test build`];
|
||||
return [
|
||||
{
|
||||
comment: `# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected`,
|
||||
},
|
||||
{ command: `./nx affected -t test build` },
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export type Command = { command: string } | { comment: string } | string;
|
||||
|
||||
export interface Schema {
|
||||
name: string;
|
||||
ci: 'github' | 'circleci';
|
||||
packageManager?: null;
|
||||
commands?: string[];
|
||||
commands?: Command[];
|
||||
}
|
||||
|
||||
export async function ciWorkflowGenerator(tree: Tree, schema: Schema) {
|
||||
@ -43,7 +57,7 @@ interface Substitutes {
|
||||
workflowFileName: string;
|
||||
packageManager: string;
|
||||
packageManagerPrefix: string;
|
||||
commands: string[];
|
||||
commands: Command[];
|
||||
nxCloudHost: string;
|
||||
connectedToCloud: boolean;
|
||||
}
|
||||
@ -64,7 +78,7 @@ function getTemplateData(tree: Tree, options: Schema): Substitutes {
|
||||
|
||||
const mainBranch = deduceDefaultBase();
|
||||
|
||||
const commands = options.commands ?? getCiCommands(options.ci, mainBranch);
|
||||
const commands = options.commands ?? getCiCommands(options.ci);
|
||||
|
||||
const connectedToCloud = isNxCloudUsed(readNxJson(tree));
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -35,15 +35,39 @@ describe('CI Workflow generator', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
|
||||
const nxJson = readNxJson(tree);
|
||||
nxJson.nxCloudAccessToken = 'test';
|
||||
updateNxJson(tree, nxJson);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vol.reset();
|
||||
});
|
||||
|
||||
describe.each([
|
||||
['connected to nxCloud', true],
|
||||
['not connected to nxCloud', false],
|
||||
] as const)(`%s`, (_, connectedToCloud) => {
|
||||
let nxCloudAccessToken: string;
|
||||
|
||||
beforeEach(() => {
|
||||
if (connectedToCloud) {
|
||||
const nxJson = readNxJson(tree);
|
||||
nxJson.nxCloudAccessToken = 'test';
|
||||
updateNxJson(tree, nxJson);
|
||||
} else {
|
||||
nxCloudAccessToken = process.env.NX_CLOUD_ACCESS_TOKEN;
|
||||
delete process.env.NX_CLOUD_ACCESS_TOKEN;
|
||||
}
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (connectedToCloud) {
|
||||
const nxJson = readNxJson(tree);
|
||||
delete nxJson.nxCloudAccessToken;
|
||||
updateNxJson(tree, nxJson);
|
||||
} else {
|
||||
process.env.NX_CLOUD_ACCESS_TOKEN = nxCloudAccessToken;
|
||||
}
|
||||
});
|
||||
|
||||
['npm', 'yarn', 'pnpm', 'bun'].forEach((packageManager: PackageManager) => {
|
||||
describe(`with ${packageManager}`, () => {
|
||||
beforeEach(() => {
|
||||
@ -97,7 +121,9 @@ describe('CI Workflow generator', () => {
|
||||
name: 'CI',
|
||||
});
|
||||
|
||||
expect(tree.read('bitbucket-pipelines.yml', 'utf-8')).toMatchSnapshot();
|
||||
expect(
|
||||
tree.read('bitbucket-pipelines.yml', 'utf-8')
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should prefix nx.json affected defaultBase with origin/ if ci is bitbucket-pipelines', async () => {
|
||||
@ -135,6 +161,7 @@ describe('CI Workflow generator', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('optional e2e', () => {
|
||||
beforeEach(() => {
|
||||
|
||||
@ -65,5 +65,6 @@ jobs:
|
||||
|
||||
# Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud
|
||||
# - script: <%= packageManagerPrefix %> nx-cloud record -- echo Hello World
|
||||
# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected
|
||||
- script: <%= packageManagerPrefix %> nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) lint test build<% if(hasE2E){ %>
|
||||
- script: <%= packageManagerPrefix %> nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) --parallel 1 e2e-ci<% } %>
|
||||
|
||||
@ -29,6 +29,7 @@ pipelines:
|
||||
- <%= packageManagerInstall %>
|
||||
|
||||
- <%= packageManagerPrefix %> nx-cloud record -- nx format:check
|
||||
# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected
|
||||
- <%= packageManagerPrefix %> nx affected --base=origin/<%= mainBranch %> -t lint test build<% if(hasE2E){ %>
|
||||
- <%= packageManagerPrefix %> nx affected --base=origin/<%= mainBranch %> --parallel 1 -t e2e-ci<% } %>
|
||||
|
||||
@ -55,4 +56,5 @@ pipelines:
|
||||
|
||||
# Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud
|
||||
# - <%= packageManagerPrefix %> nx-cloud record -- echo Hello World
|
||||
# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected
|
||||
- <%= packageManagerPrefix %> nx affected -t lint test build<% if(hasE2E){ %> e2e-ci<% } %> --base=HEAD~1
|
||||
|
||||
@ -34,6 +34,7 @@ jobs:
|
||||
|
||||
# Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud
|
||||
# - run: <%= packageManagerPrefix %> nx-cloud record -- echo Hello World
|
||||
# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected
|
||||
- run: <%= packageManagerPrefix %> nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build<% if(hasE2E){ %>
|
||||
- run: <%= packageManagerPrefix %> nx affected --base=$NX_BASE --head=$NX_HEAD --parallel 1 -t e2e-ci<% } %>
|
||||
|
||||
|
||||
@ -47,5 +47,6 @@ jobs:
|
||||
|
||||
# Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud
|
||||
# - run: <%= packageManagerPrefix %> nx-cloud record -- echo Hello World
|
||||
# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected
|
||||
- run: <%= packageManagerPrefix %> nx affected -t lint test build<% if(hasE2E){ %>
|
||||
- run: <%= packageManagerPrefix %> nx affected --parallel 1 -t e2e-ci<% } %>
|
||||
|
||||
@ -28,5 +28,6 @@ variables:
|
||||
|
||||
# Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud
|
||||
# - <%= packageManagerPrefix %> nx-cloud record -- echo Hello World
|
||||
# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected
|
||||
- <%= packageManagerPrefix %> nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build<% if(hasE2E){ %>
|
||||
- <%= packageManagerPrefix %> nx affected --base=$NX_BASE --head=$NX_HEAD --parallel 1 -t e2e-ci<% } %>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user