|
| 1 | +import { ExecutorContext } from '@nrwl/devkit'; |
| 2 | + |
| 3 | +import { promises as fs } from 'fs'; |
| 4 | + |
| 5 | +import { DotNetClient, mockDotnetFactory } from '@nx-dotnet/dotnet'; |
| 6 | +import { rimraf } from '@nx-dotnet/utils'; |
| 7 | + |
| 8 | +import executor from './executor'; |
| 9 | +import { FormatExecutorSchema } from './schema'; |
| 10 | + |
| 11 | +const options: FormatExecutorSchema = { |
| 12 | + check: true, |
| 13 | + verbosity: 'minimal', |
| 14 | +}; |
| 15 | + |
| 16 | +const root = process.cwd() + '/tmp'; |
| 17 | + |
| 18 | +jest.mock('../../../../dotnet/src/lib/core/dotnet.client'); |
| 19 | + |
| 20 | +describe('Format Executor', () => { |
| 21 | + let context: ExecutorContext; |
| 22 | + let dotnetClient: DotNetClient; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + context = { |
| 26 | + root: root, |
| 27 | + cwd: root, |
| 28 | + projectName: 'my-app', |
| 29 | + targetName: 'lint', |
| 30 | + workspace: { |
| 31 | + version: 2, |
| 32 | + projects: { |
| 33 | + 'my-app': { |
| 34 | + root: `${root}/apps/my-app`, |
| 35 | + sourceRoot: `${root}/apps/my-app`, |
| 36 | + targets: { |
| 37 | + lint: { |
| 38 | + executor: '@nx-dotnet/core:format', |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + isVerbose: false, |
| 45 | + }; |
| 46 | + dotnetClient = new DotNetClient(mockDotnetFactory()); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(async () => { |
| 50 | + await rimraf(root); |
| 51 | + }); |
| 52 | + |
| 53 | + it('detects no dotnet project', async () => { |
| 54 | + expect.assertions(1); |
| 55 | + try { |
| 56 | + await executor(options, context, dotnetClient); |
| 57 | + } catch (e) { |
| 58 | + console.log(e.message); |
| 59 | + expect(e.message).toMatch( |
| 60 | + "Unable to find a build-able project within project's source directory!", |
| 61 | + ); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + it('detects multiple dotnet projects', async () => { |
| 66 | + expect.assertions(1); |
| 67 | + |
| 68 | + try { |
| 69 | + const directoryPath = `${root}/apps/my-app`; |
| 70 | + await fs.mkdir(directoryPath, { recursive: true }); |
| 71 | + await Promise.all([ |
| 72 | + fs.writeFile(`${directoryPath}/1.csproj`, ''), |
| 73 | + fs.writeFile(`${directoryPath}/2.csproj`, ''), |
| 74 | + ]); |
| 75 | + } catch (e) { |
| 76 | + console.warn(e.message); |
| 77 | + } |
| 78 | + |
| 79 | + try { |
| 80 | + await executor(options, context, dotnetClient); |
| 81 | + } catch (e) { |
| 82 | + console.log(e.message); |
| 83 | + expect(e.message).toMatch( |
| 84 | + "More than one build-able projects are contained within the project's source directory!", |
| 85 | + ); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + it('calls build when 1 project file is found', async () => { |
| 90 | + try { |
| 91 | + const directoryPath = `${root}/apps/my-app`; |
| 92 | + await fs.mkdir(directoryPath, { recursive: true }); |
| 93 | + await Promise.all([fs.writeFile(`${directoryPath}/1.csproj`, '')]); |
| 94 | + } catch (e) { |
| 95 | + console.warn(e.message); |
| 96 | + } |
| 97 | + |
| 98 | + const res = await executor(options, context, dotnetClient); |
| 99 | + expect( |
| 100 | + (dotnetClient as jest.Mocked<DotNetClient>).format, |
| 101 | + ).toHaveBeenCalled(); |
| 102 | + expect(res.success).toBeTruthy(); |
| 103 | + }); |
| 104 | +}); |
0 commit comments