|
1 |
| -import { ExecutorContext } from '@nx/devkit'; |
| 1 | +import { ExecutorContext, logger } from '@nx/devkit'; |
2 | 2 |
|
3 | 3 | import { CleanExecutorSchema } from './schema';
|
4 | 4 | import executor from './executor';
|
| 5 | +import { join } from 'node:path'; |
| 6 | +import { rm } from 'node:fs/promises'; |
| 7 | +import { mkdirSync, rmSync, writeFileSync, existsSync } from 'node:fs'; |
| 8 | + |
| 9 | +jest.mock('node:fs/promises', () => { |
| 10 | + return { |
| 11 | + ...jest.requireActual('node:fs/promises'), |
| 12 | + rm: jest.fn(() => Promise.resolve()), |
| 13 | + }; |
| 14 | +}); |
5 | 15 |
|
6 | 16 | const options: CleanExecutorSchema = {};
|
7 | 17 | const context: ExecutorContext = {
|
8 |
| - root: '', |
| 18 | + root: join(__dirname, '__fixtures__'), |
| 19 | + projectName: 'proj', |
| 20 | + projectsConfigurations: { |
| 21 | + projects: { proj: { root: 'proj' } }, |
| 22 | + version: 2, |
| 23 | + }, |
9 | 24 | cwd: process.cwd(),
|
10 |
| - isVerbose: false, |
| 25 | + isVerbose: true, |
11 | 26 | };
|
12 | 27 |
|
| 28 | +const noop = () => { |
| 29 | + return; |
| 30 | +}; |
| 31 | +const fixtureRoot = context.root; |
| 32 | +const projRoot = join(fixtureRoot, 'proj'); |
| 33 | + |
| 34 | +function prepareFixture() { |
| 35 | + if (!existsSync(fixtureRoot)) { |
| 36 | + mkdirSync(fixtureRoot); |
| 37 | + mkdirSync(projRoot); |
| 38 | + mkdirSync(join(projRoot, 'dist')); |
| 39 | + writeFileSync(join(projRoot, 'dist', 'file.txt'), 'file', 'utf-8'); |
| 40 | + mkdirSync(join(projRoot, 'lib')); |
| 41 | + writeFileSync(join(projRoot, 'lib', 'file.txt'), 'file', 'utf-8'); |
| 42 | + } |
| 43 | + return () => { |
| 44 | + rmSync(fixtureRoot, { recursive: true, force: true }); |
| 45 | + }; |
| 46 | +} |
| 47 | + |
13 | 48 | describe('Clean Executor', () => {
|
| 49 | + let cleanup = noop; |
| 50 | + |
| 51 | + beforeAll(() => { |
| 52 | + cleanup = prepareFixture(); |
| 53 | + }); |
| 54 | + afterAll(() => { |
| 55 | + cleanup(); |
| 56 | + }); |
| 57 | + |
| 58 | + beforeEach(() => { |
| 59 | + jest.spyOn(logger, 'info').mockImplementation(noop); |
| 60 | + jest.spyOn(logger, 'error').mockImplementation(noop); |
| 61 | + }); |
| 62 | + |
14 | 63 | it('can run', async () => {
|
| 64 | + const rmMock = rm as jest.Mock; |
15 | 65 | const output = await executor(options, context);
|
16 | 66 | expect(output.success).toBe(true);
|
| 67 | + |
| 68 | + expect(rmMock.mock.calls.flat()).toEqual([ |
| 69 | + expect.stringContaining('tools/workspace-plugin/src/executors/clean/__fixtures__/proj/dist'), |
| 70 | + { |
| 71 | + force: true, |
| 72 | + recursive: true, |
| 73 | + }, |
| 74 | + expect.stringContaining('tools/workspace-plugin/src/executors/clean/__fixtures__/proj/lib'), |
| 75 | + { |
| 76 | + force: true, |
| 77 | + recursive: true, |
| 78 | + }, |
| 79 | + ]); |
| 80 | + }); |
| 81 | + |
| 82 | + it('accepts custom paths that override default ones', async () => { |
| 83 | + const rmMock = rm as jest.Mock; |
| 84 | + mkdirSync(join(projRoot, 'foo-bar')); |
| 85 | + mkdirSync(join(projRoot, 'mr-wick')); |
| 86 | + |
| 87 | + const output = await executor({ ...options, paths: ['foo-bar', 'mr-wick'] }, context); |
| 88 | + expect(output.success).toBe(true); |
| 89 | + |
| 90 | + expect(rmMock.mock.calls.flat()).toEqual([ |
| 91 | + expect.stringContaining('tools/workspace-plugin/src/executors/clean/__fixtures__/proj/foo-bar'), |
| 92 | + { |
| 93 | + force: true, |
| 94 | + recursive: true, |
| 95 | + }, |
| 96 | + expect.stringContaining('tools/workspace-plugin/src/executors/clean/__fixtures__/proj/mr-wick'), |
| 97 | + { |
| 98 | + force: true, |
| 99 | + recursive: true, |
| 100 | + }, |
| 101 | + ]); |
17 | 102 | });
|
18 | 103 | });
|
0 commit comments