Skip to content

Commit

Permalink
Merge 4e0c034 into 138e180
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton authored Oct 16, 2024
2 parents 138e180 + 4e0c034 commit 04ab516
Show file tree
Hide file tree
Showing 38 changed files with 385 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`nx-plugin > should NOT add config targets dynamically if the project is
{
"code-pushup": {
"configurations": {},
"executor": "@code-pushup/nx-plugin:autorun",
"executor": "@code-pushup/nx-plugin:cli",
"options": {},
},
}
Expand All @@ -26,7 +26,7 @@ exports[`nx-plugin > should add executor target dynamically if the project is co
{
"code-pushup": {
"configurations": {},
"executor": "@code-pushup/nx-plugin:autorun",
"executor": "@code-pushup/nx-plugin:cli",
"options": {},
},
}
Expand Down
97 changes: 85 additions & 12 deletions e2e/nx-plugin-e2e/tests/executor-cli.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
materializeTree,
} from '@code-pushup/test-nx-utils';
import { teardownTestFolder } from '@code-pushup/test-setup';
import { removeColorCodes } from '@code-pushup/test-utils';
import { executeProcess } from '@code-pushup/utils';
import { osAgnosticPath, removeColorCodes } from '@code-pushup/test-utils';
import { executeProcess, readJsonFile } from '@code-pushup/utils';

function relativePathToCwd(testDir: string): string {
return relative(join(process.cwd(), testDir), process.cwd());
Expand All @@ -27,7 +27,7 @@ async function addTargetToWorkspace(
targets: {
...projectCfg.targets,
['code-pushup']: {
executor: '@code-pushup/nx-plugin:autorun',
executor: '@code-pushup/nx-plugin:cli',
},
},
});
Expand All @@ -37,19 +37,27 @@ async function addTargetToWorkspace(
plugins: [
{
// @TODO replace with inline plugin
fileImports: `import {customPlugin} from "${join(
relativePathToCwd(cwd),
pathRelativeToPackage,
'dist/testing/test-utils',
fileImports: `import {customPlugin} from "${osAgnosticPath(
join(
relativePathToCwd(cwd),
pathRelativeToPackage,
'dist/testing/test-utils',
),
)}";`,
codeStrings: 'customPlugin()',
},
],
upload: {
server: 'https://dummy-server.dev',
organization: 'dummy-organization',
apiKey: 'dummy-api-key',
project: 'dummy-project',
},
});
await materializeTree(tree, cwd);
}

describe('executor autorun', () => {
describe('executor command', () => {
let tree: Tree;
const project = 'my-lib';
const baseDir = 'tmp/e2e/nx-plugin-e2e/__test__/executor/cli';
Expand All @@ -62,10 +70,9 @@ describe('executor autorun', () => {
await teardownTestFolder(baseDir);
});

it('should execute autorun executor', async () => {
const cwd = join(baseDir, 'execute-dynamic-executor');
it('should execute no specific command by default', async () => {
const cwd = join(baseDir, 'execute-default-command');
await addTargetToWorkspace(tree, { cwd, project });

const { stdout, code } = await executeProcess({
command: 'npx',
args: ['nx', 'run', `${project}:code-pushup`, '--dryRun'],
Expand All @@ -74,6 +81,72 @@ describe('executor autorun', () => {

expect(code).toBe(0);
const cleanStdout = removeColorCodes(stdout);
expect(cleanStdout).toContain('nx run my-lib:code-pushup --dryRun');
expect(cleanStdout).toContain('nx run my-lib:code-pushup');
});

it('should execute print-config executor', async () => {
const cwd = join(baseDir, 'execute-print-config-command');
await addTargetToWorkspace(tree, { cwd, project });

const { stdout, code } = await executeProcess({
command: 'npx',
args: ['nx', 'run', `${project}:code-pushup`, 'print-config'],
cwd,
});

expect(code).toBe(0);
const cleanStdout = removeColorCodes(stdout);
expect(cleanStdout).toContain('nx run my-lib:code-pushup print-config');

await expect(() =>
readJsonFile(join(cwd, '.code-pushup', project, 'report.json')),
).rejects.toThrow('');
});

it('should execute collect executor and add report to sub folder named by project', async () => {
const cwd = join(baseDir, 'execute-collect-command');
await addTargetToWorkspace(tree, { cwd, project });

const { stdout, code } = await executeProcess({
command: 'nx',
args: ['run', `${project}:code-pushup`, 'collect'],
cwd,
});

expect(code).toBe(0);
const cleanStdout = removeColorCodes(stdout);
expect(cleanStdout).toContain('nx run my-lib:code-pushup collect');

const report = await readJsonFile(
join(cwd, '.code-pushup', project, 'report.json'),
);
expect(report).toStrictEqual(
expect.objectContaining({
plugins: [
expect.objectContaining({
slug: 'good-feels',
audits: [
expect.objectContaining({
displayValue: '✅ Perfect! 👌',
slug: 'always-perfect',
}),
],
}),
],
}),
);
});

it('should execute upload executor to throw if no report is present', async () => {
const cwd = join(baseDir, 'execute-upload-command');
await addTargetToWorkspace(tree, { cwd, project });

await expect(
executeProcess({
command: 'npx',
args: ['nx', 'run', `${project}:code-pushup`, 'upload'],
cwd,
}),
).rejects.toThrow(/report.json/);
});
});
8 changes: 4 additions & 4 deletions e2e/nx-plugin-e2e/tests/generator-configuration.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('nx-plugin g configuration', () => {
expect.objectContaining({
targets: expect.objectContaining({
'code-pushup': {
executor: '@code-pushup/nx-plugin:autorun',
executor: '@code-pushup/nx-plugin:cli',
},
}),
}),
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('nx-plugin g configuration', () => {
expect.objectContaining({
targets: expect.objectContaining({
'code-pushup': {
executor: '@code-pushup/nx-plugin:autorun',
executor: '@code-pushup/nx-plugin:cli',
},
}),
}),
Expand Down Expand Up @@ -149,7 +149,7 @@ describe('nx-plugin g configuration', () => {
expect.objectContaining({
targets: expect.objectContaining({
'code-pushup': {
executor: '@code-pushup/nx-plugin:autorun',
executor: '@code-pushup/nx-plugin:cli',
},
}),
}),
Expand Down Expand Up @@ -193,7 +193,7 @@ describe('nx-plugin g configuration', () => {
expect.objectContaining({
targets: expect.not.objectContaining({
'code-pushup': {
executor: '@code-pushup/nx-plugin:autorun',
executor: '@code-pushup/nx-plugin:cli',
},
}),
}),
Expand Down
17 changes: 13 additions & 4 deletions e2e/nx-plugin-e2e/tests/plugin-create-nodes.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ describe('nx-plugin', () => {
expect(projectJson.targets).toStrictEqual({
['code-pushup']: {
configurations: {},
executor: `@code-pushup/nx-plugin:autorun`,
executor: `@code-pushup/nx-plugin:cli`,
options: {},
},
});
Expand Down Expand Up @@ -174,16 +174,25 @@ describe('nx-plugin', () => {
codeStrings: 'customPlugin()',
},
],
upload: {
server: 'http://staging.code-pushup.dev',
organization: 'code-pushup',
apiKey: '12345678',
},
});

await materializeTree(tree, cwd);

const { stdout } = await executeProcess({
const { stdout, stderr } = await executeProcess({
command: 'npx',
args: ['nx', 'run', `${project}:code-pushup`, '--dryRun'],
cwd,
});

const cleanStderr = removeColorCodes(stderr);
// @TODO create test environment for working plugin. This here misses package-lock.json to execute correctly
expect(cleanStderr).toContain('DryRun execution of: npx @code-pushup/cli');

const cleanStdout = removeColorCodes(stdout);
expect(cleanStdout).toContain(
'NX Successfully ran target code-pushup for project my-lib',
Expand All @@ -208,7 +217,7 @@ describe('nx-plugin', () => {

expect(projectJson.targets).toStrictEqual({
['code-pushup']: expect.objectContaining({
executor: 'XYZ:autorun',
executor: 'XYZ:cli',
}),
});
});
Expand All @@ -231,7 +240,7 @@ describe('nx-plugin', () => {

expect(projectJson.targets).toStrictEqual({
['code-pushup']: expect.objectContaining({
executor: `@code-pushup/nx-plugin:autorun`,
executor: `@code-pushup/nx-plugin:cli`,
options: {
projectPrefix: 'cli',
},
Expand Down
2 changes: 1 addition & 1 deletion e2e/nx-plugin-e2e/vite.config.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default defineConfig({
cacheDir: '../../node_modules/.vite/nx-plugin-e2e',
test: {
reporters: ['basic'],
testTimeout: 60_000,
testTimeout: 160_000,
globals: true,
alias: tsconfigPathAliases(),
pool: 'threads',
Expand Down
3 changes: 3 additions & 0 deletions global-setup.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { rm, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { setup as globalSetup } from './global-setup';
import { setupTestFolder, teardownTestFolder } from './testing/test-setup/src';
Expand Down Expand Up @@ -35,6 +36,7 @@ export async function setup() {

// package publish
const { registry } = activeRegistry.registryData;
await writeFile('.npmrc', `@code-pushup:registry=${registry}`);
try {
console.info('Publish packages');
nxRunManyPublish({
Expand Down Expand Up @@ -64,5 +66,6 @@ export async function teardown() {
stopLocalRegistry(stop);
nxRunManyNpmUninstall({ parallel: 1 });
}
await rm('.npmrc');
await teardownTestFolder(e2eDir);
}
4 changes: 2 additions & 2 deletions global-setup.verdaccio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ export async function setup() {
export async function teardown() {
// NOTICE - Time saving optimization
// We skip uninstalling packages as the folder is deleted anyway
// comment out to see the folder and web interface
// await nxStopVerdaccioAndTeardownEnv(activeRegistry);

await nxStopVerdaccioAndTeardownEnv(activeRegistry);
}
49 changes: 47 additions & 2 deletions packages/nx-plugin/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
# @code-pushup/nx-plugin

### Plugin

Register this plugin in your `nx.json` to leverage a set of generators and executors to integrate Code PushUp into a Nx workspace.

#### Registration

```jsonc
// nx.json
{
//...
"plugins": ["@code-pushup/nx-plugin"]
}
```

Resulting targets:

- `nx run <project-name>:code-pushup--configuration` (no config file present)
- `nx run <project-name>:code-pushup` (`code-pushup.config.{ts,mjs,js}` is present)

### Generators

#### Init

Install JS packages and register plugin.
See [init docs](./src/generators/init/README.md) for details
See [init generator docs](./src/generators/init/README.md) for details

Examples:

Expand All @@ -15,9 +34,35 @@ Examples:
#### Configuration

Adds a `code-pushup` target to your `project.json`.
See [configuration docs](./src/generators/configuration/README.md) for details
See [configuration generator docs](./src/generators/configuration/README.md) for details

Examples:

- `nx g @code-pushup/nx-plugin:configuration --project=<project-name>`
- `nx g @code-pushup/nx-plugin:configuration --project=<project-name> --targetName=cp`

### Executor

#### CLI

Install JS packages configure a target in your project json.
See [CLI executor docs](./src/executor/cli/README.md) for details

Examples:

```json
{
"name": "my-project",
"targets": {
"code-pushup": {
"executor": "@code-pushup/nx-plugin:cli",
"options": {
"projectPrefix": "workspace-name"
}
}
}
}
```

- `nx run <project-name>:code-pushup`
- `nx run <project-name>:code-pushup print-config --persist.filename=custom-report`
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions packages/nx-plugin/executors.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"executors": {
"autorun": {
"implementation": "./src/executors/autorun/executor",
"schema": "./src/executors/autorun/schema.json",
"description": "CodePushup CLI autorun command executor. Executes the @code-pushup/cli autorun command See: https://github.com/code-pushup/cli/blob/main/packages/cli/README.md#autorun-command"
"cli": {
"implementation": "./src/executors/cli/executor",
"schema": "./src/executors/cli/schema.json",
"description": "CodePushup CLI executor. Executes the @code-pushup/cli command's See: https://github.com/code-pushup/cli/blob/main/packages/cli/README.md#commands"
}
}
}
2 changes: 1 addition & 1 deletion packages/nx-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"dependencies": {
"@nx/devkit": "^17.1.3",
"tslib": "^2.6.2",
"tslib": "2.6.3",
"nx": "^17.1.3",
"@code-pushup/models": "0.51.0",
"zod": "^3.22.4",
Expand Down
1 change: 0 additions & 1 deletion packages/nx-plugin/src/executors/autorun/constants.ts

This file was deleted.

Loading

0 comments on commit 04ab516

Please sign in to comment.