Skip to content

Commit

Permalink
chore(core): add unit tests for not connected to nx cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Jul 12, 2024
1 parent 0b0db78 commit 5396c27
Show file tree
Hide file tree
Showing 12 changed files with 1,597 additions and 338 deletions.
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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
"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %><% } %><% } %>
46 changes: 35 additions & 11 deletions packages/gradle/src/generators/ci-workflow/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,45 @@ describe('ci-workflow generator', () => {

beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
const nxJson = readNxJson(tree);
nxJson.nxCloudAccessToken = 'test';
updateNxJson(tree, nxJson);
});

describe.each([
['github', '.github/workflows/ci.yml'],
['circleci', '.circleci/config.yml'],
] as const)(`%s pipeline`, (ciProvider, output) => {
it('should match snapshot', async () => {
await ciWorkflowGenerator(tree, {
name: 'CI',
ci: ciProvider,
['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'],
] as const)(`%s pipeline`, (ciProvider, output) => {
it('should match snapshot', async () => {
await ciWorkflowGenerator(tree, {
name: 'CI',
ci: ciProvider,
});
expect(tree.read(output, 'utf-8')).toMatchSnapshot();
});
expect(tree.read(output, 'utf-8')).toMatchSnapshot();
});
});
});
26 changes: 20 additions & 6 deletions packages/gradle/src/generators/ci-workflow/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -43,7 +57,7 @@ interface Substitutes {
workflowFileName: string;
packageManager: string;
packageManagerPrefix: string;
commands: string[];
commands: Command[];
nxCloudHost: string;
connectedToCloud: boolean;
}
Expand All @@ -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));

Expand Down
Loading

0 comments on commit 5396c27

Please sign in to comment.