|
6 | 6 | * found in the LICENSE file at https://angular.dev/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import { execSync, spawnSync } from 'child_process'; |
10 | | -import { BUILD_TOOL, BuildToolInput, runBuild } from './build'; |
11 | | -import { McpToolContext } from './tool-registry'; |
12 | | - |
13 | | -// Mock the execSync function |
14 | | -const mockedExecSync = jasmine.createSpy('execSync'); |
15 | | - |
16 | | -// Replace the actual execSync with our mock |
17 | | -Object.defineProperty(require('child_process'), 'execSync', { |
18 | | - value: mockedExecSync, |
19 | | -}); |
| 9 | +import { CommandError, Host } from '../host'; |
| 10 | +import { BuildToolInput, runBuild } from './build'; |
20 | 11 |
|
21 | 12 | describe('Build Tool', () => { |
| 13 | + let mockHost: Host; |
| 14 | + |
22 | 15 | beforeEach(() => { |
23 | | - mockedExecSync.calls.reset(); |
| 16 | + mockHost = { |
| 17 | + runCommand: jasmine.createSpy('runCommand').and.resolveTo({ stdout: '', stderr: '' }), |
| 18 | + stat: jasmine.createSpy('stat'), |
| 19 | + existsSync: jasmine.createSpy('existsSync'), |
| 20 | + }; |
24 | 21 | }); |
25 | 22 |
|
26 | | - it('should handle a successful build and extract the output path', () => { |
27 | | - const buildLogs = |
| 23 | + it('should handle a successful build and extract the output path', async () => { |
| 24 | + const buildStdout = |
28 | 25 | 'Build successful!\nSome other log lines...\nOutput location: dist/my-cool-app'; |
29 | | - mockedExecSync.and.returnValue(Buffer.from(buildLogs)); |
30 | | - |
31 | | - const result = runBuild({ project: 'my-cool-app' }); |
32 | | - |
33 | | - expect(mockedExecSync).toHaveBeenCalledWith('ng build my-cool-app -c development'); |
34 | | - expect(result.structuredContent.status).toBe('success'); |
35 | | - expect(result.structuredContent.logs).toBe(buildLogs); |
36 | | - expect(result.structuredContent.path).toBe('dist/my-cool-app'); |
| 26 | + (mockHost.runCommand as jasmine.Spy).and.resolveTo({ |
| 27 | + stdout: buildStdout, |
| 28 | + stderr: 'some warning', |
| 29 | + }); |
| 30 | + |
| 31 | + const { structuredContent } = await runBuild({ project: 'my-cool-app' }, mockHost); |
| 32 | + |
| 33 | + expect(mockHost.runCommand).toHaveBeenCalledWith('ng', [ |
| 34 | + 'build', |
| 35 | + 'my-cool-app', |
| 36 | + '-c development', |
| 37 | + ]); |
| 38 | + expect(structuredContent.status).toBe('success'); |
| 39 | + expect(structuredContent.stdout).toBe(buildStdout); |
| 40 | + expect(structuredContent.stderr).toBe('some warning'); |
| 41 | + expect(structuredContent.path).toBe('dist/my-cool-app'); |
37 | 42 | }); |
38 | 43 |
|
39 | | - it('should handle a failed build and capture logs', () => { |
40 | | - const error = new Error('Build failed'); |
41 | | - // Errors thrown from execSync contain the entire result like it would be returned from spawnSync. |
42 | | - const execSyncError = error as unknown as ReturnType<typeof spawnSync>; |
43 | | - execSyncError.stdout = Buffer.from('Some output before the crash.'); |
44 | | - execSyncError.stderr = Buffer.from('Error: Something went wrong!'); |
45 | | - mockedExecSync.and.throwError(error); |
46 | | - |
47 | | - const result = runBuild({ project: 'my-failed-app', configuration: 'production' }); |
48 | | - |
49 | | - expect(mockedExecSync).toHaveBeenCalledWith('ng build my-failed-app'); |
50 | | - expect(result.structuredContent.status).toBe('failure'); |
51 | | - expect(result.structuredContent.logs).toContain('Build failed'); |
52 | | - expect(result.structuredContent.logs).toContain('STDOUT:\nSome output before the crash.'); |
53 | | - expect(result.structuredContent.logs).toContain('STDERR:\nError: Something went wrong!'); |
54 | | - expect(result.structuredContent.path).toBeUndefined(); |
| 44 | + it('should handle a failed build and capture logs', async () => { |
| 45 | + const error = new CommandError( |
| 46 | + 'Build failed', |
| 47 | + 'Some output before the crash.', |
| 48 | + 'Error: Something went wrong!', |
| 49 | + 1, |
| 50 | + ); |
| 51 | + (mockHost.runCommand as jasmine.Spy).and.rejectWith(error); |
| 52 | + |
| 53 | + const { structuredContent } = await runBuild( |
| 54 | + { project: 'my-failed-app', configuration: 'production' }, |
| 55 | + mockHost, |
| 56 | + ); |
| 57 | + |
| 58 | + expect(mockHost.runCommand).toHaveBeenCalledWith('ng', ['build', 'my-failed-app']); |
| 59 | + expect(structuredContent.status).toBe('failure'); |
| 60 | + expect(structuredContent.stdout).toBe('Some output before the crash.'); |
| 61 | + expect(structuredContent.stderr).toBe('Error: Something went wrong!'); |
| 62 | + expect(structuredContent.path).toBeUndefined(); |
55 | 63 | }); |
56 | 64 |
|
57 | | - it('should construct the command correctly with default configuration', () => { |
58 | | - mockedExecSync.and.returnValue(Buffer.from('Success')); |
59 | | - runBuild({}); |
60 | | - expect(mockedExecSync).toHaveBeenCalledWith('ng build -c development'); |
| 65 | + it('should construct the command correctly with default configuration', async () => { |
| 66 | + await runBuild({}, mockHost); |
| 67 | + expect(mockHost.runCommand).toHaveBeenCalledWith('ng', ['build', '-c development']); |
61 | 68 | }); |
62 | 69 |
|
63 | | - it('should construct the command correctly with a specified project', () => { |
64 | | - mockedExecSync.and.returnValue(Buffer.from('Success')); |
65 | | - runBuild({ project: 'another-app' }); |
66 | | - expect(mockedExecSync).toHaveBeenCalledWith('ng build another-app -c development'); |
| 70 | + it('should construct the command correctly with a specified project', async () => { |
| 71 | + await runBuild({ project: 'another-app' }, mockHost); |
| 72 | + expect(mockHost.runCommand).toHaveBeenCalledWith('ng', [ |
| 73 | + 'build', |
| 74 | + 'another-app', |
| 75 | + '-c development', |
| 76 | + ]); |
67 | 77 | }); |
68 | 78 |
|
69 | | - it('should construct the command correctly for production configuration', () => { |
70 | | - mockedExecSync.and.returnValue(Buffer.from('Success')); |
71 | | - runBuild({ configuration: 'production' }); |
72 | | - expect(mockedExecSync).toHaveBeenCalledWith('ng build'); |
| 79 | + it('should construct the command correctly for production configuration', async () => { |
| 80 | + await runBuild({ configuration: 'production' }, mockHost); |
| 81 | + expect(mockHost.runCommand).toHaveBeenCalledWith('ng', ['build']); |
73 | 82 | }); |
74 | 83 |
|
75 | | - it('should handle builds where the output path is not found in logs', () => { |
76 | | - const buildLogs = 'Build finished, but we could not find the output path string.'; |
77 | | - mockedExecSync.and.returnValue(Buffer.from(buildLogs)); |
| 84 | + it('should handle builds where the output path is not found in logs', async () => { |
| 85 | + const buildStdout = 'Build finished, but we could not find the output path string.'; |
| 86 | + (mockHost.runCommand as jasmine.Spy).and.resolveTo({ stdout: buildStdout, stderr: '' }); |
78 | 87 |
|
79 | | - const result = runBuild({}); |
| 88 | + const { structuredContent } = await runBuild({}, mockHost); |
80 | 89 |
|
81 | | - expect(result.structuredContent.status).toBe('success'); |
82 | | - expect(result.structuredContent.logs).toBe(buildLogs); |
83 | | - expect(result.structuredContent.path).toBeUndefined(); |
| 90 | + expect(structuredContent.status).toBe('success'); |
| 91 | + expect(structuredContent.stdout).toBe(buildStdout); |
| 92 | + expect(structuredContent.path).toBeUndefined(); |
84 | 93 | }); |
85 | 94 | }); |
0 commit comments