|
| 1 | +import { Tree, updateProjectConfiguration } from '@nx/devkit'; |
| 2 | +import { rm } from 'node:fs/promises'; |
| 3 | +import { join, relative } from 'node:path'; |
| 4 | +import { readProjectConfiguration } from 'nx/src/generators/utils/project-configuration'; |
| 5 | +import { afterEach, expect } from 'vitest'; |
| 6 | +import { generateCodePushupConfig } from '@code-pushup/nx-plugin'; |
| 7 | +import { |
| 8 | + generateWorkspaceAndProject, |
| 9 | + materializeTree, |
| 10 | +} from '@code-pushup/test-nx-utils'; |
| 11 | +import { removeColorCodes } from '@code-pushup/test-utils'; |
| 12 | +import { executeProcess } from '@code-pushup/utils'; |
| 13 | + |
| 14 | +function relativePathToCwd(testDir: string): string { |
| 15 | + return relative(join(process.cwd(), testDir), process.cwd()); |
| 16 | +} |
| 17 | + |
| 18 | +async function addTargetToWorkspace( |
| 19 | + tree: Tree, |
| 20 | + options: { cwd: string; project: string }, |
| 21 | +) { |
| 22 | + const { cwd, project } = options; |
| 23 | + const pathRelativeToPackage = relative(join(cwd, 'libs', project), cwd); |
| 24 | + const projectCfg = readProjectConfiguration(tree, project); |
| 25 | + updateProjectConfiguration(tree, project, { |
| 26 | + ...projectCfg, |
| 27 | + targets: { |
| 28 | + ...projectCfg.targets, |
| 29 | + ['code-pushup']: { |
| 30 | + executor: `${join( |
| 31 | + relativePathToCwd(cwd), |
| 32 | + 'dist/packages/nx-plugin', |
| 33 | + )}:autorun`, |
| 34 | + }, |
| 35 | + }, |
| 36 | + }); |
| 37 | + const { root } = projectCfg; |
| 38 | + generateCodePushupConfig(tree, root, { |
| 39 | + fileImports: `import type {CoreConfig} from "${join( |
| 40 | + relativePathToCwd(cwd), |
| 41 | + pathRelativeToPackage, |
| 42 | + 'dist/packages/models', |
| 43 | + )}";`, |
| 44 | + plugins: [ |
| 45 | + { |
| 46 | + fileImports: `import {customPlugin} from "${join( |
| 47 | + relativePathToCwd(cwd), |
| 48 | + pathRelativeToPackage, |
| 49 | + 'dist/testing/test-utils', |
| 50 | + )}";`, |
| 51 | + codeStrings: 'customPlugin()', |
| 52 | + }, |
| 53 | + ], |
| 54 | + }); |
| 55 | + await materializeTree(tree, cwd); |
| 56 | +} |
| 57 | + |
| 58 | +describe('executor autorun', () => { |
| 59 | + let tree: Tree; |
| 60 | + const project = 'my-lib'; |
| 61 | + const baseDir = 'tmp/nx-plugin-e2e/executor'; |
| 62 | + |
| 63 | + beforeEach(async () => { |
| 64 | + tree = await generateWorkspaceAndProject(project); |
| 65 | + }); |
| 66 | + |
| 67 | + afterEach(async () => { |
| 68 | + await rm(baseDir, { recursive: true, force: true }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should execute autorun executor', async () => { |
| 72 | + const cwd = join(baseDir, 'execute-dynamic-executor'); |
| 73 | + await addTargetToWorkspace(tree, { cwd, project }); |
| 74 | + |
| 75 | + const { stdout, code } = await executeProcess({ |
| 76 | + command: 'npx', |
| 77 | + args: ['nx', 'run', `${project}:code-pushup`, '--dryRun'], |
| 78 | + cwd, |
| 79 | + }); |
| 80 | + |
| 81 | + expect(code).toBe(0); |
| 82 | + const cleanStdout = removeColorCodes(stdout); |
| 83 | + expect(cleanStdout).toContain('nx run my-lib:code-pushup --dryRun'); |
| 84 | + }); |
| 85 | +}); |
0 commit comments